Created
March 28, 2013 11:09
-
-
Save abidibo/5262403 to your computer and use it in GitHub Desktop.
Reverse selector engineering. Get css selector from element.
Works with browsers implementing the :nth-of-type selector.
Requires mootools selectors.
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
// requires mootools selectors | |
var path = function path(element) { | |
if(element.get('id')) { | |
return '#' + element.get('id'); | |
} | |
if(element == document.body) { | |
return 'body'; | |
} | |
var prev_equal_siblings_length = element.getAllPrevious(element.get('tag')).length; | |
var part = element.get('tag') + ':nth-of-type(' + (prev_equal_siblings_length + 1) + ')'; | |
return path(element.getParent()) + ' > ' + part; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment