-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpath.ts
279 lines (233 loc) · 11.2 KB
/
xpath.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// tslint:disable
import { DATA_ATTR, DATA_SCREEN_READERS_ATTR } from '../../injectHighlightWrappers';
type Nullable<T> = T | null | undefined;
const findNonTextChild = (node: Node) => Array.prototype.find.call(node.childNodes,
(node: Node) => node.nodeType === Node.ELEMENT_NODE && !isTextHighlightOrScreenReaderNode(node)
);
const isHighlight = (node: Nullable<Node>): node is HTMLElement => !!node && (node as Element).getAttribute && (node as Element).getAttribute(DATA_ATTR) !== null;
const isHighlightOrScreenReaderNode = (node: Nullable<Node>) => isHighlight(node) || isScreenReaderNode(node);
const isTextHighlight = (node: Nullable<Node>): node is HTMLElement => isHighlight(node) && !findNonTextChild(node);
const isTextHighlightOrScreenReaderNode = (node: Nullable<Node>): node is HTMLElement => (isHighlight(node) || isScreenReaderNode(node)) && !findNonTextChild(node);
const isText = (node: Nullable<Node>): node is Text => !!node && node.nodeType === 3;
const isTextOrTextHighlight = (node: Nullable<Node>): node is Text | HTMLElement => isText(node) || isTextHighlight(node);
const isTextOrTextHighlightOrScreenReaderNode = (node: Nullable<Node | HTMLElement>) => isText(node) || isTextHighlightOrScreenReaderNode(node) || isScreenReaderNode(node);
const isElement = (node: Node): node is HTMLElement => node && node.nodeType === 1;
const isElementNotHighlight = (node: Node) => isElement(node) && !isHighlight(node);
const nodeIndex = (list: NodeList, element: Node) => Array.prototype.indexOf.call(list, element);
const isScreenReaderNode = (node: Nullable<Node>): node is HTMLElement => !!node && isElement(node) && node.getAttribute(DATA_SCREEN_READERS_ATTR) !== null;
const IS_PATH_PART_SELF = /^\.$/;
const IS_PATH_PART_TEXT = /^text\(\)\[(\d+)\]$/;
const IS_PATH_PART_ELEMENT = /\*\[name\(\)='(.+)'\]\[(\d+)\]/;
const getTextLength = (node: Node) => {
if (isText(node)) {
return node.length;
} else if (node && node.textContent) {
return node.textContent.length;
} else {
return 0;
}
};
const getMaxOffset = (node: Node) => {
if (isText(node)) {
return node.length;
} else {
return node.childNodes.length;
}
};
const recurseBackwardsThroughText = (current: Node, container: Node, resultOffset: number): [Node, number] => {
// we don't count the current, we count the previous
const previous = current.previousSibling;
if (previous && isTextOrTextHighlightOrScreenReaderNode(previous)) {
return recurseBackwardsThroughText(previous, container, resultOffset + getTextLength(previous));
} else if (current.parentNode && isTextHighlightOrScreenReaderNode(current.parentNode)) {
return recurseBackwardsThroughText(current.parentNode, container, resultOffset);
} else {
return [current, resultOffset];
}
};
const resolveTextHighlightsToTextOffset = (element: Node, offset: number, container: Node): [Node, number] => {
// this won't catch things that are right at the tail of the container, which is good, because we
// want to contiue using element offset if possible
if (isElement(element) && isTextOrTextHighlightOrScreenReaderNode(element.childNodes[offset])) {
return recurseBackwardsThroughText(element.childNodes[offset], container, 0);
// however, if the element, is a highlgiht, then we should float
} else if (isTextHighlightOrScreenReaderNode(element)) {
return recurseBackwardsThroughText(element, container, getTextLength(element));
// preserve the offset if the elment is text
} else if (isText(element)) {
return recurseBackwardsThroughText(element, container, offset);
} else {
return [element, offset];
}
};
const floatThroughText = (element: Node, offset: number, container: Node): [Node, number] => {
if (isTextOrTextHighlightOrScreenReaderNode(element) && offset === 0 && element.parentNode && element.parentNode !== container) {
return floatThroughText(element.parentNode, nodeIndex(element.parentNode.childNodes, element), container);
} else if (isTextOrTextHighlightOrScreenReaderNode(element) && offset === getMaxOffset(element) && element.parentNode && element.parentNode !== container) {
return floatThroughText(element.parentNode, nodeIndex(element.parentNode.childNodes, element) + 1, container);
} else if (
isTextOrTextHighlight(element)
&& (offset + 1) === getMaxOffset(element)
&& isElement(element.childNodes[offset])
&& isScreenReaderNode(element.childNodes[offset])
&& element.parentNode
&& element.parentNode !== container
) {
return floatThroughText(element.parentNode, nodeIndex(element.parentNode.childNodes, element) + 1, container);
} else {
return [element, offset];
}
};
const resolveToNextElementOffsetIfPossible = (element: Node, offset: number) => {
if (isTextOrTextHighlightOrScreenReaderNode(element) && element.parentNode && offset === getMaxOffset(element) && (!element.nextSibling || !isHighlightOrScreenReaderNode(element.nextSibling))) {
return [element.parentNode, nodeIndex(element.parentNode.childNodes, element) + 1] as const;
}
return [element, offset] as const;
};
const resolveToPreviousElementOffsetIfPossible = (element: Node, offset: number) => {
if (isTextOrTextHighlightOrScreenReaderNode(element) && element.parentNode && offset === 0 && (!element.previousSibling || !isHighlightOrScreenReaderNode(element.previousSibling))) {
return [element.parentNode, nodeIndex(element.parentNode.childNodes, element)] as const;
}
return [element, offset] as const;
};
// kinda copied from https://developer.mozilla.org/en-US/docs/Web/XPath/Snippets#getXPathForElement
export function getXPathForElement(targetElement: Node, offset: number, reference: HTMLElement): [string, number] {
[targetElement, offset] = floatThroughText(targetElement, offset, reference);
[targetElement, offset] = resolveToNextElementOffsetIfPossible(targetElement, offset);
[targetElement, offset] = resolveTextHighlightsToTextOffset(targetElement, offset, reference);
[targetElement, offset] = resolveToPreviousElementOffsetIfPossible(targetElement, offset);
let xpath = '';
let pos
, element = targetElement.previousSibling!
, focus = targetElement;
// for element targets, highlight children might be artifically
// inflating the range offset, fix.
if (isElement(focus)) {
let search: Node | null = focus.childNodes[offset - 1];
while (search) {
if (isTextOrTextHighlight(search)) {
search = search.previousSibling;
while (isTextOrTextHighlight(search!)) {
offset--;
search = search!.previousSibling;
}
}
search = search ? search.previousSibling : null;
}
}
while (focus !== reference) {
pos = 1;
while (element) {
// highlights in text change the number of nodes in the nodelist,z
// compensate by gobbling adjacent highlights and text
if (isTextOrTextHighlightOrScreenReaderNode(focus) && isTextOrTextHighlightOrScreenReaderNode(element)) {
while (isTextOrTextHighlightOrScreenReaderNode(element)) {
element = element.previousSibling!;
}
pos += 1;
} else {
if (isElementNotHighlight(focus) && isElementNotHighlight(element) && (element as Node).nodeName === (focus as Node).nodeName) {
pos += 1;
}
element = element.previousSibling!;
}
}
if (isText(focus) || isTextHighlightOrScreenReaderNode(focus)) {
xpath = 'text()[' + pos + ']' + '/' + xpath;
} else if (!isHighlightOrScreenReaderNode(focus)) {
xpath = '*[name()=\'' + focus.nodeName.toLowerCase() + '\'][' + pos + ']' + '/' + xpath;
}
focus = focus.parentNode!;
element = focus.previousSibling!;
}
xpath = './' + xpath;
xpath = xpath.replace(/\/$/, '');
return [xpath, offset];
}
export function getFirstByXPath(path: string, offset: number, referenceElement: HTMLElement): [HTMLElement | null, number] {
const parts = path.split('/');
let node: HTMLElement | null = referenceElement;
let part = parts.shift();
while (node && part) {
node = followPart(node, part);
part = parts.shift();
}
// the part following is greedy, so walk back to the first matching
// textish node before computing offset
while (isTextOrTextHighlightOrScreenReaderNode(node!) && isTextOrTextHighlightOrScreenReaderNode(node!.previousSibling!)) {
node = node!.previousSibling as HTMLElement;
}
// highligts split up text nodes that should be treated as one, iterate through
// until we find the text node that the offset specifies, modifying the offset
// as we go. prefer leaving highlights if we have the option to deal with
// adjacent highlights.
while ((isTextHighlightOrScreenReaderNode(node!) && offset >= node!.textContent!.length) || (isText(node!) && offset > node!.textContent!.length)) {
offset -= node!.textContent!.length;
node = isTextOrTextHighlightOrScreenReaderNode(node!.nextSibling!) ? node!.nextSibling as HTMLElement : null;
}
// for element targets, highlight children might be artifically
// inflating the range offset, fix.
if (node && isElement(node)) {
let search: Node | null = node.childNodes[0];
let offsetElementsFound = 0;
let modifyOffset = 0;
while (search && offsetElementsFound < offset) {
offsetElementsFound++;
if (isTextOrTextHighlightOrScreenReaderNode(search)) {
search = search.nextSibling;
while (isTextOrTextHighlightOrScreenReaderNode(search)) {
modifyOffset++;
search = search!.nextSibling;
}
} else {
search = search.nextSibling;
}
}
offset+=modifyOffset;
}
if (node && isHighlightOrScreenReaderNode(node)) {
node = null;
}
if (isElement(node!) && (node as Node).childNodes.length < offset) {
node = null;
}
return [node, offset];
}
function followPart(node: Node, part: string) {
const findFirst = (nodeList: NodeList, predicate: (node: Node) => boolean) =>
Array.prototype.find.call(nodeList, (node: Node) => predicate(node));
const findFirstAfter = (nodeList: NodeList, afterThis: Node, predicate: (node: Node) => boolean) => findFirst(
Array.prototype.slice.call(nodeList, Array.prototype.indexOf.call(nodeList, afterThis) + 1) as unknown as NodeList,
predicate
);
if (IS_PATH_PART_SELF.test(part)) {
return node;
}
if (IS_PATH_PART_TEXT.test(part)) {
let [, index] = part.match(IS_PATH_PART_TEXT) as any;
let text = findFirst(node.childNodes, isTextOrTextHighlightOrScreenReaderNode);
while (text && index > 1) {
let search = text;
while (isTextOrTextHighlightOrScreenReaderNode(search)) {
search = search.nextSibling;
}
index--;
if (search) {
text = findFirstAfter(node.childNodes, search, isTextOrTextHighlightOrScreenReaderNode);
} else {
text = search;
}
}
return text;
}
if (IS_PATH_PART_ELEMENT.test(part)) {
let [, type, index] = part.match(IS_PATH_PART_ELEMENT) as any;
const nodeMatches = (node: Node) => isElement(node) && node.nodeName.toLowerCase() === type.toLowerCase() && !isHighlightOrScreenReaderNode(node);
let element = findFirst(node.childNodes, nodeMatches);
while (element && index > 1) {
index--;
element = findFirstAfter(node.childNodes, element, nodeMatches);
}
return element;
}
}