Skip to content

Commit dbfe8f8

Browse files
author
Steven Orvell
committed
Correct style ordering when remote and static resources are mixed.
1 parent d6f91a4 commit dbfe8f8

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

src/standard/styling.html

+23-27
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050

5151
// search for extra style modules via `styleModules`
52+
// TODO(sorvell): consider dropping support for `styleModules`
5253
_prepareStyles: function() {
5354
var cssText = '', m$ = this.styleModules;
5455
if (m$) {
@@ -72,34 +73,29 @@
7273
var m = Polymer.DomModule.import(moduleId);
7374
if (m && !m._cssText) {
7475
var cssText = '';
75-
var e$ = Array.prototype.slice.call(m.querySelectorAll('style'));
76-
this._unapplyStyles(e$);
77-
e$ = e$.concat(Array.prototype.map.call(
78-
//TODO(sorvell): add test for 404'ing import
79-
m.querySelectorAll(REMOTE_SHEET_SELECTOR), function(l) {
80-
return l.import && l.import.body;
81-
}));
82-
m._cssText = this._cssFromStyles(e$);
83-
}
84-
return m && m._cssText || '';
85-
},
86-
87-
_cssFromStyles: function(styles) {
88-
var cssText = '';
89-
for (var i=0, l=styles.length, s; (i<l) && (s = styles[i]); i++) {
90-
if (s && s.textContent) {
91-
cssText +=
92-
Polymer.ResolveUrl.resolveCss(s.textContent, s.ownerDocument);
76+
var e$ = Array.prototype.slice.call(
77+
m.querySelectorAll(STYLES_SELECTOR));
78+
for (var i=0, e; i < e$.length; i++) {
79+
e = e$[i];
80+
// style elements inside dom-modules will apply to the main document
81+
// we don't want this, so we remove them here.
82+
if (e.localName === 'style') {
83+
// get style element applied to main doc via HTMLImports polyfill
84+
e = e.__appliedElement || e;
85+
e.parentNode.removeChild(e);
86+
// it's an import, assume this is a text file of css content.
87+
} else {
88+
e = e.import && e.import.body;
89+
}
90+
// adjust paths in css.
91+
if (e) {
92+
cssText +=
93+
Polymer.ResolveUrl.resolveCss(e.textContent, e.ownerDocument);
94+
}
9395
}
96+
m._cssText = cssText;
9497
}
95-
return cssText;
96-
},
97-
98-
_unapplyStyles: function(styles) {
99-
for (var i=0, l=styles.length, s; (i<l) && (s = styles[i]); i++) {
100-
s = s.__appliedElement || s;
101-
s.parentNode.removeChild(s);
102-
}
98+
return m && m._cssText || '';
10399
},
104100

105101
_transformStyles: function(styles, callback) {
@@ -188,7 +184,7 @@
188184

189185
});
190186

191-
var REMOTE_SHEET_SELECTOR = 'link[rel=import][type~=css]';
187+
var STYLES_SELECTOR = 'style, link[rel=import][type~=css]';
192188

193189
})();
194190

test/unit/styling-remote-elements.html

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
<dom-module id="x-order">
2+
<link rel="import" type="css" href="styling-remote-sheet.css">
3+
<style>
4+
:host {
5+
margin: 10px;
6+
}
7+
8+
.border {
9+
border: 10px solid seagreen;
10+
}
11+
</style>
12+
<template>
13+
<div id="me" class="border">border should be 10px</div>
14+
</template>
15+
<script>
16+
Polymer({is: 'x-order'});
17+
</script>
18+
</dom-module>
19+
120
<dom-module id="x-child">
221
<template>
322
<div id="simple">simple</div>

test/unit/styling-remote-sheet.css

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818
.computed {
1919
border: 15px solid orange;
2020
}
21+
22+
.border {
23+
border: 1px solid red;
24+
}

test/unit/styling-remote.html

+10-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
<x-dynamic-scope></x-dynamic-scope>
4040

41+
<x-order></x-order>
42+
4143
<script>
4244
suite('scoped-styling', function() {
4345

@@ -166,7 +168,13 @@
166168
assertComputed(b, '18px');
167169
});
168170

169-
if (window.Polymer && !Polymer.Settings.useNativeShadow) {
171+
test('styles ordered correctly when remote and static resources are mixed', function() {
172+
var order = document.querySelector('x-order');
173+
assertComputed(order.$.me, '10px');
174+
});
175+
176+
// weird check allows testing under HTMLImports polyfill
177+
if (!window.Polymer || !Polymer.Settings.useNativeShadow) {
170178

171179
suite('scoped-styling-shady-only', function() {
172180

@@ -176,7 +184,7 @@
176184

177185
test('styles shimmed in registration order', function() {
178186
var s$ = document.head.querySelectorAll('style[scope]');
179-
var expected = ['x-child2', 'x-styled', 'x-button', 'x-dynamic-scope'];
187+
var expected = ['x-order', 'x-child2', 'x-styled', 'x-button', 'x-dynamic-scope'];
180188
var actual = [];
181189
for (var i=0; i<s$.length; i++) {
182190
actual.push(s$[i].getAttribute('scope'));

0 commit comments

Comments
 (0)