Skip to content

Commit

Permalink
Sync with changes made in master.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Oct 31, 2018
1 parent 1d67cec commit 620ae42
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 55 deletions.
18 changes: 0 additions & 18 deletions lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,6 @@
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
83 changes: 53 additions & 30 deletions lib/mixins/element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
}

/**
* Returns a memoized version of the the `observers` array.
* Returns a memoized version of the `observers` array.
* @param {PolymerElementConstructor} constructor Element class
* @return {Array} Array containing own observers for the given class
* @protected
Expand All @@ -154,7 +154,7 @@
* alter these settings. However, additional `observers` may be added
* by subclasses.
*
* The info object should may contain property metadata as follows:
* The info object should contain property metadata as follows:
*
* * `type`: {function} type to which an attribute matching the property
* is deserialized. Note the property is camel-cased from a dash-cased
Expand All @@ -166,7 +166,7 @@
* property 'foo',
*
* * `computed`: {string} creates a computed property. A computed property
* also automatically is set to `readOnly: true`. The value is calculated
* is also automatically set to `readOnly: true`. The value is calculated
* by running a method and arguments parsed from the given string. For
* example 'compute(foo)' will compute a given property when the
* 'foo' property changes by executing the 'compute' method. This method
Expand Down Expand Up @@ -277,6 +277,27 @@
}

/**
* 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
*/
function getTemplateFromDomModule(is) {
let template = null;
if (is && Polymer.DomModule) {
template = Polymer.DomModule.import(is, 'template');
// Under strictTemplatePolicy, require any element with an `is`
// specified to have a dom-module
if (Polymer.strictTemplatePolicy && !template) {
throw new Error(`strictTemplatePolicy: expecting dom-module or null template for ${is}`);
}
}
return template;
}

/**
* @polymer
* @mixinClass
* @unrestricted
Expand Down Expand Up @@ -348,30 +369,6 @@
}
}

/**
* 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 && Polymer.DomModule) {
template = Polymer.DomModule.import(is, 'template');
// Under strictTemplatePolicy, require any element with an `is`
// specified to have a dom-module
if (Polymer.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 @@ -410,25 +407,51 @@
* @return {HTMLTemplateElement|string} Template to be stamped
*/
static get template() {
// Explanation of template-related properties:
// - constructor.template (this getter): the template for the class.
// This can come from the prototype (for legacy elements), from a
// dom-module, or from the super class's template (or can be overridden
// altogether by the user)
// - constructor._template: memoized version of constructor.template
// - prototype._template: working template for the element, which will be
// parsed and modified in place. It is a cloned version of
// constructor.template, saved in _finalizeClass(). Note that before
// this getter is called, for legacy elements this could be from a
// _template field on the info object passed to Polymer(), a behavior,
// 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))) {
this._template =
// If user has put template on prototype (e.g. in legacy via registered
// callback or info object), prefer that first
this.prototype.hasOwnProperty(JSCompiler_renameProperty('_template', this.prototype)) ?
this.prototype._template :
// Look in dom-module associated with this element's is
this._getTemplateFromDomModule() ||
(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;
Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.template);
}
return this._template;
}

/**
* Set the template.
*
* @param {!HTMLTemplateElement|string} value Template to set.
*/
static set template(value) {
this._template = value;
}

/**
* Path matching the url from which the element was imported.
*
* This path is used to resolve url's in template style cssText.
* The `importPath` property is also set on element instances and can be
* used to create bindings relative to the import path.
*
* For elements defined in ES modules, users should implement
* For elements defined in ES modules, users should implement
* `static get importMeta() { return import.meta; }`, and the default
* implementation of `importPath` will return `import.meta.url`'s path.
* For elements defined in HTML imports, this getter will return the path
Expand Down
15 changes: 8 additions & 7 deletions test/unit/strict-template-policy.html
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
assertThrows(function() {
el = document.createElement('trusted-element');
document.getElementById('target').appendChild(el);
}, /expecting dom-module or null template/);
}, /expecting dom-module or null template for trusted-element/);
assert.notOk(el && el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});
Expand All @@ -238,7 +238,7 @@
}, /trusted-element re-registered/);
const el = document.createElement('trusted-element');
document.getElementById('target').appendChild(el);
assert.notOk(el && el.shadowRoot);
assert.notOk(el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});

Expand Down Expand Up @@ -273,7 +273,7 @@
assertThrows(function() {
el = document.createElement('trusted-element-legacy');
document.getElementById('target').appendChild(el);
}, /expecting dom-module or null template/);
}, /expecting dom-module or null template for trusted-element-legacy/);
assert.notOk(el && el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});
Expand All @@ -287,9 +287,9 @@
' </template>`' +
'</dom-module>';
}, /trusted-element-legacy re-registered/);
const el = document.createElement('trusted-element');
const el = document.createElement('trusted-element-legacy');
document.getElementById('target').appendChild(el);
assert.notOk(el && el.shadowRoot);
assert.notOk(el.shadowRoot);
assert.notOk(document.getElementById('injected'));
});

Expand All @@ -303,7 +303,7 @@
Polymer({
is: 'has-no-template-legacy',
_template: null
}, /expecting dom-module or null template/);
});
let el = document.createElement('has-no-template-legacy');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
Expand Down Expand Up @@ -331,7 +331,7 @@
}, /expecting dom-module or null template/);
});

test('template helpers in trusted templates work', function() {
test('template helpers in trusted templates work', function() {
var el = document.createElement('trusted-templates');
document.getElementById('target').appendChild(el);
Polymer.flush();
Expand All @@ -352,6 +352,7 @@
});

});

</script>

</body>
Expand Down

0 comments on commit 620ae42

Please sign in to comment.