Skip to content

Commit a1ec6e4

Browse files
alxscmsmarcj
authored andcommitted
Fix cross-domain cssRules access breaking code (marcj#214)
When @import rule points to an other domain, accessing the style sheet with .cssRules will cause trouble on Chrome and Firefox (at least, haven't tested on other browsers). Firefox will raise a "Security error code: 1000" while Chrome will raise "Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules" which (I d'ont know why) can't be caught with a regular try catch statement and will break the for loop and thus not parse the other rules if any. I had this problem while having a @import rule for google fonts. I fixed the issue by checking first if the property cssRules actually exist.
1 parent 3817e42 commit a1ec6e4

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/ElementQueries.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@
384384
} else if (4 === rules[i].type) {
385385
readRules(rules[i].cssRules || rules[i].rules);
386386
} else if (3 === rules[i].type) {
387-
readRules(rules[i].styleSheet.cssRules);
387+
if(rules[i].styleSheet.hasOwnProperty("cssRules")) {
388+
readRules(rules[i].styleSheet.cssRules);
389+
}
388390
}
389391
}
390392
}

0 commit comments

Comments
 (0)