Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
remove platform caching of css in favor of xhr'ing. The caching was t…
Browse files Browse the repository at this point in the history
…oo brittle as it relied on browser cache state.
  • Loading branch information
sorvell committed Feb 19, 2014
1 parent 8498890 commit 09d2e1f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
40 changes: 3 additions & 37 deletions src/styleloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,59 +44,25 @@ StyleResolver.prototype = {
text = text.replace(match.matched, intermediate);
}
return text;
}
};

var styleResolver = new StyleResolver();
var loader = {
cacheStyles: function(styles, callback) {
var css = [];
for (var i=0, l=styles.length, s; (i<l) && (s=styles[i]); i++) {
css.push(s.textContent);
}
cacheCssText(css.join('\n'), callback);
},
xhrStyles: function(styles, callback) {
loadStyles: function(styles, callback) {
var loaded=0, l = styles.length;
// called in the context of the style
function loadedStyle(style) {
//console.log(style.textContent);
loaded++;
if (loaded === l && callback) {
callback();
}
}
for (var i=0, s; (i<l) && (s=styles[i]); i++) {
styleResolver.resolveNode(s, loadedStyle);
this.resolveNode(s, loadedStyle);
}
}
};

// use the platform to preload styles
var preloadElement = document.createElement('preloader');
preloadElement.style.display = 'none';
var preloadRoot = preloadElement.createShadowRoot();
document.head.appendChild(preloadElement);

function cacheCssText(cssText, callback) {
var style = createStyleElement(cssText);
if (callback) {
style.addEventListener('load', callback);
style.addEventListener('error', callback);
}
preloadRoot.appendChild(style);
}

function createStyleElement(cssText, scope) {
scope = scope || document;
scope = scope.createElement ? scope : scope.ownerDocument;
var style = scope.createElement('style');
style.textContent = cssText;
return style;
}
var styleResolver = new StyleResolver();

// exports
scope.loader = loader;
scope.styleResolver = styleResolver;

})(window.Platform);
2 changes: 1 addition & 1 deletion test/html/styling/recursive-style-import.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
assertStyle(target, 'border-left-color', 'rgb(128, 128, 128)');
assertStyle(target, 'border-bottom-style', 'solid');
// flatten the @import rules and test again
Platform.loader.xhrStyles([style], function() {
Platform.styleResolver.loadStyles([style], function() {
var cssRules = style.sheet.cssRules;
assert.equal(cssRules.length, 6);
for(var i = 0; i < cssRules.length; i++) {
Expand Down

0 comments on commit 09d2e1f

Please sign in to comment.