Skip to content

Commit 278005b

Browse files
committed
make strict polyfill styling optional via Polymer.strictPolyfillStyling flag.
1 parent e226596 commit 278005b

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/shimStyling.js

+40-10
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,31 @@
3737
}
3838
3939
* encapsultion: Styles defined within shadowDOM, apply only to
40-
dom inside the shadowDOM. To shim this feature, non-@host rules within
41-
style elements are scoped by adding an attribute selector suffix to each
42-
simple selector that contains the scope name. Each element in the definition
43-
template content is also given the scope attribute. Thus, these rules match
44-
only elements that have the scope attribute.
40+
dom inside the shadowDOM. Polymer uses one of two techniques to imlement
41+
this feature.
42+
43+
By default, rules are prefixed with the host element tag name
44+
as a descendant selector. This ensures styling does not leak out of the 'top'
45+
of the element's shadowDOM. For example,
46+
47+
div {
48+
font-weight: bold;
49+
}
50+
51+
becomes:
52+
53+
x-foo div {
54+
font-weight: bold;
55+
}
56+
57+
becomes:
58+
59+
60+
Alternatively, if Polymer.strictPolyfillStyling is set to true then
61+
selectors are scoped by adding an attribute selector suffix to each
62+
simple selector that contains the host element tag name. Each element
63+
in the element's shadowDOM template is also given the scope attribute.
64+
Thus, these rules match only elements that have the scope attribute.
4565
For example, given a scope name of x-foo, a rule like this:
4666
4767
div {
@@ -136,9 +156,11 @@ var stylizer = {
136156
// lookup of extendee
137157
var name = element.options.name;
138158
stylizer.cacheDefinition(element);
139-
stylizer.applyScopeToContent(element.templateContent, name);
140159
stylizer.shimPolyfillDirectives(element.styles, name);
141160
// find styles and apply shimming...
161+
if (Polymer.strictPolyfillStyling) {
162+
stylizer.applyScopeToContent(element.templateContent, name);
163+
}
142164
stylizer.applyShimming(stylizer.stylesForElement(element), name);
143165
}
144166
},
@@ -157,7 +179,6 @@ var stylizer = {
157179
cssText += this.shimScoping(styles, name);
158180
this.addCssToDocument(cssText);
159181
},
160-
//TODO(sorvell): use SideTable
161182
cacheDefinition: function(element) {
162183
var name = element.options.name;
163184
var template = element.querySelector('template');
@@ -314,9 +335,11 @@ var stylizer = {
314335
// change a selector like 'div' to 'name div'
315336
scopeRules: function(cssRules, name) {
316337
var cssText = '';
338+
var scopeFn = Polymer.strictPolyfillStyling ? this.scopeSelectorStrict
339+
: this.scopeSelector;
317340
forEach(cssRules, function(rule) {
318341
if (rule.selectorText && (rule.style && rule.style.cssText)) {
319-
cssText += this.scopeSelector(rule.selectorText, name) + ' {\n\t';
342+
cssText += scopeFn.call(this, rule.selectorText, name) + ' {\n\t';
320343
cssText += rule.style.cssText + '\n}\n\n';
321344
} else if (rule.media) {
322345
cssText += '@media ' + rule.media.mediaText + ' {\n';
@@ -329,6 +352,13 @@ var stylizer = {
329352
return cssText;
330353
},
331354
scopeSelector: function(selector, name) {
355+
var r = [], parts = selector.split(',');
356+
parts.forEach(function(p) {
357+
r.push(name + ' ' + p.trim());
358+
});
359+
return r.join(', ');
360+
},
361+
scopeSelectorStrict: function(selector, name) {
332362
var r = [], parts = selector.split(','), t;
333363
var selectorRe = new RegExp('^' + name + this.selectorReSuffix, 'm');
334364
parts.forEach(function(p) {
@@ -344,8 +374,7 @@ var stylizer = {
344374
// return a selector with [name] suffix on each simple selector
345375
// e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name]
346376
scopeCompoundSelector: function(selector, name) {
347-
// TODO(sorvell): process complete set of complex selector markers
348-
var splits = [' ', '.', '>'],
377+
var splits = [' ', '>', '+', '~'],
349378
scoped = selector,
350379
attrName = '[' + name + ']';
351380
splits.forEach(function(sep) {
@@ -417,5 +446,6 @@ if (window.ShadowDOMPolyfill) {
417446
Polymer.shimStyling = stylizer.shimStyling;
418447
Polymer.shimShadowDOMStyling = stylizer.shimShadowDOMStyling;
419448
Polymer.shimPolyfillDirectives = stylizer.shimPolyfillDirectives.bind(stylizer);
449+
Polymer.strictPolyfillStyling = false;
420450

421451
})(window);

0 commit comments

Comments
 (0)