UNPKG

style-to-js

Version:

Parses CSS inline style to JavaScript object (camelCased).

3 lines 2.35 kB
(function(e,t){typeof exports==`object`&&typeof module<`u`?module.exports=t():typeof define==`function`&&define.amd?define([],t):(e=typeof globalThis<`u`?globalThis:e||self,e.StyleToJS=t())})(this,function(){var e=/\/\*(?:[^*]|\*(?!\/))*\*\//g,t=/\n/g,n=/^\s*/,r=/^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/,i=/^:\s*/,a=/^((?:'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|url\((?:'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|[^)]*)\)|[^};])+)/,o=/^[;\s]*/,s=` `,c=`/`,l=`*`,u=``,d=`comment`,f=`declaration`;function p(p,m){if(typeof p!=`string`)throw TypeError(`First argument must be a string`);if(!p)return[];m||={};var h=1,g=1;function _(e){var n=e.match(t);n&&(h+=n.length);var r=e.lastIndexOf(s);g=~r?e.length-r:g+e.length}function v(){var e={line:h,column:g};return function(t){return t.position=new y(e),S(),t}}function y(e){this.start=e,this.end={line:h,column:g},this.source=m.source}y.prototype.content=p;function b(e){var t=Error(m.source+`:`+h+`:`+g+`: `+e);if(t.reason=e,t.filename=m.source,t.line=h,t.column=g,t.source=p,!m.silent)throw t}function x(e){var t=e.exec(p);if(t){var n=t[0];return _(n),p=p.slice(n.length),t}}function S(){x(n)}function C(e){for(var t;t=w();)e.push(t);return e}function w(){var e=v();if(!(c!=p.charAt(0)||l!=p.charAt(1))){for(var t=2;u!=p.charAt(t)&&(l!=p.charAt(t)||c!=p.charAt(t+1));)++t;if(t+=2,u===p.charAt(t-1))return b(`End of comment missing`);var n=p.slice(2,t-2);return g+=2,_(n),p=p.slice(t),g+=2,e({type:d,comment:n})}}function T(){var t=v(),n=x(r);if(n){if(w(),!x(i))return b(`property missing ':'`);var s=x(a),c=t({type:f,property:n[0].replace(e,u).trim(),value:s?s[0].replace(e,u).trim():u});return x(o),c}}function E(){var e=[];C(e);for(var t;t=T();)e.push(t),C(e);return e}return S(),E()}function m(e,t){let n=null;if(!e||typeof e!=`string`)return n;let r=p(e),i=typeof t==`function`;return r.forEach(e=>{if(e.type!==`declaration`)return;let{property:r,value:a}=e;i?t(r,a,e):a&&(n??={},n[r]=a)}),n}let h=/^--[a-zA-Z0-9_-]+$/,g=/-([a-z])/g,_=/^[^-]+$/,v=/^-(webkit|moz|ms|o|khtml)-/,y=/^-(ms)-/,b=e=>!e||_.test(e)||h.test(e),x=(e,t)=>t.toUpperCase(),S=(e,t)=>`${t}-`,C=(e,t={})=>b(e)?e:(e=e.toLowerCase(),e=t.reactCompat?e.replace(y,S):e.replace(v,S),e.replace(g,x));function w(e,t){let n={};return!e||typeof e!=`string`||m(e,(e,r)=>{e&&r&&(n[C(e,t)]=r)}),n}return w}); //# sourceMappingURL=style-to-js.min.js.map