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

Commit

Permalink
Shim styles containing combinators in stylesheets and style elements; f…
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Nov 25, 2013
1 parent 7b103dc commit 0dc81d3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
64 changes: 50 additions & 14 deletions src/ShadowCSS.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,8 @@ var ShadowCSS = {
if (this.strictStyling) {
this.applyScopeToContent(root, name);
}
// insert @polyfill and @polyfill-rule rules into style elements
// scoping process takes care of shimming these
this.insertPolyfillDirectives(def.rootStyles);
this.insertPolyfillRules(def.rootStyles);
var cssText = this.stylesToShimmedCssText(def.scopeStyles, name,
typeExtension);
// note: we only need to do rootStyles since these are unscoped.
cssText += this.extractPolyfillUnscopedRules(def.rootStyles);
var cssText = this.stylesToShimmedCssText(def.rootStyles, def.scopeStyles,
name, typeExtension);
// provide shimmedStyle for user extensibility
def.shimmedStyle = cssTextToStyle(cssText);
if (root) {
Expand All @@ -186,6 +180,20 @@ var ShadowCSS = {
// add style to document
addCssToDocument(cssText);
},
// apply @polyfill rules + @host and scope shimming
stylesToShimmedCssText: function(rootStyles, scopeStyles, name,
typeExtension) {
name = name || '';
// insert @polyfill and @polyfill-rule rules into style elements
// scoping process takes care of shimming these
this.insertPolyfillDirectives(rootStyles);
this.insertPolyfillRules(rootStyles);
var cssText = this.shimAtHost(scopeStyles, name, typeExtension) +
this.shimScoping(scopeStyles, name, typeExtension);
// note: we only need to do rootStyles since these are unscoped.
cssText += this.extractPolyfillUnscopedRules(rootStyles);
return cssText;
},
registerDefinition: function(root, name, extendsName) {
var def = this.registry[name] = {
root: root,
Expand Down Expand Up @@ -304,11 +312,6 @@ var ShadowCSS = {
}
return r;
},
// apply @host and scope shimming
stylesToShimmedCssText: function(styles, name, typeExtension) {
return this.shimAtHost(styles, name, typeExtension) +
this.shimScoping(styles, name, typeExtension);
},
// form: @host { .foo { declarations } }
// becomes: scopeName.foo { declarations }
shimAtHost: function(styles, name, typeExtension) {
Expand Down Expand Up @@ -381,7 +384,9 @@ var ShadowCSS = {
cssText = this.convertParts(cssText);
cssText = this.convertCombinators(cssText);
var rules = cssToRules(cssText);
cssText = this.scopeRules(rules, name, typeExtension);
if (name) {
cssText = this.scopeRules(rules, name, typeExtension);
}
return cssText;
},
convertPseudos: function(cssText) {
Expand Down Expand Up @@ -575,6 +580,7 @@ function getSheet() {
if (!sheet) {
sheet = document.createElement("style");
sheet.setAttribute('ShadowCSSShim', '');
sheet.shadowCssShim = true;
}
return sheet;
}
Expand All @@ -584,6 +590,36 @@ if (window.ShadowDOMPolyfill) {
addCssToDocument('style { display: none !important; }\n');
var head = document.querySelector('head');
head.insertBefore(getSheet(), head.childNodes[0]);

document.addEventListener('DOMContentLoaded', function() {
if (window.HTMLImports) {
HTMLImports.importer.preloadSelectors +=
', link[rel=stylesheet]:not([nopolyfill])';
HTMLImports.parser.parseGeneric = function(elt) {
if (elt.shadowCssShim) {
return;
}
var style = elt;
if (!elt.hasAttribute('nopolyfill')) {
if (elt.__resource) {
style = document.createElement('style');
style.textContent = elt.__resource;
// remove links from main document
if (elt.ownerDocument === document) {
elt.parentNode.removeChild(elt);
}
}
var styles = [style];
style.textContent = ShadowCSS.stylesToShimmedCssText(styles, styles);
style.shadowCssShim = true;
}
// place in document
if (style.parentNode !== document.head) {
document.head.appendChild(style);
}
}
}
});
}

// exports
Expand Down
3 changes: 3 additions & 0 deletions test/html/styling/combinators-import.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x-foo ^ .foo2 {
background: green;
}
1 change: 1 addition & 0 deletions test/html/styling/combinators-import.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="stylesheet" href="combinators-import.css">
3 changes: 3 additions & 0 deletions test/html/styling/combinators.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body ^^ x-zot ^ .zot-inner {
background: blue;
}
30 changes: 25 additions & 5 deletions test/html/styling/combinators.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
<script src="register.js"></script>
<script src="../../../../tools/test/htmltest.js"></script>
<script src="../../../../tools/test/chai/chai.js"></script>
<style>
x-foo ^ .foo {
background: orange;
}
</style>
<link rel="stylesheet" href="combinators.css">
<link rel="import" href="combinators-import.html">
</head>
<body>

Expand All @@ -29,6 +36,8 @@
color: white;
}
</style>
<div class="foo">orange</div>
<div class="foo2">green</div>
<x-bar></x-bar>
</template>
<template id="x-bar">
Expand All @@ -44,6 +53,7 @@

<template id="x-zot">
<div class="zot">Zot</div>
<div class="zot-inner">inner</div>
</template>


Expand All @@ -66,17 +76,27 @@
var noogy = xBarRoot.querySelector('.noogy');
chai.assert.equal(getComputedStyle(noogy).backgroundColor, 'rgb(0, 0, 255)',
'^ styles are applied (backgroundColor)');

var zot = foo.shadowRoot.querySelector('x-bar').shadowRoot
.querySelector('x-zot').shadowRoot.querySelector('.zot');
var xZot = foo.shadowRoot.querySelector('x-bar').shadowRoot
.querySelector('x-zot');
var zot = xZot.shadowRoot.querySelector('.zot');

chai.assert.equal(getComputedStyle(zot).backgroundColor, 'rgb(0, 128, 0)',
'^ styles are applied (backgroundColor)');
chai.assert.equal(getComputedStyle(zot).color, 'rgb(255, 255, 255)',
'^^ styles are applied (color)');
done();


var fooDiv = foo.shadowRoot.querySelector('.foo');
chai.assert.equal(getComputedStyle(fooDiv).backgroundColor, 'rgb(255, 165, 0)',
'combinators applied via style in main document');
if (window.ShadowDOMPolyfill) {
var fooDiv2 = foo.shadowRoot.querySelector('.foo2');
chai.assert.equal(getComputedStyle(fooDiv2).backgroundColor, 'rgb(0, 128, 0)',
'combinators applied via stylesheet in import');
}
var zotDiv = xZot.shadowRoot.querySelector('.zot-inner');
chai.assert.equal(getComputedStyle(zotDiv).backgroundColor, 'rgb(0, 0, 255)',
'combinators applied via stylesheet in main document');
done();
});
</script>
</body>
Expand Down

0 comments on commit 0dc81d3

Please sign in to comment.