Skip to content

Commit

Permalink
Revert to legacy template getter, update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Jul 24, 2018
1 parent f318661 commit c4b94a0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 43 deletions.
18 changes: 18 additions & 0 deletions lib/legacy/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
54 changes: 26 additions & 28 deletions lib/mixins/element-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 24 additions & 8 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</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>
Expand Down Expand Up @@ -299,9 +299,9 @@
_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>`;
}
};

Expand All @@ -312,6 +312,13 @@
}
});

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

Polymer({
is: 'template-from-behavior',
behaviors: [
Expand All @@ -330,8 +337,7 @@
Polymer({
is: 'template-from-behavior-registered',
behaviors: [
window.templateBehavior1,
window.templateBehaviorFromRegister
window.templateBehaviorFromRegistered
]
});
</script>
Expand Down Expand Up @@ -366,6 +372,12 @@
</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>
Expand Down Expand Up @@ -588,9 +600,14 @@
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'));
});

Expand All @@ -602,8 +619,7 @@

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'));
});

});
Expand Down
25 changes: 18 additions & 7 deletions test/unit/strict-template-policy.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@

suite('strictTemplatePolicy', function() {

teardown(function() {
function restoreOnError() {
window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = null;
}

teardown(function() {
restoreOnError();
document.getElementById('target').textContent = '';
});

Expand Down Expand Up @@ -117,6 +121,7 @@
throw new Error(uncaughtError.message);
}
}, re);
restoreOnError();
}

test('dom-bind', function() {
Expand Down Expand Up @@ -181,9 +186,12 @@
' </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'));
});

Expand Down Expand Up @@ -229,9 +237,12 @@
' </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'));
});

Expand Down

0 comments on commit c4b94a0

Please sign in to comment.