-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtypes.ts
More file actions
207 lines (186 loc) · 5.68 KB
/
types.ts
File metadata and controls
207 lines (186 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import type { Selector } from "css-what";
/**
* Selector token used internally by css-select.
*/
export type InternalSelector = Selector | { type: "_flexibleDescendant" };
/**
* Predicate function for element matching.
*/
export type Predicate<Value> = (v: Value) => boolean;
/**
* Adapter interface used to traverse and inspect DOM nodes.
*/
export interface Adapter<Node, ElementNode extends Node> {
/**
* Is the node a tag?
*/
isTag: (node: Node) => node is ElementNode;
/**
* Get the attribute value.
*/
getAttributeValue: (
element: ElementNode,
name: string,
) => string | undefined;
/**
* Get the node's children
*/
getChildren: (node: Node) => Node[];
/**
* Get the name of the tag
*/
getName: (element: ElementNode) => string;
/**
* Get the parent of the node
*/
getParent: (node: ElementNode) => Node | null;
/**
* Get the siblings of the node. Note that unlike jQuery's `siblings` method,
* this is expected to include the current node as well
*/
getSiblings: (node: Node) => Node[];
/**
* Returns the previous element sibling of a node.
*/
prevElementSibling?: (node: Node) => ElementNode | null;
/**
* Get the text content of the node, and its children if it has any.
*/
getText: (node: Node) => string;
/**
* Does the element have the named attribute?
*/
hasAttrib: (element: ElementNode, name: string) => boolean;
/**
* Takes an array of nodes, and removes any duplicates, as well as any
* nodes whose ancestors are also in the array.
*/
removeSubsets: (nodes: Node[]) => Node[];
/**
* The adapter can also optionally include an equals method, if your DOM
* structure needs a custom equality test to compare two objects which refer
* to the same underlying node. If not provided, `css-select` will fall back to
* `a === b`.
*/
equals?: (a: Node, b: Node) => boolean;
/**
* Is the element in hovered state?
*/
isHovered?: (element: ElementNode) => boolean;
/**
* Is the element in visited state?
*/
isVisited?: (element: ElementNode) => boolean;
/**
* Is the element in active state?
*/
isActive?: (element: ElementNode) => boolean;
}
/**
* Public query options for css-select.
*/
export interface Options<Node, ElementNode extends Node> {
/**
* When enabled, tag names will be case-sensitive.
* @default false
*/
xmlMode?: boolean;
/**
* Lower-case attribute names.
* @default !xmlMode
*/
lowerCaseAttributeNames?: boolean;
/**
* Lower-case tag names.
* @default !xmlMode
*/
lowerCaseTags?: boolean;
/**
* Is the document in quirks mode?
*
* This will lead to .className and #id being case-insensitive.
* @default false
*/
quirksMode?: boolean;
/**
* Pseudo-classes that override the default ones.
*
* Maps from names to either strings of functions.
* - A string value is a selector that the element must match to be selected.
* - A function is called with the element as its first argument, and optional
* parameters second. If it returns true, the element is selected.
*/
pseudos?:
| Record<
string,
| string
| ((element: ElementNode, value?: string | null) => boolean)
>
| undefined;
/**
* The last function in the stack, will be called with the last element
* that's looked at.
*/
rootFunc?: (element: ElementNode) => boolean;
/**
* The adapter to use when interacting with the backing DOM structure. By
* default it uses the `domutils` module.
*/
adapter?: Adapter<Node, ElementNode> | undefined;
/**
* The context of the current query. Used to limit the scope of searches.
* Can be matched directly using the `:scope` pseudo-class.
*/
context?: Node | Node[];
/**
* Indicates whether to consider the selector as a relative selector.
*
* Relative selectors that don't include a `:scope` pseudo-class behave
* as if they have a `:scope ` prefix (a `:scope` pseudo-class, followed by
* a descendant selector).
*
* If relative selectors are disabled, selectors starting with a traversal
* will lead to an error.
* @default true
* @see {@link https://www.w3.org/TR/selectors-4/#relative}
*/
relativeSelector?: boolean;
/**
* Allow css-select to cache results for some selectors, sometimes greatly
* improving querying performance. Disable this if your document can
* change in between queries with the same compiled selector.
* @default true
*/
cacheResults?: boolean;
}
// Internally, we want to ensure that no propterties are accessed on the passed objects
/**
* Normalized internal options with required adapter helpers.
*/
export interface InternalOptions<Node, ElementNode extends Node>
extends Options<Node, ElementNode> {
adapter: Adapter<Node, ElementNode>;
equals: (a: Node, b: Node) => boolean;
}
/**
* Executable selector query.
*/
export interface CompiledQuery<ElementNode> {
(node: ElementNode): boolean;
shouldTestNextSiblings?: boolean;
}
/**
* Query input accepted by the public API.
*/
export type Query<ElementNode> =
| string
| CompiledQuery<ElementNode>
| Selector[][];
/**
* Function used to compile parsed selector tokens.
*/
export type CompileToken<Node, ElementNode extends Node> = (
token: InternalSelector[][],
options: InternalOptions<Node, ElementNode>,
context?: Node[] | Node,
) => CompiledQuery<ElementNode>;