epiphany/node_modules/@csstools/postcss-oklab-function/dist/index.mjs
2023-12-09 22:48:07 -08:00

1 line
2.1 KiB
JavaScript

import s from"@csstools/postcss-progressive-custom-properties";import{tokenize as e}from"@csstools/css-tokenizer";import{color as r,SyntaxFlag as t,serializeRGB as o,colorDataFitsRGB_Gamut as a,serializeP3 as n}from"@csstools/css-color-parser";import{replaceComponentValues as l,parseCommaSeparatedListOfComponentValues as u,isFunctionNode as i,stringify as p}from"@csstools/css-parser-algorithms";function hasFallback(s){const e=s.parent;if(!e)return!1;const r=s.prop.toLowerCase(),t=e.index(s);for(let s=0;s<t;s++){const t=e.nodes[s];if("decl"===t.type&&t.prop.toLowerCase()===r)return!0}return!1}function hasSupportsAtRuleAncestor(s){let e=s.parent;for(;e;)if("atrule"===e.type){if("supports"===e.name.toLowerCase()){if(-1!==e.params.toLowerCase().indexOf("oklab("))return!0;if(-1!==e.params.toLowerCase().indexOf("oklch("))return!0}e=e.parent}else e=e.parent;return!1}const c=/(oklab|oklch)\(/i,f=/^(oklab|oklch)$/i,basePlugin=s=>({postcssPlugin:"postcss-oklab-function",Declaration:m=>{const b=m.value;if(!c.test(b))return;if(hasFallback(m))return;if(hasSupportsAtRuleAncestor(m))return;const g=e({css:b}),y=l(u(g),(s=>{if(i(s)&&f.test(s.getName())){const e=r(s);if(!e)return;if(e.syntaxFlags.has(t.HasNoneKeywords))return;if(e.syntaxFlags.has(t.RelativeColorSyntax))return;return o(e)}})),d=p(y);if(d===b)return;let h=d;null!=s&&s.subFeatures.displayP3&&(h=p(l(u(g),(s=>{if(i(s)&&f.test(s.getName())){const e=r(s);if(!e)return;if(e.syntaxFlags.has(t.HasNoneKeywords))return;if(e.syntaxFlags.has(t.RelativeColorSyntax))return;return a(e)?o(e):n(e)}})))),m.cloneBefore({value:d}),null!=s&&s.subFeatures.displayP3&&h!==d&&m.cloneBefore({value:h}),null!=s&&s.preserve||m.remove()}});basePlugin.postcss=!0;const postcssPlugin=e=>{const r=Object.assign({enableProgressiveCustomProperties:!0,preserve:!1,subFeatures:{displayP3:!0}},e);return r.subFeatures=Object.assign({displayP3:!0},r.subFeatures),r.enableProgressiveCustomProperties&&(r.preserve||r.subFeatures.displayP3)?{postcssPlugin:"postcss-oklab-function",plugins:[s(),basePlugin(r)]}:basePlugin(r)};postcssPlugin.postcss=!0;export{postcssPlugin as default};