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