(node)
| 45 | * @returns {Object[] | null} |
| 46 | */ |
| 47 | export 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 |
no outgoing calls
no test coverage detected