-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathindex.mjs
1 lines (1 loc) · 3.26 KB
/
index.mjs
1
import e from"postcss-value-parser";import{hasFallback as t}from"@csstools/utilities";const n=/^(?:cross-fade|image|(?:repeating-)?(?:conic|linear|radial)-gradient|url|var)$/i;function getImage(t){return!(!t||!t.type)&&("string"===t.type?"url("+e.stringify(t)+")":!("function"!==t.type||!n.test(t.value))&&e.stringify(t))}const i=new Map([["dpcm",2.54],["dpi",1],["dppx",96],["x",96]]);function getMedia(e,t,n){if("boolean"==typeof e)return!1;const i=Math.floor(e/96*100)/100;return t.atRule({name:"media",params:`(-webkit-min-device-pixel-ratio: ${i}), (min-resolution: ${e}dpi)`,source:n.source})}function getMediaDPI(t){if(!t)return!1;if("word"!==t.type)return!1;if(!isNumericNode(t))return!1;const n=e.unit(t.value);if(!n)return!1;const r=i.get(n.unit.toLowerCase());return!!r&&Number(n.number)*r}function isNumericNode(t){if(!t||!t.value)return!1;try{return!1!==e.unit(t.value)}catch{return!1}}const handleInvalidation=(e,t,n)=>{if("warn"===e.oninvalid)e.decl.warn(e.result,t,{word:String(n)});else if("throw"===e.oninvalid)throw e.decl.error(t,{word:String(n)})},processImageSet=(t,n,i)=>{const r=n.parent;if(!r)return;const o=new Map,s=n.value;for(let r=0;r<t.length;r++){const{imageSetFunction:l,imageSetOptionNodes:u}=t[r],c=new Map,f=u.length;let p=-1;for(;p<f;){const t=p<0||!!(a=u[p])&&"div"===a.type&&","===a.value,r=getImage(u[p+1]),f=getMediaDPI(u[p+2]),d=getMedia(f,i.postcss,n);if(!t)return void handleInvalidation(i,"expected a comma",e.stringify(u));if(!r)return void handleInvalidation(i,"unexpected image",e.stringify(u));if(!d||!f||c.has(f))return void handleInvalidation(i,"unexpected resolution",e.stringify(u));if(c.set(f,d),o.has(f)){const t=o.get(f);t.value=t.value.replace(e.stringify(l),r.trim()),o.set(f,t)}else o.set(f,{atRule:d,value:s.replace(e.stringify(l),r.trim())});p+=3}}var a;for(const{atRule:e,value:t}of o.values()){const i=r.clone().removeAll(),o=n.clone({value:t});i.append(o),e.append(i)}const l=Array.from(o.keys()).sort(((e,t)=>e-t)),u=l.map((e=>o.get(e)?.atRule)).filter((e=>!!e));if(!u.length)return;const c=o.get(l[0])?.value;if(!c)return;const f=u.slice(1);f.length&&r.after(f),n.cloneBefore({value:c.trim()}),i.preserve||(n.remove(),r.nodes?.length||r.remove())},r=/(?:^|[^\w-])(?:-webkit-)?image-set\(/i,o=/^(?:-webkit-)?image-set$/i,creator=n=>{const i=!("preserve"in Object(n))||Boolean(n?.preserve),s="onInvalid"in Object(n)?n?.onInvalid:"ignore";if("oninvalid"in Object(n))throw new Error('"oninvalid" was changed to "onInvalid" to match other plugins with similar options');return{postcssPlugin:"postcss-image-set-function",Declaration(n,{result:a,postcss:l}){const u=n.value;if(!r.test(u))return;if(t(n))return;let c;try{c=e(u)}catch{n.warn(a,`Failed to parse value '${u}' as an image-set function. Leaving the original value intact.`)}if(void 0===c)return;const f=[];c.walk((t=>{if("function"!==t.type)return;if(!o.test(t.value))return;let i=!1;if(e.walk(t.nodes,(e=>{"function"===e.type&&o.test(e.value)&&(i=!0)})),i)return handleInvalidation({decl:n,oninvalid:s,result:a},"nested image-set functions are not allowed",e.stringify(t)),!1;const r=t.nodes.filter((e=>"comment"!==e.type&&"space"!==e.type));f.push({imageSetFunction:t,imageSetOptionNodes:r})})),processImageSet(f,n,{decl:n,oninvalid:s,preserve:i,result:a,postcss:l})}}};creator.postcss=!0;export{creator as default};