Re: [w3c/webcomponents] Expose shadowRoot on element internals (#871)

Can we split the change for `attachInternals` to its own PR instead of tying it to the change to expose `shadowRoot`?

I also wonder if there is a way to restrict `attachShadow` until the constructor is called. Right now, when you call `attachInternals` and get the shadow root, you don't know whether it's coming from the declarative shadow root or some shadow root other scripts have attached. The combination of upgrading and backwards compatibility makes this challenging. Because of upgrades, we wouldn't know whether a given element will allow shadow root or not. If backwards compatibility weren't an issue, we could make `attachShadow` fail until the constructor is called.

One possibility is to automatically fail an upgrade when non-declarative shadow root had been attached before the constructor has been called when internals is enabled. Something like this:

```js
const someElement = document.createElement('some-element');
someElement.attachShadow({mode: 'closed'});

customElements.define('some-element', class SomeElement extends HTMLElement {
    static enableInternals = true
    #internals
    constructor() {
        super();
        #internals = this.attachInternals();
        if (!#internals.shadowRoot)
            populateShadow(#internals.attachShadow());
    }
});

customElements.upgrade(someElement); // This will fail to upgrade someElement.

const otherElement = document.createElement('some-element');
otherElement.attachShadow({mode: 'closed'}); // This will throw because shadow root has already been attached.
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/871#issuecomment-691398569

Received on Saturday, 12 September 2020 03:45:28 UTC