css-gradient-parser
Advanced tools
Comparing version
interface ColorStop { | ||
color: string; | ||
offset: string; | ||
hint?: string; | ||
offset?: { | ||
unit: string; | ||
value: string; | ||
}; | ||
hint?: { | ||
unit: string; | ||
value: string; | ||
}; | ||
} | ||
@@ -6,0 +12,0 @@ |
@@ -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,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 c(e){let o=[];for(let t=0,n=e.length;t<n;){let[i,r]=p(e[t],/\s+/);m(e[t+1])?(o.push({color:i,offset:g(r),hint:g(e[t+1])}),t+=2):(o.push({color:i,offset:g(r)}),t++)}return o}var d=/^(-?\d+\.?\d*)(%|vw|vh|px|em|rem|deg|rad|grad|turn)$/;function m(e){return d.test(e)}function g(e){if(!e)return;let[,o,t]=e.trim().match(d)||[];return{value:o,unit:t}}function E(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=p(t),r=x(i[0]);return r&&(n.orientation=r,i.shift()),{...n,stops:c(i)}}function x(e){return e.startsWith("to ")?{type:"directional",value:e.replace("to ","")}:["turn","deg"].some(o=>e.endsWith(o))?{type:"angular",value:e}:null}var w=new Set(["closest-corner","closest-side","farthest-corner","farthest-side"]),v=new Set(["center","left","top","right","bottom"]);function h(e){return w.has(e)}function u(e){return v.has(e)}function S(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 P(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=p(t);if(C(i[0]))return{...n,stops:c(i)};let r=i[0].split("at").map(f=>f.trim()),l=((r[0]||"").match(/(circle|ellipse)/)||[])[1],a=(r[0]||"").match(/(-?\d+\.?\d*(vw|vh|px|em|rem)?|closest-corner|closest-side|farthest-corner|farthest-side)/g)||[],s=S((r[1]||"").split(" "));return l?n.shape=l:a.length===1&&!h(a[0])?n.shape="circle":n.shape="ellipse",a.length===0&&a.push("farthest-corner"),n.size=a.map(f=>({type:h(f)?"keyword":"length",value:f})),n.position.x={type:!s[0]||u(s[0])?"keyword":"length",value:s[0]||"center"},n.position.y={type:u(s[1])?"keyword":"length",value:s[1]},(l||a.length>0||r[1])&&i.shift(),{...n,stops:c(i)}}function C(e){return/^rgba?|hwb|hsl|lab|lch|oklab|color|#/.test(e)}var R=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[,o,t]=e.match(/(repeating-)?conic-gradient\((.+)\)/),n={angle:"0deg",repeating:!!o,position:"center",stops:[]},i=p(t).map(s=>s.trim()),r=p(i[0],/\s+/),l="",a=0;for(let s=0,f=r.length;s<f;s++)R.has(r[s])&&(s>0&&Object.assign(n,y(l,r,a,s)),l=r[s],a=s+1);return l&&(Object.assign(n,y(l,r,a,r.length)),i.shift()),{...n,stops:c(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{O as parseConicGradient,E as parseLinearGradient,P as parseRadialGradient}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "css-gradient-parser", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "a css gradient parser", | ||
@@ -5,0 +5,0 @@ "packageManager": "pnpm@8.7.0", |
@@ -17,7 +17,3 @@ # css-gradient-parser | ||
repeating: boolean | ||
stops: Array<{ | ||
color: string | ||
offset: string | ||
hint?: string | ||
}> | ||
stops: ColorStop[] | ||
} | ||
@@ -56,7 +52,3 @@ ``` | ||
color?: Color | ||
stops: Array<{ | ||
color: string | ||
offset: string | ||
hint?: string | ||
}> | ||
stops: ColorStop[] | ||
} | ||
@@ -63,0 +55,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
Sorry, the diff of this file is not supported yet
41876
3.75%87
10.13%71
-10.13%