Skip to content

Commit

Permalink
Handle case where there are no elements in the template
Browse files Browse the repository at this point in the history
Always put `<link rel="import" type="css">` styles at the top
  • Loading branch information
dfreedm committed Dec 13, 2017
1 parent 706e602 commit 2d6b468
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
19 changes: 12 additions & 7 deletions lib/mixins/element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,22 @@
* @private
*/
function processElementStyles(klass, template, is, baseURI) {
const styles = Polymer.StyleGather.stylesFromModuleImports(is).concat(
Polymer.StyleGather.stylesFromTemplate(template));
const templateStyles = template.content.querySelectorAll('style');
const stylesWithImports = Polymer.StyleGather.stylesFromTemplate(template);
// insert styles from <link rel="import" type="css"> at the top of the template
const linkedStyles = Polymer.StyleGather.stylesFromModuleImports(is);
const firstTemplateChild = template.content.firstElementChild;
for (let idx = 0; idx < linkedStyles.length; idx++) {
let s = linkedStyles[idx];
s.textContent = klass._processStyleText(s.textContent, baseURI);
template.content.insertBefore(s, firstTemplateChild);
}
// 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 no styles in the template, fall back to first element child of template
let templateStyle = templateStyles.length ?
templateStyles[templateStyleIndex] : template.content.firstElementChild;
for (let i = 0; i < stylesWithImports.length; i++) {
let s = stylesWithImports[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 before the style that included it
if (templateStyle !== s) {
Expand Down
13 changes: 13 additions & 0 deletions test/unit/styling-import-host.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
:host {
display: block;
border: 1px solid black;
}
13 changes: 13 additions & 0 deletions test/unit/styling-import-host2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
:host {
display: block;
border: 2px solid black;
}
9 changes: 4 additions & 5 deletions test/unit/styling-scoped.html
Original file line number Diff line number Diff line change
Expand Up @@ -716,10 +716,9 @@
</dom-module>

<dom-module id="x-only-link-style">
<link rel="import" type="css" href="styling-import1.css">
<template>
<div id="one"></div>
</template>
<link rel="import" type="css" href="styling-import-host.css">
<link rel="import" type="css" href="styling-import-host2.css">
<template></template>
<script>
addEventListener('WebComponentsReady', () => {
class XOnlyLink extends Polymer.Element {
Expand Down Expand Up @@ -1003,7 +1002,7 @@
test('elements without styles work', function() {
var e = document.createElement('x-only-link-style');
document.body.appendChild(e);
assertComputed(e.$.one, '1px');
assertComputed(e, '2px');
});

});
Expand Down

0 comments on commit 2d6b468

Please sign in to comment.