@@ -270,6 +270,29 @@ export const ElementMixin = dedupingMixin(base => {
270270 }
271271 }
272272
273+ /**
274+ * Look up template from dom-module for element
275+ *
276+ * @param {!string } is Element name to look up
277+ * @return {!HTMLTemplateElement } Template found in dom module, or
278+ * undefined if not found
279+ * @protected
280+ */
281+ function getTemplateFromDomModule ( is ) {
282+ let template = null ;
283+ // Under strictTemplatePolicy in 3.x+, dom-module lookup is only allowed
284+ // when opted-in via allowTemplateFromDomModule
285+ if ( is && ( ! strictTemplatePolicy || allowTemplateFromDomModule ) ) {
286+ template = DomModule . import ( is , 'template' ) ;
287+ // Under strictTemplatePolicy, require any element with an `is`
288+ // specified to have a dom-module
289+ if ( strictTemplatePolicy && ! template ) {
290+ throw new Error ( `strictTemplatePolicy: expecting dom-module or null template for ${ is } ` ) ;
291+ }
292+ }
293+ return template ;
294+ }
295+
273296 /**
274297 * @polymer
275298 * @mixinClass
@@ -341,30 +364,6 @@ export const ElementMixin = dedupingMixin(base => {
341364 }
342365 }
343366
344- /**
345- * Look up template from dom-module for element
346- *
347- * @param {!string } is Element name to look up
348- * @return {!HTMLTemplateElement } Template found in dom module, or
349- * undefined if not found
350- * @protected
351- */
352- static _getTemplateFromDomModule ( ) {
353- let template = null ;
354- const is = /** @type {PolymerElementConstructor }*/ ( this ) . is ;
355- // Under strictTemplatePolicy in 3.x+, dom-module lookup is only allowed
356- // when opted-in via allowTemplateFromDomModule
357- if ( is && ( ! strictTemplatePolicy || allowTemplateFromDomModule ) ) {
358- template = DomModule . import ( is , 'template' ) ;
359- // Under strictTemplatePolicy, require any element with an `is`
360- // specified to have a dom-module
361- if ( strictTemplatePolicy && ! template ) {
362- throw new Error ( `strictTemplatePolicy: expecting dom-module or null template for ${ is } ` ) ;
363- }
364- }
365- return template ;
366- }
367-
368367 /**
369368 * Returns the template that will be stamped into this element's shadow root.
370369 *
@@ -402,13 +401,30 @@ export const ElementMixin = dedupingMixin(base => {
402401 * @return {HTMLTemplateElement|string } Template to be stamped
403402 */
404403 static get template ( ) {
404+ // Explanation of template-related properties:
405+ // - constructor.template (this getter): the template for the class.
406+ // This can come from the prototype (for legacy elements), from a
407+ // dom-module, or from the super class's template (or can be overridden
408+ // altogether by the user)
409+ // - constructor._template: memoized version of constructor.template
410+ // - prototype._template: working template for the element, which will be
411+ // parsed and modified in place. It is a cloned version of
412+ // constructor.template, saved in _finalizeClass(). Note that before
413+ // this getter is called, for legacy elements this could be from a
414+ // _template field on the info object passed to Polymer(), a behavior,
415+ // or set in registered(); once the static getter runs, a clone of it
416+ // will overwrite it on the prototype as the working template.
405417 if ( ! this . hasOwnProperty ( JSCompiler_renameProperty ( '_template' , this ) ) ) {
406418 this . _template =
419+ // If user has put template on prototype (e.g. in legacy via registered
420+ // callback or info object), prefer that first
421+ this . prototype . hasOwnProperty ( JSCompiler_renameProperty ( '_template' , this . prototype ) ) ?
422+ this . prototype . _template :
407423 // Look in dom-module associated with this element's is
408- this . _getTemplateFromDomModule ( ) ||
424+ ( getTemplateFromDomModule ( /** @type { PolymerElementConstructor }*/ ( this ) . is ) ||
409425 // Next look for superclass template (call the super impl this
410426 // way so that `this` points to the superclass)
411- Object . getPrototypeOf ( /** @type {PolymerElementConstructor }*/ ( this ) . prototype ) . constructor . template ;
427+ Object . getPrototypeOf ( /** @type {PolymerElementConstructor }*/ ( this ) . prototype ) . constructor . template ) ;
412428 }
413429 return this . _template ;
414430 }
0 commit comments