2 lines
6.6 KiB
JavaScript
2 lines
6.6 KiB
JavaScript
|
import{isFunctionNode as e,isTokenNode as t,gatherNodeAncestry as r}from"@csstools/css-parser-algorithms";import{TokenType as i,NumberType as n,stringify as a}from"@csstools/css-tokenizer";import{invertComparison as o,MediaFeatureEQ as s,MediaFeatureLT as u,MediaFeatureGT as l,newMediaFeaturePlain as c,matchesRatioExactly as d,matchesRatio as m,isMediaFeatureRange as f,isMediaFeature as v,isMediaFeatureRangeNameValue as p,isMediaFeatureRangeValueName as h,isMediaInParens as g,MediaInParens as w,MediaAnd as N,MediaConditionListWithAnd as y,MediaCondition as b,isMediaConditionListWithAnd as x,isMediaAnd as P,isMediaCondition as W,isMediaQuery as A,parse as L,parseCustomMedia as S}from"@csstools/media-query-list-parser";import{mathFunctionNames as C,calcFromComponentValues as I}from"@csstools/css-calc";const Q=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(Q,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const O={width:"px",height:"px","device-width":"px","device-height":"px","aspect-ratio":"","device-aspect-ratio":"",color:"","color-index":"",monochrome:"",resolution:"dpi"},M={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===u.LT||e===u.LT_OR_EQ?"max-":e===l.GT||e===l.GT_OR_EQ?"min-":""}const T={">":1,"<":-1},_=.001,E=.02;function transformSingleNameValuePair(r,a,f,v){let p=f.before,h=f.after;if(v||(p=f.after,h=f.before),!v){const e=o(a);if(!1===e)return;a=e}if(a===s.EQ||a===u.LT_OR_EQ||a===l.GT_OR_EQ)return Array.isArray(f.value)?c(featureNamePrefix(a)+r,...p,...f.value.flatMap((e=>e.tokens())),...h):c(featureNamePrefix(a)+r,...p,...f.value.tokens(),...h);let g,w,N=!1;if(Array.isArray(f.value)){if(!d(f.value))return;if("aspect-ratio"!==r&&"device-aspect-ratio"!==r)return;const e=m(f.value);if(-1===e)return;N=!0,g=f.value[e[0]],w=[...f.value.slice(e[0]+1).flatMap((e=>e.tokens()))]}else g=f.value,w=[];const y=O[r.toLowerCase()];if(e(g)){const e=toLowerCaseAZ(g.getName());if(C.has(e)||"env"===e){const[[e]]=I([[g]],{precision:5,toCanonicalUnits:!0});if(!e||!t(e)||e.value[0]!==i.Number&&e.value[0]!==i.Percentage&&e.value[0]!==i.Dimension||!Number.isInteger(e.value[4].value)){let e;if(void 0!==y){const t=T[a]*("px"===y?E:_);e=[i.Dimension,`${t.toString()}${y}`,-1,-1,{value:t,unit:y,type:n.Integer}]}else if(!0===M[r]){const t=T[a];e=[i.Number,t.toString(),-1,-1,{value:t,type:n.Integer}]}else if(N){const t=T[a]*_;e=[i.Number,t.toString(),-1,-1,{value:t,type:n.Integer}]}else{const t=T[a];e=[i.Number,t.toString(),-1,-1,{value:t,type:n.Integer}]}return c(featureNamePrefix(a)+r,...p,[i.Function,"calc(",-1,-1,{value:"calc("}],[i.OpenParen,"(",-1,-1,void 0],...g.tokens().slice(1),[i.Whitespace," ",-1,-1,void 0],[i.Delim,"+",-1,-1,{value:"+"}],[i.Whitespace," ",-1,-1,void 0],e,[i.CloseParen,")",-1,-1,void 0],...w,...h)}g=e}}if(!t(g))return;let b,x=g.value,P="";if(void 0!==y&&x[0]===i.Number&&0===x[4].value)b=T[a],P=y;else if(x[0]===i.Number&&0===x[4].value)b=T[a],P="";else if(x[0]===i.Dimension&&0===x[4].value)b=T[a],P=x[4].unit;else if(x[0]===i.Number&&!0===M[r])b=x[4].value+T[a];else if(x[0]===i.Dimension&&"px"===x[4].unit&&x[4].type===n.Integer)b=Number(Math.round(Number(x[4].value+E*T[a]+"e6"))+"e-6");else{if(x[0]!==i.Dimension&&x[0]!==i.Number)return;b=Number(Math.round(Number(x[4].value+_*T[a]+"e6"))+"e-6")}return P&&(x=[i.Dimension,x[1],x[2],x[3],{value:x[4].value,unit:P,type:x[4].type}]),x[4].value=b,x[0]===i.Dimension?x[1]=x[4].value.toString()+x[4].unit:x[1]=x[4].value.toString(),c(featureNamePrefix(a)+r,...p,x,...w,...h)}const D=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(e){return e.map(((e,t)=>{const n=r(e);e.walk((t=>{const r=t.node;if(!f(r))return;const a=t.parent;if(!v(a))return;const o=toLowerCaseAZ(r.name.getName());if(!D.has(o))return;if(p(r)||h(r)){const e=r.operatorKind();if(!1===e)return;
|