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 am intrigued by the portion of code presented in the TypeScript snippet below:
value(this: HTMLBodyElement): NodeListOf<Element>
In my interpretation, it appears that this section is replacing the default querySelectorAll
method. However, the method is being implemented on an Element
, while specifying this
as HTMLBodyElement
within the function. Does this indicate that the method can solely be invoked from a body
element? Alternatively, could it be called from an Element
and subsequently casted to HTMLBodyElement
? Could someone provide insight into the rationale behind this code snippet? The context of this code segment can be found in the following repository.