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

Commit fc0bba3

Browse files
committed
Fixes #40 by adding @polyfill-unscoped-rule
1 parent 6db75b5 commit fc0bba3

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/ShadowCSS.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ var ShadowCSS = {
156156
this.insertPolyfillDirectives(def.rootStyles);
157157
this.insertPolyfillRules(def.rootStyles);
158158
var cssText = this.stylesToShimmedCssText(def.scopeStyles, name);
159+
// note: we only need to do rootStyles since these are unscoped.
160+
cssText += this.extractPolyfillUnscopedRules(def.rootStyles);
159161
// provide shimmedStyle for user extensibility
160162
def.shimmedStyle = cssTextToStyle(cssText);
161163
if (root) {
@@ -263,6 +265,37 @@ var ShadowCSS = {
263265
r += cssText.substring(l, cssText.length);
264266
return r;
265267
},
268+
/*
269+
* Process styles to add rules which will only apply under the polyfill
270+
* and do not process via CSSOM. (CSSOM is destructive to rules on rare
271+
* occasions, e.g. -webkit-calc on Safari.)
272+
* For example, we convert this rule:
273+
*
274+
* (comment start) @polyfill-unscoped-rule menu-item {
275+
* ... } (comment end)
276+
*
277+
* to this:
278+
*
279+
* menu-item {...}
280+
*
281+
**/
282+
extractPolyfillUnscopedRules: function(styles) {
283+
var cssText = '';
284+
if (styles) {
285+
Array.prototype.forEach.call(styles, function(s) {
286+
cssText += this.extractPolyfillUnscopedRulesFromCssText(
287+
s.textContent) + '\n\n';
288+
}, this);
289+
}
290+
return cssText;
291+
},
292+
extractPolyfillUnscopedRulesFromCssText: function(cssText) {
293+
var r = '', l = 0, matches;
294+
while (matches = cssPolyfillUnscopedRuleCommentRe.exec(cssText)) {
295+
r += matches[1].slice(0, -1) + '\n\n';
296+
}
297+
return r;
298+
},
266299
// apply @host and scope shimming
267300
stylesToShimmedCssText: function(styles, name) {
268301
return this.shimAtHost(styles, name) + this.shimScoping(styles, name);
@@ -434,6 +467,7 @@ var hostRuleRe = /@host[^{]*{(([^}]*?{[^{]*?}[\s\S]*?)+)}/gim,
434467
cssCommentRe = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//gim,
435468
cssPolyfillCommentRe = /\/\*\s*@polyfill ([^*]*\*+([^/*][^*]*\*+)*\/)([^{]*?){/gim,
436469
cssPolyfillRuleCommentRe = /\/\*\s@polyfill-rule([^*]*\*+([^/*][^*]*\*+)*)\//gim,
470+
cssPolyfillUnscopedRuleCommentRe = /\/\*\s@polyfill-unscoped-rule([^*]*\*+([^/*][^*]*\*+)*)\//gim,
437471
cssPseudoRe = /::(x-[^\s{,(]*)/gim,
438472
selectorReSuffix = '([>\\s~+\[.,{:][\\s\\S]*)?$',
439473
hostRe = /@host/gim,

test/html/styling/polyfill-rule.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616
<x-foo>
1717
<div class="zonk">red?</div>
1818
</x-foo>
19-
19+
<div class="unscoped">unscoped</div>
2020
<template id="x-foo">
2121
<style>
2222
/* @polyfill-rule :host > .zonk {
2323
background: red;
2424
} */
25+
26+
/* @polyfill-unscoped-rule .unscoped {
27+
background: black;
28+
color: white;
29+
} */
2530
</style>
2631
<content></content>
2732
</template>
@@ -30,13 +35,16 @@
3035
XFoo = register('x-foo', '', HTMLElement.prototype);
3136

3237
document.addEventListener('WebComponentsReady', function() {
33-
setTimeout(function() {
38+
//setTimeout(function() {
3439
var foo = document.querySelector('x-foo');
3540
fooDiv = foo.firstElementChild;
3641
chai.assert.equal(getComputedStyle(fooDiv).backgroundColor,
3742
'rgb(255, 0, 0)', '@polyfill styles are applied');
43+
var unscoped = document.querySelector('.unscoped');
44+
chai.assert.equal(getComputedStyle(unscoped).backgroundColor,
45+
'rgb(0, 0, 0)', '@polyfill-unscoped-rule styles are applied');
3846
done();
39-
});
47+
//});
4048
});
4149
</script>
4250

0 commit comments

Comments
 (0)