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');
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()