css-gradient-parser
Advanced tools
Comparing version
@@ -13,6 +13,12 @@ interface ColorStop { | ||
interface LinearOrientation { | ||
type: 'directional' | 'angular'; | ||
type LinearOrientation = { | ||
type: 'directional'; | ||
value: string; | ||
} | ||
} | { | ||
type: 'angular'; | ||
value: { | ||
unit: string; | ||
value: string; | ||
}; | ||
}; | ||
interface LinearResult { | ||
@@ -19,0 +25,0 @@ orientation: LinearOrientation; |
@@ -1,2 +0,2 @@ | ||
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}; | ||
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:f(r),hint:f(e[t+1])}),t+=2):(o.push({color:i,offset:f(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 f(e){if(!e)return;let[,o,t]=e.trim().match(u)||[];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","grad","rad"].some(o=>e.endsWith(o))?{type:"angular",value:f(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 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 z(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(g=>g.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&&!d(a[0])?n.shape="circle":n.shape="ellipse",a.length===0&&a.push("farthest-corner"),n.size=a.map(g=>({type:d(g)?"keyword":"length",value:g})),n.position.x={type:!s[0]||h(s[0])?"keyword":"length",value:s[0]||"center"},n.position.y={type:h(s[1])?"keyword":"length",value:s[1]},(l||a.length>0||r[1])&&i.shift(),{...n,stops:c(i)}}function C(e){return/(circle|ellipse)/.test(e)?!1:/^(rgba?|hwb|hsl|lab|lch|oklab|color|#|[a-zA-Z]+)/.test(e)}var R=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=p(t).map(s=>s.trim()),r=p(i[0],/\s+/),l="",a=0;for(let s=0,g=r.length;s<g;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{j as parseConicGradient,E as parseLinearGradient,z as parseRadialGradient}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "css-gradient-parser", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"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
Sorry, the diff of this file is not supported yet
42578
1.68%93
6.9%