Skip to content

Commit c6dacba

Browse files
committed
read rules from imported style sheet
also rewrite readRules(..): - potential bug when rules is plain String - use switch/case instead of if..else if
1 parent 294e6de commit c6dacba

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/ElementQueries.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
}
7575
}
7676

77+
var attributes = ['min-width', 'min-height', 'max-width', 'max-height'];
78+
7779
/**
7880
*
7981
* @param {HTMLElement} element
@@ -92,7 +94,6 @@
9294
this.options[idx] = option;
9395
};
9496

95-
var attributes = ['min-width', 'min-height', 'max-width', 'max-height'];
9697

9798
/**
9899
* Extracts the computed width/height and sets to min/max- attribute.
@@ -350,27 +351,36 @@
350351
}
351352
if ('string' === typeof rules) {
352353
rules = rules.toLowerCase();
353-
if (-1 !== rules.indexOf('min-width') || -1 !== rules.indexOf('max-width')) {
354+
if (isElementQuery(rules)) {
354355
extractQuery(rules);
355356
}
356357
} else {
357-
for (var i = 0, j = rules.length; i < j; i++) {
358-
if (1 === rules[i].type) {
359-
selector = rules[i].selectorText || rules[i].cssText;
360-
if (-1 !== selector.indexOf('min-height') || -1 !== selector.indexOf('max-height')) {
361-
extractQuery(selector);
362-
}else if(-1 !== selector.indexOf('min-width') || -1 !== selector.indexOf('max-width')) {
363-
extractQuery(selector);
364-
}
365-
} else if (4 === rules[i].type) {
366-
readRules(rules[i].cssRules || rules[i].rules);
367-
} else if (3 === rules[i].type) { // CSSRule.IMPORT_RULE
368-
readRules(rules[i].styleSheet.cssRules);
358+
for (var i = 0, j = rules.length, r; i < j; i++) {
359+
r = rules[i];
360+
switch (r.type) {
361+
case 1: // CSSRule.STYLE_RULE
362+
selector = r.selectorText || r.cssText;
363+
if (isElementQuery(selector)) {
364+
extractQuery(selector);
365+
}
366+
break;
367+
case 3: // CSSRule.IMPORT_RULE
368+
r = r.styleSheet; // then continue to next case statement
369+
case 4: // CSSRule.MEDIA_RULE
370+
readRules(r.cssRules || r.rules);
371+
break;
369372
}
370373
}
371374
}
372375
}
373376

377+
378+
function isElementQuery(s) {
379+
for (var i in attributes) {
380+
if (-1 !== s.indexOf(attributes[i])) return true;
381+
}
382+
}
383+
374384
var defaultCssInjected = false;
375385

376386
/**

0 commit comments

Comments
 (0)