epiphany/node_modules/@csstools/postcss-media-queries-aspect-ratio-number-values/dist/index.cjs

2 lines
5.8 KiB
JavaScript
Raw Normal View History

2023-12-09 22:48:07 -08:00
"use strict";var e=require("@csstools/media-query-list-parser"),r=require("@csstools/css-parser-algorithms"),n=require("@csstools/css-tokenizer");const o=1e5,t=2147483647;function transformMediaFeatureValue(a){if(Array.isArray(a.value)&&e.matchesRatioExactly(a.value)){const e=[];for(let o=0;o<a.value.length;o++){const t=a.value[o];r.isTokenNode(t)&&t.value[0]===n.TokenType.Number?e.push(t):r.isFunctionNode(t)&&"calc"===t.getName().toLowerCase()&&e.push(t)}if(2!==e.length)return;const i=e[0],u=a.value.indexOf(i),s=e[1],l=a.value.indexOf(s);if(r.isTokenNode(s)&&s.value[0]===n.TokenType.Number&&0===s.value[4].value)return a.value.splice(u,1,new r.TokenNode([n.TokenType.Number,t.toString(),-1,-1,{value:t,type:n.NumberType.Integer}])),void a.value.splice(l,1,new r.TokenNode([n.TokenType.Number,"1",-1,-1,{value:1,type:n.NumberType.Integer}]));if(r.isTokenNode(i)&&i.value[0]===n.TokenType.Number&&i.value[4].type===n.NumberType.Integer&&r.isTokenNode(s)&&s.value[0]===n.TokenType.Number&&s.value[4].type===n.NumberType.Integer)return;let c=null,p=null;if(r.isFunctionNode(i)&&"calc"===i.getName().toLowerCase()){if(i.toString().includes(o.toString()))return;c=modifyCalc(i)}if(r.isFunctionNode(s)&&"calc"===s.getName().toLowerCase()){if(s.toString().includes(o.toString()))return;p=modifyCalc(s)}if(r.isTokenNode(i)&&i.value[0]===n.TokenType.Number&&r.isTokenNode(s)&&s.value[0]===n.TokenType.Number){const e=i.value,t=s.value,a=Math.round(e[4].value*o),u=Math.round(t[4].value*o),l=greatestCommonDivisor(a,u);c=new r.TokenNode([n.TokenType.Number,Math.round(a/l).toString(),-1,-1,{value:Math.round(a/l),type:n.NumberType.Integer}]),p=new r.TokenNode([n.TokenType.Number,Math.round(u/l).toString(),-1,-1,{value:Math.round(u/l),type:n.NumberType.Integer}])}else{if(r.isTokenNode(i)&&i.value[0]===n.TokenType.Number){const e=i.value;c=new r.TokenNode([n.TokenType.Number,Math.round(e[4].value*o).toString(),-1,-1,{value:Math.round(e[4].value*o),type:n.NumberType.Integer}])}if(r.isTokenNode(s)&&s.value[0]===n.TokenType.Number){const e=s.value;p=new r.TokenNode([n.TokenType.Number,Math.round(e[4].value*o).toString(),-1,-1,{value:Math.round(e[4].value*o),type:n.NumberType.Integer}])}}return c&&p?(a.value.splice(u,1,c),void a.value.splice(l,1,p)):void 0}const i=Array.isArray(a.value)?a.value:[a.value];for(let e=0;e<i.length;e++){const t=i[e];if(r.isTokenNode(t)){const u=t.value;if(u[0]!==n.TokenType.Number)return;if(u[4].type===n.NumberType.Integer)return i.splice(e+1,0,new r.TokenNode([n.TokenType.Delim,"/",-1,-1,{value:"/"}]),new r.TokenNode([n.TokenType.Number,"1",-1,-1,{value:1,type:n.NumberType.Integer}])),void(a.value=i);if(u[4].type===n.NumberType.Number){const t=Math.round(u[4].value*o),s=greatestCommonDivisor(t,o);return i.splice(e,1,new r.TokenNode([n.TokenType.Number,Math.round(t/s).toString(),-1,-1,{value:Math.round(t/s),type:n.NumberType.Integer}]),new r.TokenNode([n.TokenType.Delim,"/",-1,-1,{value:"/"}]),new r.TokenNode([n.TokenType.Number,Math.round(o/s).toString(),-1,-1,{value:Math.round(o/s),type:n.NumberType.Integer}])),void(a.value=i)}return}if(r.isFunctionNode(t)&&"calc"===t.getName().toLowerCase())return i.splice(e,1,modifyCalc(t),new r.TokenNode([n.TokenType.Delim,"/",-1,-1,{value:"/"}]),new r.TokenNode([n.TokenType.Number,o.toString(),-1,-1,{value:o,type:n.NumberType.Integer}])),void(a.value=i)}}function modifyCalc(e){return new r.FunctionNode([n.TokenType.Function,"calc(",-1,-1,{value:"calc("}],[n.TokenType.CloseParen,")",-1,-1,void 0],[new r.SimpleBlockNode([n.TokenType.OpenParen,"(",-1,-1,void 0],[n.TokenType.CloseParen,")",-1,-1,void 0],e.value),new r.WhitespaceNode([[n.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([n.TokenType.Delim,"*",-1,-1,{value:"*"}]),new r.WhitespaceNode([[n.TokenType.Whitespace," ",-1,-1,void 0]]),new r.TokenNode([n.TokenType.Number,o.toString(),-1,-1,{value:o,type:n.NumberType.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