MCPcopy Create free account
hub / github.com/andreadev-it/stacking-contexts-inspector / getPathFromNode

Function getPathFromNode

src/scripts/utils/utils.js:47–86  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

45 * @returns {Object[] | null}
46 */
47export function getPathFromNode(node) {
48
49 let curElement = node;
50 let path = [];
51 let curRoot = null;
52
53 do {
54 // get root node of current element. Might be a shadow-root or an iframe document
55 curRoot = curElement.getRootNode();
56
57
58 let pathFragment = {
59 index: Array.from(curRoot.querySelectorAll('*')).findIndex((el) => el === curElement),
60 type: "document"
61 }
62
63 if (curRoot instanceof ShadowRoot) {
64 // element is inside a shadowRoot
65 pathFragment.type = "shadow";
66
67 // if it's inside a closed shadow root, just return null, since it will be unreachable from the outside
68 if (curRoot.mode === "closed") {
69 return null;
70 }
71
72 curElement = curRoot.host;
73 }
74 else if (curRoot !== document) {
75 // is inside an iframe
76 pathFragment.type = "iframe";
77
78 curElement = curRoot.defaultView.frameElement;
79 }
80
81 path.push(pathFragment);
82
83 } while (curRoot !== document);
84
85 return path.reverse();
86}
87
88/**
89 * Get a Node from the path generated by the getPathFromNode function

Callers 1

getPathFromContextFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected