Revert to legacy template getter, update tests.

This commit is contained in:
Kevin Schaaf
2018-07-24 11:48:02 -07:00
parent f318661416
commit c4b94a02d1
4 changed files with 86 additions and 43 deletions

View File

@@ -148,6 +148,24 @@ function GenerateClassFromInfo(info, Base) {
return info.observers;
}
/**
* @return {HTMLTemplateElement} template for this class
*/
static get template() {
if (!this.hasOwnProperty(JSCompiler_renameProperty('_template', this))) {
this._template =
// Accept template: _null to short-circuit dom-module lookup
info._template !== undefined ? info._template :
// Look in dom-module associated with this element's is
this._getTemplateFromDomModule() ||
// Look up in the chain
Base.template ||
// Use any template set on the prototype via registered callback
this.prototype._template;
}
return this._template;
}
/**
* @return {void}
*/

View File

@@ -270,29 +270,6 @@ export const ElementMixin = dedupingMixin(base => {
}
}
/**
* Look up template from dom-module for element
*
* @param {!string} is Element name to look up
* @return {!HTMLTemplateElement} Template found in dom module, or
* undefined if not found
* @private
*/
function getTemplateFromDomModule(is) {
let template = null;
// Under strictTemplatePolicy in 3.x+, dom-module lookup is only allowed
// when opted-in via allowTemplateFromDomModule
if (is && (!strictTemplatePolicy || allowTemplateFromDomModule)) {
template = DomModule.import(is, 'template');
// Under strictTemplatePolicy, require any element with an `is`
// specified to have a dom-module
if (strictTemplatePolicy && !template) {
throw new Error(`strictTemplatePolicy: expecting dom-module or null template for ${is}`);
}
}
return template;
}
/**
* @polymer
* @mixinClass
@@ -364,6 +341,30 @@ export const ElementMixin = dedupingMixin(base => {
}
}
/**
* Look up template from dom-module for element
*
* @param {!string} is Element name to look up
* @return {!HTMLTemplateElement} Template found in dom module, or
* undefined if not found
* @protected
*/
static _getTemplateFromDomModule() {
let template = null;
const is = /** @type {PolymerElementConstructor}*/ (this).is;
// Under strictTemplatePolicy in 3.x+, dom-module lookup is only allowed
// when opted-in via allowTemplateFromDomModule
if (is && (!strictTemplatePolicy || allowTemplateFromDomModule)) {
template = DomModule.import(is, 'template');
// Under strictTemplatePolicy, require any element with an `is`
// specified to have a dom-module
if (strictTemplatePolicy && !template) {
throw new Error(`strictTemplatePolicy: expecting dom-module or null template for ${is}`);
}
}
return template;
}
/**
* Returns the template that will be stamped into this element's shadow root.
*
@@ -403,17 +404,14 @@ export const ElementMixin = dedupingMixin(base => {
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) ||
this._getTemplateFromDomModule() ||
// 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;
}
return this._template;
}
}
/**
* Path matching the url from which the element was imported.

View File

@@ -23,7 +23,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
</template>
</dom-module>
<dom-module id="template-from-behavior">
<dom-module id="template-from-base">
<template>
<div id="from-base">should not be used</div>
</template>
@@ -299,9 +299,9 @@ window.templateBehavior2 = {
_template: html`<div id="from-behavior2"></div>`
};
window.templateBehaviorFromRegister = {
window.templateBehaviorFromRegistered = {
registered: function() {
this._template = html`<div id="behavior-from-register"></div>`;
this._template = html`<div id="behavior-from-registered"></div>`;
}
};
@@ -312,6 +312,13 @@ Polymer({
}
});
Polymer({
is: 'template-from-base',
behaviors: [
window.templateBehavior1
]
});
Polymer({
is: 'template-from-behavior',
behaviors: [
@@ -330,8 +337,7 @@ Polymer({
Polymer({
is: 'template-from-behavior-registered',
behaviors: [
window.templateBehavior1,
window.templateBehaviorFromRegister
window.templateBehaviorFromRegistered
]
});
</script>
@@ -366,6 +372,12 @@ Polymer({
</template>
</test-fixture>
<test-fixture id="from-base">
<template>
<template-from-base></template-from-base>
</template>
</test-fixture>
<test-fixture id="from-behavior">
<template>
<template-from-behavior></template-from-behavior>
@@ -588,9 +600,14 @@ suite('templates from behaviors', function() {
assert.ok(el.shadowRoot.querySelector('#from-registered'));
});
test('template from base', function() {
var el = fixture('from-base');
assert.ok(el.shadowRoot.querySelector('#from-base'));
assert.notOk(el.shadowRoot.querySelector('#from-behavior1'));
});
test('template from behavior', function() {
var el = fixture('from-behavior');
assert.notOk(el.shadowRoot.querySelector('#from-base'));
assert.ok(el.shadowRoot.querySelector('#from-behavior1'));
});
@@ -602,8 +619,7 @@ suite('templates from behaviors', function() {
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'));
assert.ok(el.shadowRoot.querySelector('#behavior-from-registered'));
});
});

View File

@@ -85,8 +85,12 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
suite('strictTemplatePolicy', function() {
teardown(function() {
function restoreOnError() {
window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = null;
}
teardown(function() {
restoreOnError();
document.getElementById('target').textContent = '';
});
@@ -117,6 +121,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
throw new Error(uncaughtError.message);
}
}, re);
restoreOnError();
}
test('dom-bind', function() {
@@ -181,9 +186,12 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
' </template>`' +
'</dom-module>';
}, /trusted-element re-registered/);
const el = document.createElement('trusted-element');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
let el;
assertThrows(function() {
el = document.createElement('trusted-element');
document.getElementById('target').appendChild(el);
}, /expecting dom-module or null template for trusted-element/);
assert.notOk(el && el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});
@@ -229,9 +237,12 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
' </template>`' +
'</dom-module>';
}, /trusted-element-legacy re-registered/);
const el = document.createElement('trusted-element-legacy');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
let el;
assertThrows(function() {
el = document.createElement('trusted-element-legacy');
document.getElementById('target').appendChild(el);
}, /expecting dom-module or null template for trusted-element-legacy/);
assert.notOk(el && el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});