From 579d039946d6924b2d5ff7dc098fbcdc45bbfc47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Sat, 16 Dec 2017 16:22:39 +0100 Subject: [PATCH] Fix #49 CSS @import rules --- cq-prolyfill.js | 38 +++++++++++++++++----------- test-files/visibility-cq.css | 3 +++ tests-functional.js | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 test-files/visibility-cq.css diff --git a/cq-prolyfill.js b/cq-prolyfill.js index 6881161..01044b7 100644 --- a/cq-prolyfill.js +++ b/cq-prolyfill.js @@ -537,22 +537,29 @@ function parseRules() { queries = {}; var rules; for (var i = 0; i < styleSheets.length; i++) { - if (styleSheets[i].disabled) { - continue; - } - try { - rules = styleSheets[i].cssRules; - if (!rules || !rules.length) { - continue; - } - } - catch(e) { - continue; - } - for (var j = 0; j < rules.length; j++) { - parseRule(rules[j]); + parseStyleSheet(styleSheets[i]); + } +} + +/** + * @param {CSSStyleSheet} styleSheet + */ +function parseStyleSheet(styleSheet) { + if (styleSheet.disabled) { + return; + } + try { + var rules = styleSheet.cssRules; + if (!rules || !rules.length) { + return; } } + catch(e) { + return; + } + for (var i = 0; i < rules.length; i++) { + parseRule(rules[i]); + } } /** @@ -565,6 +572,9 @@ function parseRule(rule) { } return; } + if (rule.styleSheet) { + parseStyleSheet(rule.styleSheet); + } if (rule.type !== 1) { return; } diff --git a/test-files/visibility-cq.css b/test-files/visibility-cq.css new file mode 100644 index 0000000..2f9d0a3 --- /dev/null +++ b/test-files/visibility-cq.css @@ -0,0 +1,3 @@ +.test.\:container\(visibility\=visible\) { + font-family: visible; +} diff --git a/tests-functional.js b/tests-functional.js index adeaabe..9e58594 100644 --- a/tests-functional.js +++ b/tests-functional.js @@ -426,6 +426,55 @@ QUnit.test('Opacity Query', function(assert) { }); +QUnit.test('CSS Import rule', function(assert) { + + var style = document.createElement('style'); + style.type = 'text/css'; + style.innerHTML = '@import url("../test-files/visibility-cq.css");' + + '@font-face { font-family: visible; src: local("Times New Roman"), local("Droid Serif") }'; + fixture.appendChild(style); + + var element = document.createElement('div'); + element.innerHTML = '
'; + fixture.appendChild(element); + var test = element.firstChild; + + var done = assert.async(); + + var start = Date.now(); + run(); + + function run() { + try { + if (!style.sheet.cssRules[0].styleSheet.cssRules[0]) { + throw new Error(); + } + } + catch(e) { + if (Date.now() - start > 1000) { + assert.ok(false, 'Timeout'); + done(); + } + else { + setTimeout(run); + } + return; + } + window.cqApi.reprocess(function () { + + var font = function(node) { + return window.getComputedStyle(node).fontFamily; + }; + + assert.equal(font(test), 'visible', 'Style visible'); + + done(); + + }); + } + +}); + QUnit.test('PostCSS skip step 1', function(assert) { var style = document.createElement('style');