unique-selector
Version:
Given a DOM node, return a unique CSS selector matching only that element
20 lines (18 loc) • 476 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getID = getID;
/**
* Returns the Tag of the element
* @param { Object } element
* @return { String }
*/
function getID(el) {
var id = el.getAttribute('id');
if (id !== null && id !== '') {
// if the ID starts with a number or contains ":" selecting with a hash will cause a DOMException
return id.match(/(?:^\d|:)/) ? '[id="' + id + '"]' : '#' + id;
}
return null;
}