Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept function in legacy _template field for template parsing #5661

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/mixins/element-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,14 @@ export const ElementMixin = dedupingMixin(base => {
// or set in registered(); once the static getter runs, a clone of it
// will overwrite it on the prototype as the working template.
if (!this.hasOwnProperty(JSCompiler_renameProperty('_template', this))) {
const protoTemplate = this.prototype.hasOwnProperty(
let protoTemplate = this.prototype.hasOwnProperty(
JSCompiler_renameProperty('_template', this.prototype)) ?
this.prototype._template : undefined;
// Accept a function for the legacy Polymer({_template:...}) field for
// lazy parsing
if (typeof protoTemplate === 'function') {
protoTemplate = protoTemplate();
}
this._template =
// If user has put template on prototype (e.g. in legacy via registered
// callback or info object), prefer that first. Note that `null` is
Expand Down
49 changes: 48 additions & 1 deletion test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,23 @@
]
});

const getTemplateFromFunction = () => html`<div id="from-function">[[prop]]</div>`;
Polymer({
is: 'template-from-function',
properties: { prop: {value: 'from-function'} },
_template: getTemplateFromFunction
});

const getTemplateFromBehaviorFunction = () => html`<div id="from-behavior-function">[[prop]]</div>`;
const functionTemplateBehavior = {
_template: getTemplateFromBehaviorFunction
};
Polymer({
is: 'template-from-behavior-function',
properties: { prop: {value: 'from-behavior-function'} },
behaviors: [functionTemplateBehavior]
});

window.ModifyObserversBehavior = {

__barChangedCalled: 0,
Expand Down Expand Up @@ -495,6 +512,19 @@
</template>
</test-fixture>

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

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


<test-fixture id="modify-observers-via-behavior">
<template>
<modify-observers-via-behavior></modify-observers-via-behavior>
Expand Down Expand Up @@ -649,7 +679,6 @@

});


suite('multi-behaviors element', function() {
var el;

Expand Down Expand Up @@ -792,6 +821,24 @@

});

suite('template from function', function() {

test('template function assigned to _template', () => {
var el = fixture('from-function');
const div = el.shadowRoot.querySelector('#from-function');
assert.ok(div);
assert.equal(div.textContent, 'from-function');
});

test('template function assigned to _template in behavior', () => {
var el = fixture('from-behavior-function');
const div = el.shadowRoot.querySelector('#from-behavior-function');
assert.ok(div);
assert.equal(div.textContent, 'from-behavior-function');
});

});

</script>

</body>
Expand Down