Skip to content

Commit 61831a6

Browse files
committed
Be defensive when rewriting rule
1 parent 4de63eb commit 61831a6

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/mediaQueryHelper.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,21 @@ var mediaQueryHelper = (function (cssMediaQuery) {
7474
'}';
7575
};
7676

77+
var exchangeRuleWithNewContent = function (styleSheet, ruleIdx, newRuleText) {
78+
try {
79+
styleSheet.insertRule(newRuleText, ruleIdx+1);
80+
} catch (e) {
81+
// In case the browser does not like our new rule we just keep the existing one and quietly leave
82+
return;
83+
}
84+
styleSheet.deleteRule(ruleIdx);
85+
};
86+
7787
var changeCssRule = function (rule, newRuleText) {
7888
var styleSheet = rule.parentStyleSheet,
7989
ruleIdx = asArray(styleSheet.cssRules).indexOf(rule);
8090

81-
// Exchange rule with the new text
82-
styleSheet.insertRule(newRuleText, ruleIdx+1);
83-
styleSheet.deleteRule(ruleIdx);
91+
exchangeRuleWithNewContent(styleSheet, ruleIdx, newRuleText);
8492
};
8593

8694
var rewriteStyleContent = function (styleElement) {

0 commit comments

Comments
 (0)