UNPKG

css-gradient-parser

Version:

a css gradient parser

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