css-gradient-parser
Advanced tools
Comparing version
@@ -19,7 +19,20 @@ interface ColorStop { | ||
type RgExtentKeyword = 'closest-corner' | 'closest-side' | 'farthest-corner' | 'farthest-side'; | ||
type ValueType = 'keyword' | 'length'; | ||
interface RadialResult { | ||
shape: 'circle' | 'ellipse'; | ||
repeating: boolean; | ||
size: string; | ||
position: string; | ||
size: { | ||
type: ValueType; | ||
value: string; | ||
}[]; | ||
position: { | ||
x: { | ||
type: ValueType; | ||
value: string; | ||
}; | ||
y: { | ||
type: ValueType; | ||
value: string; | ||
}; | ||
}; | ||
stops: ColorStop[]; | ||
@@ -26,0 +39,0 @@ } |
@@ -1,2 +0,2 @@ | ||
function a(e,i=","){let o=[],n=0,r=0;i=new RegExp(i);for(let t=0;t<e.length;t++)e[t]==="("?r++:e[t]===")"&&r--,r===0&&i.test(e[t])&&(o.push(e.slice(n,t).trim()),n=t+1);return o.push(e.slice(n).trim()),o}function p(e){let i=[];for(let o=0,n=e.length;o<n;){let[r,t]=a(e[o],/\s+/);h(e[o+1])?(i.push({color:r,offset:t,hint:e[o+1]}),o+=2):(i.push({color:r,offset:t}),o++)}return i}function h(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[,i,o]=e.match(/(repeating-)?linear-gradient\((.+)\)/),n={orientation:{type:"directional",value:"bottom"},repeating:!!i,stops:[]},r=a(o),t=d(r[0]);return t&&(n.orientation=t,r.shift()),{...n,stops:p(r)}}function d(e){return e.startsWith("to ")?{type:"directional",value:e.replace("to ","")}:["turn","deg"].some(i=>e.endsWith(i))?{type:"angular",value:e}:null}function R(e){if(!/(repeating-)?radial-gradient/.test(e))throw new SyntaxError(`could not find syntax for this item: ${e}`);let[,i,o]=e.match(/(repeating-)?radial-gradient\((.+)\)/),n={shape:"ellipse",repeating:!!i,size:"farthest-corner",position:"center",stops:[]},r=a(o);if(u(r[0]))return{...n,stops:p(r)};let t=r[0].split("at").map(f=>f.trim()),l=((t[0]||"").match(/(circle|ellipse)/)||[])[1],c=((t[0]||"").match(/(\d+\.?\d*(vw|vh|px|em|rem)?|closest-corner|closest-side|farthest-corner|farthest-side)/)||[])[1],s=t[1];return(l||c||s)&&(r.shift(),Object.assign(n,{shape:l||"ellipse",size:c||"farthest-corner",position:s||"center"})),{...n,stops:p(r)}}function u(e){return/^rgba?|hwb|hsl|lab|lch|oklab|color|#/.test(e)}var m=new Set(["from","in","at"]);function L(e){if(!/(repeating-)?conic-gradient/.test(e))throw new SyntaxError(`could not find syntax for this item: ${e}`);let[,i,o]=e.match(/(repeating-)?conic-gradient\((.+)\)/),n={angle:"0deg",repeating:!!i,position:"center",stops:[]},r=a(o).map(s=>s.trim()),t=a(r[0],/\s+/),l="",c=0;for(let s=0,f=t.length;s<f;s++)m.has(t[s])&&(s>0&&Object.assign(n,g(l,t,c,s)),l=t[s],c=s+1);return l&&(Object.assign(n,g(l,t,c,t.length)),r.shift()),{...n,stops:p(r)}}function g(e,i,o,n){switch(e){case"from":return{angle:i.slice(o,n).join(" ")};case"at":return{position:i.slice(o,n).join(" ")};case"in":{let[r,...t]=i.slice(o,n);return{color:{space:r,method:t.length>0?t.join(" "):void 0}}}}}export{L as parseConicGradient,C as parseLinearGradient,R 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.8", | ||
"version": "0.0.9", | ||
"description": "a css gradient parser", | ||
@@ -5,0 +5,0 @@ "packageManager": "pnpm@8.7.0", |
@@ -27,12 +27,16 @@ # css-gradient-parser | ||
```ts | ||
type ValueType = 'keyword' | 'length' | ||
interface RadialResult { | ||
shape: 'circle' | 'ellipse' | ||
size: string | ||
repeating: boolean | ||
position: string | ||
stops: Array<{ | ||
color: string | ||
offset: string | ||
hint?: string | ||
}> | ||
size: { | ||
type: ValueType | ||
value: string | ||
}[] | ||
position: { | ||
x: { type: ValueType, value: string } | ||
y: { type: ValueType, value: string } | ||
} | ||
stops: ColorStop[] | ||
} | ||
@@ -39,0 +43,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
54327
16.64%89
23.61%79
5.33%