css-gradient-parser
Version:
a css gradient parser
2 lines • 3.87 kB
JavaScript
var u=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var R=(e,t)=>{for(var r in t)u(e,r,{get:t[r],enumerable:!0})},S=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of w(t))!v.call(e,n)&&n!==r&&u(e,n,{get:()=>t[n],enumerable:!(o=x(t,n))||o.enumerable});return e};var C=e=>S(u({},"__esModule",{value:!0}),e);var W={};R(W,{parseConicGradient:()=>O,parseLinearGradient:()=>E,parseRadialGradient:()=>z});module.exports=C(W);function c(e,t=","){let r=[],o=0,n=0;t=new RegExp(t);for(let i=0;i<e.length;i++)e[i]==="("?n++:e[i]===")"&&n--,n===0&&t.test(e[i])&&(r.push(e.slice(o,i).trim()),o=i+1);return r.push(e.slice(o).trim()),r}function g(e){let t=[];for(let r=0,o=e.length;r<o;){let[n,i]=c(e[r],/\s+/);b(e[r+1])?(t.push({color:n,offset:l(i),hint:l(e[r+1])}),r+=2):(t.push({color:n,offset:l(i)}),r++)}return t}var d=/^(-?\d+\.?\d*)(%|vw|vh|px|em|rem|deg|rad|grad|turn)$/;function b(e){return d.test(e)}function l(e){if(!e)return;let[,t,r]=e.trim().match(d)||[];return{value:t,unit:r}}function E(e){if(!/^(repeating-)?linear-gradient/.test(e))throw new SyntaxError(`could not find syntax for this item: ${e}`);let[,t,r]=e.match(/(repeating-)?linear-gradient\((.+)\)/),o={orientation:{type:"directional",value:"bottom"},repeating:!!t,stops:[]},n=c(r),i=P(n[0]);return i&&(o.orientation=i,n.shift()),{...o,stops:g(n)}}function P(e){return e.startsWith("to ")?{type:"directional",value:e.replace("to ","")}:["turn","deg","grad","rad"].some(t=>e.endsWith(t))?{type:"angular",value:l(e)}:null}var k=new Set(["closest-corner","closest-side","farthest-corner","farthest-side"]),L=new Set(["center","left","top","right","bottom"]);function h(e){return k.has(e)}function y(e){return L.has(e)}function K(e){let t=Array(2).fill("");for(let r=0;r<2;r++)e[r]?t[r]=e[r]:t[r]="center";return t}function z(e){if(!/(repeating-)?radial-gradient/.test(e))throw new SyntaxError(`could not find syntax for this item: ${e}`);let[,t,r]=e.match(/(repeating-)?radial-gradient\((.+)\)/),o={shape:"ellipse",repeating:!!t,size:[{type:"keyword",value:"farthest-corner"}],position:{x:{type:"keyword",value:"center"},y:{type:"keyword",value:"center"}},stops:[]},n=c(r);if(G(n[0]))return{...o,stops:g(n)};let i=n[0].split("at").map(f=>f.trim()),p=((i[0]||"").match(/(circle|ellipse)/)||[])[1],a=(i[0]||"").match(/(-?\d+\.?\d*(vw|vh|px|em|rem|%|rad|grad|turn|deg)?|closest-corner|closest-side|farthest-corner|farthest-side)/g)||[],s=K((i[1]||"").split(" "));return p?o.shape=p:a.length===1&&!h(a[0])?o.shape="circle":o.shape="ellipse",a.length===0&&a.push("farthest-corner"),o.size=a.map(f=>h(f)?{type:"keyword",value:f}:{type:"length",value:l(f)}),o.position.x=y(s[0])?{type:"keyword",value:s[0]}:{type:"length",value:l(s[0])},o.position.y=y(s[1])?{type:"keyword",value:s[1]}:{type:"length",value:l(s[1])},(p||a.length>0||i[1])&&n.shift(),{...o,stops:g(n)}}function G(e){return/(circle|ellipse|at)/.test(e)?!1:/^(rgba?|hwb|hsl|lab|lch|oklab|color|#|[a-zA-Z]+)/.test(e)}var j=new Set(["from","in","at"]);function O(e){if(!/(repeating-)?conic-gradient/.test(e))throw new SyntaxError(`could not find syntax for this item: ${e}`);let[,t,r]=e.match(/(repeating-)?conic-gradient\((.+)\)/),o={angle:"0deg",repeating:!!t,position:"center",stops:[]},n=c(r).map(s=>s.trim()),i=c(n[0],/\s+/),p="",a=0;for(let s=0,f=i.length;s<f;s++)j.has(i[s])&&(s>0&&Object.assign(o,m(p,i,a,s)),p=i[s],a=s+1);return p&&(Object.assign(o,m(p,i,a,i.length)),n.shift()),{...o,stops:g(n)}}function m(e,t,r,o){switch(e){case"from":return{angle:t.slice(r,o).join(" ")};case"at":return{position:t.slice(r,o).join(" ")};case"in":{let[n,...i]=t.slice(r,o);return{color:{space:n,method:i.length>0?i.join(" "):void 0}}}}}0&&(module.exports={parseConicGradient,parseLinearGradient,parseRadialGradient});
//# sourceMappingURL=index.cjs.map