UNPKG

postcss-color-functional-notation

Version:
2 lines (1 loc) 1.43 kB
import s from"@csstools/postcss-progressive-custom-properties";import{color as o,SyntaxFlag as t,ColorNotation as e,serializeHSL as r,serializeRGB as a}from"@csstools/css-color-parser";import{hasFallback as n,hasSupportsAtRuleAncestor as c}from"@csstools/utilities";import{replaceComponentValues as i,parseCommaSeparatedListOfComponentValues as l,isFunctionNode as p,stringify as g}from"@csstools/css-parser-algorithms";import{tokenize as u}from"@csstools/css-tokenizer";const m=/^(?:rgb|hsl)a?$/i,f=/\b(?:rgb|hsl)a?\(/i,basePlugin=s=>({postcssPlugin:"postcss-color-functional-notation",Declaration(v){const h=v.value;if(!f.test(h))return;if(n(v))return;if(c(v,f))return;const y=i(l(u({css:h})),(s=>{if(!p(s)||!m.test(s.getName()))return;const n=o(s);return!n||n.syntaxFlags.has(t.Experimental)||n.syntaxFlags.has(t.HasNoneKeywords)||n.syntaxFlags.has(t.RelativeColorSyntax)||(n.syntaxFlags.has(t.LegacyRGB)||n.syntaxFlags.has(t.LegacyHSL))&&!n.syntaxFlags.has(t.HasPercentageAlpha)?void 0:n.colorNotation===e.HSL?r(n):a(n)})),x=g(y);x!==h&&(v.cloneBefore({value:x}),s?.preserve||v.remove())}});basePlugin.postcss=!0;const postcssPlugin=o=>{const t=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},o);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-color-functional-notation",plugins:[s(),basePlugin(t)]}:basePlugin(t)};postcssPlugin.postcss=!0;export{postcssPlugin as default};