Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions injected/src/features/broker-protection/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* Gets the owner document of an element or uses document as fallback
*
* @param {Node|Element} element
* @return {Document} The owner document
*/
function getOwnerDocument(element) {
return element.ownerDocument || document;
}

/**
* Get a single element.
*
Expand Down Expand Up @@ -75,7 +85,8 @@ export function getElementMatches(element, selector) {
* @return {boolean}
*/
function matchesXPath(element, selector) {
const xpathResult = document.evaluate(selector, element, null, XPathResult.BOOLEAN_TYPE, null);
const ownerDoc = getOwnerDocument(element);
const xpathResult = ownerDoc.evaluate(selector, element, null, XPathResult.BOOLEAN_TYPE, null);

return xpathResult.booleanValue;
}
Expand Down Expand Up @@ -131,7 +142,8 @@ function safeQuerySelector(element, selector) {
*/
function safeQuerySelectorXPath(element, selector) {
try {
const match = document.evaluate(selector, element, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
const ownerDoc = getOwnerDocument(element);
const match = ownerDoc.evaluate(selector, element, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
const single = match?.singleNodeValue;
if (single) {
return /** @type {HTMLElement} */ (single);
Expand All @@ -150,8 +162,9 @@ function safeQuerySelectorXPath(element, selector) {
*/
function safeQuerySelectorAllXpath(element, selector) {
try {
const ownerDoc = getOwnerDocument(element);
// gets all elements matching the xpath query
const xpathResult = document.evaluate(selector, element, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
const xpathResult = ownerDoc.evaluate(selector, element, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (xpathResult) {
/** @type {HTMLElement[]} */
const matchedNodes = [];
Expand Down
Loading