Skip to content

Instantly share code, notes, and snippets.

@daveredfern
Created October 8, 2019 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daveredfern/550a45e060bcc867ad5c204dc5ce6b78 to your computer and use it in GitHub Desktop.
Save daveredfern/550a45e060bcc867ad5c204dc5ce6b78 to your computer and use it in GitHub Desktop.
/*
CSS Selector Generator, v1.2.0
by Riki Fridrich <riki@fczbkk.com> (http://fczbkk.com)
https://github.com/fczbkk/css-selector-generator/
*/(function(){var a,b,c=[].indexOf||function(a){for(var b=0,c=this.length;b<c;b++)if(b in this&&this[b]===a)return b;return-1};a=function(){function a(a){null==a&&(a={}),this.options={},this.setOptions(this.default_options),this.setOptions(a)}return a.prototype.default_options={selectors:["id","class","tag","nthchild"],prefix_tag:!1,attribute_blacklist:[],attribute_whitelist:[],quote_attribute_when_needed:!1,id_blacklist:[],class_blacklist:[]},a.prototype.setOptions=function(a){var b,c,d;null==a&&(a={}),c=[];for(b in a)d=a[b],this.default_options.hasOwnProperty(b)?c.push(this.options[b]=d):c.push(void 0);return c},a.prototype.isElement=function(a){return!(1!==(null!=a?a.nodeType:void 0))},a.prototype.getParents=function(a){var b,c;if(c=[],this.isElement(a))for(b=a;this.isElement(b);)c.push(b),b=b.parentNode;return c},a.prototype.getTagSelector=function(a){return this.sanitizeItem(a.tagName.toLowerCase())},a.prototype.sanitizeItem=function(a){var b;return b=a.split("").map(function(a){return":"===a?"\\"+":".charCodeAt(0).toString(16).toUpperCase()+" ":/[ !"#$%&'()*+,.\/;<=>?@\[\\\]^`{|}~]/.test(a)?"\\"+a:escape(a).replace(/\%/g,"\\")}),b.join("")},a.prototype.sanitizeAttribute=function(a){var b;return this.options.quote_attribute_when_needed?this.quoteAttribute(a):(b=a.split("").map(function(a){return":"===a?"\\"+":".charCodeAt(0).toString(16).toUpperCase()+" ":/[ !"#$%&'()*+,.\/;<=>?@\[\\\]^`{|}~]/.test(a)?"\\"+a:escape(a).replace(/\%/g,"\\")}),b.join(""))},a.prototype.quoteAttribute=function(a){var b,c;return c=!1,b=a.split("").map(function(a){return":"===a?(c=!0,a):"'"===a?(c=!0,"\\"+a):(c=c||escape(a===!a),a)}),c?"'"+b.join("")+"'":b.join("")},a.prototype.getIdSelector=function(a){var b,c,d,e;return d=this.options.prefix_tag?this.getTagSelector(a):"",b=a.getAttribute("id"),c=this.options.id_blacklist.concat(["",/\s/,/^\d/]),b&&null!=b&&""!==b&&this.notInList(b,c)&&(e=d+("#"+this.sanitizeItem(b)),1===a.ownerDocument.querySelectorAll(e).length)?e:null},a.prototype.notInList=function(a,b){return!b.find(function(b){return"string"==typeof b?b===a:b.exec(a)})},a.prototype.getClassSelectors=function(a){var b,c,d,e,f,g;if(g=[],b=a.getAttribute("class"),null!=b&&(b=b.replace(/\s+/g," "),b=b.replace(/^\s|\s$/g,""),""!==b))for(f=b.split(/\s+/),d=0,e=f.length;d<e;d++)c=f[d],this.notInList(c,this.options.class_blacklist)&&g.push("."+this.sanitizeItem(c));return g},a.prototype.getAttributeSelectors=function(a){var b,d,e,f,g,h,i,j,k,l,m,n;for(m=[],n=this.options.attribute_whitelist,f=0,h=n.length;f<h;f++)d=n[f],a.hasAttribute(d)&&m.push("["+d+"="+this.sanitizeAttribute(a.getAttribute(d))+"]");for(e=this.options.attribute_blacklist.concat(["id","class"]),j=a.attributes,g=0,i=j.length;g<i;g++)b=j[g],k=b.nodeName,c.call(e,k)>=0||(l=b.nodeName,c.call(n,l)>=0)||m.push("["+b.nodeName+"="+this.sanitizeAttribute(b.nodeValue)+"]");return m},a.prototype.getNthChildSelector=function(a){var b,c,d,e,f,g,h;if(e=a.parentNode,f=this.options.prefix_tag?this.getTagSelector(a):"",null!=e)for(b=0,h=e.childNodes,c=0,d=h.length;c<d;c++)if(g=h[c],this.isElement(g)&&(b++,g===a))return f+(":nth-child("+b+")");return null},a.prototype.testSelector=function(a,b){var c,d;return c=!1,null!=b&&""!==b&&(d=a.ownerDocument.querySelectorAll(b),1===d.length&&d[0]===a&&(c=!0)),c},a.prototype.testUniqueness=function(a,b){var c,d;return d=a.parentNode,c=d.querySelectorAll(b),1===c.length&&c[0]===a},a.prototype.testCombinations=function(a,b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p;if(null==c&&(c=this.getTagSelector(a)),!this.options.prefix_tag){for(m=this.getCombinations(b),e=0,g=m.length;e<g;e++)if(d=m[e],this.testSelector(a,d))return d;for(n=this.getCombinations(b),f=0,h=n.length;f<h;f++)if(d=n[f],this.testUniqueness(a,d))return d}for(o=this.getCombinations(b).map(function(a){return c+a}),k=0,i=o.length;k<i;k++)if(d=o[k],this.testSelector(a,d))return d;for(p=this.getCombinations(b).map(function(a){return c+a}),l=0,j=p.length;l<j;l++)if(d=p[l],this.testUniqueness(a,d))return d;return null},a.prototype.getUniqueSelector=function(a){var b,c,d,e,f,g,h;for(h=this.getTagSelector(a),d=this.options.selectors,b=0,c=d.length;b<c;b++){switch(f=d[b]){case"id":e=this.getIdSelector(a);break;case"tag":h&&this.testUniqueness(a,h)&&(e=h);break;case"class":g=this.getClassSelectors(a),null!=g&&0!==g.length&&(e=this.testCombinations(a,g,h));break;case"attribute":g=this.getAttributeSelectors(a),null!=g&&0!==g.length&&(e=this.testCombinations(a,g,h));break;case"nthchild":e=this.getNthChildSelector(a)}if(e)return e}return"*"},a.prototype.getSelector=function(a){var b,c,d,e,f,g,h;for(h=[],e=this.getParents(a),c=0,d=e.length;c<d;c++)if(b=e[c],g=this.getUniqueSelector(b),null!=g&&(h.unshift(g),f=h.join(" > "),this.testSelector(a,f)))return f;return null},a.prototype.getCombinations=function(a){var b,c,d,e,f,g,h;for(null==a&&(a=[]),h=[[]],b=d=0,f=a.length-1;0<=f?d<=f:d>=f;b=0<=f?++d:--d)for(c=e=0,g=h.length-1;0<=g?e<=g:e>=g;c=0<=g?++e:--e)h.push(h[c].concat(a[b]));return h.shift(),h=h.sort(function(a,b){return a.length-b.length}),h=h.map(function(a){return a.join("")})},a}(),("undefined"!=typeof define&&null!==define?define.amd:void 0)?define([],function(){return a}):(b="undefined"!=typeof exports&&null!==exports?exports:this,b.CssSelectorGenerator=a)}).call(this);
// first, create instance of the object with default options
my_selector_generator = new CssSelectorGenerator;
jQuery('*').click(function(e) {
e.stopPropagation();
console.log(my_selector_generator.getSelector(jQuery(this)[0]));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment