Re: [w3c/webcomponents] Is there a way to detect if a custom element was constructed during parsing? (#789)

When a custom element had been upgraded, then `this.isConnected && this.parentNode` would also true. Also, when a custom element had been defined before the HTML parser (not fragment parser) parsed an element, then `this.isConnected && this.parentNode` would be false because the element is neither connected nor has a parent node in that case. Also, it's probably redundant to check `this.parentNode` because you can't have a connected tree without having a parent except `Document` itself. So I don't see how such a check could ever be correct.

In general, I'd be interested to know what the exact use case is for detecting when a parser is creating a custom element. Some builtin elements do this (e.g. form control, script, etc...) but mostly due to legacy behaviors that is needed for backwards compatibility)

Finally, the best way to polyfill isConnected is something like this:
```js
if (!Object.getOwnPropertyDescriptor(Node.prototype, 'isConnected')) {
    let rootNode = null;
    if (Node.prototype.getRootNode)
        rootNode  = (node) => node.getRootNode({composed: true});
    else {
        rootNode = (node) => {
            for (let ancestor = node, ancestorParent; ancestor; ancestor = ancestorParent) {
                ancestorParent = ancestor.parentNode || ancestor.host;
                if (!ancestorParent)
                    return ancestor;
            };
            return node;
        }
    }
    Object.defineProperty(Node.prototype, 'isConnected', {
        get() { return rootNode(this).nodeType == Node.DOCUMENT_NODE; },
        enumerable: true,
        configurable: true,
    });
}
```



-- 
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/789#issuecomment-459858405

Received on Friday, 1 February 2019 20:32:55 UTC