Skip to content

Commit 06190c9

Browse files
committed
Fix impl of _contentForTemplate. Add template-stamp tests.
Fixes #4597
1 parent 7d5e448 commit 06190c9

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

lib/mixins/template-stamp.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@
379379
* @return {DocumentFragment} Content fragment
380380
*/
381381
static _contentForTemplate(template) {
382-
let templateInfo = template.__templateInfo;
382+
let templateInfo = template._templateInfo;
383383
return (templateInfo && templateInfo.content) || template.content;
384384
}
385385

test/runner.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
var suites = [
2727
'unit/globals.html',
2828
'unit/property-accessors.html',
29+
'unit/template-stamp.html',
2930
'unit/property-effects.html',
3031
'unit/property-effects-template.html',
3132
'unit/path-effects.html',

test/unit/template-stamp.html

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<!DOCTYPE html>
2+
<!--
3+
@license
4+
Copyright (c) 2017 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+
<html>
12+
<head>
13+
<meta charset="UTF-8">
14+
15+
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
16+
<script src="../../../web-component-tester/browser.js"></script>
17+
18+
<link rel="import" href="../../lib/mixins/template-stamp.html">
19+
</head>
20+
<body>
21+
22+
<template id="test-template">
23+
<div id="a">a</div>
24+
<div id="eventHandler" on-click="handleClick"></div>
25+
<div id="b">b</div>
26+
<template id="nestedTemplate">
27+
<div on-click="handleClick"></div>
28+
</template>
29+
<template id="preservedTemplate" preserve-content>
30+
<div on-click="shouldNotBeRemoved"></div>
31+
</template>
32+
</template>
33+
34+
<script>
35+
HTMLImports.whenReady(() => {
36+
class TemplateStamper extends Polymer.TemplateStamp(HTMLElement) {
37+
connectedCallback() {
38+
this.handleClick = sinon.spy(event => {
39+
this.handleClickTarget = event.target;
40+
});
41+
let template = document.getElementById('test-template');
42+
this.dom = this._stampTemplate(template);
43+
this.attachShadow({mode:'open'}).appendChild(this.dom);
44+
}
45+
}
46+
customElements.define('template-stamper', TemplateStamper);
47+
});
48+
</script>
49+
50+
<script>
51+
52+
suite('template-stamp', () => {
53+
54+
test('id map', () => {
55+
let el = document.createElement('template-stamper');
56+
document.body.appendChild(el);
57+
assert.equal(el.dom.$.a.textContent, 'a');
58+
assert.equal(el.dom.$.b.textContent, 'b');
59+
document.body.removeChild(el);
60+
});
61+
62+
test('declarative events', () => {
63+
let el = document.createElement('template-stamper');
64+
document.body.appendChild(el);
65+
el.dom.$.eventHandler.click();
66+
assert.equal(el.handleClick.callCount, 1);
67+
assert.equal(el.handleClick.firstCall.args[0].type, 'click');
68+
assert.equal(el.handleClickTarget, el.dom.$.eventHandler);
69+
document.body.removeChild(el);
70+
});
71+
72+
test('cached template content', () => {
73+
let el = document.createElement('template-stamper');
74+
document.body.appendChild(el);
75+
let content = el.constructor._contentForTemplate(el.dom.$.nestedTemplate);
76+
assert.isTrue(content instanceof DocumentFragment);
77+
assert.equal(content.ownerDocument, el.dom.$.nestedTemplate.content.ownerDocument);
78+
assert.equal(content.firstElementChild.getAttribute('on-click'), null);
79+
document.body.removeChild(el);
80+
});
81+
82+
test('preserved template content', () => {
83+
let el = document.createElement('template-stamper');
84+
document.body.appendChild(el);
85+
let content = el.dom.$.preservedTemplate.content;
86+
assert.isTrue(content instanceof DocumentFragment);
87+
assert.equal(content.firstElementChild.getAttribute('on-click'), 'shouldNotBeRemoved');
88+
document.body.removeChild(el);
89+
});
90+
91+
});
92+
93+
// Note template parsing API is tested in `property-effects-template.html`,
94+
// combined with adding property effects
95+
96+
</script>
97+
98+
</body>
99+
</html>

0 commit comments

Comments
 (0)