This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Polymer/recursive-loader
Recursive loader refactor
- Loading branch information
Showing
17 changed files
with
334 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2014 The Polymer Authors. All rights reserved. | ||
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file. | ||
*/ | ||
(function(scope) { | ||
|
||
var urlResolver = scope.urlResolver; | ||
var Loader = scope.Loader; | ||
|
||
function StyleResolver() { | ||
this.loader = new Loader(this.regex); | ||
} | ||
StyleResolver.prototype = { | ||
regex: /@import\s+(?:url)?["'\(]*([^'"\)]*)['"\)]*;/g, | ||
// Recursively replace @imports with the text at that url | ||
resolve: function(text, url, callback) { | ||
var done = function(map) { | ||
callback(this.flatten(text, url, map)); | ||
}.bind(this); | ||
this.loader.process(text, url, done); | ||
}, | ||
// resolve the textContent of a style node | ||
resolveNode: function(style, callback) { | ||
var text = style.textContent; | ||
var url = style.ownerDocument.baseURI; | ||
var done = function(text) { | ||
style.textContent = text; | ||
callback(style); | ||
}; | ||
this.resolve(text, url, done); | ||
}, | ||
// flatten all the @imports to text | ||
flatten: function(text, base, map) { | ||
var matches = this.loader.extractUrls(text, base); | ||
var match, url, intermediate; | ||
for (var i = 0; i < matches.length; i++) { | ||
match = matches[i]; | ||
url = match.url; | ||
// resolve any css text to be relative to the importer | ||
intermediate = urlResolver.resolveCssText(map[url], url); | ||
// flatten intermediate @imports | ||
intermediate = this.flatten(intermediate, url, map); | ||
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) { | ||
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); | ||
} | ||
}, | ||
}; | ||
|
||
// 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; | ||
} | ||
|
||
function xhrLoadStyle(style, callback) { | ||
styleResolver.resolveNode(style, function(){ | ||
callback(style); | ||
}); | ||
} | ||
|
||
// exports | ||
scope.loader = loader; | ||
scope.styleResolver = styleResolver; | ||
|
||
})(window.Platform); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE html> | ||
<!-- | ||
Copyright 2013 The Polymer Authors. All rights reserved. | ||
Use of this source code is governed by a BSD-style | ||
license that can be found in the LICENSE file. | ||
--> | ||
<html> | ||
<head> | ||
<title>Deduplicating Loader</title> | ||
<meta charset="UTF-8"> | ||
<script> | ||
var origXHR = window.XMLHttpRequest; | ||
var XHRCount = 0; | ||
window.XMLHttpRequest = function() { | ||
XHRCount++; | ||
return new origXHR(); | ||
}; | ||
</script> | ||
<script src="../../../tools/test/htmltest.js"></script> | ||
<script src="../../../tools/test/chai/chai.js"></script> | ||
<script src="../../platform.js"></script> | ||
</head> | ||
<body> | ||
<x-thing id="test"> | ||
import: styling/rules/colors.css; | ||
import: styling/rules/colors.css; | ||
</x-thing> | ||
<script> | ||
var assert = chai.assert; | ||
var loader = new Platform.Loader(/import: ([^;]*)/g); | ||
var test = document.querySelector('#test'); | ||
loader.process(test.textContent, document.baseURI, function(map) { | ||
assert.equal(XHRCount, 1); | ||
done(); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.