Last active
December 4, 2015 10:10
-
-
Save cezary/3540780 to your computer and use it in GitHub Desktop.
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
# returns an elements unique css selector http://stackoverflow.com/questions/4588119/get-elements-css-selector-without-element-id | |
selector = (el, nodeChild = false)-> | |
names = [] | |
while el.parentNode | |
if el.id | |
names.unshift "##{ el.id }" | |
break | |
else | |
c = 1 | |
e = el | |
while (e.previousElementSibling and el.nodeType isnt 3) or (e.previousSibling and el.nodeType is 3) | |
e = e.previousSibling if el.nodeType is 3 | |
e = e.previousElementSibling if el.nodeType isnt 3 | |
c++ | |
if el.tagName? | |
tagName = el.tagName | |
else if nodeChild | |
tagName = 'NODE' | |
names.unshift tagName + ":nth-child(#{ c })" if tagName? | |
el = el.parentNode | |
names.join " > " | |
getNode = (str)-> | |
selector = str.split(' > NODE:nth-child(') | |
selector[1]=parseInt(selector[1].replace(')',''))-1 | |
$(selector[0])[0].childNodes[selector[1]] |
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
var getNode, selector; | |
selector = function(el, nodeChild) { | |
var c, e, names, tagName; | |
if (nodeChild == null) { | |
nodeChild = false; | |
} | |
names = []; | |
while (el.parentNode) { | |
if (el.id) { | |
names.unshift("#" + el.id); | |
break; | |
} else { | |
c = 1; | |
e = el; | |
while ((e.previousElementSibling && el.nodeType !== 3) || (e.previousSibling && el.nodeType === 3)) { | |
if (el.nodeType === 3) { | |
e = e.previousSibling; | |
} | |
if (el.nodeType !== 3) { | |
e = e.previousElementSibling; | |
} | |
c++; | |
} | |
if (el.tagName != null) { | |
tagName = el.tagName; | |
} else if (nodeChild) { | |
tagName = 'NODE'; | |
} | |
if (tagName != null) { | |
names.unshift(tagName + (":nth-child(" + c + ")")); | |
} | |
el = el.parentNode; | |
} | |
} | |
return names.join(" > "); | |
}; | |
getNode = function(str) { | |
selector = str.split(' > NODE:nth-child('); | |
selector[1] = parseInt(selector[1].replace(')', '')) - 1; | |
return $(selector[0])[0].childNodes[selector[1]]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment