epiphany/node_modules/@csstools/postcss-media-queries-aspect-ratio-number-values/dist/index.mjs
2023-12-09 22:48:07 -08:00

1 line
5.1 KiB
JavaScript

import{matchesRatioExactly as e,parse as r,isMediaQueryInvalid as t,cloneMediaQuery as a,isMediaFeaturePlain as n,isMediaFeatureRangeNameValue as o,isMediaFeatureRangeValueName as i,isMediaFeatureRangeValueNameValue as u}from"@csstools/media-query-list-parser";import{isTokenNode as s,isFunctionNode as l,TokenNode as c,FunctionNode as m,SimpleBlockNode as v,WhitespaceNode as d}from"@csstools/css-parser-algorithms";import{TokenType as p,NumberType as f}from"@csstools/css-tokenizer";const g=1e5,w=2147483647;function transformMediaFeatureValue(r){if(Array.isArray(r.value)&&e(r.value)){const e=[];for(let t=0;t<r.value.length;t++){const a=r.value[t];s(a)&&a.value[0]===p.Number?e.push(a):l(a)&&"calc"===a.getName().toLowerCase()&&e.push(a)}if(2!==e.length)return;const t=e[0],a=r.value.indexOf(t),n=e[1],o=r.value.indexOf(n);if(s(n)&&n.value[0]===p.Number&&0===n.value[4].value)return r.value.splice(a,1,new c([p.Number,w.toString(),-1,-1,{value:w,type:f.Integer}])),void r.value.splice(o,1,new c([p.Number,"1",-1,-1,{value:1,type:f.Integer}]));if(s(t)&&t.value[0]===p.Number&&t.value[4].type===f.Integer&&s(n)&&n.value[0]===p.Number&&n.value[4].type===f.Integer)return;let i=null,u=null;if(l(t)&&"calc"===t.getName().toLowerCase()){if(t.toString().includes(g.toString()))return;i=modifyCalc(t)}if(l(n)&&"calc"===n.getName().toLowerCase()){if(n.toString().includes(g.toString()))return;u=modifyCalc(n)}if(s(t)&&t.value[0]===p.Number&&s(n)&&n.value[0]===p.Number){const e=t.value,r=n.value,a=Math.round(e[4].value*g),o=Math.round(r[4].value*g),s=greatestCommonDivisor(a,o);i=new c([p.Number,Math.round(a/s).toString(),-1,-1,{value:Math.round(a/s),type:f.Integer}]),u=new c([p.Number,Math.round(o/s).toString(),-1,-1,{value:Math.round(o/s),type:f.Integer}])}else{if(s(t)&&t.value[0]===p.Number){const e=t.value;i=new c([p.Number,Math.round(e[4].value*g).toString(),-1,-1,{value:Math.round(e[4].value*g),type:f.Integer}])}if(s(n)&&n.value[0]===p.Number){const e=n.value;u=new c([p.Number,Math.round(e[4].value*g).toString(),-1,-1,{value:Math.round(e[4].value*g),type:f.Integer}])}}return i&&u?(r.value.splice(a,1,i),void r.value.splice(o,1,u)):void 0}const t=Array.isArray(r.value)?r.value:[r.value];for(let e=0;e<t.length;e++){const a=t[e];if(s(a)){const n=a.value;if(n[0]!==p.Number)return;if(n[4].type===f.Integer)return t.splice(e+1,0,new c([p.Delim,"/",-1,-1,{value:"/"}]),new c([p.Number,"1",-1,-1,{value:1,type:f.Integer}])),void(r.value=t);if(n[4].type===f.Number){const a=Math.round(n[4].value*g),o=greatestCommonDivisor(a,g);return t.splice(e,1,new c([p.Number,Math.round(a/o).toString(),-1,-1,{value:Math.round(a/o),type:f.Integer}]),new c([p.Delim,"/",-1,-1,{value:"/"}]),new c([p.Number,Math.round(g/o).toString(),-1,-1,{value:Math.round(g/o),type:f.Integer}])),void(r.value=t)}return}if(l(a)&&"calc"===a.getName().toLowerCase())return t.splice(e,1,modifyCalc(a),new c([p.Delim,"/",-1,-1,{value:"/"}]),new c([p.Number,g.toString(),-1,-1,{value:g,type:f.Integer}])),void(r.value=t)}}function modifyCalc(e){return new m([p.Function,"calc(",-1,-1,{value:"calc("}],[p.CloseParen,")",-1,-1,void 0],[new v([p.OpenParen,"(",-1,-1,void 0],[p.CloseParen,")",-1,-1,void 0],e.value),new d([[p.Whitespace," ",-1,-1,void 0]]),new c([p.Delim,"*",-1,-1,{value:"*"}]),new d([[p.Whitespace," ",-1,-1,void 0]]),new c([p.Number,g.toString(),-1,-1,{value:g,type:f.Integer}])])}function greatestCommonDivisor(e,r){if(Number.isNaN(e)||Number.isNaN(r))throw new Error("Unexpected 'NaN' when calculating the greatest common divisor.");if(!Number.isFinite(e)||!Number.isFinite(r))throw new Error("Unexpected 'Infinite' value when calculating the greatest common divisor.");for(r>e&&([e,r]=[r,e]);;){if(0==r)return e;if(0==(e%=r))return r;r%=e}}const N=new Set(["aspect-ratio","min-aspect-ratio","max-aspect-ratio","device-aspect-ratio","min-device-aspect-ratio","max-device-aspect-ratio"]);function transformMediaQueryList(e,s){const l=r(e,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw new Error(`Unable to parse media query "${e}"`)}}),c=new Set(l.map((e=>e.toString())));return l.flatMap((e=>{if(t(e))return[e.toString()];const r=a(e);r.walk((e=>{const r=e.node;if(n(r)||o(r)||i(r)){const e=r.name.getName().toLowerCase();if(!N.has(e))return;transformMediaFeatureValue(r.value)}else if(u(r)){const e=r.name.getName().toLowerCase();if(!N.has(e))return;transformMediaFeatureValue(r.valueOne);transformMediaFeatureValue(r.valueTwo)}else;}));const l=e.toString(),m=r.toString();return m===l||c.has(m)?[l]:s?[l,m]:[m]})).join(",")}const creator=e=>{const r=Object.assign({preserve:!1},e);return{postcssPlugin:"postcss-media-queries-aspect-ratio-number-values",AtRule(e,{result:t}){if("media"!==e.name.toLowerCase())return;const a=e.params.toLowerCase();if(!(a.includes("aspect-ratio")||a.includes("min-aspect-ratio")||a.includes("max-aspect-ratio")||a.includes("device-aspect-ratio")||a.includes("min-device-aspect-ratio")||a.includes("max-device-aspect-ratio")))return;let n;try{if(n=transformMediaQueryList(e.params,r.preserve),n===e.params)return}catch(r){return void e.warn(t,`Failed to transform @media params for "${e.params}" with message: "${r.message}"`)}e.cloneBefore({params:n}),e.remove()}}};creator.postcss=!0;export{creator as default};