Handle styles that are not direct children of templates correctly

Find the correct spot to add "included" styles in the template by
keeping track of which "concrete" style in the template we have
encountered and inserting the "included" styles before them.

Fixes #4975
This commit is contained in:
Daniel Freedman
2017-12-06 11:40:04 -08:00
parent 2d739c7534
commit 0b1cd70a10
2 changed files with 41 additions and 5 deletions
+9 -5
View File
@@ -417,16 +417,20 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
function processElementStyles(klass, template, is, baseURI) {
const styles = Polymer.StyleGather.stylesFromModuleImports(is).concat(
Polymer.StyleGather.stylesFromTemplate(template));
let templateStyles = template.content.querySelectorAll('style');
let lastStyle = templateStyles[templateStyles.length-1];
const templateStyles = Array.from(template.content.querySelectorAll('style'));
// keep track of the last "concrete" style in the template we have encountered
let templateStyleIndex = 0;
// ensure all gathered styles are actually in this template.
for (let i=0; i < styles.length; i++) {
let s = styles[i];
// if the style is not in this template, it's been "included" and
// we put a clone of it in the template.
if (s.getRootNode() != template.content) {
// we put a clone of it in the template before the style that included it
if (templateStyles.indexOf(s) === -1) {
s = s.cloneNode(true);
template.content.insertBefore(s, lastStyle);
let importingTemplateStyle = templateStyles[templateStyleIndex];
importingTemplateStyle.parentNode.insertBefore(s, importingTemplateStyle);
} else {
templateStyleIndex++;
}
s.textContent = klass._processStyleText(s.textContent, baseURI);
}