UNPKG

@remotemerge/xpath-parser

Version:

JavaScript utility for extracting data from HTML and XML documents!

85 lines (84 loc) 3.62 kB
/** * Expression type definition. * * @property {string} root - The root XPath expression. * @property {string} [pagination] - The optional pagination XPath expression. * @property {Record<string, string>} queries - A dictionary of query names and their XPath expressions. */ type Expression = { root: string; pagination?: string; queries: Record<string, string>; }; /** * This class is used to parse HTML content using XPath expressions. * * @class XPathParser */ export default class XPathParser { private options; private domContent; /** * Initialize Node from DOM Node or HTML string. * * @param {Node|string} content - A DOM Node or an HTML string. */ constructor(content: Node | string); /** * Evaluate an XPath expression in the specified Node and return the result. * * @param {string} expression - The XPath expression to evaluate. * @return {XPathResult} The result of evaluating the XPath expression. */ evaluate(expression: string): XPathResult; /** * Extract the text value from a given DOM Node. * * @param {Node|null} node - The DOM Node to extract the text value from. * @return {string} The text value of the DOM Node. */ getValue(node: Node | null): string; /** * Evaluate the expression and return the first matching result. * * @param {string} expression - The XPath expression to evaluate. * @return {string} The first matching result of evaluating the XPath expression. */ queryFirst(expression: string): string; /** * Evaluate the expression and return all matching results. * * @param {string} expression - The XPath expression to evaluate. * @return {string[]} An array of all the matching results of evaluating the XPath expression. */ queryList(expression: string): string[]; /** * Evaluate the expressions and return the matching result in the associative format. * * @param {Expression[]} expressions - An object with XPath expressions as values. * @return {Record<string, string>} An object with the results of evaluating the XPath expressions as key-value pairs. */ multiQuery(expressions: Expression['queries']): Record<string, string>; /** * This method selects all matching parent nodes and runs sub queries on child nodes and generate an associative array result. * * @param {Expression} expression - An object that specifies the XPath expressions to use for the root node, child nodes, and pagination. * @return {Record<string, string>[]} An array of objects with the results of evaluating the XPath expressions as key-value pairs, and optionally, a pagination URL. */ subQuery(expression: Expression): { paginationUrl?: string; results: Record<string, string>[]; }; /** * This method tries to match a given expression every second up to a maximum number of seconds. * * @param {string} expression - The XPath expression to match. * @param {number} maxSeconds - The maximum number of seconds to keep trying. Default is 10. * @return {Promise<{ found: boolean; message: string }>} A Promise that resolves with an object containing a boolean indicating whether the expression was found, and a message with the first match if it was found, or rejects with a TimeoutError if the expression was not found within the maximum number of seconds. */ waitXPath(expression: string, maxSeconds?: number): Promise<{ found: boolean; message: string; }>; } export {};