Skip to content

Commit 08c3a02

Browse files
author
Steven Orvell
committed
Prep for processing of shady-unscoped moving to ShadyCSS
1 parent 9a46833 commit 08c3a02

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

lib/mixins/element-mixin.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@
414414
Polymer.StyleGather.stylesFromTemplate(template));
415415
let templateStyles = template.content.querySelectorAll('style');
416416
let lastStyle = templateStyles[templateStyles.length-1];
417+
// ensure all gathered styles are actually in this template.
417418
for (let i=0; i < styles.length; i++) {
418419
let s = styles[i];
419420
// if the style is not in this template, it's been "included" and

lib/utils/style-gather.html

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
return PolymerDomModule.import(moduleId);
2525
}
2626

27+
function styleForImport(importDoc) {
28+
// NOTE: polyfill affordance.
29+
// under the HTMLImports polyfill, there will be no 'body',
30+
// but the import pseudo-doc can be used directly.
31+
let container = importDoc.body ? importDoc.body : importDoc;
32+
const importCss = Polymer.ResolveUrl.resolveCss(container.textContent,
33+
importDoc.baseURI);
34+
const style = document.createElement('style');
35+
style.textContent = importCss;
36+
return style;
37+
}
38+
2739
/** @typedef {{assetpath: string}} */
2840
let templateWithAssetPath; // eslint-disable-line no-unused-vars
2941

@@ -108,13 +120,10 @@
108120
if (include) {
109121
styles.push(...this.stylesFromModules(include));
110122
}
111-
if (window.ShadyDOM && e.hasAttribute(SHADY_UNSCOPED_ATTR)) {
112-
e.textContent = baseURI ?
113-
Polymer.ResolveUrl.resolveCss(e.textContent, baseURI) : e.textContent;
114-
document.head.insertBefore(e, document.head.firstChild);
115-
} else {
116-
styles.push(e);
123+
if (baseURI) {
124+
e.textContent = Polymer.ResolveUrl.resolveCss(e.textContent, baseURI);
117125
}
126+
styles.push(e);
118127
}
119128
template._styles = styles;
120129
}
@@ -147,28 +156,15 @@
147156
let p = p$[i];
148157
if (p.import) {
149158
const importDoc = p.import;
150-
if (!importDoc._style) {
151-
// NOTE: polyfill affordance.
152-
// under the HTMLImports polyfill, there will be no 'body',
153-
// but the import pseudo-doc can be used directly.
154-
let container = importDoc.body ? importDoc.body : importDoc;
155-
const importCss = Polymer.ResolveUrl.resolveCss(container.textContent,
156-
importDoc.baseURI);
157-
const style = document.createElement('style');
158-
if (p.hasAttribute(SHADY_UNSCOPED_ATTR)) {
159-
style.setAttribute(SHADY_UNSCOPED_ATTR, '');
160-
}
161-
style.textContent = importCss;
162-
importDoc._style = style;
163-
}
164-
// support the shady-unscoped promoting styles to main document
165-
if (window.ShadyDOM &&
166-
importDoc._style.hasAttribute(SHADY_UNSCOPED_ATTR) &&
167-
!importDoc._style.parentNode) {
168-
document.head.insertBefore(importDoc._style, document.head.firstChild);
169-
} else {
170-
styles.push(importDoc._style);
159+
const unscoped = p.hasAttribute(SHADY_UNSCOPED_ATTR);
160+
if (unscoped && !importDoc._unscopedStyle) {
161+
const style = styleForImport(importDoc);
162+
style.setAttribute(SHADY_UNSCOPED_ATTR, '');
163+
importDoc._unscopedStyle = style;
164+
} else if (!importDoc._style) {
165+
importDoc._style = styleForImport(importDoc);
171166
}
167+
styles.push(unscoped ? importDoc._unscopedStyle : importDoc._style);
172168
}
173169
}
174170
return styles;
@@ -218,7 +214,8 @@
218214
// include css from the first template in the module
219215
let t = m.querySelector('template');
220216
if (t) {
221-
cssText += this.cssFromTemplate(t, /** @type {templateWithAssetPath} */(m).assetpath);
217+
cssText += this.cssFromTemplate(t,
218+
/** @type {templateWithAssetPath} */(m).assetpath);
222219
}
223220
m._cssText = cssText || null;
224221
}
@@ -250,14 +247,7 @@
250247
if (e.parentNode) {
251248
e.parentNode.removeChild(e);
252249
}
253-
const styleCss = baseURI ?
254-
Polymer.ResolveUrl.resolveCss(e.textContent, baseURI) : e.textContent;
255-
if (window.ShadyDOM && e.hasAttribute(SHADY_UNSCOPED_ATTR)) {
256-
e.textContent = styleCss;
257-
document.head.insertBefore(e, document.head.firstChild);
258-
} else {
259-
cssText += styleCss;
260-
}
250+
cssText += e.textContent;
261251
}
262252
return cssText;
263253
},
@@ -294,7 +284,7 @@
294284
cssText += styles[i].textContent;
295285
}
296286
return cssText;
297-
},
287+
}
298288
};
299289

300290
Polymer.StyleGather = StyleGather;

0 commit comments

Comments
 (0)