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

Commit

Permalink
Add polyfill support for /shadow/ and /shadow-deep/ combinators. Fixes
Browse files Browse the repository at this point in the history
…#12
  • Loading branch information
sorvell committed Mar 6, 2014
1 parent e3985da commit 1be1d20
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/ShadowCSS.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,10 @@ var ShadowCSS = {
* Convert ^ and ^^ combinators by replacing with space.
*/
convertCombinators: function(cssText) {
return cssText.replace(/\^\^/g, ' ').replace(/\^/g, ' ');
for (var i=0; i < combinatorsRe.length; i++) {
cssText = cssText.replace(combinatorsRe[i], ' ');
}
return cssText;
},
// change a selector like 'div' to 'name div'
scopeRules: function(cssRules, scopeSelector) {
Expand Down Expand Up @@ -518,7 +521,13 @@ var selectorRe = /([^{]*)({[\s\S]*?})/gim,
/* host name without combinator */
polyfillHostNoCombinator = polyfillHost + '-no-combinator',
polyfillHostRe = new RegExp(polyfillHost, 'gim'),
polyfillAncestorRe = new RegExp(polyfillAncestor, 'gim');
polyfillAncestorRe = new RegExp(polyfillAncestor, 'gim'),
combinatorsRe = [
/\^\^/g,
/\^/g,
/\/shadow\//g,
/\/shadow-deep\//g
];

function stylesToCssText(styles, preserveComments) {
var cssText = '';
Expand Down
3 changes: 3 additions & 0 deletions test/html/styling/combinators-shadow-import.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x-foo /shadow/ .foo2 {
background: green;
}
1 change: 1 addition & 0 deletions test/html/styling/combinators-shadow-import.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="stylesheet" href="combinators-shadow-import.css" shim-shadowdom>
3 changes: 3 additions & 0 deletions test/html/styling/combinators-shadow.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body /shadow-deep/ x-zot /shadow/ .zot-inner {
background: blue;
}
109 changes: 109 additions & 0 deletions test/html/styling/combinators-shadow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!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>
<meta name="viewport" content="initial-scale=1.0">
<title>Using combinators styling</title>
<script src="../../../platform.js"></script>
<script src="register.js"></script>
<script src="../../../../tools/test/htmltest.js"></script>
<script src="../../../../tools/test/chai/chai.js"></script>
<style shim-shadowdom>
x-foo /shadow/ .foo {
background: orange;
}
</style>
<link rel="stylesheet" href="combinators-shadow.css" shim-shadowdom>
<link rel="import" href="combinators-shadow-import.html">
</head>
<body>

<template id="x-foo">
<style>
x-bar /shadow/ .bar {
background: red;
}

x-bar /shadow/ .noogy {
background: blue;
}

x-bar /shadow-deep/ .zot {
color: white;
}
</style>
<div class="foo">orange</div>
<div class="foo2">green</div>
<x-bar></x-bar>
</template>
<template id="x-bar">
<style>
x-zot /shadow/ .zot {
background: green;
}
</style>
<div class="bar">Bar</div>
<div class="noogy">Noogy</div>
<x-zot></x-zot>
</template>

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


<script>
XZot = register('x-zot', '', HTMLElement.prototype, ['x-zot']);
XBar = register('x-bar', '', HTMLElement.prototype, ['x-bar']);
XFoo = register('x-foo', '', HTMLElement.prototype, ['x-foo']);
</script>

<x-foo></x-foo>

<script>
document.addEventListener('WebComponentsReady', function() {
var foo = document.querySelector('x-foo');
var xBarRoot = foo.shadowRoot.querySelector('x-bar').shadowRoot;
var bar = xBarRoot.querySelector('.bar');
chai.assert.equal(getComputedStyle(bar).backgroundColor, 'rgb(255, 0, 0)',
'^ styles are applied (backgroundColor)');

var noogy = xBarRoot.querySelector('.noogy');
chai.assert.equal(getComputedStyle(noogy).backgroundColor, 'rgb(0, 0, 255)',
'^ styles are applied (backgroundColor)');
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)');

// NOTE: native import styles not shimmed under SD polyfill
var unsupported = window.ShadowDOMPolyfill && HTMLImports.useNative;
if (!unsupported) {
var fooDiv = foo.shadowRoot.querySelector('.foo');
var zotDiv = xZot.shadowRoot.querySelector('.zot-inner');
var fooDiv2 = foo.shadowRoot.querySelector('.foo2');

// TODO(sorvell): test blocked on
// https://code.google.com/p/chromium/issues/detail?id=339657
//chai.assert.equal(getComputedStyle(fooDiv2).backgroundColor, 'rgb(0, 128, 0)',
// 'combinators applied via stylesheet in import');

chai.assert.equal(getComputedStyle(fooDiv).backgroundColor, 'rgb(255, 165, 0)',
'combinators applied via style in main document');
chai.assert.equal(getComputedStyle(zotDiv).backgroundColor, 'rgb(0, 0, 255)',
'combinators applied via stylesheet in main document');
}
done();
});
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion test/js/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ htmlSuite('styling', function() {
htmlTest('html/styling/polyfill-rule.html');
htmlTest('html/styling/colon-host.html');
htmlTest('html/styling/colon-host.html?shadow&register');
htmlTest('html/styling/combinators.html');
htmlTest('html/styling/combinators.html?shadow&register');
htmlTest('html/styling/combinators-shadow.html');
htmlTest('html/styling/combinators-shadow.html?shadow&register');
htmlTest('html/styling/before-content.html');
htmlTest('html/styling/before-content.html?shadow&register');
htmlTest('html/styling/before-content.html');
Expand Down

0 comments on commit 1be1d20

Please sign in to comment.