MCPcopy Create free account
hub / github.com/ChromeDevTools/devtools-frontend / xPathIndex

Function xPathIndex

front_end/panels/elements/DOMPath.ts:238–286  ·  view source on GitHub ↗
(node: SDK.DOMModel.DOMNode)

Source from the content-addressed store, hash-verified

236};
237
238const xPathIndex = function(node: SDK.DOMModel.DOMNode): number {
239 /**
240 * Returns -1 in case of error, 0 if no siblings matching the same expression,
241 * <XPath index among the same expression-matching sibling nodes> otherwise.
242 */
243 function areNodesSimilar(left: SDK.DOMModel.DOMNode, right: SDK.DOMModel.DOMNode): boolean {
244 if (left === right) {
245 return true;
246 }
247
248 if (left.nodeType() === Node.ELEMENT_NODE && right.nodeType() === Node.ELEMENT_NODE) {
249 return left.localName() === right.localName();
250 }
251
252 if (left.nodeType() === right.nodeType()) {
253 return true;
254 }
255
256 // XPath treats CDATA as text nodes.
257 const leftType = left.nodeType() === Node.CDATA_SECTION_NODE ? Node.TEXT_NODE : left.nodeType();
258 const rightType = right.nodeType() === Node.CDATA_SECTION_NODE ? Node.TEXT_NODE : right.nodeType();
259 return leftType === rightType;
260 }
261
262 const siblings = node.parentNode ? node.parentNode.children() : null;
263 if (!siblings) {
264 return 0;
265 } // Root node - no siblings.
266 let hasSameNamedElements;
267 for (let i = 0; i < siblings.length; ++i) {
268 if (areNodesSimilar(node, siblings[i]) && siblings[i] !== node) {
269 hasSameNamedElements = true;
270 break;
271 }
272 }
273 if (!hasSameNamedElements) {
274 return 0;
275 }
276 let ownIndex = 1; // XPath indices start with 1.
277 for (let i = 0; i < siblings.length; ++i) {
278 if (areNodesSimilar(node, siblings[i])) {
279 if (siblings[i] === node) {
280 return ownIndex;
281 }
282 ++ownIndex;
283 }
284 }
285 return -1; // An error occurred: |node| not found in parent's children.
286};
287
288export class Step {
289 value: string;

Callers 1

xPathValueFunction · 0.85

Calls 2

areNodesSimilarFunction · 0.70
childrenMethod · 0.65

Tested by

no test coverage detected