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

🚮 Eliminate elementName method #27470

Merged
merged 1 commit into from
Mar 30, 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: 0 additions & 7 deletions build-system/externs/amp.extern.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,6 @@ var AmpElement;
/** @return {!Signals} */
AmpElement.prototype.signals = function() {};

/**
* Must be externed to avoid Closure DCE'ing this function on
* custom-element.CustomAmpElement.prototype in single-pass compilation.
* @return {string}
*/
AmpElement.prototype.elementName = function() {};

var Signals = class {};
/**
* @param {string} unusedName
Expand Down
52 changes: 12 additions & 40 deletions src/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,15 @@ function isTemplateTagSupported() {
* Creates a named custom element class.
*
* @param {!Window} win The window in which to register the custom element.
* @param {string} name The name of the custom element.
* @return {typeof AmpElement} The custom element class.
*/
export function createCustomElementClass(win, name) {
const baseCustomElement = /** @type {typeof HTMLElement} */ (createBaseCustomElementClass(
export function createCustomElementClass(win) {
const BaseCustomElement = /** @type {typeof HTMLElement} */ (createBaseCustomElementClass(
win
));
class CustomAmpElement extends baseCustomElement {
/**
* @see https://github.com/WebReflection/document-register-element#v1-caveat
* @suppress {checkTypes}
* @param {HTMLElement} self
*/
constructor(self) {
return super(self);
}
/**
* The name of the custom element.
* @return {string}
*/
elementName() {
return name;
}
}
// It's necessary to create a subclass, because the same "base" class cannot
// be registered to multiple custom elements.
class CustomAmpElement extends BaseCustomElement {}
return /** @type {typeof AmpElement} */ (CustomAmpElement);
}

Expand All @@ -127,15 +112,10 @@ function createBaseCustomElementClass(win) {
* @abstract @extends {HTMLElement}
*/
class BaseCustomElement extends htmlElement {
/**
* @see https://github.com/WebReflection/document-register-element#v1-caveat
* @suppress {checkTypes}
* @param {HTMLElement} self
*/
constructor(self) {
self = super(self);
self.createdCallback();
return self;
/** */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does something complain when removing this empty comment?

constructor() {
super();
this.createdCallback();
}

/**
Expand Down Expand Up @@ -251,7 +231,7 @@ function createBaseCustomElementClass(win) {
// `opt_implementationClass` is only used for tests.
let Ctor =
win.__AMP_EXTENDED_ELEMENTS &&
win.__AMP_EXTENDED_ELEMENTS[this.elementName()];
win.__AMP_EXTENDED_ELEMENTS[this.localName];
if (getMode().test && nonStructThis['implementationClassForTesting']) {
Ctor = nonStructThis['implementationClassForTesting'];
}
Expand Down Expand Up @@ -307,13 +287,6 @@ function createBaseCustomElementClass(win) {
}
}

/**
* The name of the custom element.
* @abstract
* @return {string}
*/
elementName() {}

/** @return {!Signals} */
signals() {
return this.signals_;
Expand Down Expand Up @@ -1920,12 +1893,11 @@ function isInternalOrServiceNode(node) {
* Creates a new custom element class prototype.
*
* @param {!Window} win The window in which to register the custom element.
* @param {string} name The name of the custom element.
* @param {(typeof ./base-element.BaseElement)=} opt_implementationClass For testing only.
* @return {!Object} Prototype of element.
*/
export function createAmpElementForTesting(win, name, opt_implementationClass) {
const Element = createCustomElementClass(win, name);
export function createAmpElementForTesting(win, opt_implementationClass) {
const Element = createCustomElementClass(win);
if (getMode().test && opt_implementationClass) {
Element.prototype.implementationClassForTesting = opt_implementationClass;
}
Expand Down
2 changes: 1 addition & 1 deletion src/service/custom-element-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function copyElementToChildWindow(parentWin, childWin, name) {
export function registerElement(win, name, implementationClass) {
const knownElements = getExtendedElements(win);
knownElements[name] = implementationClass;
const klass = createCustomElementClass(win, name);
const klass = createCustomElementClass(win);
win['customElements'].define(name, klass);
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/test-base-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describes.realWin('BaseElement', {amp: true}, env => {
doc = win.document;
win.customElements.define(
'amp-test-element',
createAmpElementForTesting(win, 'amp-test-element', BaseElement)
createAmpElementForTesting(win, BaseElement)
);
customElement = doc.createElement('amp-test-element');
element = new BaseElement(customElement);
Expand Down
26 changes: 5 additions & 21 deletions test/unit/test-custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,8 @@ describes.realWin('CustomElement', {amp: true}, env => {
doc.body.appendChild(container);
chunkInstanceForTesting(env.ampdoc);

ElementClass = createAmpElementForTesting(win, 'amp-test', TestElement);
StubElementClass = createAmpElementForTesting(
win,
'amp-stub',
ElementStub
);
ElementClass = createAmpElementForTesting(win, TestElement);
StubElementClass = createAmpElementForTesting(win, ElementStub);

win.customElements.define('amp-test', ElementClass);
win.customElements.define('amp-stub', StubElementClass);
Expand Down Expand Up @@ -1668,11 +1664,7 @@ describes.realWin('CustomElement Service Elements', {amp: true}, env => {
beforeEach(() => {
win = env.win;
doc = win.document;
StubElementClass = createAmpElementForTesting(
win,
'amp-stub2',
ElementStub
);
StubElementClass = createAmpElementForTesting(win, ElementStub);
win.customElements.define('amp-stub2', StubElementClass);
env.ampdoc.declareExtension('amp-stub2');
element = new StubElementClass();
Expand Down Expand Up @@ -1833,11 +1825,7 @@ describes.realWin('CustomElement', {amp: true}, env => {
win = env.win;
doc = win.document;
clock = lolex.install({target: win});
ElementClass = createAmpElementForTesting(
win,
'amp-test-loader',
TestElement
);
ElementClass = createAmpElementForTesting(win, TestElement);
win.customElements.define('amp-test-loader', ElementClass);
win.__AMP_EXTENDED_ELEMENTS['amp-test-loader'] = TestElement;
LOADING_ELEMENTS_['amp-test-loader'.toUpperCase()] = true;
Expand Down Expand Up @@ -2210,11 +2198,7 @@ describes.realWin('CustomElement Overflow Element', {amp: true}, env => {
beforeEach(() => {
win = env.win;
doc = win.document;
ElementClass = createAmpElementForTesting(
win,
'amp-test-overflow',
TestElement
);
ElementClass = createAmpElementForTesting(win, TestElement);
win.customElements.define('amp-test-overflow', ElementClass);
mutator = Services.mutatorForDoc(doc);
mutatorMock = env.sandbox.mock(mutator);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ describes.realWin(
env.win.setTimeout(() => {
env.win.customElements.define(
'amp-test',
createAmpElementForTesting(env.win, 'amp-test', TestElement)
createAmpElementForTesting(env.win, TestElement)
);
}, 100);
return dom.whenUpgradedToCustomElement(element).then(element => {
Expand Down
6 changes: 1 addition & 5 deletions test/unit/test-intersection-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,7 @@ describe('IntersectionObserver', () => {
}
}

const ElementClass = createAmpElementForTesting(
window,
'amp-int',
TestElement
);
const ElementClass = createAmpElementForTesting(window, TestElement);
customElements.define('amp-int', ElementClass);

const iframeSrc =
Expand Down
2 changes: 1 addition & 1 deletion testing/describes.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ function installAmpAdStylesPromise(win) {
function createAmpElement(win, opt_name, opt_implementationClass) {
// Create prototype and constructor.
const name = opt_name || 'amp-element';
const proto = createAmpElementForTesting(win, name).prototype;
const proto = createAmpElementForTesting(win).prototype;
const ctor = function() {
const el = win.document.createElement(name);
el.__proto__ = proto;
Expand Down