UNPKG

j2c

Version:

A tiny CSS in JS solution.

1 lines 4.72 kB
"use strict";function cartesian(e,t,n,r,a,l){r=[];for(l in t)if(own.call(t,l))for(a in e)own.call(e,a)&&r.push(concat(e[a],t[l],n));return r}function concat(e,t,n){return n&&(/^[-\w$]+$/.test(t)&&":-error-bad-sub-selector-"+t||/&/.test(t)&&t.replace(/&/g,e))||e+t}function decamelize(e){return"-"+e.toLowerCase()}function declarations(e,t,n,r,a,l,o,s,c){if(null!=e)if(/\$/.test(n))for(c in n=n.split("$"))own.call(n,c)&&declarations(e,t,n[c],r,a,l);else switch(type.call(e=e.valueOf())){case ARRAY:for(o=0;o<e.length;o++)declarations(e[o],t,n,r,a,l);break;case OBJECT:n=n&&n+"-";for(o in e)if(own.call(e,o))if(s=e[o],/\$/.test(o))for(c in o=o.split("$"))own.call(o,c)&&declarations(s,t,n+o[c],r,a,l);else declarations(s,t,n+o,r,a,l);break;default:for(o=n.replace(/_/g,"-").replace(/[A-Z]/g,decamelize),!a||"animation-name"!=o&&"animation"!=o||(e=e.split(",").map(function(e){return e.replace(/()(?::global\(\s*([-\w]+)\s*\)|()([-\w]+))/,l.l)}).join(",")),/^animation|^transition/.test(o)&&(r=["webkit"]),o=o.replace(/^@/,"*"),c=0;c<r.length;c++)t.push("-",r[c],"-",o,o?":":"",e,";\n");t.push(o,o?":":"",e,";\n")}}function at(e,t,n,r,a,l,o,s){var c;if(/^@(?:namespace|import|charset)$/.test(e))if(type.call(t)==ARRAY)for(c=0;c<t.length;c++)n.push(e," ",t[c],";\n");else n.push(e," ",t,";\n");else if(/^@keyframes /.test(e))e=o?e.replace(/( )(?::global\(\s*([-\w]+)\s*\)|()([-\w]+))/,s.l):e,n.push("@-webkit-",e.slice(1)," {\n"),sheet(t,n,"","",["webkit"]),n.push("}\n"),n.push(e," {\n"),sheet(t,n,"","",l,o,s),n.push("}\n");else if(/^@extends?$/.test(e)){for(;c=findClass.exec(a);)e=c[4];if(null==e||!o)return void n.push("@-error-cannot-extend-in-global-context ",JSON.stringify(a),";\n");if(/^@extends?$/.test(e))return void n.push("@-error-no-class-to-extend-in ",JSON.stringify(a),";\n");s.e(type.call(t)==ARRAY?t.map(function(e){return e.replace(/()(?::global\(\s*(\.[-\w]+)\s*\)|()\.([-\w]+))/,s.l)}).join(" "):t.replace(/()(?::global\(\s*(\.[-\w]+)\s*\)|()\.([-\w]+))/,s.l),e)}else/^@(?:font-face$|viewport$|page )/.test(e)?sheet(t,n,e,e,emptyArray):/^@global$/.test(e)?sheet(t,n,r,a,l,0,s):/^@local$/.test(e)?sheet(t,n,r,a,l,1,s):/^@(?:media |supports |document )./.test(e)?(n.push(e," {\n"),sheet(t,n,r,a,l,o,s),n.push("}\n")):n.push("@-error-unsupported-at-rule ",JSON.stringify(e),";\n")}function sheet(e,t,n,r,a,l,o){var s,c,i,u;switch(type.call(e)){case ARRAY:for(s=0;s<e.length;s++)sheet(e[s],t,n,r,a,l,o);break;case OBJECT:for(s in e)i=e[s],n&&/^[-\w$]+$/.test(s)?(u||(u=1,t.push(n||"*"," {\n")),declarations(i,t,s,a,l,o)):/^@/.test(s)?(u=u&&t.push("}\n")&&0,at(s,i,t,n,r,a,l,o)):(u=u&&t.push("}\n")&&0,sheet(i,t,(c=/,/.test(n)||n&&/,/.test(s))?cartesian(n.split(","),(l?s.replace(/()(?::global\(\s*(\.[-\w]+)\s*\)|(\.)([-\w]+))/g,o.l):s).split(","),n).join(","):concat(n,l?s.replace(/()(?::global\(\s*(\.[-\w]+)\s*\)|(\.)([-\w]+))/g,o.l):s,n),c?cartesian(r.split(","),s.split(","),r).join(","):concat(r,s,r),a,l,o));u&&t.push("}\n");break;case STRING:t.push(n||":-error-no-selector"," {\n"),declarations(e,t,"",a,l,o),t.push("}\n")}}function j2c(e){function t(e,t){for(t=0;t<n.length;t++)e=n[t](e)||e;return e.join("")}e=e||{};var n=[];return e.use=function(){for(var t=arguments,r=0;r<t.length;r++)n.push(t[r]);return e},e.sheet=function(e,n){1===arguments.length&&(n=e,e={});var r,a=scope_root+counter++,l={},o=[];for(r in e)r-0!=r-0&&own.call(e,r)&&(l[r]=e[r]);sheet(n,o,"","",emptyArray,1,{e:function(e,t){var n=l[t];l[t]=n.slice(0,n.lastIndexOf(" ")+1)+e+" "+n.slice(n.lastIndexOf(" ")+1)},l:function(e,t,n,r,o){return n?t+n:(l[o]||(l[o]=o+a),t+r+l[o].match(/\S+$/))}}),o=new String(t(o));for(r in l)own.call(l,r)&&(o[r]=l[r]);return o},e.inline=function(e,n,r){return 1===arguments.length&&(n=e,e={}),declarations(n,r=[],"",emptyArray,1,{l:function(t,n,r,a,l){return r?n+r:e[l]?n+a+e[l]:l}}),t(r)},e.prefix=function(e,t){return cartesian(t.map(function(e){return"-"+e+"-"}).concat([""]),[e])},e}function kv(e,t,n){return n={},n[e]=t,n}var emptyObject={},emptyArray=[],type=emptyObject.toString,own=emptyObject.hasOwnProperty,OBJECT=type.call(emptyObject),ARRAY=type.call(emptyArray),STRING=type.call(""),findClass=/()(?::global\(\s*(\.[-\w]+)\s*\)|(\.)([-\w]+))/g,scope_root="_j2c_"+Math.floor(4294967296*Math.random()).toString(36)+"_"+Math.floor(4294967296*Math.random()).toString(36)+"_"+Math.floor(4294967296*Math.random()).toString(36)+"_"+Math.floor(4294967296*Math.random()).toString(36)+"_",counter=0;j2c.global=function(e){return":global("+e+")"},j2c.kv=kv,j2c.at=function e(t,n,r){if(arguments.length<3){var a=e.bind.apply(e,[null].concat([].slice.call(arguments,0)));return a.toString=function(){return"@"+t+" "+n},a}return kv("@"+t+" "+n,r)},j2c(j2c),delete j2c.use,module.exports=j2c;