forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xpath.d.ts
197 lines (177 loc) · 7.64 KB
/
xpath.d.ts
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
// Type definitions for xpath v0.0.7
// Project: https://github.com/goto100/xpath
// Definitions by: Andrew Bradley <https://github.com/cspotcode/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Some documentation prose is copied from the XPath documentation at https://developer.mozilla.org.
declare module 'xpath' {
// select1 can return any of: `Node`, `boolean`, `string`, `number`.
// select and selectWithResolver can return any of the above return types or `Array<Node>`.
// For this reason, their return types are `any`.
interface SelectFn {
/**
* Evaluate an XPath expression against a DOM node. Returns the result as one of the following:
* * Array<Node>
* * Node
* * boolean
* * number
* * string
* @param xpathText
* @param contextNode
* @param single If true and the evaluation result is one or more Nodes, will return only the first Node instead of an Array<Node>
*/
(xpathText: string, contextNode: Node, single?: boolean): any;
}
var select: SelectFn;
/**
* Evaluate an xpath expression against a DOM node, returning the first result only.
* Equivalent to `select(xpathText, contextNode, true)`
* @param xpathText
* @param contextNode
*/
function select1(xpathText: string, contextNode: Node): any;
/**
* Evaluate an XPath expression against a DOM node using a given namespace resolver. Returns the result as one of the following:
* * Array<Node>
* * Node
* * boolean
* * number
* * string
* @param xpathText
* @param contextNode
* @param resolver
* @param single If true and the evaluation result is one or more Nodes, will return only the first Node instead of an Array<Node>
*/
function selectWithResolver(xpathText: string, contextNode: Node, resolver: XPathNSResolver, single?: boolean): any;
/**
* Evaluate an xpath expression against a DOM.
* @param xpathText xpath expression as a string.
* @param contextNode xpath expression is evaluated relative to this DOM node.
* @param resolver XML namespace resolver
* @param resultType
* @param result If non-null, xpath *may* reuse this XPathResult object instead of creating a new one. However, it is not required to do so.
* @return XPathResult object containing the result of the expression.
*/
function evaluate(xpathText: string, contextNode: Node, resolver: XPathNSResolver, resultType: number, result?: XPathResult): XPathResult;
/**
* Creates a `select` function that uses the given namespace prefix to URI mappings when evaluating queries.
* @param namespaceMappings an object mapping namespace prefixes to namespace URIs. Each key is a prefix; each value is a URI.
* @return a function with the same signature as `xpath.select`
*/
function useNamespaces(namespaceMappings: NamespaceMap): typeof select;
interface NamespaceMap {
[namespacePrefix: string]: string;
}
/**
* Compile an XPath expression into an XPathExpression which can be (repeatedly) evaluated against a DOM.
* @param xpathText XPath expression as a string
* @param namespaceURLMapper Namespace resolver
* @return compiled expression
*/
function createExpression(xpathText: string, namespaceURLMapper: XPathNSResolver): XPathExpression;
/**
* Create an XPathNSResolver that resolves based on the information available in the context of a DOM node.
* @param node
*/
function createNSResolver(node: Node): XPathNSResolver;
/**
* Result of evaluating an XPathExpression.
*/
class XPathResult {
/**
* A result set containing whatever type naturally results from evaluation of the expression. Note that if the result is a node-set then UNORDERED_NODE_ITERATOR_TYPE is always the resulting type.
*/
static ANY_TYPE: number;
/**
* A result containing a single number. This is useful for example, in an XPath expression using the count() function.
*/
static NUMBER_TYPE: number;
/**
* A result containing a single string.
*/
static STRING_TYPE: number;
/**
* A result containing a single boolean value. This is useful for example, in an XPath expression using the not() function.
*/
static BOOLEAN_TYPE: number;
/**
* A result node-set containing all the nodes matching the expression. The nodes may not necessarily be in the same order that they appear in the document.
*/
static UNORDERED_NODE_ITERATOR_TYPE: number;
/**
* A result node-set containing all the nodes matching the expression. The nodes in the result set are in the same order that they appear in the document.
*/
static ORDERED_NODE_ITERATOR_TYPE: number;
/**
* A result node-set containing snapshots of all the nodes matching the expression. The nodes may not necessarily be in the same order that they appear in the document.
*/
static UNORDERED_NODE_SNAPSHOT_TYPE: number;
/**
* A result node-set containing snapshots of all the nodes matching the expression. The nodes in the result set are in the same order that they appear in the document.
*/
static ORDERED_NODE_SNAPSHOT_TYPE: number;
/**
* A result node-set containing any single node that matches the expression. The node is not necessarily the first node in the document that matches the expression.
*/
static ANY_UNORDERED_NODE_TYPE: number;
/**
* A result node-set containing the first node in the document that matches the expression.
*/
static FIRST_ORDERED_NODE_TYPE: number;
/**
* Type of this result. It is one of the enumerated result types.
*/
resultType: number;
/**
* Returns the next node in this result, if this result is one of the _ITERATOR_ result types.
*/
iterateNext(): Node;
/**
* returns the result node for a given index, if this result is one of the _SNAPSHOT_ result types.
* @param index
*/
snapshotItem(index: number): Node;
/**
* Number of nodes in this result, if this result is one of the _SNAPSHOT_ result types.
*/
snapshotLength: number;
/**
* Value of this result, if it is a BOOLEAN_TYPE result.
*/
booleanValue: boolean;
/**
* Value of this result, if it is a NUMBER_TYPE result.
*/
numberValue: number;
/**
* Value of this result, if it is a STRING_TYPE result.
*/
stringValue: string;
/**
* Value of this result, if it is a FIRST_ORDERED_NODE_TYPE result.
*/
singleNodeValue: Node;
}
/**
* A compiled XPath expression, ready to be (repeatedly) evaluated against a DOM node.
*/
interface XPathExpression {
/**
* evaluate this expression against a DOM node.
* @param contextNode
* @param resultType
* @param result
*/
evaluate(contextNode: Node, resultType: number, result?: XPathResult): XPathResult;
}
/**
* Object that can resolve XML namespace prefixes to namespace URIs.
*/
interface XPathNSResolver {
/**
* Given an XML namespace prefix, returns the corresponding XML namespace URI.
* @param prefix XML namespace prefix
* @return XML namespace URI
*/
lookupNamespaceURI(prefix: string): string;
}
}