-import o from"@csstools/postcss-progressive-custom-properties";import{hasFallback as e,hasSupportsAtRuleAncestor as t}from"@csstools/utilities";import{WhitespaceNode as r,TokenNode as i,FunctionNode as n,isCommentNode as s,isWhitespaceNode as l,isTokenNode as a,stringify as c,replaceComponentValues as u,parseCommaSeparatedListOfComponentValues as p,isFunctionNode as v}from"@csstools/css-parser-algorithms";import{TokenType as f,isTokenComma as h,isTokenIdent as m,tokenize as d}from"@csstools/css-tokenizer";import{serializeP3 as g,color as w,colorDataFitsRGB_Gamut as D,serializeRGB as C,SyntaxFlag as b}from"@csstools/css-color-parser";const x=/(?:repeating-)?(?:linear|radial|conic)-gradient\(/i,W=/\bin\b/i,P={test:o=>x.test(o)&&W.test(o)},A=/^(repeating-)?(linear|radial|conic)-gradient$/i;function interpolateColorsInColorStopsList(o,e,t,s=!1){const l=[],a=[];for(let s=0;s<o.length-1;s++){const l=o[s],c=o[s+1];if(a.push(l),t||g(l.colorData,!1).toString()!==g(c.colorData,!1).toString()&&l.position.toString()!==c.position.toString())for(let o=1;o<=9;o++){const s=10*o;let u=[];t&&(u=[new r([[f.Whitespace," ",-1,-1,void 0]]),t,new r([[f.Whitespace," ",-1,-1,void 0]]),new i([f.Ident,"hue",-1,-1,{value:"hue"}])]);const p=new n([f.Function,"color-mix(",-1,-1,{value:"color-mix"}],[f.CloseParen,")",-1,-1,void 0],[new i([f.Ident,"in",-1,-1,{value:"in"}]),new r([[f.Whitespace," ",-1,-1,void 0]]),e,...u,new i([f.Comma,",",-1,-1,void 0]),new r([[f.Whitespace," ",-1,-1,void 0]]),l.color,new r([[f.Whitespace," ",-1,-1,void 0]]),new i([f.Percentage,100-s+"%",-1,-1,{value:100-s}]),new i([f.Comma,",",-1,-1,void 0]),new r([[f.Whitespace," ",-1,-1,void 0]]),c.color,new r([[f.Whitespace," ",-1,-1,void 0]]),new i([f.Percentage,`${s}%`,-1,-1,{value:s}])]),v=w(p);if(!v)return!1;a.push({colorData:v})}s===o.length-2&&a.push(c)}for(let o=0;o<a.length;o++)s&&!D(a[o].colorData)?a[o].color=g(a[o].colorData,!1):a[o].color=C(a[o].colorData,!1);for(let o=0;o<a.length;o++){const e=a[o];e.position?l.push(e.color,new r([[f.Whitespace," ",-1,-1,void 0]]),e.position):l.push(e.color),o!==a.length-1&&l.push(new i([f.Comma,",",-1,-1,void 0]),new r([[f.Whitespace," ",-1,-1,void 0]]))}return l}function parseColorStops(o){const e=[];let t={};for(let r=0;r<o.length;r++){const i=o[r];if(s(i)||l(i))continue;if(a(i)&&h(i.value)){if(t.color&&t.colorData&&t.positionA){e.push({color:t.color,colorData:t.colorData,position:t.positionA}),t.positionB&&e.push({color:t.color,colorData:t.colorData,position:t.positionB}),t={};continue}return!1}const n=w(i);if(n){if(t.color)return!1;if(n.syntaxFlags.has(b.Experimental))return!1;t.color=i,t.colorData=n}else{if(!t.color)return!1;if(t.positionA){if(!t.positionA||t.positionB)return!1;t.positionB=i}else t.positionA=i}}return!(!t.color||!t.positionA)&&(t.color&&t.colorData&&t.positionA&&(e.push({color:t.color,colorData:t.colorData,position:t.positionA}),t.positionB&&e.push({color:t.color,colorData:t.colorData,position:t.positionB})),!(e.length<2)&&e)}const B=/^(?:srgb|srgb-linear|lab|oklab|xyz|xyz-d50|xyz-d65|hsl|hwb|lch|oklch)$/i,S=/^(?:hsl|hwb|lch|oklch)$/i,y=/^(?:shorter|longer|increasing|decreasing)$/i,$=/^in$/i,k=/^hue$/i;function modifyGradientFunctionComponentValues(o,e=!1){const t=o.getName();if(!A.test(t))return!1;let n,c,u,p="srgb",v=null,d=null,g=null;{let e=0,t=o.value[e];for(;t&&!(a(t)&&m(t.value)&&$.test(t.value[4].value));){if(a(t)&&h(t.value))return!1;e++,t=o.value[e]}for(n=t,e++,t=o.value[e];s(t)||l(t);)e++,t=o.value[e];if(a(t)&&m(t.value)&&B.test(t.value[4].value)){if(v)return!1;v=t,p=t.value[4].value,e++,t=o.value[e]}for(;s(t)||l(t);)e++,t=o.value[e];if(a(t)&&m(t.value)&&y.test(t.value[4].value)&&S.test(p)){if(d||!v)return!1;d=t,e++,t=o.value[e]}for(;s(t)||l(t);)e++,t=o.value[e];if(a(t)&&m(t.value)&&k.test(t.value[4].value)){if(g||!v||!d)return!1;g=t,e++,t=o.value[e]}for(;t&&(!a(t)||!h(t.value));)e++,t=o.value[e];if(c=t,!c)return!1;u=o.value.slice(e+1)}if(!v)return!1;if(d&&!g)return!1;if(g&&!d)return!1;const w=parseColorStops(u);if(!w)return!1;const D=interpolateColorsInColorStopsList(w,v,d,e);if(!D)return!1;const C=trim([...o.value.slice(0,o.value.indexOf(n)),...o.value.slice(o.value.indexOf(g||v)+1,o.value.indexOf(c))]);return C.length>0&&C.some(o=>!s(o))&&C.push(new i([f.Comma,",",-1,-1,void 0]),new r([[f.Whitespace," ",-1,-1,void 0]])),trim([...C,...trim(D)])}function trim(o){let e=0,t=o.length-1;for(let t=0;t<o.length;t++)if(!l(o[t])){e=t;break}for(let e=o.length-1;e>=0;e--)if(!l(o[e])){t=e;break}return o.slice(e,t+1)}const basePlugin=o=>({postcssPlugin:"postcss-gradients-interpolation-method",Declaration(r){if(!P.test(r.value))return;if(e(r))return;if(t(r,P))return;const i=d({css:r.value}),n=c(u(p(i),o=>{if(!v(o))return;const e=modifyGradientFunctionComponentValues(o);e&&(o.value=e)}));if(n===r.value)return;const s=c(u(p(i),o=>{if(!v(o))return;const e=modifyGradientFunctionComponentValues(o,!0);e&&(o.value=e)}));r.cloneBefore({value:n}),n!==s&&r.cloneBefore({value:s}),o?.preserve||r.remove()}});basePlugin.postcss=!0;const postcssPlugin=e=>{const t=Object.assign({enableProgressiveCustomProperties:!0,preserve:!0},e);return t.enableProgressiveCustomProperties?{postcssPlugin:"postcss-gradients-interpolation-method",plugins:[o(),basePlugin(t)]}:basePlugin(t)};postcssPlugin.postcss=!0;export{postcssPlugin as default,postcssPlugin as"module.exports"};
0 commit comments