Last active
September 23, 2019 14:18
-
-
Save akovantsev/f4131b09540e0cbbbf6ddb344329d720 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // path needs tail tweaks | |
| var tagname = function (node) { | |
| return node.tagName || "#text"; | |
| }; | |
| var xpath = function (node, idx, tag, path) { | |
| idx = idx || 0; | |
| tag = tag || tagname(node); | |
| path = path || ""; | |
| var left = node.previousSibling; | |
| var parent = node.parentNode; | |
| // console.log(node, node.nodeType, idx, tag, path); | |
| if (node.nodeType === 9) { // #document, root | |
| return path; | |
| } if (left) { | |
| if (tagname(left) === tag) { idx = idx + 1;} | |
| return xpath(left, idx, tag, path); | |
| } else if (parent) { | |
| path = "/" + tag + "[" + idx + "]" + path; | |
| return xpath(parent, 0, null, path); | |
| } | |
| }; | |
| // xpath(document.getSelection().focusNode) | |
| // "/HTML[0]/BODY[0]/DIV[0]/DIV[0]/DIV[1]/DIV[1]/DIV[2]/DIV[0]/DIV[6]/DIV[0]/DIV[0]/P[0]/SPAN[0]/#text[1]" | |
| // xpath(document.getSelection().anchorNode) | |
| // "/HTML[0]/BODY[0]/DIV[0]/DIV[0]/DIV[1]/DIV[1]/DIV[2]/DIV[0]/DIV[6]/DIV[0]/DIV[0]/P[0]/SPAN[0]/#text[1]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment