zoltan-dulac / cssSandpaper

a CSS3 Polyfill that implements CSS3 transforms, box-shadow, gradients, opacity and RGBA/HSL/HSLA colours in browser that don't support them.
http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/
208 stars 47 forks source link

CSS from external domains shouldn't be fetched #8

Open wuservices opened 12 years ago

wuservices commented 12 years ago

I made a small tweak to cssSandpaper which you might want to integrate. I noticed that it tries to AJAX in stylesheets but most of the time that isn't possible due to same origin policy. Ignoring CORS, I think the best is to just exclude cross domain files.

Of course that poses an issue for files on CDN but I think that should be up to the implementor to figure out. In my case I'm doing everything programatically anyways so maybe I'd even want to disable all the AJAX requests to boost performance but that's a separate issue.

    function getStyleSheet(node){
        var sheetCssText;
        switch (node.nodeName.toLowerCase()) {
            case 'style':
                sheetCssText = StringHelpers.uncommentHTML(node.innerHTML); //does not work with inline styles because IE doesn't allow you to get the text content of a STYLE element
                break;
            case 'link':
                // Don't fetch stylesheets cross domain since cross domain errors will appear
                var domainMatch = /\/\/(.*?)\//.exec(node.href);
                if (domainMatch !== null && domainMatch[1] !== location.hostname) {
                  return '';
                }

                var xhr = XMLHelpers.getXMLHttpRequest(node.href, null, "GET", null, false);
                sheetCssText = xhr.responseText;

                break;
        }

        sheetCssText = sheetCssText.replace(reMultiLineComment, '').replace(reAtRule, '');

        return sheetCssText;
    }