Skip to content

Commit

Permalink
More updates based on code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Jul 24, 2018
1 parent ed79071 commit 275491e
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 18 deletions.
5 changes: 0 additions & 5 deletions lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,6 @@
Polymer.LegacyElementMixin(HTMLElement));
// decorate klass with registration info
klass.is = info.is;
// if user provided template on info, make sure the static _template
// is set so the static template getter uses it
if (info._template !== undefined) {
klass._template = info._template;
}
return klass;
};

Expand Down
8 changes: 4 additions & 4 deletions lib/mixins/element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@
static get template() {
if (!this.hasOwnProperty(JSCompiler_renameProperty('_template', this))) {
this._template =
// Take any template set on the prototype, including null (for legacy
// support, setting in registered callback, etc.)
this.prototype._template !== undefined ? this.prototype._template :
// Look in dom-module associated with this element's is
getTemplateFromDomModule(/** @type {PolymerElementConstructor}*/ (this).is) ||
// Next look for superclass template (call the super impl this
// way so that `this` points to the superclass)
Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.template ||
// Finally, fall back to any _template set on prototype, e.g.
// via registered callback
this.prototype._template;
Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.template;
}
return this._template;
}
Expand Down
103 changes: 103 additions & 0 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
</template>
</dom-module>

<dom-module id="template-from-behavior">
<template>
<div id="from-base">should not be used</div>
</template>
</dom-module>

<script>
HTMLImports.whenReady(function() {
window.BehaviorA = {
Expand Down Expand Up @@ -283,6 +289,51 @@

is: 'behavior-registered'
});

window.templateBehavior1 = {
_template: Polymer.html`<div id="from-behavior1"></div>`
};

window.templateBehavior2 = {
_template: Polymer.html`<div id="from-behavior2"></div>`
};

window.templateBehaviorFromRegister = {
registered: function() {
this._template = Polymer.html`<div id="behavior-from-register"></div>`;
}
};

Polymer({
is: 'template-from-registered',
registered: function() {
this._template = Polymer.html`<div id="from-registered"></div>`;
}
});

Polymer({
is: 'template-from-behavior',
behaviors: [
window.templateBehavior1
]
});

Polymer({
is: 'template-from-behavior-overridden',
behaviors: [
window.templateBehavior1,
window.templateBehavior2
]
});

Polymer({
is: 'template-from-behavior-registered',
behaviors: [
window.templateBehavior1,
templateBehaviorFromRegister
]
});

});
</script>

Expand All @@ -309,6 +360,31 @@
<behavior-registered></behavior-registered>
</template>
</test-fixture>

<test-fixture id="from-registered">
<template>
<template-from-registered></template-from-registered>
</template>
</test-fixture>

<test-fixture id="from-behavior">
<template>
<template-from-behavior></template-from-behavior>
</template>
</test-fixture>

<test-fixture id="from-behavior-overridden">
<template>
<template-from-behavior-overridden></template-from-behavior-overridden>
</template>
</test-fixture>

<test-fixture id="from-behavior-registered">
<template>
<template-from-behavior-registered></template-from-behavior-registered>
</template>
</test-fixture>

<script>

suite('single behavior element', function() {
Expand Down Expand Up @@ -505,6 +581,33 @@

});

suite('templates from behaviors', function() {

test('template from registered callback', function() {
var el = fixture('from-registered');
assert.ok(el.shadowRoot.querySelector('#from-registered'));
});

test('template from behavior', function() {
var el = fixture('from-behavior');
assert.notOk(el.shadowRoot.querySelector('#from-base'));
assert.ok(el.shadowRoot.querySelector('#from-behavior1'));
});

test('template from overriding behavior', function() {
var el = fixture('from-behavior-overridden');
assert.notOk(el.shadowRoot.querySelector('#from-behavior1'));
assert.ok(el.shadowRoot.querySelector('#from-behavior2'));
});

test('template from behavior registered callback', function() {
var el = fixture('from-behavior-registered');
assert.notOk(el.shadowRoot.querySelector('#from-behavior1'));
assert.ok(el.shadowRoot.querySelector('#behavior-from-register'));
});

});

</script>

</body>
Expand Down
52 changes: 43 additions & 9 deletions test/unit/strict-template-policy.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,27 @@
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-module>' +
'<trusted-element></trusted-element>';
'</dom-module>';
}, /trusted-element re-registered/);
const el = document.querySelector('trusted-element');
assert.notOk(el && el.shadowRoot && el.shadowRoot.querySelector('#injected'));
const el = document.createElement('trusted-element');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});

test('dom-module after registration, again', function() {
assertThrows(function() {
document.getElementById('target').innerHTML =
'<dom-module id="trusted-element">' +
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-module>';
}, /trusted-element re-registered/);
const el = document.createElement('trusted-element');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});

test('dom-module before registration', function() {
Expand All @@ -179,6 +195,7 @@
let el = document.createElement('has-no-template');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});

test('dom-module after registration (legacy)', function() {
Expand All @@ -188,11 +205,27 @@
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-module>' +
'<trusted-element-legacy></trusted-element-legacy>';
'</dom-module>';
}, /trusted-element-legacy re-registered/);
const el = document.querySelector('trusted-element-legacy');
assert.notOk(el && el.shadowRoot && el.shadowRoot.querySelector('#injected'));
const el = document.createElement('trusted-element-legacy');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});

test('dom-module after registration, again (legacy)', function() {
assertThrows(function() {
document.getElementById('target').innerHTML =
'<dom-module id="trusted-element-legacy">' +
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-module>';
}, /trusted-element-legacy re-registered/);
const el = document.createElement('trusted-element-legacy');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});

test('dom-module before registration (legacy)', function() {
Expand All @@ -208,7 +241,8 @@
});
let el = document.createElement('has-no-template-legacy');
document.getElementById('target').appendChild(el);
assert.notOk(document.querySelector('has-no-template-legacy').shadowRoot);
assert.notOk(el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});

test('element without explicit template throws', function() {
Expand Down

0 comments on commit 275491e

Please sign in to comment.