2 lines
7.2 KiB
JavaScript
2 lines
7.2 KiB
JavaScript
|
"use strict";var e=require("@csstools/css-parser-algorithms"),i=require("@csstools/css-tokenizer"),n=require("@csstools/media-query-list-parser"),a=require("@csstools/css-calc");const t=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(t,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const r={width:"px",height:"px","device-width":"px","device-height":"px","aspect-ratio":"","device-aspect-ratio":"",color:"","color-index":"",monochrome:"",resolution:"dpi"},o={width:!1,height:!1,"device-width":!1,"device-height":!1,"aspect-ratio":!1,"device-aspect-ratio":!1,color:!0,"color-index":!0,monochrome:!0,resolution:"dpi"};function featureNamePrefix(e){return e===n.MediaFeatureLT.LT||e===n.MediaFeatureLT.LT_OR_EQ?"max-":e===n.MediaFeatureGT.GT||e===n.MediaFeatureGT.GT_OR_EQ?"min-":""}const s={">":1,"<":-1},u=.001,d=.02;function transformSingleNameValuePair(t,l,c,p){let T=c.before,m=c.after;if(p||(T=c.after,m=c.before),!p){const e=n.invertComparison(l);if(!1===e)return;l=e}if(l===n.MediaFeatureEQ.EQ||l===n.MediaFeatureLT.LT_OR_EQ||l===n.MediaFeatureGT.GT_OR_EQ)return Array.isArray(c.value)?n.newMediaFeaturePlain(featureNamePrefix(l)+t,...T,...c.value.flatMap((e=>e.tokens())),...m):n.newMediaFeaturePlain(featureNamePrefix(l)+t,...T,...c.value.tokens(),...m);let f,v,y=!1;if(Array.isArray(c.value)){if(!n.matchesRatioExactly(c.value))return;if("aspect-ratio"!==t&&"device-aspect-ratio"!==t)return;const e=n.matchesRatio(c.value);if(-1===e)return;y=!0,f=c.value[e[0]],v=[...c.value.slice(e[0]+1).flatMap((e=>e.tokens()))]}else f=c.value,v=[];const h=r[t.toLowerCase()];if(e.isFunctionNode(f)){const r=toLowerCaseAZ(f.getName());if(a.mathFunctionNames.has(r)||"env"===r){const[[r]]=a.calcFromComponentValues([[f]],{precision:5,toCanonicalUnits:!0});if(!r||!e.isTokenNode(r)||r.value[0]!==i.TokenType.Number&&r.value[0]!==i.TokenType.Percentage&&r.value[0]!==i.TokenType.Dimension||!Number.isInteger(r.value[4].value)){let e;if(void 0!==h){const n=s[l]*("px"===h?d:u);e=[i.TokenType.Dimension,`${n.toString()}${h}`,-1,-1,{value:n,unit:h,type:i.NumberType.Integer}]}else if(!0===o[t]){const n=s[l];e=[i.TokenType.Number,n.toString(),-1,-1,{value:n,type:i.NumberType.Integer}]}else if(y){const n=s[l]*u;e=[i.TokenType.Number,n.toString(),-1,-1,{value:n,type:i.NumberType.Integer}]}else{const n=s[l];e=[i.TokenType.Number,n.toString(),-1,-1,{value:n,type:i.NumberType.Integer}]}return n.newMediaFeaturePlain(featureNamePrefix(l)+t,...T,[i.TokenType.Function,"calc(",-1,-1,{value:"calc("}],[i.TokenType.OpenParen,"(",-1,-1,void 0],...f.tokens().slice(1),[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Delim,"+",-1,-1,{value:"+"}],[i.TokenType.Whitespace," ",-1,-1,void 0],e,[i.TokenType.CloseParen,")",-1,-1,void 0],...v,...m)}f=r}}if(!e.isTokenNode(f))return;let g,k=f.value,M="";if(void 0!==h&&k[0]===i.TokenType.Number&&0===k[4].value)g=s[l],M=h;else if(k[0]===i.TokenType.Number&&0===k[4].value)g=s[l],M="";else if(k[0]===i.TokenType.Dimension&&0===k[4].value)g=s[l],M=k[4].unit;else if(k[0]===i.TokenType.Number&&!0===o[t])g=k[4].value+s[l];else if(k[0]===i.TokenType.Dimension&&"px"===k[4].unit&&k[4].type===i.NumberType.Integer)g=Number(Math.round(Number(k[4].value+d*s[l]+"e6"))+"e-6");else{if(k[0]!==i.TokenType.Dimension&&k[0]!==i.TokenType.Number)return;g=Number(Math.round(Number(k[4].value+u*s[l]+"e6"))+"e-6")}return M&&(k=[i.TokenType.Dimension,k[1],k[2],k[3],{value:k[4].value,unit:M,type:k[4].type}]),k[4].value=g,k[0]===i.TokenType.Dimension?k[1]=k[4].value.toString()+k[4].unit:k[1]=k[4].value.toString(),n.newMediaFeaturePlain(featureNamePrefix(l)+t,...T,k,...v,...m)}const l=new Set(["aspect-ratio","color","color-index","device-aspect-ratio","device-height","device-width","height","horizontal-viewport-segments","monochrome","resolution","vertical-viewport-segments","width"]);function transform(a){return a.map(((a,t)=>{const r=e.gatherNodeAncestry(a);a.walk((e=>{const t=e.node;if(!n.isMediaFeatureRange(t))return;const o=e.parent;if(!n.isMediaFeature(o))return;const s=toLowerCaseAZ(t.name.getName());if(!l.has(s))return;if(n.isMediaFeatureRangeNameValue(t)||n.i
|