Skip to content

Prevent calling #hasAttribute or #getAttribute in element constructor #10058

Description

@zhouyx

follow up of #10045

Reason
customElement's #createdCallback() is called when the element is created, while #connectedCallback() is called when the element is first connected to DOM.

We construct an AMP Element in createdCallback(). Using DOM API in element's constructor is DOM API before adding element to DOM, thus should be forbidden.

Failing Example
In the following example, when element.getAttribute() is called before #setAttribute()

class AmpTest extends AMP.BaseElement {
  constructor(element) {
    this.url = element.getAttribute('url);
  }
}
const ele = document.createElement('amp-test');
/** AmpTest constructor gets called when element is created. At this point `<amp-test>` element's attributes are not ready yet */
ele.setAttribute('url', 'fakeurl');

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions