epiphany/node_modules/@csstools/postcss-media-minmax/dist/index.mjs
2023-12-09 22:48:07 -08:00

1 line
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;const t=transformSingleNameValuePair(o,e,r.value,p(r));return void(t&&(a.feature=t.feature))}const s=n.get(a);if(!g(s))return;let l=null,c=null;{const e=r.valueOneOperatorKind();if(!1===e)return;const t=transformSingleNameValuePair(o,e,r.valueOne,!1);if(!t)return;e===u.LT||e===u.LT_OR_EQ?(l=t,l.before=a.before):(c=t,c.after=a.after)}{const e=r.valueTwoOperatorKind();if(!1===e)return;const t=transformSingleNameValuePair(o,e,r.valueTwo,!0);if(!t)return;e===u.LT||e===u.LT_OR_EQ?(c=t,c.before=a.before):(l=t,l.after=a.after)}if(!l||!c)return;const d=new w(l),m=new w(c),x=getMediaConditionListWithAndFromAncestry(s,n);if(x)return x.leading===s?(x.leading=d,void(x.list=[new N([[i.Whitespace," ",-1,-1,void 0],[i.Ident,"and",-1,-1,{value:"and"}],[i.Whitespace," ",-1,-1,void 0]],m),...x.list])):void x.list.splice(x.indexOf(n.get(s)),1,new N([[i.Whitespace," ",-1,-1,void 0],[i.Ident,"and",-1,-1,{value:"and"}],[i.Whitespace," ",-1,-1,void 0]],d),new N([[i.Whitespace," ",-1,-1,void 0],[i.Ident,"and",-1,-1,{value:"and"}],[i.Whitespace," ",-1,-1,void 0]],m));const P=new y(d,[new N([[i.Whitespace," ",-1,-1,void 0],[i.Ident,"and",-1,-1,{value:"and"}],[i.Whitespace," ",-1,-1,void 0]],m)],[[i.Whitespace," ",-1,-1,void 0]]),W=getMediaConditionInShallowMediaQueryFromAncestry(s,e,n);W?W.media=P:s.media=new b(new w(new b(P),[[i.Whitespace," ",-1,-1,void 0],[i.OpenParen,"(",-1,-1,void 0]],[[i.CloseParen,")",-1,-1,void 0]]))}));const o=e.tokens();return a(...o.filter(((e,r)=>(0!==r||0!==t||e[0]!==i.Whitespace)&&(e[0]!==i.Whitespace||!o[r+1]||o[r+1][0]!==i.Whitespace))))})).join(",")}function getMediaConditionListWithAndFromAncestry(e,t){let r=e;if(r){if(r=t.get(r),x(r))return r;if(P(r))return r=t.get(r),x(r)?r:void 0}}function getMediaConditionInShallowMediaQueryFromAncestry(e,t,r){let i=e;if(!i)return;if(i=r.get(i),!W(i))return;const n=i;return i=r.get(i),A(i)&&i===t?n:void 0}const creator=()=>({postcssPlugin:"postcss-media-minmax",AtRule:{media:e=>{if(!(e.params.includes("<")||e.params.includes(">")||e.params.includes("=")))return;const t=transform(L(e.params,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw e.error(`Unable to parse media query "${e.params}"`)}}));e.params!==t&&(e.params=t)},"custom-media":e=>{if(!(e.params.includes("<")||e.params.includes(">")||e.params.includes("=")))return;const t=S(e.params,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw e.error(`Unable to parse media query "${e.params}"`)}});if(!t||!t.mediaQueryList)return;const r=t.mediaQueryList.map((e=>e.toString())).join(","),i=transform(t.mediaQueryList);r!==i&&(e.params=`${a(...t.name)} ${i}`)}}});creator.postcss=!0;export{creator as default};