From 294e6de8b1f58b25b0a02678536dcc19eec7cec4 Mon Sep 17 00:00:00 2001 From: Feng Dihai Date: Wed, 20 Apr 2016 17:16:03 +0900 Subject: [PATCH 1/2] read rules from imported style sheet --- src/ElementQueries.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ElementQueries.js b/src/ElementQueries.js index c98329a..0bc24ae 100755 --- a/src/ElementQueries.js +++ b/src/ElementQueries.js @@ -364,6 +364,8 @@ } } else if (4 === rules[i].type) { readRules(rules[i].cssRules || rules[i].rules); + } else if (3 === rules[i].type) { // CSSRule.IMPORT_RULE + readRules(rules[i].styleSheet.cssRules); } } } From c6dacbaa14150510517331f1f7874185ae5d5502 Mon Sep 17 00:00:00 2001 From: Feng Dihai Date: Wed, 20 Apr 2016 17:53:56 +0900 Subject: [PATCH 2/2] read rules from imported style sheet also rewrite readRules(..): - potential bug when rules is plain String - use switch/case instead of if..else if --- src/ElementQueries.js | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/ElementQueries.js b/src/ElementQueries.js index 0bc24ae..8486c1f 100755 --- a/src/ElementQueries.js +++ b/src/ElementQueries.js @@ -74,6 +74,8 @@ } } + var attributes = ['min-width', 'min-height', 'max-width', 'max-height']; + /** * * @param {HTMLElement} element @@ -92,7 +94,6 @@ this.options[idx] = option; }; - var attributes = ['min-width', 'min-height', 'max-width', 'max-height']; /** * Extracts the computed width/height and sets to min/max- attribute. @@ -350,27 +351,36 @@ } if ('string' === typeof rules) { rules = rules.toLowerCase(); - if (-1 !== rules.indexOf('min-width') || -1 !== rules.indexOf('max-width')) { + if (isElementQuery(rules)) { extractQuery(rules); } } else { - for (var i = 0, j = rules.length; i < j; i++) { - if (1 === rules[i].type) { - selector = rules[i].selectorText || rules[i].cssText; - if (-1 !== selector.indexOf('min-height') || -1 !== selector.indexOf('max-height')) { - extractQuery(selector); - }else if(-1 !== selector.indexOf('min-width') || -1 !== selector.indexOf('max-width')) { - extractQuery(selector); - } - } else if (4 === rules[i].type) { - readRules(rules[i].cssRules || rules[i].rules); - } else if (3 === rules[i].type) { // CSSRule.IMPORT_RULE - readRules(rules[i].styleSheet.cssRules); + for (var i = 0, j = rules.length, r; i < j; i++) { + r = rules[i]; + switch (r.type) { + case 1: // CSSRule.STYLE_RULE + selector = r.selectorText || r.cssText; + if (isElementQuery(selector)) { + extractQuery(selector); + } + break; + case 3: // CSSRule.IMPORT_RULE + r = r.styleSheet; // then continue to next case statement + case 4: // CSSRule.MEDIA_RULE + readRules(r.cssRules || r.rules); + break; } } } } + + function isElementQuery(s) { + for (var i in attributes) { + if (-1 !== s.indexOf(attributes[i])) return true; + } + } + var defaultCssInjected = false; /**