You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
4.0 KiB

2 years ago
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=e(require("postcss-value-parser"));function t(e){const n=e[0];let t=e[1],r=e[2];if(t/=100,r/=100,t+r>=1){const e=t/(t+r);return[e,e,e].map((e=>Math.round(255*e)))}const u=function(e){let n=e[0],t=e[1],r=e[2];n%=360,n<0&&(n+=360);function u(e){const u=(e+n/30)%12,a=t*Math.min(r,1-r);return r-a*Math.max(-1,Math.min(u-3,9-u,1))}return t/=100,r/=100,[u(0),u(8),u(4)]}([n,100,50]);for(let e=0;e<3;e++)u[e]*=1-t-r,u[e]+=t;return u.map((e=>Math.round(255*e)))}function r(e){const r=e.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type)),l=function(e){if(!function(e){if(!e||"word"!==e.type)return!1;if(!o(e))return!1;const t=n.default.unit(e.value);if(!t)return!1;return!!t.number&&("deg"===t.unit||"grad"===t.unit||"rad"===t.unit||"turn"===t.unit||""===t.unit)}(e[0]))return null;if(!u(e[1]))return null;if(!u(e[2]))return null;const t={h:n.default.unit(e[0].value),hNode:e[0],w:n.default.unit(e[1].value),wNode:e[1],b:n.default.unit(e[2].value),bNode:e[2]};if(function(e){switch(e.unit){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=(.9*parseFloat(e.number)).toString());case"turn":e.unit="",e.number=(360*parseFloat(e.number)).toString()}}(t.h),""!==t.h.unit)return null;i(t.w),i(t.b),function(e){return e&&"div"===e.type&&"/"===e.value}(e[3])&&(t.slash=e[3]);(u(e[4])||function(e){return e&&"function"===e.type&&"calc"===e.value}(e[4])||function(e){return e&&"function"===e.type&&"var"===e.value}(e[4]))&&(t.alpha=e[4]);return t}(r);if(!l)return;if(r.length>3&&(!l.slash||!l.alpha))return;e.value="rgb",function(e,t,r){if(!t||!r)return;if(e.value="rgba",t.value=",",t.before="",!function(e){if(!e||"word"!==e.type)return!1;if(!o(e))return!1;const t=n.default.unit(e.value);if(!t)return!1;return!!t.number}(r))return;const u=n.default.unit(r.value);if(!u)return;"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),r.value=String(u.number))}(e,l.slash,l.alpha);const[s,c,f]=[(d=l).hNode,d.wNode,d.bNode];var d;const[p,v,b]=function(e){return[e.h,e.w,e.b]}(l),m=t([p.number,v.number,b.number].map((e=>parseFloat(e))));e.nodes.splice(e.nodes.indexOf(s)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.splice(e.nodes.indexOf(c)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),a(e.nodes,s,{...s,value:String(m[0])}),a(e.nodes,c,{...c,value:String(m[1])}),a(e.nodes,f,{...f,value:String(m[2])})}function u(e){if(!e||"word"!==e.type)return!1;if(!o(e))return!1;const t=n.default.unit(e.value);return!!t&&("%"===t.unit||""===t.unit)}function a(e,n,t){const r=e.indexOf(n);e[r]=t}function i(e){if("%"!==e.unit)return e.unit="%",void(e.number=(100*parseFloat(e.number)).toString())}function o(e){if(!e||!e.value)return!1;try{return!1!==n.default.unit(e.value)}catch(e){return!1}}const l=e=>{const t="preserve"in Object(e)&&Boolean(e.preserve);return{postcssPlugin:"postcss-hwb-function",Declaration:(e,{result:u,postcss:a})=>{if(t&&function(e){let n=e.parent;for(;n;)if("atrule"===n.type){if("supports"===n.name&&-1!==n.params.indexOf("(color: hwb(0% 0 0))"))return!0;n=n.parent}else n=n.parent;return!1}(e))return;const i=e.value;if(!i.includes("hwb"))return;const o=function(e,t,u){let a;try{a=n.default(e)}catch(n){t.warn(u,`Failed to parse value '${e}' as a hwb function. Leaving the original value intact.`)}if(void 0===a)return;a.walk((e=>{e.type&&"function"===e.type&&"hwb"===e.value&&r(e)}));const i=String(a);if(i===e)return;return i}(i,e,u);if(void 0!==o)if(e.variable&&t){const n=e.parent,t=a.atRule({name:"supports",params:"(color: hwb(0% 0 0))",source:e.source}),r=n.clone();r.removeAll(),r.append(e.clone()),t.append(r),function(e,n,t){let r=n,u=n.next();for(;r&&u&&"atrule"===u.type&&"supports"===u.name&&u.params===t;)r=u,u=u.next();r.after(e)}(t,n,"(color: hwb(0% 0 0))"),e.value=o}else t?e.cloneBefore({value:o}):e.value=o}}};l.postcss=!0,module.exports=l;