From bb494666b8bf8c81d3fcf83a24ac847f956fd209 Mon Sep 17 00:00:00 2001 From: Dima Voytenko Date: Wed, 3 Mar 2021 17:44:19 -0800 Subject: [PATCH] Explicitly specify the version of automatic legacy extensions (#33048) * Explicitly specify the version of automatic legacy extensions * fix tests --- src/custom-element.js | 6 ++-- test/unit/test-custom-element-registry.js | 2 +- test/unit/test-custom-element.js | 37 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/custom-element.js b/src/custom-element.js index ba300c719ac6..a8f075a3f10a 100644 --- a/src/custom-element.js +++ b/src/custom-element.js @@ -964,11 +964,13 @@ function createBaseCustomElementClass(win) { const ampdoc = ampdocService.getAmpDoc(this); this.ampdoc_ = ampdoc; // Load the pre-stubbed extension if needed. - const extensionId = this.tagName.toLowerCase(); + const extensionId = this.localName; if (!this.implClass_ && !ampdoc.declaresExtension(extensionId)) { Services.extensionsFor(win).installExtensionForDoc( ampdoc, - extensionId + extensionId, + // The legacy auto-extensions are always 0.1. + '0.1' ); } } diff --git a/test/unit/test-custom-element-registry.js b/test/unit/test-custom-element-registry.js index 7d1856c211ca..c27495c3de67 100644 --- a/test/unit/test-custom-element-registry.js +++ b/test/unit/test-custom-element-registry.js @@ -153,7 +153,7 @@ describes.realWin('CustomElement register', {amp: true}, (env) => { const element = doc.createElement('amp-element2'); doc.body.appendChild(element); expect(stub).to.be.calledOnce; - expect(stub).to.be.calledWithExactly(ampdoc, 'amp-element2'); + expect(stub).to.be.calledWithExactly(ampdoc, 'amp-element2', '0.1'); }); it('should not install declared pre-stubbed element extension', () => { diff --git a/test/unit/test-custom-element.js b/test/unit/test-custom-element.js index 1d784a8df2d5..6d48b94c4e97 100644 --- a/test/unit/test-custom-element.js +++ b/test/unit/test-custom-element.js @@ -243,6 +243,43 @@ describes.realWin('CustomElement', {amp: true}, (env) => { // expect(build.calledOnce).to.equal(true); }); + it('StubElement - should try to install an unregistered legacy extensions', () => { + const LegacyElementClass = createAmpElementForTesting(win, ElementStub); + win.customElements.define('amp-legacy', LegacyElementClass); + win.__AMP_EXTENDED_ELEMENTS['amp-legacy'] = ElementStub; + + const extensions = Services.extensionsFor(win); + env.sandbox.stub(extensions, 'installExtensionForDoc'); + + const element = new LegacyElementClass(); + env.sandbox.stub(element, 'buildInternal'); + + container.appendChild(element); + expect(element.readyState).to.equal('upgrading'); + expect(extensions.installExtensionForDoc).to.be.calledOnce.calledWith( + ampdoc, + 'amp-legacy', + '0.1' + ); + }); + + it('StubElement - should not try to install a pre-registered legacy extensions', () => { + const LegacyElementClass = createAmpElementForTesting(win, ElementStub); + win.customElements.define('amp-legacy', LegacyElementClass); + win.__AMP_EXTENDED_ELEMENTS['amp-legacy'] = ElementStub; + ampdoc.declareExtension('amp-legacy'); + + const extensions = Services.extensionsFor(win); + env.sandbox.stub(extensions, 'installExtensionForDoc'); + + const element = new LegacyElementClass(); + env.sandbox.stub(element, 'buildInternal'); + + container.appendChild(element); + expect(element.readyState).to.equal('upgrading'); + expect(extensions.installExtensionForDoc).to.not.be.called; + }); + it('Element - should only add classes on first attachedCallback', () => { const element = new ElementClass(); const buildPromise = Promise.resolve();