Skip to content

Commit 8a4c00c

Browse files
jcmooreJustin Moore
authored and
Justin Moore
committed
Fixes #3676: retain <style in <template preserve-content/>
1 parent 409ad83 commit 8a4c00c

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/lib/style-util.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@
142142
e = e$[i];
143143
// look inside templates for elements
144144
if (e.localName === 'template') {
145-
cssText += this.cssFromElement(e);
145+
// retain css content when specified,
146+
if (!e.hasAttribute('preserve-content')) {
147+
cssText += this.cssFromElement(e);
148+
}
146149
} else {
147150
// style elements inside dom-modules will apply to the main document
148151
// we don't want this, so we remove them here.

test/runner.html

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
'unit/custom-style-late.html',
6666
'unit/dynamic-import.html',
6767
'unit/templatizer.html',
68+
'unit/template-preserve-content.html',
6869
'unit/dom-repeat.html',
6970
'unit/dom-if.html',
7071
'unit/dom-template.html',
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!DOCTYPE html>
2+
<!--
3+
@license
4+
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8+
Code distributed by Google as part of the polymer project is also
9+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10+
-->
11+
<!DOCTYPE html>
12+
<html>
13+
<head>
14+
<meta charset='utf-8'>
15+
<script src='../../../webcomponentsjs/webcomponents-lite.js'></script>
16+
<script src='../../../web-component-tester/browser.js'></script>
17+
<link rel='import' href='../../polymer.html'>
18+
</head>
19+
<body>
20+
<preservation-templatizer></preservation-templatizer>
21+
22+
<template id='stampable'>
23+
<h2>Unmodified</h2>
24+
<span id='html'>{{html}}</span>
25+
<template preserve-content id='unmodified'>
26+
<style>
27+
[attribute] {
28+
display: none,
29+
}
30+
</style>
31+
<div>style tag present</div>
32+
</template>
33+
</template>
34+
35+
<dom-module id='preservation-templatizer'>
36+
37+
<template>
38+
<h2>Modified</h2>
39+
<span>{{html}}<span/>
40+
<template preserve-content id='modified'>
41+
<style>
42+
#identifier {
43+
color: blue;
44+
}
45+
</style>
46+
<div>style tag missing</div>
47+
</template>
48+
</template>
49+
<script>
50+
HTMLImports.whenReady(function() {
51+
Polymer({
52+
is: 'preservation-templatizer',
53+
behaviors: [
54+
Polymer.Templatizer
55+
],
56+
created: function () {
57+
this.templatize(document.body.querySelector('#stampable'));
58+
},
59+
ready: function () {
60+
var div = document.createElement('DIV');
61+
var instance = this.stamp({html: 'unresolved'});
62+
var unmodified = instance.root.querySelector('#unmodified');
63+
64+
div.innerHTML = (this.$.modified).innerHTML;
65+
console.log(div.firstElementChild.localName, div.firstElementChild);
66+
67+
div.innerHTML = (unmodified).innerHTML;
68+
console.log(div.firstElementChild.localName, div.firstElementChild);
69+
}
70+
});
71+
});
72+
</script>
73+
</dom-module>
74+
75+
<script>
76+
suite('polymer: template preserve-content', function() {
77+
78+
test('nested <template preserve-content/> should retain <style/> in a <dom-module/>', function() {
79+
var element = document.querySelector('preservation-templatizer');
80+
assert.ok(element.$.modified.content.querySelector('style'));
81+
});
82+
83+
test('nested <template preserve-content/> should retain <style/> in a stamped <template/>', function() {
84+
var element = document.querySelector('preservation-templatizer');
85+
element.templatize(document.querySelector('#stampable'));
86+
assert.ok(element.stamp().root.querySelector('#unmodified').content.querySelector('style'));
87+
});
88+
89+
});
90+
</script>
91+
</body>
92+
</html>

0 commit comments

Comments
 (0)