Skip to content

Commit

Permalink
Add JSDoc
Browse files Browse the repository at this point in the history
Rename xPathElements to getXPathElements
  • Loading branch information
AdamWr committed Aug 16, 2023
1 parent 0f4588e commit bf846a7
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/scriptlets/xml-prune.js
Expand Up @@ -62,10 +62,14 @@ import {
* 1. Remove `Period` tag whose `id` contains `pre-roll` and remove `duration` attribute from the `Period` tag
* by using XPath expression
*
* <!-- markdownlint-disable line-length -->
*
* ```adblock
* example.org#%#//scriptlet('xml-prune', 'xpath(//*[name()="Period"][contains(@id, "pre-roll") and contains(@id, "-ad-")] | //*[name()="Period"]/@duration)')
* ```
*
* <!-- markdownlint-enable line-length -->
*
* 1. Call with no arguments will log response payload and URL at the console
*
* ```adblock
Expand Down Expand Up @@ -101,7 +105,14 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch =
const XPATH_MARKER = 'xpath(';
const isXpath = propsToRemove && propsToRemove.startsWith(XPATH_MARKER);

const xPathElements = (contextNode) => {
/**
* Checks if the document node from the XML document contains propsToRemove
* if so, returns an array with matched elements, otherwise returns an empty array
*
* @param {Node} contextNode - document node from XML document
* @returns {Array}
*/
const getXPathElements = (contextNode) => {
const result = [];
try {
const elementsToRemove = propsToRemove.slice(XPATH_MARKER.length, -1);
Expand All @@ -116,7 +127,7 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch =
result.push(xpathResult.snapshotItem(i));
}
} catch (ex) {
const message = `Invalid XPath element: ${ex}`;
const message = `Invalid XPath parameter: ${propsToRemove}\n${ex}`;
logMessage(source, message);
}
return result;
Expand Down Expand Up @@ -160,7 +171,7 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch =
return false;
}
const docXML = createXMLDocument(response);
return isXpath ? !!xPathElements(docXML) : !!docXML.querySelector(propsToRemove);
return isXpath ? getXPathElements(docXML) : !!docXML.querySelector(propsToRemove);
};

const pruneXML = (text) => {
Expand All @@ -178,7 +189,7 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch =
return text;
}
if (isXpath) {
const elements = xPathElements(xmlDoc);
const elements = getXPathElements(xmlDoc);
if (!elements.length) {
shouldPruneResponse = false;
return text;
Expand Down

0 comments on commit bf846a7

Please sign in to comment.