This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcss-selector.js
320 lines (277 loc) · 8.94 KB
/
css-selector.js
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var EXPORTED_SYMBOLS = [
"findAllCssSelectors",
"findCssSelector",
"getCssPath",
"getXPath",
];
/**
* Traverse getBindingParent until arriving upon the bound element
* responsible for the generation of the specified node.
* See https://developer.mozilla.org/en-US/docs/XBL/XBL_1.0_Reference/DOM_Interfaces#getBindingParent.
*
* @param {DOMNode} node
* @return {DOMNode}
* If node is not anonymous, this will return node. Otherwise,
* it will return the bound element
*
*/
function getRootBindingParent(node) {
let doc = node.ownerDocument;
if (!doc) {
return node;
}
let parent;
while ((parent = doc.getBindingParent(node))) {
node = parent;
}
return node;
}
/**
* Return the node's parent shadow root if the node in shadow DOM, null
* otherwise.
*/
function getShadowRoot(node) {
let doc = node.ownerDocument;
if (!doc) {
return null;
}
const parent = doc.getBindingParent(node);
const shadowRoot = parent && parent.openOrClosedShadowRoot;
if (shadowRoot) {
return shadowRoot;
}
return null;
}
/**
* Find the position of [element] in [nodeList].
* @returns an index of the match, or -1 if there is no match
*/
function positionInNodeList(element, nodeList) {
for (let i = 0; i < nodeList.length; i++) {
if (element === nodeList[i]) {
return i;
}
}
return -1;
}
/**
* For a provided node, find the appropriate container/node couple so that
* container.contains(node) and a CSS selector can be created from the
* container to the node.
*/
function findNodeAndContainer(node) {
const shadowRoot = getShadowRoot(node);
if (shadowRoot) {
// If the node is under a shadow root, the shadowRoot contains the node and
// we can find the node via shadowRoot.querySelector(path).
return {
containingDocOrShadow: shadowRoot,
node,
};
}
// Otherwise, get the root binding parent to get a non anonymous element that
// will be accessible from the ownerDocument.
const bindingParent = getRootBindingParent(node);
return {
containingDocOrShadow: bindingParent.ownerDocument,
node: bindingParent,
};
}
/**
* Find a unique CSS selector for a given element
* @returns a string such that:
* - ele.containingDocOrShadow.querySelector(reply) === ele
* - ele.containingDocOrShadow.querySelectorAll(reply).length === 1
*/
const findCssSelector = function(ele) {
const { node, containingDocOrShadow } = findNodeAndContainer(ele);
ele = node;
if (!containingDocOrShadow || !containingDocOrShadow.contains(ele)) {
// findCssSelector received element not inside container.
return "";
}
let cssEscape = ele.ownerGlobal.CSS.escape;
// document.querySelectorAll("#id") returns multiple if elements share an ID
if (ele.id &&
containingDocOrShadow.querySelectorAll("#" + cssEscape(ele.id)).length === 1) {
return "#" + cssEscape(ele.id);
}
// Inherently unique by tag name
let tagName = ele.localName;
if (tagName === "html") {
return "html";
}
if (tagName === "head") {
return "head";
}
if (tagName === "body") {
return "body";
}
// We might be able to find a unique class name
let selector, index, matches;
for (let i = 0; i < ele.classList.length; i++) {
// Is this className unique by itself?
selector = "." + cssEscape(ele.classList.item(i));
matches = containingDocOrShadow.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
// Maybe it's unique with a tag name?
selector = cssEscape(tagName) + selector;
matches = containingDocOrShadow.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
// Maybe it's unique using a tag name and nth-child
index = positionInNodeList(ele, ele.parentNode.children) + 1;
selector = selector + ":nth-child(" + index + ")";
matches = containingDocOrShadow.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
}
// Not unique enough yet.
index = positionInNodeList(ele, ele.parentNode.children) + 1;
selector = cssEscape(tagName) + ":nth-child(" + index + ")";
if (ele.parentNode !== containingDocOrShadow) {
selector = findCssSelector(ele.parentNode) + " > " + selector;
}
return selector;
};
/**
* If the element is in a frame or under a shadowRoot, return the corresponding
* element.
*/
function getSelectorParent(node) {
const shadowRoot = getShadowRoot(node);
if (shadowRoot) {
// The element is in a shadowRoot, return the host component.
return shadowRoot.host;
}
// Otherwise return the parent frameElement.
return node.ownerGlobal.frameElement;
}
/**
* Retrieve the array of CSS selectors corresponding to the provided node.
*
* The selectors are ordered starting with the root document and ending with the deepest
* nested frame. Additional items are used if the node is inside a frame or a shadow root,
* each representing the CSS selector for finding the frame or root element in its parent
* document.
*
* This format is expected by DevTools in order to handle the Inspect Node context menu
* item.
*
* @param {node}
* The node for which the CSS selectors should be computed
* @return {Array}
* An array of CSS selectors to find the target node. Several selectors can be
* needed if the element is nested in frames and not directly in the root
* document. The selectors are ordered starting with the root document and
* ending with the deepest nested frame or shadow root.
*/
const findAllCssSelectors = function(node) {
let selectors = [];
while (node) {
selectors.unshift(findCssSelector(node));
node = getSelectorParent(node);
}
return selectors;
};
/**
* Get the full CSS path for a given element.
* @returns a string that can be used as a CSS selector for the element. It might not
* match the element uniquely. It does however, represent the full path from the root
* node to the element.
*/
function getCssPath(ele) {
const { node, containingDocOrShadow } = findNodeAndContainer(ele);
ele = node;
if (!containingDocOrShadow || !containingDocOrShadow.contains(ele)) {
// getCssPath received element not inside container.
return "";
}
const nodeGlobal = ele.ownerGlobal.Node;
const getElementSelector = element => {
if (!element.localName) {
return "";
}
let label = element.nodeName == element.nodeName.toUpperCase()
? element.localName.toLowerCase()
: element.localName;
if (element.id) {
label += "#" + element.id;
}
if (element.classList) {
for (const cl of element.classList) {
label += "." + cl;
}
}
return label;
};
const paths = [];
while (ele) {
if (!ele || ele.nodeType !== nodeGlobal.ELEMENT_NODE) {
break;
}
paths.splice(0, 0, getElementSelector(ele));
ele = ele.parentNode;
}
return paths.length ? paths.join(" ") : "";
}
/**
* Get the xpath for a given element.
* @param {DomNode} ele
* @returns a string that can be used as an XPath to find the element uniquely.
*/
function getXPath(ele) {
const { node, containingDocOrShadow } = findNodeAndContainer(ele);
ele = node;
if (!containingDocOrShadow || !containingDocOrShadow.contains(ele)) {
// getXPath received element not inside container.
return "";
}
// Create a short XPath for elements with IDs.
if (ele.id) {
return `//*[@id="${ele.id}"]`;
}
// Otherwise walk the DOM up and create a part for each ancestor.
const parts = [];
const nodeGlobal = ele.ownerGlobal.Node;
// Use nodeName (instead of localName) so namespace prefix is included (if any).
while (ele && ele.nodeType === nodeGlobal.ELEMENT_NODE) {
let nbOfPreviousSiblings = 0;
let hasNextSiblings = false;
// Count how many previous same-name siblings the element has.
let sibling = ele.previousSibling;
while (sibling) {
// Ignore document type declaration.
if (sibling.nodeType !== nodeGlobal.DOCUMENT_TYPE_NODE &&
sibling.nodeName == ele.nodeName) {
nbOfPreviousSiblings++;
}
sibling = sibling.previousSibling;
}
// Check if the element has at least 1 next same-name sibling.
sibling = ele.nextSibling;
while (sibling) {
if (sibling.nodeName == ele.nodeName) {
hasNextSiblings = true;
break;
}
sibling = sibling.nextSibling;
}
const prefix = ele.prefix ? ele.prefix + ":" : "";
const nth = nbOfPreviousSiblings || hasNextSiblings
? `[${nbOfPreviousSiblings + 1}]` : "";
parts.push(prefix + ele.localName + nth);
ele = ele.parentNode;
}
return parts.length ? "/" + parts.reverse().join("/") : "";
}