UNPKG

5.81 kBTypeScriptView Raw
1import type { Selector } from "css-what";
2export declare type InternalSelector = Selector | {
3 type: "_flexibleDescendant";
4};
5export declare type Predicate<Value> = (v: Value) => boolean;
6export interface Adapter<Node, ElementNode extends Node> {
7 /**
8 * Is the node a tag?
9 */
10 isTag: (node: Node) => node is ElementNode;
11 /**
12 * Does at least one of passed element nodes pass the test predicate?
13 */
14 existsOne: (test: Predicate<ElementNode>, elems: Node[]) => boolean;
15 /**
16 * Get the attribute value.
17 */
18 getAttributeValue: (elem: ElementNode, name: string) => string | undefined;
19 /**
20 * Get the node's children
21 */
22 getChildren: (node: Node) => Node[];
23 /**
24 * Get the name of the tag
25 */
26 getName: (elem: ElementNode) => string;
27 /**
28 * Get the parent of the node
29 */
30 getParent: (node: ElementNode) => Node | null;
31 /**
32 * Get the siblings of the node. Note that unlike jQuery's `siblings` method,
33 * this is expected to include the current node as well
34 */
35 getSiblings: (node: Node) => Node[];
36 /**
37 * Returns the previous element sibling of a node.
38 */
39 prevElementSibling?: (node: Node) => ElementNode | null;
40 /**
41 * Get the text content of the node, and its children if it has any.
42 */
43 getText: (node: Node) => string;
44 /**
45 * Does the element have the named attribute?
46 */
47 hasAttrib: (elem: ElementNode, name: string) => boolean;
48 /**
49 * Takes an array of nodes, and removes any duplicates, as well as any
50 * nodes whose ancestors are also in the array.
51 */
52 removeSubsets: (nodes: Node[]) => Node[];
53 /**
54 * Finds all of the element nodes in the array that match the test predicate,
55 * as well as any of their children that match it.
56 */
57 findAll: (test: Predicate<ElementNode>, nodes: Node[]) => ElementNode[];
58 /**
59 * Finds the first node in the array that matches the test predicate, or one
60 * of its children.
61 */
62 findOne: (test: Predicate<ElementNode>, elems: Node[]) => ElementNode | null;
63 /**
64 * The adapter can also optionally include an equals method, if your DOM
65 * structure needs a custom equality test to compare two objects which refer
66 * to the same underlying node. If not provided, `css-select` will fall back to
67 * `a === b`.
68 */
69 equals?: (a: Node, b: Node) => boolean;
70 /**
71 * Is the element in hovered state?
72 */
73 isHovered?: (elem: ElementNode) => boolean;
74 /**
75 * Is the element in visited state?
76 */
77 isVisited?: (elem: ElementNode) => boolean;
78 /**
79 * Is the element in active state?
80 */
81 isActive?: (elem: ElementNode) => boolean;
82}
83export interface Options<Node, ElementNode extends Node> {
84 /**
85 * When enabled, tag names will be case-sensitive.
86 *
87 * @default false
88 */
89 xmlMode?: boolean;
90 /**
91 * Lower-case attribute names.
92 *
93 * @default !xmlMode
94 */
95 lowerCaseAttributeNames?: boolean;
96 /**
97 * Lower-case tag names.
98 *
99 * @default !xmlMode
100 */
101 lowerCaseTags?: boolean;
102 /**
103 * Is the document in quirks mode?
104 *
105 * This will lead to .className and #id being case-insensitive.
106 *
107 * @default false
108 */
109 quirksMode?: boolean;
110 /**
111 * Pseudo-classes that override the default ones.
112 *
113 * Maps from names to either strings of functions.
114 * - A string value is a selector that the element must match to be selected.
115 * - A function is called with the element as its first argument, and optional
116 * parameters second. If it returns true, the element is selected.
117 */
118 pseudos?: Record<string, string | ((elem: ElementNode, value?: string | null) => boolean)> | undefined;
119 /**
120 * The last function in the stack, will be called with the last element
121 * that's looked at.
122 */
123 rootFunc?: (element: ElementNode) => boolean;
124 /**
125 * The adapter to use when interacting with the backing DOM structure. By
126 * default it uses the `domutils` module.
127 */
128 adapter?: Adapter<Node, ElementNode>;
129 /**
130 * The context of the current query. Used to limit the scope of searches.
131 * Can be matched directly using the `:scope` pseudo-class.
132 */
133 context?: Node | Node[];
134 /**
135 * Indicates whether to consider the selector as a relative selector.
136 *
137 * Relative selectors that don't include a `:scope` pseudo-class behave
138 * as if they have a `:scope ` prefix (a `:scope` pseudo-class, followed by
139 * a descendant selector).
140 *
141 * If relative selectors are disabled, selectors starting with a traversal
142 * will lead to an error.
143 *
144 * @default true
145 * @see {@link https://www.w3.org/TR/selectors-4/#relative}
146 */
147 relativeSelector?: boolean;
148 /**
149 * Allow css-select to cache results for some selectors, sometimes greatly
150 * improving querying performance. Disable this if your document can
151 * change in between queries with the same compiled selector.
152 *
153 * @default true
154 */
155 cacheResults?: boolean;
156}
157export interface InternalOptions<Node, ElementNode extends Node> extends Options<Node, ElementNode> {
158 adapter: Adapter<Node, ElementNode>;
159 equals: (a: Node, b: Node) => boolean;
160}
161export interface CompiledQuery<ElementNode> {
162 (node: ElementNode): boolean;
163 shouldTestNextSiblings?: boolean;
164}
165export declare type Query<ElementNode> = string | CompiledQuery<ElementNode> | Selector[][];
166export declare type CompileToken<Node, ElementNode extends Node> = (token: InternalSelector[][], options: InternalOptions<Node, ElementNode>, context?: Node[] | Node) => CompiledQuery<ElementNode>;
167//# sourceMappingURL=types.d.ts.map
\No newline at end of file