/** * @file This is a TypeScript port of HtmlCleaner's XPath implementation, * converted to use JavaScript DOM methods. * This is used to replicate HtmlCleaner's behavior, which is quite different * from standards-compliant XPath evaluators. * * This has been tested with xmldom 0.4.0 (https://github.com/xmldom/xmldom), * but should work with other DOM Level 2-compatible implementations. * * This code is derived from: * https://sourceforge.net/p/htmlcleaner/code/HEAD/tree/tags/htmlcleaner-2.24/src/main/java/org/htmlcleaner/XPather.java * https://sourceforge.net/p/htmlcleaner/code/HEAD/tree/tags/htmlcleaner-2.24/src/main/java/org/htmlcleaner/TagNode.java */ /** * Interface that represents a DOM node. * This is a subset of the W3C DOM Level 2 Node interface. */ export interface XPatherNode { readonly nodeName: string; readonly nodeType: number; readonly parentNode: XPatherNode | null; readonly childNodes: { length: number; item(index: number): XPatherNode; [index: number]: XPatherNode; }; textContent: string | null; readonly ELEMENT_NODE: number; } /** * Interface that represents a tag node. * This is a subset of the W3C DOM Level 2 Element interface. */ export interface XPatherElement extends XPatherNode { readonly attributes: { readonly length: number; item(index: number): XPatherAttr | null; [index: number]: XPatherAttr; }; getAttribute(qualifiedName: string): string | null; hasAttribute(qualifiedName: string): boolean; } /** * Interface that represents a tag attribute. * This is a subset of the W3C DOM Level 2 Attr interface. */ export interface XPatherAttr extends XPatherNode { readonly name: string; value: string; } /** * Evaluates XPath expression on give node. * * *This is not fully supported XPath parser and evaluator.* * * Examples below show supported elements: * * - `//div//a` * - `//div//a[@id][@class]` * - `/body/*[1]/@type` * - `//div[3]//a[@id][@href='r/n4']` * - `//div[last() >= 4]//./div[position() = last()])[position() > 22]//li[2]//a` * - `//div[2]/@*[2]` * - `data(//div//a[@id][@class])` * - `//p/last()` * - `//body//div[3][@class]//span[12.2(el: T, xPathExpression: string): XPatherResult[]; export declare type XPatherResult = boolean | number | string | T; export declare class XPatherException extends Error { constructor(message?: string); } /** * Checks if the given XPathResultItem is an XPathElement. * @param value */ export declare function isElement(value: XPatherResult): value is T;