37
37
}
38
38
39
39
* 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.
45
65
For example, given a scope name of x-foo, a rule like this:
46
66
47
67
div {
@@ -136,9 +156,11 @@ var stylizer = {
136
156
// lookup of extendee
137
157
var name = element . options . name ;
138
158
stylizer . cacheDefinition ( element ) ;
139
- stylizer . applyScopeToContent ( element . templateContent , name ) ;
140
159
stylizer . shimPolyfillDirectives ( element . styles , name ) ;
141
160
// find styles and apply shimming...
161
+ if ( Polymer . strictPolyfillStyling ) {
162
+ stylizer . applyScopeToContent ( element . templateContent , name ) ;
163
+ }
142
164
stylizer . applyShimming ( stylizer . stylesForElement ( element ) , name ) ;
143
165
}
144
166
} ,
@@ -157,7 +179,6 @@ var stylizer = {
157
179
cssText += this . shimScoping ( styles , name ) ;
158
180
this . addCssToDocument ( cssText ) ;
159
181
} ,
160
- //TODO(sorvell): use SideTable
161
182
cacheDefinition : function ( element ) {
162
183
var name = element . options . name ;
163
184
var template = element . querySelector ( 'template' ) ;
@@ -314,9 +335,11 @@ var stylizer = {
314
335
// change a selector like 'div' to 'name div'
315
336
scopeRules : function ( cssRules , name ) {
316
337
var cssText = '' ;
338
+ var scopeFn = Polymer . strictPolyfillStyling ? this . scopeSelectorStrict
339
+ : this . scopeSelector ;
317
340
forEach ( cssRules , function ( rule ) {
318
341
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' ;
320
343
cssText += rule . style . cssText + '\n}\n\n' ;
321
344
} else if ( rule . media ) {
322
345
cssText += '@media ' + rule . media . mediaText + ' {\n' ;
@@ -329,6 +352,13 @@ var stylizer = {
329
352
return cssText ;
330
353
} ,
331
354
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 ) {
332
362
var r = [ ] , parts = selector . split ( ',' ) , t ;
333
363
var selectorRe = new RegExp ( '^' + name + this . selectorReSuffix , 'm' ) ;
334
364
parts . forEach ( function ( p ) {
@@ -344,8 +374,7 @@ var stylizer = {
344
374
// return a selector with [name] suffix on each simple selector
345
375
// e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name]
346
376
scopeCompoundSelector : function ( selector , name ) {
347
- // TODO(sorvell): process complete set of complex selector markers
348
- var splits = [ ' ' , '.' , '>' ] ,
377
+ var splits = [ ' ' , '>' , '+' , '~' ] ,
349
378
scoped = selector ,
350
379
attrName = '[' + name + ']' ;
351
380
splits . forEach ( function ( sep ) {
@@ -417,5 +446,6 @@ if (window.ShadowDOMPolyfill) {
417
446
Polymer . shimStyling = stylizer . shimStyling ;
418
447
Polymer . shimShadowDOMStyling = stylizer . shimShadowDOMStyling ;
419
448
Polymer . shimPolyfillDirectives = stylizer . shimPolyfillDirectives . bind ( stylizer ) ;
449
+ Polymer . strictPolyfillStyling = false ;
420
450
421
451
} ) ( window ) ;
0 commit comments