This algorithm when passed a DOM node will find a very short selector for that element.
Created
September 10, 2014 03:08
-
-
Save gampleman/6bf13be3f2ed1d09e5eb to your computer and use it in GitHub Desktop.
Algorithm to find a short unique CSS selector from an arbitrary node
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
function getPath(node) { | |
// phase 1: generation | |
var path = []; | |
while (node) { | |
name = node.localName; | |
if (!name) break; | |
name = name.toLowerCase(); | |
if (node.id && node.id !== '') { | |
path.unshift('#' + node.id); | |
break; | |
} | |
var parent = node.parentElement; | |
if (!parent) break; | |
if (node.classList.length > 0) { | |
for (var i = 0; i < node.classList.length; i++) { | |
var className = node.classList[i]; | |
var sameClassSiblings = [].filter.call(parent.children, function(e) { | |
return [].indexOf.call(e.classList, className) > 0; | |
}); | |
if (sameClassSiblings.length == 1) { | |
name = '.' + className; | |
break; | |
} | |
} | |
} else { | |
var sameTagSiblings = [].filter.call(parent.children, function(e) { return e.localName.toLowerCase() == name}); | |
if (sameTagSiblings.length > 1) { | |
allSiblings = parent.children; | |
var index = [].indexOf.call(allSiblings, node) + 1; | |
if (index > 1) { | |
name += ':nth-child(' + index + ')'; | |
} | |
} | |
} | |
path.unshift(name); | |
node = parent; | |
} | |
// phase 2: simplification | |
var results = 0, tempPath, origPath = path.slice(0);; | |
for (var i = path.length - 1; i >= 0; i--) { | |
// tempPath = path[i] + (tempPath ? '>' + tempPath : ''); | |
tempPath = path.slice(i).join(' '); | |
var newResults = document.querySelectorAll(tempPath).length; | |
if (newResults == results) { | |
path.splice(i, 1); | |
} else { | |
results = newResults; | |
} | |
} | |
// simplification failed | |
if (results != 1) { | |
path = origPath; | |
} | |
return path.join(' > '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment