| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | export = CSSselect; |
| 2 | |
| 3 | /** |
| 4 | * Alias for CSSselect.selectAll(query, elems, options). |
| 5 | * @see [CSSselect.compile] for supported selector queries. |
| 6 | */ |
| 7 | declare function CSSselect<Node, ElementNode extends Node>( |
| 8 | query: CSSselect.Query, |
| 9 | elems: Array<ElementNode> | ElementNode, |
| 10 | options?: CSSselect.Options<Node, ElementNode> |
| 11 | ): Array<ElementNode>; |
| 12 | |
| 13 | declare namespace CSSselect { |
| 14 | type Predicate<Value> = (v: Value) => boolean; |
| 15 | interface Adapter<Node, ElementNode extends Node> { |
| 16 | /** |
| 17 | * is the node a tag? |
| 18 | */ |
| 19 | isTag(node: Node): node is ElementNode; |
| 20 | |
| 21 | /** |
| 22 | * Does at least one of passed element nodes pass the test predicate? |
| 23 | */ |
| 24 | existsOne(test: Predicate<ElementNode>, elems: Array<ElementNode>): boolean; |
| 25 | |
| 26 | /** |
| 27 | * get the attribute value. |
| 28 | */ |
| 29 | getAttributeValue(elem: ElementNode, name: string): string; |
| 30 | |
| 31 | /** |
| 32 | * get the node's children |
| 33 | */ |
| 34 | getChildren(node: Node): Array<Node>; |
| 35 | |
| 36 | /** |
| 37 | * get the name of the tag |
| 38 | */ |
| 39 | getName(elem: ElementNode): string; |
| 40 | |
| 41 | /** |
| 42 | * get the parent of the node |
| 43 | */ |
| 44 | getParent(node: Node): Node; |
| 45 | |
| 46 | /* |
| 47 | Get the siblings of the node. Note that unlike jQuery's `siblings` method, |
| 48 | this is expected to include the current node as well |
| 49 | */ |
| 50 | getSiblings(node: Node): Array<Node>; |
| 51 | |
| 52 | /* |
| 53 | * Get the text content of the node, and its children if it has any. |
| 54 | */ |
| 55 | getText(node: Node): string; |
| 56 | |
| 57 | /** |
| 58 | * Does the element have the named attribute? |
| 59 | */ |
| 60 | hasAttrib(elem: ElementNode, name: string): boolean; |
| 61 | |
| 62 | /** |
| 63 | * takes an array of nodes, and removes any duplicates, as well as any |
| 64 | * nodes whose ancestors are also in the array. |
| 65 | */ |
| 66 | removeSubsets(nodes: Array<Node>): Array<Node>; |
| 67 | |
| 68 | /** |
| 69 | * finds all of the element nodes in the array that match the test predicate, |
| 70 | * as well as any of their children that match it. |
| 71 | */ |
| 72 | findAll(test: Predicate<ElementNode>, nodes: Array<Node>): Array<ElementNode>; |
| 73 | |
| 74 | /** |
| 75 | * finds the first node in the array that matches the test predicate, or one |
| 76 | * of its children. |
| 77 | */ |
| 78 | findOne(test: Predicate<ElementNode>, elems: Array<ElementNode>): ElementNode | undefined, |
| 79 | |
| 80 | /** |
| 81 | The adapter can also optionally include an equals method, if your DOM |
| 82 | structure needs a custom equality test to compare two objects which refer |
| 83 | to the same underlying node. If not provided, `css-select` will fall back to |
| 84 | `a === b`. |
| 85 | */ |
| 86 | equals?: (a: Node, b: Node) => boolean; |
| 87 | |
| 88 | /** |
| 89 | * is the element in hovered state? |
| 90 | */ |
| 91 | isHovered?: (elem: ElementNode) => boolean; |
| 92 | |
| 93 | /** |
| 94 | * is the element in visited state? |
| 95 | */ |
| 96 | isVisited?: (elem: ElementNode) => boolean; |
| 97 | |
| 98 | /** |
| 99 | * is the element in active state? |
| 100 | */ |
| 101 | isActive?: (elem: ElementNode) => boolean; |
| 102 | } |
| 103 | |
| 104 | // TODO default types to the domutil/httpparser2 types |
| 105 | interface Options<Node, ElementNode extends Node> { |
| 106 | /** |
| 107 | * When enabled, tag names will be case-sensitive. Default: false. |
| 108 | */ |
| 109 | xmlMode?: boolean; |
| 110 | /** |
| 111 | * Limits the module to only use CSS3 selectors. Default: false. |
| 112 | */ |
| 113 | strict?: boolean; |
| 114 | /** |
| 115 | * The last function in the stack, will be called with the last element |
| 116 | * that's looked at. Should return true. |
| 117 | */ |
| 118 | rootFunc?: (element: ElementNode) => true; |
| 119 | /** |
| 120 | * The adapter to use when interacting with the backing DOM structure. By |
| 121 | * default it uses domutils. |
| 122 | */ |
| 123 | adapter?: Adapter<Node, ElementNode>; |
| 124 | } |
| 125 | |
| 126 | type CompiledQuery = (node: any) => boolean; |
| 127 | type Query = string | CompiledQuery; |
| 128 | |
| 129 | /** |
| 130 | * Compiles the query, returns a function. |
| 131 | * |
| 132 | * Supported simple selectors: |
| 133 | * * Universal (*) |
| 134 | * * Tag (<tagname>) |
| 135 | * * Attribute ([attr=foo]), with supported comparisons: |
| 136 | * * [attr] (existential) |
| 137 | * * = |
| 138 | * * ~= |
| 139 | * * |= |
| 140 | * * *= |
| 141 | * * ^= |
| 142 | * * $= |
| 143 | * * != |
| 144 | * * Can be case insensitive (E.g. [attr=foo i]) |
| 145 | * * Pseudos: |
| 146 | * * :not |
| 147 | * * :root |
| 148 | * * :empty |
| 149 | * * :[first|last]-child[-of-type] |
| 150 | * * :only-of-type, :only-child |
| 151 | * * :nth-[last-]child[-of-type] |
| 152 | * * :link, :visited (the latter doesn't match any elements) |
| 153 | * * :checked |
| 154 | * * :enabled, :disabled |
| 155 | * * :required, :optional |
| 156 | * * Nonstandard Pseudos (available when strict mode is not enabled): |
| 157 | * * `:contains` |
| 158 | * * `:icontains` (case-insensitive version of :contains) |
| 159 | * * `:has` |
| 160 | * * `:parent` |
| 161 | * * `:selected` |
| 162 | * * `:header, :button, :input, :text, :checkbox, :file, :password, :reset, :radio etc. |
| 163 | * * :matches |
| 164 | * |
| 165 | * Supported Combinators: |
| 166 | * |
| 167 | * * Descendant (` `) |
| 168 | * * Child (`>`) |
| 169 | * * Parent (`<`) (when strict mode is not enabled) |
| 170 | * * Sibling (`~`) |
| 171 | * * Adjacent (`+`) |
| 172 | */ |
| 173 | function compile(query: string): CompiledQuery; |
| 174 | /** |
| 175 | * @template Node The generic Node type for the DOM adapter being used. |
| 176 | * @template ElementNode The Node type for elements for the DOM adapter being used. |
| 177 | * @param elems Elements to query. If it is an element, its children will be queried.. |
| 178 | * @param query can be either a CSS selector string or a compiled query function. |
| 179 | * @param [options] options for querying the document. |
| 180 | * @see CSSselect.compile for supported selector queries. |
| 181 | * @returns All matching elements. |
| 182 | */ |
| 183 | function selectAll<Node, ElementNode extends Node>( |
| 184 | query: Query, |
| 185 | elems: Array<ElementNode> | ElementNode, |
| 186 | options?: Options<Node, ElementNode> |
| 187 | ): Array<ElementNode>; |
| 188 | /** |
| 189 | * @template Node The generic Node type for the DOM adapter being used. |
| 190 | * @template ElementNode The Node type for elements for the DOM adapter being used. |
| 191 | * @param elems Elements to query. If it is an element, its children will be queried.. |
| 192 | * @param query can be either a CSS selector string or a compiled query function. |
| 193 | * @param [options] options for querying the document. |
| 194 | * @see CSSselect.compile for supported selector queries. |
| 195 | * @returns the first match, or null if there was no match. |
| 196 | */ |
| 197 | function selectOne<Node, ElementNode extends Node>( |
| 198 | query: Query, |
| 199 | elems: Array<ElementNode> | ElementNode, |
| 200 | options?: Options<Node, ElementNode> |
| 201 | ): ElementNode | null; |
| 202 | |
| 203 | /** |
| 204 | * Tests whether or not an element is matched by query. |
| 205 | * |
| 206 | * @template Node The generic Node type for the DOM adapter being used. |
| 207 | * @template ElementNode The Node type for elements for the DOM adapter being used. |
| 208 | * @param elem The element to test if it matches the query. |
| 209 | * @param query can be either a CSS selector string or a compiled query function. |
| 210 | * @param [options] options for querying the document. |
| 211 | * @see CSSselect.compile for supported selector queries. |
| 212 | * @returns |
| 213 | */ |
| 214 | function is<Node, ElementNode extends Node>( |
| 215 | elem: ElementNode, |
| 216 | query: Query, |
| 217 | options?: Options<Node, ElementNode> |
| 218 | ): boolean; |
| 219 | } |