- From: Anne van Kesteren <annevk@annevk.nl>
- Date: Fri, 17 Jul 2015 11:43:29 +0200
- To: Domenic Denicola <d@domenic.me>
- Cc: public-webapps <public-webapps@w3.org>
On Thu, Jul 16, 2015 at 10:36 PM, Domenic Denicola <d@domenic.me> wrote: > I have a related question: what happens if the constructor throws? Right, this is the kind of thing we need to figure out. > <!DOCTYPE html> > <script> > "use strict"; > > window.throwingMode = true; > > class XFoo extends HTMLElement { > constructor() { > if (window.throwingMode) { > throw new Error("uh-oh!"); > } > } > } > > document.registerElement("x-foo", XFoo); > </script> > > <x-foo></x-foo> > > <script> > "use strict"; > > // What does the DOM tree look like here? Is an x-foo present in some form? > // HTMLUnknownElement maybe? Just removed from existence? I guess if the constructor doesn't return an instance but an exception, we'd rethrow the exception (ends up at window.onerror). We could have fallback for creating an element anyway, but I'm not sure that's necessary. > // This will presumably throw: > document.body.innerHTML = "<x-foo></x-foo>"; That makes sense. Provided we don't offer fallback instance creation for the parser case. > // But will it wipe out body first? Based on https://dvcs.w3.org/hg/innerhtml/raw-file/tip/index.html#widl-Element-innerHTML I'd say no. Fragment parsing fails, so we never reach replace all. > // What about > document.body.innerHTML = "[512 KiB of normal HTML] <x-foo></x-foo>"; > // ? does the HTML make it in, or does the operation fail atomically, or something else? It fails atomically, based on the definition of innerHTML. > // Now let's try something weirder. > // Assume <x-bar> / XBar is a well-behaved custom element. > > window.throwingMode = false; > const el = document.createElement("div"); > el.innerHTML = "<p>a</p><x-bar></x-bar><x-foo>b</x-foo><p>b</p><x-bar></x-bar>"; > > window.throwingMode = true; > el.cloneNode(true); // this will throw, presumably... > // ... but does the XBar constructor run or not? > // ... if so, how many times? If we decide to rethrow, it would only be invoked once. However, given the other algorithms that use cloneNode(), it's not clear that throwing is acceptable or what that means when you copy something as a user (though maybe you just end up copying nothing at all, doesn't seem too bad). -- https://annevankesteren.nl/
Received on Friday, 17 July 2015 09:43:54 UTC