4
defineProperties(Element.prototype, {
    querySelector: {
        value: querySelectorPatched,
        writable: true,
        enumerable: true,
        configurable: true,
    },
    querySelectorAll: {
        value(this: HTMLBodyElement): NodeListOf<Element> {
            const nodeList = arrayFromCollection(
                elementQuerySelectorAll.apply(this, ArraySlice.call(arguments) as [string])
            );

            if (!featureFlags.ENABLE_NODE_LIST_PATCH) {
                const filteredResults = getFilteredArrayOfNodes(
                    this,
                    nodeList,
                    ShadowDomSemantic.Disabled
                );
                return createStaticNodeList(filteredResults);
            }

            return createStaticNodeList(
                getFilteredArrayOfNodes(this, nodeList, ShadowDomSemantic.Enabled)
            );
        },
        writable: true,
        enumerable: true,
        configurable: true,
    },

});

I want to understand this part of the following Typescript code here:

value(this: HTMLBodyElement): NodeListOf<Element>

I believe this code is overriding the standard querySelectorAll method, therefore simulating Shadow DOM. But the method is being defined on an Element, yet the value for this in the function is HTMLBodyElement. Does this mean the method can only be called from a body element? Or perhaps it's called from Element and then casted to HTMLBodyElement? Can someone explain the logic of this code? This is a part of the following LWC source. I know there are some people here that probably wrote this code so any help is appreciated.

3
  • The author of that code, Caridy, should be able to answer more definitively. I can't see any good reason why it should be HTMLBodyElement vs just Element, since HTMLBodyElement is a child class of Element and HTMLElement.
    – sfdcfox
    Nov 28, 2021 at 23:21
  • Do you know if he is a member here? Nov 29, 2021 at 6:16
  • They have an account here, but haven't logged in for 4+ years.
    – sfdcfox
    Nov 29, 2021 at 9:59

0

You must log in to answer this question.

Browse other questions tagged .