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

CustomElements: upgrade runs connectedCallback on an element with a throwing constructor #563

Closed
davaajav-shino opened this issue Sep 7, 2016 · 5 comments

Comments

@davaajav-shino
Copy link

Upgrade runs connectedCallback on an element with a throwing constructor. After executing the following code log contains 'connected'.

let element = document.createElement('a-a');
let log = [];
w.customElements.define('a-a', class extends w.HTMLElement {
  constructor() {
    throw new Error();
  }
  connectedCallback() {
    log.push('connected');
  }
});
document.body.appendChild(element);

https://html.spec.whatwg.org/#concept-upgrade-an-element
enqueueing attributeChangedCallback, connectedCallback after custom elements state check in Step 2 is not enough when the the elements state is changed in Step 9.

@kojiishi
Copy link

kojiishi commented Sep 7, 2016

Agree that running connectedCallback on failed elements does not look good. @domenic @dominiccooney @rniwa ?

@rniwa
Copy link
Collaborator

rniwa commented Sep 7, 2016

Indeed, we should make sure we only invoke reactions on a "defined" custom element. It just doesn't make much sense to invoke callback when it failed to construct/upgrade.

@domenic
Copy link
Collaborator

domenic commented Sep 8, 2016

Agreed that it should not run.

These were moved early in the algorithm by whatwg/html#1366 for the reasons explained there. Given those reasons, I am not sure there is an elegant patch here where we avoid queuing in this sort of scenario. Probably the best solution is to just insert a "is not failed" check inside "invoke custom element reactions".

Hmm. Or maybe whenever we set the state to failed, we empty the element's custom element reaction queue.

@dominiccooney
Copy link
Contributor

It would be no problem to drain the queue of these failed elements.

FWIW I think there may always be elements running around whose constructors threw, though; they can be created with 'new' which calls super(), discloses 'this', and then throws. We must continue to run callbacks for them because there's no effective way to detect them, that I know of.

domenic added a commit to whatwg/html that referenced this issue Sep 8, 2016
Fixes WICG/webcomponents#563, and an analogous
issue when the constructor uses return-override, by ensuring that all
exceptions go through the same path, which both sets the custom element
state to "failed", and clears the custom element reaction queue.
@domenic
Copy link
Collaborator

domenic commented Sep 8, 2016

FWIW I think there may always be elements running around whose constructors threw, though; they can be created with 'new' which calls super(), discloses 'this', and then throws. We must continue to run callbacks for them because there's no effective way to detect them, that I know of.

Yeah. Such elements aren't failed; they're just elements with weird constructors, I guess. This is an inevitable consequence of us handing the constructor over to developers instead of using a createdCallback design.

domenic added a commit to whatwg/html that referenced this issue Sep 15, 2016
Fixes WICG/webcomponents#563, and an analogous
issue when the constructor uses return-override, by ensuring that all
exceptions go through the same path, which both sets the custom element
state to "failed", and clears the custom element reaction queue.
alice pushed a commit to alice/html that referenced this issue Jan 8, 2019
Fixes WICG/webcomponents#563, and an analogous
issue when the constructor uses return-override, by ensuring that all
exceptions go through the same path, which both sets the custom element
state to "failed", and clears the custom element reaction queue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants