diff --git a/lib/mixins/element-mixin.html b/lib/mixins/element-mixin.html
index 7f26212c3e..2b51ac2bfe 100644
--- a/lib/mixins/element-mixin.html
+++ b/lib/mixins/element-mixin.html
@@ -417,16 +417,20 @@
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 = 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];
+ let templateStyle = templateStyles[templateStyleIndex];
// 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 (templateStyle !== s) {
s = s.cloneNode(true);
- template.content.insertBefore(s, lastStyle);
+ templateStyle.parentNode.insertBefore(s, templateStyle);
+ } else {
+ templateStyleIndex++;
}
s.textContent = klass._processStyleText(s.textContent, baseURI);
}
diff --git a/test/unit/styling-scoped.html b/test/unit/styling-scoped.html
index 0330b19ea5..656a8ec119 100644
--- a/test/unit/styling-scoped.html
+++ b/test/unit/styling-scoped.html
@@ -660,6 +660,61 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+