-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"esversion": 6 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,86 @@ | ||
self.on("click", function(node, data) { | ||
self.postMessage(node, data); | ||
self.postMessage(findCssSelector(node)); | ||
}); | ||
|
||
jQuery.fn.getPath = function() { | ||
if (this.length != 1) throw 'Requires one element.'; | ||
findCssSelector = function(ele) { | ||
ele = getRootBindingParent(ele); | ||
let document = ele.ownerDocument; | ||
if (!document || !document.contains(ele)) { | ||
throw new Error("findCssSelector received element not inside document"); | ||
} | ||
|
||
var path, node = this; | ||
while (node.length) { | ||
var realNode = node[0], | ||
name = realNode.localName; | ||
if (!name) break; | ||
name = name.toLowerCase(); | ||
// document.querySelectorAll("#id") returns multiple if elements share an ID | ||
if (ele.id && | ||
document.querySelectorAll("#" + CSS.escape(ele.id)).length === 1) { | ||
return "#" + CSS.escape(ele.id); | ||
} | ||
|
||
var parent = node.parent(); | ||
// Inherently unique by tag name | ||
let tagName = ele.localName; | ||
if (tagName === "html") { | ||
return "html"; | ||
} | ||
if (tagName === "head") { | ||
return "head"; | ||
} | ||
if (tagName === "body") { | ||
return "body"; | ||
} | ||
|
||
var siblings = parent.children(name); | ||
if (siblings.length > 1) { | ||
name += ':eq(' + siblings.index(realNode) + ')'; | ||
// We might be able to find a unique class name | ||
let selector, index, matches; | ||
if (ele.classList.length > 0) { | ||
for (let i = 0; i < ele.classList.length; i++) { | ||
// Is this className unique by itself? | ||
selector = "." + CSS.escape(ele.classList.item(i)); | ||
matches = document.querySelectorAll(selector); | ||
if (matches.length === 1) { | ||
return selector; | ||
} | ||
// Maybe it's unique with a tag name? | ||
selector = tagName + selector; | ||
matches = document.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 = document.querySelectorAll(selector); | ||
if (matches.length === 1) { | ||
return selector; | ||
} | ||
} | ||
} | ||
|
||
path = name + (path ? '>' + path : ''); | ||
node = parent; | ||
// Not unique enough yet. As long as it's not a child of the document, | ||
// continue recursing up until it is unique enough. | ||
if (ele.parentNode !== document) { | ||
index = positionInNodeList(ele, ele.parentNode.children) + 1; | ||
selector = findCssSelector(ele.parentNode) + " > " + | ||
tagName + ":nth-child(" + index + ")"; | ||
} | ||
|
||
return path; | ||
return selector; | ||
}; | ||
|
||
function positionInNodeList(element, nodeList) { | ||
for (let i = 0; i < nodeList.length; i++) { | ||
if (element === nodeList[i]) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
function getRootBindingParent(node) { | ||
let parent; | ||
let doc = node.ownerDocument; | ||
if (!doc) { | ||
return node; | ||
} | ||
//while ((parent = doc.getBindingParent(node))) { | ||
// node = parent; | ||
//} | ||
return node; | ||
} |
This file contains 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
This file contains 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