forked from webcomponents/webcomponentsjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShadowCSS.js
783 lines (730 loc) · 25.5 KB
/
ShadowCSS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
/*
This is a limited shim for ShadowDOM css styling.
https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/shadow/index.html#styles
The intention here is to support only the styling features which can be
relatively simply implemented. The goal is to allow users to avoid the
most obvious pitfalls and do so without compromising performance significantly.
For ShadowDOM styling that's not covered here, a set of best practices
can be provided that should allow users to accomplish more complex styling.
The following is a list of specific ShadowDOM styling features and a brief
discussion of the approach used to shim.
Shimmed features:
* :host, :host-context: ShadowDOM allows styling of the shadowRoot's host
element using the :host rule. To shim this feature, the :host styles are
reformatted and prefixed with a given scope name and promoted to a
document level stylesheet.
For example, given a scope name of .foo, a rule like this:
:host {
background: red;
}
}
becomes:
.foo {
background: red;
}
* encapsultion: Styles defined within ShadowDOM, apply only to
dom inside the ShadowDOM. Polymer uses one of two techniques to imlement
this feature.
By default, rules are prefixed with the host element tag name
as a descendant selector. This ensures styling does not leak out of the 'top'
of the element's ShadowDOM. For example,
div {
font-weight: bold;
}
becomes:
x-foo div {
font-weight: bold;
}
becomes:
Alternatively, if WebComponents.ShadowCSS.strictStyling is set to true then
selectors are scoped by adding an attribute selector suffix to each
simple selector that contains the host element tag name. Each element
in the element's ShadowDOM template is also given the scope attribute.
Thus, these rules match only elements that have the scope attribute.
For example, given a scope name of x-foo, a rule like this:
div {
font-weight: bold;
}
becomes:
div[x-foo] {
font-weight: bold;
}
Note that elements that are dynamically added to a scope must have the scope
selector added to them manually.
* upper/lower bound encapsulation: Styles which are defined outside a
shadowRoot should not cross the ShadowDOM boundary and should not apply
inside a shadowRoot.
This styling behavior is not emulated. Some possible ways to do this that
were rejected due to complexity and/or performance concerns include: (1) reset
every possible property for every possible selector for a given scope name;
(2) re-implement css in javascript.
As an alternative, users should make sure to use selectors
specific to the scope in which they are working.
* ::distributed: This behavior is not emulated. It's often not necessary
to style the contents of a specific insertion point and instead, descendants
of the host element can be styled selectively. Users can also create an
extra node around an insertion point and style that node's contents
via descendent selectors. For example, with a shadowRoot like this:
<style>
::content(div) {
background: red;
}
</style>
<content></content>
could become:
<style>
/ *@polyfill .content-container div * /
::content(div) {
background: red;
}
</style>
<div class="content-container">
<content></content>
</div>
Note the use of @polyfill in the comment above a ShadowDOM specific style
declaration. This is a directive to the styling shim to use the selector
in comments in lieu of the next selector when running under polyfill.
*/
(function(scope) {
var ShadowCSS = {
strictStyling: false,
registry: {},
// Shim styles for a given root associated with a name and extendsName
// 1. cache root styles by name
// 2. optionally tag root nodes with scope name
// 3. shim polyfill directives /* @polyfill */ and /* @polyfill-rule */
// 4. shim :host and scoping
shimStyling: function(root, name, extendsName) {
var scopeStyles = this.prepareRoot(root, name, extendsName);
var typeExtension = this.isTypeExtension(extendsName);
var scopeSelector = this.makeScopeSelector(name, typeExtension);
// use caching to make working with styles nodes easier and to facilitate
// lookup of extendee
var cssText = stylesToCssText(scopeStyles, true);
cssText = this.scopeCssText(cssText, scopeSelector);
// cache shimmed css on root for user extensibility
if (root) {
root.shimmedStyle = cssText;
}
// add style to document
this.addCssToDocument(cssText, name);
},
/*
* Shim a style element with the given selector. Returns cssText that can
* be included in the document via WebComponents.ShadowCSS.addCssToDocument(css).
*/
shimStyle: function(style, selector) {
return this.shimCssText(style.textContent, selector);
},
/*
* Shim some cssText with the given selector. Returns cssText that can
* be included in the document via WebComponents.ShadowCSS.addCssToDocument(css).
*/
shimCssText: function(cssText, selector) {
cssText = this.insertDirectives(cssText);
return this.scopeCssText(cssText, selector);
},
makeScopeSelector: function(name, typeExtension) {
if (name) {
return typeExtension ? '[is=' + name + ']' : name;
}
return '';
},
isTypeExtension: function(extendsName) {
return extendsName && extendsName.indexOf('-') < 0;
},
prepareRoot: function(root, name, extendsName) {
var def = this.registerRoot(root, name, extendsName);
this.replaceTextInStyles(def.rootStyles, this.insertDirectives);
// remove existing style elements
this.removeStyles(root, def.rootStyles);
// apply strict attr
if (this.strictStyling) {
this.applyScopeToContent(root, name);
}
return def.scopeStyles;
},
removeStyles: function(root, styles) {
for (var i=0, l=styles.length, s; (i<l) && (s=styles[i]); i++) {
s.parentNode.removeChild(s);
}
},
registerRoot: function(root, name, extendsName) {
var def = this.registry[name] = {
root: root,
name: name,
extendsName: extendsName
}
var styles = this.findStyles(root);
def.rootStyles = styles;
def.scopeStyles = def.rootStyles;
var extendee = this.registry[def.extendsName];
if (extendee) {
def.scopeStyles = extendee.scopeStyles.concat(def.scopeStyles);
}
return def;
},
findStyles: function(root) {
if (!root) {
return [];
}
var styles = root.querySelectorAll('style');
return Array.prototype.filter.call(styles, function(s) {
return !s.hasAttribute(NO_SHIM_ATTRIBUTE);
});
},
applyScopeToContent: function(root, name) {
if (root) {
// add the name attribute to each node in root.
Array.prototype.forEach.call(root.querySelectorAll('*'),
function(node) {
node.setAttribute(name, '');
});
// and template contents too
Array.prototype.forEach.call(root.querySelectorAll('template'),
function(template) {
this.applyScopeToContent(template.content, name);
},
this);
}
},
insertDirectives: function(cssText) {
cssText = this.insertPolyfillDirectivesInCssText(cssText);
return this.insertPolyfillRulesInCssText(cssText);
},
/*
* Process styles to convert native ShadowDOM rules that will trip
* up the css parser; we rely on decorating the stylesheet with inert rules.
*
* For example, we convert this rule:
*
* polyfill-next-selector { content: ':host menu-item'; }
* ::content menu-item {
*
* to this:
*
* scopeName menu-item {
*
**/
insertPolyfillDirectivesInCssText: function(cssText) {
// TODO(sorvell): remove either content or comment
cssText = cssText.replace(cssCommentNextSelectorRe, function(match, p1) {
// remove end comment delimiter and add block start
return p1.slice(0, -2) + '{';
});
return cssText.replace(cssContentNextSelectorRe, function(match, p1) {
return p1 + ' {';
});
},
/*
* Process styles to add rules which will only apply under the polyfill
*
* For example, we convert this rule:
*
* polyfill-rule {
* content: ':host menu-item';
* ...
* }
*
* to this:
*
* scopeName menu-item {...}
*
**/
insertPolyfillRulesInCssText: function(cssText) {
// TODO(sorvell): remove either content or comment
cssText = cssText.replace(cssCommentRuleRe, function(match, p1) {
// remove end comment delimiter
return p1.slice(0, -1);
});
return cssText.replace(cssContentRuleRe, function(match, p1, p2, p3) {
var rule = match.replace(p1, '').replace(p2, '');
return p3 + rule;
});
},
/* Ensure styles are scoped. Pseudo-scoping takes a rule like:
*
* .foo {... }
*
* and converts this to
*
* scopeName .foo { ... }
*/
scopeCssText: function(cssText, scopeSelector) {
var unscoped = this.extractUnscopedRulesFromCssText(cssText);
cssText = this.insertPolyfillHostInCssText(cssText);
cssText = this.convertColonHost(cssText);
cssText = this.convertColonHostContext(cssText);
cssText = this.convertShadowDOMSelectors(cssText);
if (scopeSelector) {
var self = this, cssText;
withCssRules(cssText, function(rules) {
cssText = self.scopeRules(rules, scopeSelector);
});
}
cssText = cssText + '\n' + unscoped;
return cssText.trim();
},
/*
* Process styles to add rules which will only apply under the polyfill
* and do not process via CSSOM. (CSSOM is destructive to rules on rare
* occasions, e.g. -webkit-calc on Safari.)
* For example, we convert this rule:
*
* (comment start) @polyfill-unscoped-rule menu-item {
* ... } (comment end)
*
* to this:
*
* menu-item {...}
*
**/
extractUnscopedRulesFromCssText: function(cssText) {
// TODO(sorvell): remove either content or comment
var r = '', m;
while (m = cssCommentUnscopedRuleRe.exec(cssText)) {
r += m[1].slice(0, -1) + '\n\n';
}
while (m = cssContentUnscopedRuleRe.exec(cssText)) {
r += m[0].replace(m[2], '').replace(m[1], m[3]) + '\n\n';
}
return r;
},
/*
* convert a rule like :host(.foo) > .bar { }
*
* to
*
* scopeName.foo > .bar
*/
convertColonHost: function(cssText) {
return this.convertColonRule(cssText, cssColonHostRe,
this.colonHostPartReplacer);
},
/*
* convert a rule like :host-context(.foo) > .bar { }
*
* to
*
* scopeName.foo > .bar, .foo scopeName > .bar { }
*
* and
*
* :host-context(.foo:host) .bar { ... }
*
* to
*
* scopeName.foo .bar { ... }
*/
convertColonHostContext: function(cssText) {
return this.convertColonRule(cssText, cssColonHostContextRe,
this.colonHostContextPartReplacer);
},
convertColonRule: function(cssText, regExp, partReplacer) {
// p1 = :host, p2 = contents of (), p3 rest of rule
return cssText.replace(regExp, function(m, p1, p2, p3) {
p1 = polyfillHostNoCombinator;
if (p2) {
var parts = p2.split(','), r = [];
for (var i=0, l=parts.length, p; (i<l) && (p=parts[i]); i++) {
p = p.trim();
r.push(partReplacer(p1, p, p3));
}
return r.join(',');
} else {
return p1 + p3;
}
});
},
colonHostContextPartReplacer: function(host, part, suffix) {
if (part.match(polyfillHost)) {
return this.colonHostPartReplacer(host, part, suffix);
} else {
return host + part + suffix + ', ' + part + ' ' + host + suffix;
}
},
colonHostPartReplacer: function(host, part, suffix) {
return host + part.replace(polyfillHost, '') + suffix;
},
/*
* Convert combinators like ::shadow and pseudo-elements like ::content
* by replacing with space.
*/
convertShadowDOMSelectors: function(cssText) {
for (var i=0; i < shadowDOMSelectorsRe.length; i++) {
cssText = cssText.replace(shadowDOMSelectorsRe[i], ' ');
}
return cssText;
},
// change a selector like 'div' to 'name div'
scopeRules: function(cssRules, scopeSelector) {
var cssText = '';
if (cssRules) {
Array.prototype.forEach.call(cssRules, function(rule) {
if (rule.selectorText && (rule.style && rule.style.cssText !== undefined)) {
cssText += this.scopeSelector(rule.selectorText, scopeSelector,
this.strictStyling) + ' {\n\t';
cssText += this.propertiesFromRule(rule) + '\n}\n\n';
} else if (rule.type === CSSRule.MEDIA_RULE) {
cssText += '@media ' + rule.media.mediaText + ' {\n';
cssText += this.scopeRules(rule.cssRules, scopeSelector);
cssText += '\n}\n\n';
} else {
// KEYFRAMES_RULE in IE throws when we query cssText
// when it contains a -webkit- property.
// if this happens, we fallback to constructing the rule
// from the CSSRuleSet
// https://connect.microsoft.com/IE/feedbackdetail/view/955703/accessing-csstext-of-a-keyframe-rule-that-contains-a-webkit-property-via-cssom-generates-exception
try {
if (rule.cssText) {
cssText += rule.cssText + '\n\n';
}
} catch(x) {
if (rule.type === CSSRule.KEYFRAMES_RULE && rule.cssRules) {
cssText += this.ieSafeCssTextFromKeyFrameRule(rule);
}
}
}
}, this);
}
return cssText;
},
ieSafeCssTextFromKeyFrameRule: function(rule) {
var cssText = '@keyframes ' + rule.name + ' {';
Array.prototype.forEach.call(rule.cssRules, function(rule) {
cssText += ' ' + rule.keyText + ' {' + rule.style.cssText + '}';
});
cssText += ' }';
return cssText;
},
scopeSelector: function(selector, scopeSelector, strict) {
var r = [], parts = selector.split(',');
parts.forEach(function(p) {
p = p.trim();
if (this.selectorNeedsScoping(p, scopeSelector)) {
p = (strict && !p.match(polyfillHostNoCombinator)) ?
this.applyStrictSelectorScope(p, scopeSelector) :
this.applySelectorScope(p, scopeSelector);
}
r.push(p);
}, this);
return r.join(', ');
},
selectorNeedsScoping: function(selector, scopeSelector) {
if (Array.isArray(scopeSelector)) {
return true;
}
var re = this.makeScopeMatcher(scopeSelector);
return !selector.match(re);
},
makeScopeMatcher: function(scopeSelector) {
scopeSelector = scopeSelector.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
return new RegExp('^(' + scopeSelector + ')' + selectorReSuffix, 'm');
},
applySelectorScope: function(selector, selectorScope) {
return Array.isArray(selectorScope) ?
this.applySelectorScopeList(selector, selectorScope) :
this.applySimpleSelectorScope(selector, selectorScope);
},
// apply an array of selectors
applySelectorScopeList: function(selector, scopeSelectorList) {
var r = [];
for (var i=0, s; (s=scopeSelectorList[i]); i++) {
r.push(this.applySimpleSelectorScope(selector, s));
}
return r.join(', ');
},
// scope via name and [is=name]
applySimpleSelectorScope: function(selector, scopeSelector) {
if (selector.match(polyfillHostRe)) {
selector = selector.replace(polyfillHostNoCombinator, scopeSelector);
return selector.replace(polyfillHostRe, scopeSelector + ' ');
} else {
return scopeSelector + ' ' + selector;
}
},
// return a selector with [name] suffix on each simple selector
// e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name]
applyStrictSelectorScope: function(selector, scopeSelector) {
scopeSelector = scopeSelector.replace(/\[is=([^\]]*)\]/g, '$1');
var splits = [' ', '>', '+', '~'],
scoped = selector,
attrName = '[' + scopeSelector + ']';
splits.forEach(function(sep) {
var parts = scoped.split(sep);
scoped = parts.map(function(p) {
// remove :host since it should be unnecessary
var t = p.trim().replace(polyfillHostRe, '');
if (t && (splits.indexOf(t) < 0) && (t.indexOf(attrName) < 0)) {
p = t.replace(/([^:]*)(:*)(.*)/, '$1' + attrName + '$2$3');
}
return p;
}).join(sep);
});
return scoped;
},
insertPolyfillHostInCssText: function(selector) {
return selector.replace(colonHostContextRe, polyfillHostContext).replace(
colonHostRe, polyfillHost);
},
propertiesFromRule: function(rule) {
var cssText = rule.style.cssText;
// TODO(sorvell): Safari cssom incorrectly removes quotes from the content
// property. (https://bugs.webkit.org/show_bug.cgi?id=118045)
// don't replace attr rules
if (rule.style.content && !rule.style.content.match(/['"]+|attr/)) {
cssText = cssText.replace(/content:[^;]*;/g, 'content: \'' +
rule.style.content + '\';');
}
// TODO(sorvell): we can workaround this issue here, but we need a list
// of troublesome properties to fix https://github.com/Polymer/platform/issues/53
//
// inherit rules can be omitted from cssText
// TODO(sorvell): remove when Blink bug is fixed:
// https://code.google.com/p/chromium/issues/detail?id=358273
var style = rule.style;
for (var i in style) {
if (style[i] === 'initial') {
cssText += i + ': initial; ';
}
}
return cssText;
},
replaceTextInStyles: function(styles, action) {
if (styles && action) {
if (!(styles instanceof Array)) {
styles = [styles];
}
Array.prototype.forEach.call(styles, function(s) {
s.textContent = action.call(this, s.textContent);
}, this);
}
},
addCssToDocument: function(cssText, name) {
if (cssText.match('@import')) {
addOwnSheet(cssText, name);
} else {
addCssToDocument(cssText);
}
}
};
var selectorRe = /([^{]*)({[\s\S]*?})/gim,
cssCommentRe = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//gim,
// TODO(sorvell): remove either content or comment
cssCommentNextSelectorRe = /\/\*\s*@polyfill ([^*]*\*+([^/*][^*]*\*+)*\/)([^{]*?){/gim,
cssContentNextSelectorRe = /polyfill-next-selector[^}]*content\:[\s]*?['"](.*?)['"][;\s]*}([^{]*?){/gim,
// TODO(sorvell): remove either content or comment
cssCommentRuleRe = /\/\*\s@polyfill-rule([^*]*\*+([^/*][^*]*\*+)*)\//gim,
cssContentRuleRe = /(polyfill-rule)[^}]*(content\:[\s]*['"](.*?)['"])[;\s]*[^}]*}/gim,
// TODO(sorvell): remove either content or comment
cssCommentUnscopedRuleRe = /\/\*\s@polyfill-unscoped-rule([^*]*\*+([^/*][^*]*\*+)*)\//gim,
cssContentUnscopedRuleRe = /(polyfill-unscoped-rule)[^}]*(content\:[\s]*['"](.*?)['"])[;\s]*[^}]*}/gim,
cssPseudoRe = /::(x-[^\s{,(]*)/gim,
cssPartRe = /::part\(([^)]*)\)/gim,
// note: :host pre-processed to -shadowcsshost.
polyfillHost = '-shadowcsshost',
// note: :host-context pre-processed to -shadowcsshostcontext.
polyfillHostContext = '-shadowcsscontext',
parenSuffix = ')(?:\\((' +
'(?:\\([^)(]*\\)|[^)(]*)+?' +
')\\))?([^,{]*)';
var cssColonHostRe = new RegExp('(' + polyfillHost + parenSuffix, 'gim'),
cssColonHostContextRe = new RegExp('(' + polyfillHostContext + parenSuffix, 'gim'),
selectorReSuffix = '([>\\s~+\[.,{:][\\s\\S]*)?$',
colonHostRe = /\:host/gim,
colonHostContextRe = /\:host-context/gim,
/* host name without combinator */
polyfillHostNoCombinator = polyfillHost + '-no-combinator',
polyfillHostRe = new RegExp(polyfillHost, 'gim'),
polyfillHostContextRe = new RegExp(polyfillHostContext, 'gim'),
shadowDOMSelectorsRe = [
/>>>/g,
/::shadow/g,
/::content/g,
// Deprecated selectors
/\/deep\//g, // former >>>
/\/shadow\//g, // former ::shadow
/\/shadow-deep\//g, // former /deep/
/\^\^/g, // former /shadow/
/\^(?!=)/g // former /shadow-deep/
];
function stylesToCssText(styles, preserveComments) {
var cssText = '';
Array.prototype.forEach.call(styles, function(s) {
cssText += s.textContent + '\n\n';
});
// strip comments for easier processing
if (!preserveComments) {
cssText = cssText.replace(cssCommentRe, '');
}
return cssText;
}
function cssTextToStyle(cssText) {
var style = document.createElement('style');
style.textContent = cssText;
return style;
}
function cssToRules(cssText) {
var style = cssTextToStyle(cssText);
document.head.appendChild(style);
var rules = [];
if (style.sheet) {
// TODO(sorvell): Firefox throws when accessing the rules of a stylesheet
// with an @import
// https://bugzilla.mozilla.org/show_bug.cgi?id=625013
try {
rules = style.sheet.cssRules;
} catch(e) {
//
}
} else {
console.warn('sheet not found', style);
}
style.parentNode.removeChild(style);
return rules;
}
var frame = document.createElement('iframe');
frame.style.display = 'none';
function initFrame() {
frame.initialized = true;
document.body.appendChild(frame);
var doc = frame.contentDocument;
var base = doc.createElement('base');
base.href = document.baseURI;
doc.head.appendChild(base);
}
function inFrame(fn) {
if (!frame.initialized) {
initFrame();
}
document.body.appendChild(frame);
fn(frame.contentDocument);
document.body.removeChild(frame);
}
// TODO(sorvell): use an iframe if the cssText contains an @import to workaround
// https://code.google.com/p/chromium/issues/detail?id=345114
var isChrome = navigator.userAgent.match('Chrome');
function withCssRules(cssText, callback) {
if (!callback) {
return;
}
var rules;
if (cssText.match('@import') && isChrome) {
var style = cssTextToStyle(cssText);
inFrame(function(doc) {
doc.head.appendChild(style.impl);
rules = Array.prototype.slice.call(style.sheet.cssRules, 0);
callback(rules);
});
} else {
rules = cssToRules(cssText);
callback(rules);
}
}
function rulesToCss(cssRules) {
for (var i=0, css=[]; i < cssRules.length; i++) {
css.push(cssRules[i].cssText);
}
return css.join('\n\n');
}
function addCssToDocument(cssText) {
if (cssText) {
getSheet().appendChild(document.createTextNode(cssText));
}
}
function addOwnSheet(cssText, name) {
var style = cssTextToStyle(cssText);
style.setAttribute(name, '');
style.setAttribute(SHIMMED_ATTRIBUTE, '');
document.head.appendChild(style);
}
var SHIM_ATTRIBUTE = 'shim-shadowdom';
var SHIMMED_ATTRIBUTE = 'shim-shadowdom-css';
var NO_SHIM_ATTRIBUTE = 'no-shim';
var sheet;
function getSheet() {
if (!sheet) {
sheet = document.createElement("style");
sheet.setAttribute(SHIMMED_ATTRIBUTE, '');
sheet[SHIMMED_ATTRIBUTE] = true;
}
return sheet;
}
// add polyfill stylesheet to document
if (window.ShadowDOMPolyfill) {
addCssToDocument('style { display: none !important; }\n');
var doc = ShadowDOMPolyfill.wrap(document);
var head = doc.querySelector('head');
head.insertBefore(getSheet(), head.childNodes[0]);
// TODO(sorvell): monkey-patching HTMLImports is abusive;
// consider a better solution.
document.addEventListener('DOMContentLoaded', function() {
var urlResolver = scope.urlResolver;
if (window.HTMLImports && !HTMLImports.useNative) {
var SHIM_SHEET_SELECTOR = 'link[rel=stylesheet]' +
'[' + SHIM_ATTRIBUTE + ']';
var SHIM_STYLE_SELECTOR = 'style[' + SHIM_ATTRIBUTE + ']';
HTMLImports.importer.documentPreloadSelectors += ',' + SHIM_SHEET_SELECTOR;
HTMLImports.importer.importsPreloadSelectors += ',' + SHIM_SHEET_SELECTOR;
HTMLImports.parser.documentSelectors = [
HTMLImports.parser.documentSelectors,
SHIM_SHEET_SELECTOR,
SHIM_STYLE_SELECTOR
].join(',');
var originalParseGeneric = HTMLImports.parser.parseGeneric;
HTMLImports.parser.parseGeneric = function(elt) {
if (elt[SHIMMED_ATTRIBUTE]) {
return;
}
var style = elt.__importElement || elt;
if (!style.hasAttribute(SHIM_ATTRIBUTE)) {
originalParseGeneric.call(this, elt);
return;
}
if (elt.__resource) {
style = elt.ownerDocument.createElement('style');
style.textContent = elt.__resource;
}
// relay on HTMLImports for path fixup
HTMLImports.path.resolveUrlsInStyle(style, elt.href);
style.textContent = ShadowCSS.shimStyle(style);
style.removeAttribute(SHIM_ATTRIBUTE, '');
style.setAttribute(SHIMMED_ATTRIBUTE, '');
style[SHIMMED_ATTRIBUTE] = true;
// place in document
if (style.parentNode !== head) {
// replace links in head
if (elt.parentNode === head) {
head.replaceChild(style, elt);
} else {
this.addElementToDocument(style);
}
}
style.__importParsed = true;
this.markParsingComplete(elt);
this.parseNext();
}
var hasResource = HTMLImports.parser.hasResource;
HTMLImports.parser.hasResource = function(node) {
if (node.localName === 'link' && node.rel === 'stylesheet' &&
node.hasAttribute(SHIM_ATTRIBUTE)) {
return (node.__resource);
} else {
return hasResource.call(this, node);
}
}
}
});
}
// exports
scope.ShadowCSS = ShadowCSS;
})(window.WebComponents);