dvtng / jss

JavaScript library for getting and setting CSS stylesheet rules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jss._getRules sometimes causing null dereference when using Chrome

asrashley opened this issue · comments

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.

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 :)