dvtng / jss

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

jss._getRules sometimes causing null dereference when using Chrome #3

Closed asrashley closed 10 years ago

asrashley commented 13 years ago

I am finding a null dereference exception is being triggered when using jss with Chrome 12.0. In jss._getRules, the error is on the line "if (typeof sheet.length == 'number') {" where sheet is null.

Having done some digging, it appears to be that when jss._addSheet is called (to add an extra sheet to store the new rules) document.styleSheets does not get updated directly after the head.appendChild(styleNode). This causes the jss._nodeToSheet function to fail to find the newly created sheet.

My solution (hack?) was to add some code after the appendChild to look in the DOM for the stylesheets and update the sheets variable accordingly:

jss._addSheet = function () {
    var styleNode = doc.createElement('style'),i;
    styleNode.type = 'text/css';
    styleNode.rel = 'alternate stylesheet';
    head.appendChild(styleNode);
    var s = doc.getElementsByTagName('style');
    sheets = [];
    for(i=0; i<s.length; ++i){
        sheets.push(s[i].sheet); 
    }
    return jss._nodeToSheet(styleNode);
};

I'm sure there are more elegant solutions, but this got me out of my problem.

editht commented 10 years ago

Thanks for the feedback! This is now fixed on master of JSS and will make its way into version 0.7.

We're now reading the sheet directly from the DOM node, so there's no need to update the sheets variable for the _nodeToSheet function to work properly, yay :)