Skip to content

Commit 679a49e

Browse files
committed
imported css modules should always be before element's styles
This is still slightly different from 1.x, which used ordering, but most `<link rel="import" type="css">` style imports came before the template. Fixes #4770
1 parent b9c3688 commit 679a49e

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

lib/mixins/element-mixin.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@
416416
function finalizeTemplate(proto, template, baseURI, is, ext) {
417417
// support `include="module-name"`
418418
let cssText =
419-
Polymer.StyleGather.cssFromTemplate(template, baseURI) +
420-
Polymer.StyleGather.cssFromModuleImports(is);
419+
Polymer.StyleGather.cssFromModuleImports(is) +
420+
Polymer.StyleGather.cssFromTemplate(template, baseURI);
421421
if (cssText) {
422422
let style = document.createElement('style');
423423
style.textContent = cssText;

lib/utils/style-gather.html

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@
7070
cssFromModule(moduleId) {
7171
let m = importModule(moduleId);
7272
if (m && m._cssText === undefined) {
73-
let cssText = '';
73+
// module imports: <link rel="import" type="css">
74+
let cssText = this._cssFromModuleImports(m);
7475
// include css from the first template in the module
7576
let t = m.querySelector('template');
7677
if (t) {
77-
cssText += this.cssFromTemplate(t, /** @type {templateWithAssetPath }*/(m).assetpath);
78+
cssText += this.cssFromTemplate(t, /** @type {templateWithAssetPath} */(m).assetpath);
7879
}
79-
// module imports: <link rel="import" type="css">
80-
cssText += this.cssFromModuleImports(moduleId);
8180
m._cssText = cssText || null;
8281
}
8382
if (!m) {
@@ -126,12 +125,18 @@
126125
* @this {StyleGather}
127126
*/
128127
cssFromModuleImports(moduleId) {
129-
let cssText = '';
130128
let m = importModule(moduleId);
131-
if (!m) {
132-
return cssText;
133-
}
134-
let p$ = m.querySelectorAll(MODULE_STYLE_LINK_SELECTOR);
129+
return m ? this._cssFromModuleImports(m) : '';
130+
},
131+
/**
132+
* @memberof Polymer.StyleGather
133+
* @this {StyleGather}
134+
* @param {!HTMLElement} module dom-module element that could contain `<link rel="import" type="css">` styles
135+
* @return {string} Concatenated CSS content from links in the dom-module
136+
*/
137+
_cssFromModuleImports(module) {
138+
let cssText = '';
139+
let p$ = module.querySelectorAll(MODULE_STYLE_LINK_SELECTOR);
135140
for (let i=0; i < p$.length; i++) {
136141
let p = p$[i];
137142
if (p.import) {

test/unit/styling-import.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@
5252
</script>
5353
</dom-module>
5454

55+
<dom-module id="x-import-order">
56+
<link rel="import" type="css" href="styling-import2.css">
57+
<template>
58+
<style>
59+
#two {
60+
border: 1px solid black;
61+
}
62+
</style>
63+
<div id="two"></div>
64+
</template>
65+
<script>
66+
HTMLImports.whenReady(function() {
67+
Polymer({is: 'x-import-order'});
68+
})
69+
</script>
70+
</dom-module>
71+
5572

5673
<script>
5774
suite('style-imports', function() {
@@ -85,6 +102,14 @@
85102

86103
});
87104

105+
test('styles included via link rel="import" type="css" are overridden by styles in template', function() {
106+
var el = document.createElement('x-import-order');
107+
document.body.appendChild(el);
108+
Polymer.flush();
109+
assertComputed(el.$.two, '1px');
110+
document.body.removeChild(el);
111+
});
112+
88113
});
89114

90115
</script>

0 commit comments

Comments
 (0)