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

Commit

Permalink
repair path fixup functionality when using SD polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Jan 22, 2014
1 parent ffc2e2c commit 75ede4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ShadowCSS.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@
*/
(function(scope) {

var loader = scope.loader;

var ShadowCSS = {
strictStyling: false,
registry: {},
Expand Down Expand Up @@ -603,12 +605,15 @@ if (window.ShadowDOMPolyfill) {
var style = elt;
if (!elt.hasAttribute('nopolyfill')) {
if (elt.__resource) {
style = doc.createElement('style');
style.textContent = elt.__resource;
style = elt.ownerDocument.createElement('style');
style.textContent = Platform.loader.resolveUrlsInCssText(
elt.__resource, elt.href);
// remove links from main document
if (elt.ownerDocument === doc) {
elt.parentNode.removeChild(elt);
}
} else {
Platform.loader.resolveUrlsInStyle(style);
}
var styles = [style];
style.textContent = ShadowCSS.stylesToShimmedCssText(styles, styles);
Expand Down
33 changes: 33 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ function createStyleElement(cssText, scope) {
return style;
}

// TODO(sorvell): integrate a real URL polyfill, this is cribbed from
// https://github.com/arv/DOM-URL-Polyfill/blob/master/src/url.js.
// NOTE: URL seems difficult to polyfill since chrome and safari implement
// it but only chrome's appears to work.
function getUrl(base, url) {
url = url || ''
var doc = document.implementation.createHTMLDocument('');
if (base) {
var baseElement = doc.createElement('base');
baseElement.href = base;
doc.head.appendChild(baseElement);
}
var anchorElement = doc.createElement('a');
anchorElement.href = url;
doc.body.appendChild(anchorElement);
return anchorElement;
}

// TODO(sorvell): factor path fixup for easier reuse; parts are currently
// needed by HTMLImports and ShadowDOM style shimming.
function resolveUrlsInCssText(cssText, url) {
return HTMLImports.path.resolveUrlsInCssText(cssText,
getUrl(url));
}

function resolveUrlsInStyle(style) {
return HTMLImports.path.resolveUrlsInStyle(style);
}

// TODO(sorvell): use a common loader shared with HTMLImports polyfill
// currently, this just loads the first @import per style element
// and does not recurse into loaded elements; we'll address this with a
Expand All @@ -88,6 +117,7 @@ function polyfillLoadStyle(style, callback) {
HTMLImports.xhr.load(atImportUrlFromStyle(style), function (err, resource,
url) {
replaceAtImportWithCssText(this, url, resource);
this.textContent = resolveUrlsInCssText(this.textContent, url);
callback && callback(this);
}, style);
}
Expand All @@ -105,6 +135,9 @@ function replaceAtImportWithCssText(style, url, cssText) {
style.textContent = style.textContent.replace(re, cssText);
}

// exports
loader.resolveUrlsInCssText = resolveUrlsInCssText;
loader.resolveUrlsInStyle = resolveUrlsInStyle;
scope.loader = loader;

})(window.Platform);

0 comments on commit 75ede4f

Please sign in to comment.