simmerjs
Version:
A pure Javascript reverse CSS selector engine which calculates a DOM element's unique CSS selector on the current page.
48 lines (43 loc) • 1.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (hierarchy, state, validateSelector) {
var elm = hierarchy[0];
var tag = elm.el.nodeName;
if (handlers[tag]) {
state = handlers[tag](state, elm);
if (validateSelector(state)) {
// the unique attribute worked!
state.verified = true;
} else {
// turns out our so called unique attribute isn't as unique as we thought,
// we'll remove it to keep the selector's noise level down
state.stack[0].pop();
}
}
return state;
};
var handlers = {
A: function A(state, elm) {
var attribute = elm.el.getAttribute('href');
if (attribute) {
state.stack[0].push('A[href="' + attribute + '"]');
state.specificity += 10;
}
return state;
},
IMG: function IMG(state, elm) {
var attribute = elm.el.getAttribute('src');
if (attribute) {
state.stack[0].push('IMG[src="' + attribute + '"]');
state.specificity += 10;
}
return state;
}
/**
* Inspect the elements' special attributes which are likely to be unique to the element
* @param {array} hierarchy. The hierarchy of elements
* @param {object} state. The current calculated CSS selector
*/
};
;