-
Notifications
You must be signed in to change notification settings - Fork 0
/
css2html.min.js
5 lines (5 loc) · 2.49 KB
/
css2html.min.js
1
2
3
4
5
(function(k,o){function s(c){var b="document"in this?document:require("jsdom").jsdom("<html><body /></html>",null,{features:{QuerySelector:true}}),a=b.createElement("div"),e=k+ +new Date;a.id=e;c.sort().forEach(function(f){f=f.trim();f.split(/[+~](?![^\(\[]+[\)\]])/).forEach(function(d){var p=[a],l=0;d.split(/[ >](?![^\[]+[\]])/).forEach(function(g){l+=(l?1:0)+g.length;var m=d.substr(0,l),h=[];try{h=a.querySelectorAll("#"+e+">"+m)}catch(t){console.error("querySelectorAll: "+t)}if(!h.length){h=[];
node=u(g,b);i.dataAttr&&node.setAttribute("data-selector",m);var j=g.match(/:nth[^(]+\(([^)]+)\)/);j=j?(parseInt(j[1],10)||1)+1:/:(first|last)-(of|child)/.test(g)?2:/^li/.test(g)?2:1;p.forEach(function(v){for(var w=j;w--;){var n=node.cloneNode(false);n.__selector__=m;v.appendChild(n);h.push(n)}})}p=[].slice.call(h)})})});return a}function u(c,b){var a=c.match(/^\w+/)||["div"],e=b.createElement(a[0]);if(a=c.match(/#(?![^\[]+[\]])([^.:\[]+)/))e.id=a[1];var f=c.match(/\.(?![^\[]+[\]])[^.:#\[]+/g)||[];
f=f.join("").replace(/\./g," ").trim();(c.match(/\[([^\]'"]+(['"])[^\2]+\2|[^\]'"]+)\]|:(enabled|disabled|checked)/g)||[]).forEach(function(d){d=d.replace(/^[:\[]|['"]|[\]]$/g,"");d=d.split(/[~|\^$*]?=/);if(d[0]==="className"){if(d[1])f+=(f?" ":"")+d[1]}else if(d[0]==="id"){if(d[1])e.id=d[1]}else e.setAttribute(d[0],d[1]||d[0])});if(f)e.className=f;return e}function q(c){i.debug&&"console"in this&&console.log(c)}function x(c){var b=[];c=c.replace(/\s+/g," ").replace(/\/\*(.|\n)*?\*\//g,"").replace(/\s*([,:>+](?![^\[]+[\]]))\s*/g,
"$1");c.split(/\{(?![^\[]+[\]])[^}]*\}/g).forEach(function(a){a=a.trim();q("rule: "+a);/^(|@.*)$/.test(a)||a.split(/,(?![^\[]+[\]])/).forEach(function(e){e=e.replace(/(:[:\-])(?![^\[]+[\]])[^ >+~]+/g,"").replace(/^:root|:(link|visited|active|hover|focus|first-l(etter|ine)|before|after|empty|target)/g,"").replace(/\[([^=]+)=([^\]'"]+)\]/,'[$1="$2"]').trim();if(b.indexOf(e)===-1){q("selector: "+e);b.push(e)}})});return b}function r(c){[].slice.call(c.childNodes).forEach(function(b){if(b.childNodes.length)r(b);
else{var a=b.__selector__;if(/^input/.test(a))b.value=a;else if(/^img/.test(a))b.src=a;else if(!/^(area|br|col|hr|param)/.test(a))b.innerHTML=a}})}var i={dataAttr:false,debug:false,out:"nodes",populate:false};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports)o=module;k="exports"}o[k]=function(c,b){b||(b={});for(var a in b)if(b[a]!==void 0)i[a]=b[a];a=x(c);a=s(a);i.populate&&r(a);return i.out==="html"?a.innerHTML:[].slice.call(a.childNodes)}})("css2html",this);