dvtng / jss

JavaScript library for getting and setting CSS stylesheet rules
335 stars 55 forks source link

jss._removeRule not working in Opera 11 #1

Closed medihack closed 13 years ago

medihack commented 13 years ago

Hello again David.

It seems that the jss._removeRule does not work in Opera 11 (Linux version). parentSheet.deleteRule(rule) throws an DOMException. It seems that Opera only allow to provide an index as parameter as parentSheet.deleteRule(0) works. Any suggestions how we can solve that in a nice way?

medihack commented 13 years ago

Here is the solution I came up with in Fanimate:

var rules = sheet.cssRules || sheet.rules;
for (var i = 0, length = rules.length; i < length; i++) {
    if (rules[i] === rule) {
        if (sheet.deleteRule) {
            sheet.deleteRule(i);
        }
        else if (sheet.removeRule) {
            sheet.removeRule(i);
        }
        break;
    }
}

I know, it is more a sledgehammer approach, but also supported by IE 6-8 (which isn't the case for parentStyleSheet). Fortunately I only have to loop through one style sheet in Fanimate. I guess in JSS you have too loop through any style sheet.

I hope that helps, Kai

dvtng commented 13 years ago

Hi Kai,

Thanks for the feedback and the code. I'll investigate this further, but if it throws a DOMException as you say, I could use a try catch block, and resort to the looping only if an error is thrown.

Cheers.

dvtng commented 13 years ago

Nope, my fault. I used deleteRule and removeRule incorrectly - they only accept an index as an argument in all browsers.

I've modified the _getRules and _addRule functions to return a custom cssrule object that exposes the sheet, index, and style properties so that _removeRule has sheet and index to work with, while you can still easily modify the rule using the style object. Not sure if you want to take this approach in fanimate as well.

Thanks again for pointing out this (important) error.