2 lines
2.2 KiB
JavaScript
2 lines
2.2 KiB
JavaScript
|
import e from"@csstools/postcss-progressive-custom-properties";import t from"postcss-value-parser";function hasFallback(e){const t=e.parent;if(!t)return!1;const r=e.prop.toLowerCase(),s=t.index(e);for(let e=0;e<s;e++){const s=t.nodes[e];if("decl"===s.type&&s.prop.toLowerCase()===r)return!0}return!1}const r=/(?:repeating-)?(?:conic|linear|radial)-gradient\(/i,s=/^(?:repeating-)?(?:conic|linear|radial)-gradient$/i;function hasSupportsAtRuleAncestor(e){let t=e.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name.toLowerCase()&&r.test(t.params))return!0;t=t.parent}else t=t.parent;return!1}const o=["at","bottom","center","circle","closest-corner","closest-side","ellipse","farthest-corner","farthest-side","from","in","left","right","to","top"],isPunctuationCommaNode=e=>"div"===e.type&&","===e.value;function isNumericNode(e){try{return!1!==t.unit(null==e?void 0:e.value)}catch(e){return!1}}const basePlugin=e=>({postcssPlugin:"postcss-double-position-gradients",Declaration(n,{result:i}){if(!r.test(n.value))return;if(hasFallback(n))return;if(hasSupportsAtRuleAncestor(n))return;let a;try{a=t(n.value)}catch(e){n.warn(i,`Failed to parse value '${n.value}' as a CSS gradient. Leaving the original value intact.`)}if(void 0===a)return;a.walk((e=>{if("function"!==e.type||!s.test(e.value))return;const t=e.nodes.filter((e=>"comment"!==e.type&&"space"!==e.type));let r=!1;t.forEach(((t,s,n)=>{if("word"===t.type&&o.includes(t.value.toLowerCase())&&(r=!0),"div"===t.type&&","===t.value&&(r=!1),r)return;const i=Object(n[s-1]),a=Object(n[s-2]),c=Object(n[s+1]);if(a.type&&isNumericNode(i)&&isNumericNode(t)){const r=a,s={type:"div",value:",",before:isPunctuationCommaNode(c)?c.before:"",after:isPunctuationCommaNode(c)?"":" ",sourceIndex:0,sourceEndIndex:0};e.nodes.splice(e.nodes.indexOf(t)-1,0,s,r)}}))}));const c=a.toString();c!==n.value&&(n.cloneBefore({value:c}),null!=e&&e.preserve||n.remove())}});basePlugin.postcss=!0;const postcssPlugin=t=>{const r=Object.assign({enableProgressiveCustomProperties:!0,preserve:!0},t);return r.enableProgressiveCustomProperties&&r.preserve?{postcssPlugin:"postcss-double-position-gradients",plugins:[e(),basePlugin(r)]}:basePlugin(r)};postcssPlugin.postcss=!0;export{postcssPlugin as default};
|