RE: The key custom elements question: custom constructors?

I have a related question: what happens if the constructor throws? Example:

<!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?

// This will presumably throw:
document.body.innerHTML = "<x-foo></x-foo>";
// But will it wipe out body first?

// 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?


// 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?
</script>

> -----Original Message-----
> From: Domenic Denicola [mailto:d@domenic.me]
> Sent: Wednesday, July 15, 2015 20:45
> To: public-webapps
> Subject: The key custom elements question: custom constructors?
> 
> Hi all,
> 
> Ahead of next week's F2F, I'm trying to pull together some clarifying and
> stage-setting materials, proposals, lists of open issues, etc. In the end, they
> all get blocked on one key question:
> 
> **Is it OK to run author code during parsing/cloning/editing/printing (in
> Gecko)/etc.?**
> 
> If we allow custom elements to have custom constructors, then those must
> run in order to create properly-allocated instances of those elements; there
> is simply no other way to create those objects. You can shuffle the timing
> around a bit: e.g., while cloning a tree, you could either run the constructors
> at the normal times, or try to do something like almost-synchronous
> constructors [1] where you run them after constructing a skeleton of the
> cloned tree, but before inserting them into the tree. But the fact remains
> that if custom elements have custom constructors, those custom
> constructors must run in the middle of all those operations.
> 
> We've danced around this question many times. But I think we need a clear
> answer from the various implementers involved before we can continue. In
> particular, I'm not interested in whether the implementers think it's
> technically feasible. I'd like to know whether they think it's something we
> should standardize.
> 
> I'm hoping we can settle this on-list over the next day or two so that we all
> come to the meeting with a clear starting point. Thanks very much, and
> looking forward to your replies,
> 
> -Domenic
> 
> [1]: https://lists.w3.org/Archives/Public/public-
> webapps/2014JanMar/0098.html

Received on Thursday, 16 July 2015 20:37:26 UTC