From 616f6662f0d51802bd9a95fc271ffcee56300c6f Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Thu, 5 Apr 2018 14:12:54 -0700 Subject: [PATCH 1/4] Add ability to define importMeta on legacy elements. Fixes #5163 --- lib/legacy/legacy-element-mixin.html | 10 ++++++++++ lib/mixins/element-mixin.html | 24 +++++++----------------- test/unit/resolveurl.html | 4 +++- test/unit/sub/resolveurl-elements.html | 16 ++++++++++++++-- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/legacy/legacy-element-mixin.html b/lib/legacy/legacy-element-mixin.html index 6c2045f155..ab6d1e1872 100644 --- a/lib/legacy/legacy-element-mixin.html +++ b/lib/legacy/legacy-element-mixin.html @@ -86,6 +86,16 @@ this._applyListeners(); } + /** + * Forwards `importMeta` from the prototype (i.e. from the info object + * passed to `Polymer({...})`) to the static API. + * + * @return {!Object} The `import.meta` object set on the prototype + */ + static get importMeta() { + return this.prototype.importMeta; + } + /** * Legacy callback called during the `constructor`, for overriding * by the user. diff --git a/lib/mixins/element-mixin.html b/lib/mixins/element-mixin.html index 677767d71f..b58b818ded 100644 --- a/lib/mixins/element-mixin.html +++ b/lib/mixins/element-mixin.html @@ -402,11 +402,13 @@ * 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 `importMeta` - * and this getter will return `import.meta.url`'s path. For elements - * defined in HTML imports, this getter will return the path to the - * document containing a `dom-module` element matching this element's - * static `is` property. + * + * 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 + * to the document containing a `dom-module` element matching this + * element's static `is` property. * * Note, this path should contain a trailing `/`. * @@ -426,18 +428,6 @@ return this._importPath; } - /** - * When an element definition is being loaded from an ES module, users - * may override this getter to return the `import.meta` object from that - * module, which will be used to derive the `importPath` for the element. - * When implementing `importMeta`, users should not implement `importPath`. - * - * @return {!Object} The `import.meta` object for the element's module - */ - static get importMeta() { - return null; - } - constructor() { super(); /** @type {HTMLTemplateElement} */ diff --git a/test/unit/resolveurl.html b/test/unit/resolveurl.html index 5cd59980ca..9da04704ad 100644 --- a/test/unit/resolveurl.html +++ b/test/unit/resolveurl.html @@ -82,7 +82,9 @@ test('Urls in styles and attributes', testStylesAndAttributes('p-r', 'sub')); - test('Urls in styles and attributes (importMeta)', testStylesAndAttributes('p-r-im', 'http://foo.com/mymodule')); + test('Urls in styles and attributes (importMeta)', testStylesAndAttributes('p-r-im', 'http://class.com/mymodule')); + + test('Urls in styles and attributes (importMeta, hybrid)', testStylesAndAttributes('p-r-hybrid', 'http://hybrid.com/mymodule')); test('url changes via setting importPath/rootPath on element instance', function() { var el = document.createElement('p-r'); diff --git a/test/unit/sub/resolveurl-elements.html b/test/unit/sub/resolveurl-elements.html index 30611b6d24..da180ee99e 100644 --- a/test/unit/sub/resolveurl-elements.html +++ b/test/unit/sub/resolveurl-elements.html @@ -39,14 +39,26 @@ } customElements.define(PR.is, PR); - class PRImportMeta extends PR { + class PRImportMeta extends Polymer.Element { + static get template() { + return Polymer.DomModule.import('p-r', 'template').cloneNode(true); + } static get importMeta() { // Idiomatically, this would be `return import.meta`, but for purposes // of stubbing the test without actual modules, it's shimmed - return { url: 'http://foo.com/mymodule/index.js' } + return { url: 'http://class.com/mymodule/index.js' }; } } customElements.define('p-r-im', PRImportMeta); + + const PRHybrid = Polymer({ + is: 'p-r-hybrid', + _template: Polymer.DomModule.import('p-r', 'template').cloneNode(true), + // Idiomatically, this would be `return import.meta`, but for purposes + // of stubbing the test without actual modules, it's shimmed + importMeta: { url: 'http://hybrid.com/mymodule/index.js' } + }); + From 412bb1e019baa11683a553e114b77e72f0ccbbd4 Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Thu, 5 Apr 2018 14:59:48 -0700 Subject: [PATCH 2/4] Avoid closure warnings. --- lib/legacy/legacy-element-mixin.html | 1 + lib/legacy/polymer.dom.html | 2 +- lib/mixins/element-mixin.html | 3 ++- lib/mixins/property-effects.html | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/legacy/legacy-element-mixin.html b/lib/legacy/legacy-element-mixin.html index ab6d1e1872..f820a95692 100644 --- a/lib/legacy/legacy-element-mixin.html +++ b/lib/legacy/legacy-element-mixin.html @@ -91,6 +91,7 @@ * passed to `Polymer({...})`) to the static API. * * @return {!Object} The `import.meta` object set on the prototype + * @suppress {missingProperties} */ static get importMeta() { return this.prototype.importMeta; diff --git a/lib/legacy/polymer.dom.html b/lib/legacy/polymer.dom.html index 5417f12e14..afae35b1c3 100644 --- a/lib/legacy/polymer.dom.html +++ b/lib/legacy/polymer.dom.html @@ -398,7 +398,7 @@ * This method facades to `Polymer.enqueueDebouncer`. * * @memberof Polymer.dom - * @param {Polymer.Debouncer} debouncer Debouncer to enqueue + * @param {!Polymer.Debouncer} debouncer Debouncer to enqueue */ Polymer.dom.addDebouncer = Polymer.enqueueDebouncer; })(); diff --git a/lib/mixins/element-mixin.html b/lib/mixins/element-mixin.html index b58b818ded..b0f7fba988 100644 --- a/lib/mixins/element-mixin.html +++ b/lib/mixins/element-mixin.html @@ -404,7 +404,7 @@ * used to create bindings relative to the import path. * * For elements defined in ES modules, users should implement - * `static get importMeta() { return import.meta; }` and the default + * `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 * to the document containing a `dom-module` element matching this @@ -413,6 +413,7 @@ * Note, this path should contain a trailing `/`. * * @return {string} The import path for this element class + * @suppress {missingProperties} */ static get importPath() { if (!this.hasOwnProperty(JSCompiler_renameProperty('_importPath', this))) { diff --git a/lib/mixins/property-effects.html b/lib/mixins/property-effects.html index d5c60613e2..de92b17c4a 100644 --- a/lib/mixins/property-effects.html +++ b/lib/mixins/property-effects.html @@ -43,7 +43,7 @@ READ_ONLY: '__readOnly' }; - /** @const {string} */ + /** @const {RegExp} */ const capitalAttributeRegex = /[A-Z]/; /** From e64bd0ba6fe8b7a2c71ca6f886a46ff05b3ea1e3 Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Thu, 5 Apr 2018 16:00:54 -0700 Subject: [PATCH 3/4] Don't rely on dom-module synchronously until WCR. --- test/unit/sub/resolveurl-elements.html | 40 ++++++++++++++------------ 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/test/unit/sub/resolveurl-elements.html b/test/unit/sub/resolveurl-elements.html index da180ee99e..b9bdfa82fb 100644 --- a/test/unit/sub/resolveurl-elements.html +++ b/test/unit/sub/resolveurl-elements.html @@ -34,31 +34,33 @@ From 61ca60e4bfecee3f6dcc86c58e3f4cdaaf741d11 Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Thu, 5 Apr 2018 18:57:59 -0700 Subject: [PATCH 4/4] Add reasoning for suppress missingProperties --- lib/legacy/legacy-element-mixin.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/legacy/legacy-element-mixin.html b/lib/legacy/legacy-element-mixin.html index f820a95692..c1302b1346 100644 --- a/lib/legacy/legacy-element-mixin.html +++ b/lib/legacy/legacy-element-mixin.html @@ -91,7 +91,8 @@ * passed to `Polymer({...})`) to the static API. * * @return {!Object} The `import.meta` object set on the prototype - * @suppress {missingProperties} + * @suppress {missingProperties} `this` is always in the instance in + * closure for some reason even in a static method, rather than the class */ static get importMeta() { return this.prototype.importMeta;