diff --git a/packages/media-query-list-parser/CHANGELOG.md b/packages/media-query-list-parser/CHANGELOG.md index e0a45f52a..a23b3fea6 100644 --- a/packages/media-query-list-parser/CHANGELOG.md +++ b/packages/media-query-list-parser/CHANGELOG.md @@ -2,7 +2,8 @@ ### Unreleased (patch) -- Improve the detection of math function in media queries. +- Add support for `env()` functions as values in media queries. +- Improve the detection of math function as values in media queries. ### 2.0.2 (March 25, 2023) diff --git a/packages/media-query-list-parser/dist/index.cjs b/packages/media-query-list-parser/dist/index.cjs index 4c37e9c72..f21b3340a 100644 --- a/packages/media-query-list-parser/dist/index.cjs +++ b/packages/media-query-list-parser/dist/index.cjs @@ -1 +1 @@ -"use strict";var e,t=require("@csstools/css-parser-algorithms"),i=require("@csstools/css-tokenizer");exports.NodeType=void 0,(e=exports.NodeType||(exports.NodeType={})).CustomMedia="custom-media",e.GeneralEnclosed="general-enclosed",e.MediaAnd="media-and",e.MediaCondition="media-condition",e.MediaConditionListWithAnd="media-condition-list-and",e.MediaConditionListWithOr="media-condition-list-or",e.MediaFeature="media-feature",e.MediaFeatureBoolean="mf-boolean",e.MediaFeatureName="mf-name",e.MediaFeaturePlain="mf-plain",e.MediaFeatureRangeNameValue="mf-range-name-value",e.MediaFeatureRangeValueName="mf-range-value-name",e.MediaFeatureRangeValueNameValue="mf-range-value-name-value",e.MediaFeatureValue="mf-value",e.MediaInParens="media-in-parens",e.MediaNot="media-not",e.MediaOr="media-or",e.MediaQueryWithType="media-query-with-type",e.MediaQueryWithoutType="media-query-without-type",e.MediaQueryInvalid="media-query-invalid";const a=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(a,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}class MediaCondition{type=exports.NodeType.MediaCondition;media;constructor(e){this.media=e}tokens(){return this.media.tokens()}toString(){return this.media.toString()}indexOf(e){return e===this.media?"media":-1}at(e){if("media"===e)return this.media}walk(e){return!1!==e({node:this.media,parent:this},"media")&&this.media.walk(e)}toJSON(){return{type:this.type,media:this.media.toJSON()}}isMediaCondition(){return MediaCondition.isMediaCondition(this)}static isMediaCondition(e){return!!e&&(e instanceof MediaCondition&&e.type===exports.NodeType.MediaCondition)}}class MediaInParens{type=exports.NodeType.MediaInParens;media;before;after;constructor(e,t=[],i=[]){this.media=e,this.before=t,this.after=i}tokens(){return[...this.before,...this.media.tokens(),...this.after]}toString(){return i.stringify(...this.before)+this.media.toString()+i.stringify(...this.after)}indexOf(e){return e===this.media?"media":-1}at(e){if("media"===e)return this.media}walk(e){return!1!==e({node:this.media,parent:this},"media")&&("walk"in this.media?this.media.walk(e):void 0)}toJSON(){return{type:this.type,media:this.media.toJSON(),before:this.before,after:this.after}}isMediaInParens(){return MediaInParens.isMediaInParens(this)}static isMediaInParens(e){return!!e&&(e instanceof MediaInParens&&e.type===exports.NodeType.MediaInParens)}}class MediaQueryWithType{type=exports.NodeType.MediaQueryWithType;modifier;mediaType;and=void 0;media=void 0;constructor(e,t,i,a){this.modifier=e,this.mediaType=t,i&&a&&(this.and=i,this.media=a)}getModifier(){if(!this.modifier.length)return"";for(let e=0;ee.tokens()))}toString(){return this.media.map((e=>e.toString())).join("")}walk(e){let t=!1;if(this.media.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),t)return!1}toJSON(){return{type:this.type,string:this.toString(),media:this.media}}isMediaQueryInvalid(){return MediaQueryInvalid.isMediaQueryInvalid(this)}static isMediaQueryInvalid(e){return!!e&&(e instanceof MediaQueryInvalid&&e.type===exports.NodeType.MediaQueryInvalid)}}class GeneralEnclosed{type=exports.NodeType.GeneralEnclosed;value;constructor(e){this.value=e}tokens(){return this.value.tokens()}toString(){return this.value.toString()}indexOf(e){return e===this.value?"value":-1}at(e){if("value"===e)return this.value}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,tokens:this.tokens()}}isGeneralEnclosed(){return GeneralEnclosed.isGeneralEnclosed(this)}static isGeneralEnclosed(e){return!!e&&(e instanceof GeneralEnclosed&&e.type===exports.NodeType.GeneralEnclosed)}}class MediaAnd{type=exports.NodeType.MediaAnd;modifier;media;constructor(e,t){this.modifier=e,this.media=t}tokens(){return[...this.modifier,...this.media.tokens()]}toString(){return i.stringify(...this.modifier)+this.media.toString()}indexOf(e){return e===this.media?"media":-1}at(e){return"media"===e?this.media:null}walk(e){return!1!==e({node:this.media,parent:this},"media")&&this.media.walk(e)}toJSON(){return{type:this.type,modifier:this.modifier,media:this.media.toJSON()}}isMediaAnd(){return MediaAnd.isMediaAnd(this)}static isMediaAnd(e){return!!e&&(e instanceof MediaAnd&&e.type===exports.NodeType.MediaAnd)}}class MediaConditionListWithAnd{type=exports.NodeType.MediaConditionListWithAnd;leading;list;before;after;constructor(e,t,i=[],a=[]){this.leading=e,this.list=t,this.before=i,this.after=a}tokens(){return[...this.before,...this.leading.tokens(),...this.list.flatMap((e=>e.tokens())),...this.after]}toString(){return i.stringify(...this.before)+this.leading.toString()+this.list.map((e=>e.toString())).join("")+i.stringify(...this.after)}indexOf(e){return e===this.leading?"leading":"media-and"===e.type?this.list.indexOf(e):-1}at(e){return"leading"===e?this.leading:"number"==typeof e?(e<0&&(e=this.list.length+e),this.list[e]):void 0}walk(e){if(!1===e({node:this.leading,parent:this},"leading"))return!1;if("walk"in this.leading&&!1===this.leading.walk(e))return!1;let t=!1;return this.list.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),!t&&void 0}toJSON(){return{type:this.type,leading:this.leading.toJSON(),list:this.list.map((e=>e.toJSON())),before:this.before,after:this.after}}isMediaConditionListWithAnd(){return MediaConditionListWithAnd.isMediaConditionListWithAnd(this)}static isMediaConditionListWithAnd(e){return!!e&&(e instanceof MediaConditionListWithAnd&&e.type===exports.NodeType.MediaConditionListWithAnd)}}class MediaConditionListWithOr{type=exports.NodeType.MediaConditionListWithOr;leading;list;before;after;constructor(e,t,i=[],a=[]){this.leading=e,this.list=t,this.before=i,this.after=a}tokens(){return[...this.before,...this.leading.tokens(),...this.list.flatMap((e=>e.tokens())),...this.after]}toString(){return i.stringify(...this.before)+this.leading.toString()+this.list.map((e=>e.toString())).join("")+i.stringify(...this.after)}indexOf(e){return e===this.leading?"leading":"media-or"===e.type?this.list.indexOf(e):-1}at(e){return"leading"===e?this.leading:"number"==typeof e?(e<0&&(e=this.list.length+e),this.list[e]):void 0}walk(e){if(!1===e({node:this.leading,parent:this},"leading"))return!1;if("walk"in this.leading&&!1===this.leading.walk(e))return!1;let t=!1;return this.list.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),!t&&void 0}toJSON(){return{type:this.type,leading:this.leading.toJSON(),list:this.list.map((e=>e.toJSON())),before:this.before,after:this.after}}isMediaConditionListWithOr(){return MediaConditionListWithOr.isMediaConditionListWithOr(this)}static isMediaConditionListWithOr(e){return!!e&&(e instanceof MediaConditionListWithOr&&e.type===exports.NodeType.MediaConditionListWithOr)}}function isNumber(e){return!!(e.type===t.ComponentValueType.Token&&e.value[0]===i.TokenType.Number||e.type===t.ComponentValueType.Function&&r.has(toLowerCaseAZ(e.name[4].value)))}const r=new Set(["abs","acos","asin","atan","atan2","calc","clamp","cos","exp","hypot","log","max","min","mod","pow","rem","round","sign","sin","sqrt","tan"]);function isDimension(e){return e.type===t.ComponentValueType.Token&&e.value[0]===i.TokenType.Dimension}function isIdent(e){return e.type===t.ComponentValueType.Token&&e.value[0]===i.TokenType.Ident}class MediaFeatureName{type=exports.NodeType.MediaFeatureName;name;before;after;constructor(e,t=[],i=[]){this.name=e,this.before=t,this.after=i}getName(){return this.name.value[4].value}getNameToken(){return this.name.value}tokens(){return[...this.before,...this.name.tokens(),...this.after]}toString(){return i.stringify(...this.before)+this.name.toString()+i.stringify(...this.after)}indexOf(e){return e===this.name?"name":-1}at(e){if("name"===e)return this.name}toJSON(){return{type:this.type,name:this.getName(),tokens:this.tokens()}}isMediaFeatureName(){return MediaFeatureName.isMediaFeatureName(this)}static isMediaFeatureName(e){return!!e&&(e instanceof MediaFeatureName&&e.type===exports.NodeType.MediaFeatureName)}}function parseMediaFeatureName(e){let i=-1;for(let a=0;ae.tokens())),e.slice(i+1).flatMap((e=>e.tokens())))}class MediaFeatureBoolean{type=exports.NodeType.MediaFeatureBoolean;name;constructor(e){this.name=e}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return this.name.tokens()}toString(){return this.name.toString()}indexOf(e){return e===this.name?"name":-1}at(e){if("name"===e)return this.name}toJSON(){return{type:this.type,name:this.name.toJSON(),tokens:this.tokens()}}isMediaFeatureBoolean(){return MediaFeatureBoolean.isMediaFeatureBoolean(this)}static isMediaFeatureBoolean(e){return!!e&&(e instanceof MediaFeatureBoolean&&e.type===exports.NodeType.MediaFeatureBoolean)}}function parseMediaFeatureBoolean(e){const t=parseMediaFeatureName(e);return!1===t?t:new MediaFeatureBoolean(t)}class MediaFeatureValue{type=exports.NodeType.MediaFeatureValue;value;before;after;constructor(e,t=[],i=[]){Array.isArray(e)&&1===e.length?this.value=e[0]:this.value=e,this.before=t,this.after=i}tokens(){return Array.isArray(this.value)?[...this.before,...this.value.flatMap((e=>e.tokens())),...this.after]:[...this.before,...this.value.tokens(),...this.after]}toString(){return Array.isArray(this.value)?i.stringify(...this.before)+this.value.map((e=>e.toString())).join("")+i.stringify(...this.after):i.stringify(...this.before)+this.value.toString()+i.stringify(...this.after)}indexOf(e){return e===this.value?"value":-1}at(e){if("value"===e)return this.value}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return Array.isArray(this.value)?{type:this.type,value:this.value.map((e=>e.toJSON())),tokens:this.tokens()}:{type:this.type,value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureValue(){return MediaFeatureValue.isMediaFeatureValue(this)}static isMediaFeatureValue(e){return!!e&&(e instanceof MediaFeatureValue&&e.type===exports.NodeType.MediaFeatureValue)}}function parseMediaFeatureValue(e){let i=-1,a=-1;for(let r=0;re.tokens())),e.slice(a+1).flatMap((e=>e.tokens())))}function matchesRatioExactly(e){let t=-1,i=-1;const a=matchesRatio(e);if(-1===a)return-1;t=a[0],i=a[1];for(let t=i+1;t2)return!1;if(e[0][0]!==i.TokenType.Delim)return!1;if(1===e.length)switch(e[0][4].value){case exports.MediaFeatureEQ.EQ:return exports.MediaFeatureEQ.EQ;case exports.MediaFeatureLT.LT:return exports.MediaFeatureLT.LT;case exports.MediaFeatureGT.GT:return exports.MediaFeatureGT.GT;default:return!1}if(e[1][0]!==i.TokenType.Delim)return!1;if(e[1][4].value!==exports.MediaFeatureEQ.EQ)return!1;switch(e[0][4].value){case exports.MediaFeatureLT.LT:return exports.MediaFeatureLT.LT_OR_EQ;case exports.MediaFeatureGT.GT:return exports.MediaFeatureGT.GT_OR_EQ;default:return!1}}exports.MediaFeatureLT=void 0,(n=exports.MediaFeatureLT||(exports.MediaFeatureLT={})).LT="<",n.LT_OR_EQ="<=",exports.MediaFeatureGT=void 0,(o=exports.MediaFeatureGT||(exports.MediaFeatureGT={})).GT=">",o.GT_OR_EQ=">=",exports.MediaFeatureEQ=void 0,(exports.MediaFeatureEQ||(exports.MediaFeatureEQ={})).EQ="=";class MediaFeatureRangeNameValue{type=exports.NodeType.MediaFeatureRangeNameValue;name;operator;value;constructor(e,t,i){this.name=e,this.operator=t,this.value=i}operatorKind(){return comparisonFromTokens(this.operator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.name.tokens(),...this.operator,...this.value.tokens()]}toString(){return this.name.toString()+i.stringify(...this.operator)+this.value.toString()}indexOf(e){return e===this.name?"name":e===this.value?"value":-1}at(e){return"name"===e?this.name:"value"===e?this.value:void 0}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,name:this.name.toJSON(),value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeNameValue(){return MediaFeatureRangeNameValue.isMediaFeatureRangeNameValue(this)}static isMediaFeatureRangeNameValue(e){return!!e&&(e instanceof MediaFeatureRangeNameValue&&e.type===exports.NodeType.MediaFeatureRangeNameValue)}}class MediaFeatureRangeValueName{type=exports.NodeType.MediaFeatureRangeValueName;name;operator;value;constructor(e,t,i){this.name=e,this.operator=t,this.value=i}operatorKind(){return comparisonFromTokens(this.operator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.value.tokens(),...this.operator,...this.name.tokens()]}toString(){return this.value.toString()+i.stringify(...this.operator)+this.name.toString()}indexOf(e){return e===this.name?"name":e===this.value?"value":-1}at(e){return"name"===e?this.name:"value"===e?this.value:void 0}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,name:this.name.toJSON(),value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeValueName(){return MediaFeatureRangeValueName.isMediaFeatureRangeValueName(this)}static isMediaFeatureRangeValueName(e){return!!e&&(e instanceof MediaFeatureRangeValueName&&e.type===exports.NodeType.MediaFeatureRangeValueName)}}class MediaFeatureRangeValueNameValue{type=exports.NodeType.MediaFeatureRangeValueNameValue;name;valueOne;valueOneOperator;valueTwo;valueTwoOperator;constructor(e,t,i,a,r){this.name=e,this.valueOne=t,this.valueOneOperator=i,this.valueTwo=a,this.valueTwoOperator=r}valueOneOperatorKind(){return comparisonFromTokens(this.valueOneOperator)}valueTwoOperatorKind(){return comparisonFromTokens(this.valueTwoOperator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.valueOne.tokens(),...this.valueOneOperator,...this.name.tokens(),...this.valueTwoOperator,...this.valueTwo.tokens()]}toString(){return this.valueOne.toString()+i.stringify(...this.valueOneOperator)+this.name.toString()+i.stringify(...this.valueTwoOperator)+this.valueTwo.toString()}indexOf(e){return e===this.name?"name":e===this.valueOne?"valueOne":e===this.valueTwo?"valueTwo":-1}at(e){return"name"===e?this.name:"valueOne"===e?this.valueOne:"valueTwo"===e?this.valueTwo:void 0}walk(e){return!1!==e({node:this.valueOne,parent:this},"valueOne")&&((!("walk"in this.valueOne)||!1!==this.valueOne.walk(e))&&(!1!==e({node:this.valueTwo,parent:this},"valueTwo")&&((!("walk"in this.valueTwo)||!1!==this.valueTwo.walk(e))&&void 0)))}toJSON(){return{type:this.type,name:this.name.toJSON(),valueOne:this.valueOne.toJSON(),valueTwo:this.valueTwo.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeValueNameValue(){return MediaFeatureRangeValueNameValue.isMediaFeatureRangeValueNameValue(this)}static isMediaFeatureRangeValueNameValue(e){return!!e&&(e instanceof MediaFeatureRangeValueNameValue&&e.type===exports.NodeType.MediaFeatureRangeValueNameValue)}}function parseMediaFeatureRange(e){let a=!1,r=!1;for(let n=0;ne.tokens())),-1!==r&&(s=e.slice(a+1,r+1).flatMap((e=>e.tokens())))):-1!==r&&(s=e.slice(0,r+1).flatMap((e=>e.tokens())));const u=parseMediaConditionWithoutOr(e.slice(Math.max(a,r,n)+1));return!1===u?new MediaQueryWithType(o,[...s,...e.slice(r+1).flatMap((e=>e.tokens()))]):new MediaQueryWithType(o,s,e.slice(r+1,n+1).flatMap((e=>e.tokens())),u)}}function parseMediaConditionListWithOr(e){let i=!1;const a=[];let r=-1,n=-1;for(let o=0;oe.tokens())),e.slice(n+1).flatMap((e=>e.tokens())))}function parseMediaConditionListWithAnd(e){let i=!1;const a=[];let r=-1,n=-1;for(let o=0;oe.tokens())),e.slice(n+1).flatMap((e=>e.tokens())))}function parseMediaCondition(e){const t=parseMediaNot(e);if(!1!==t)return new MediaCondition(t);const i=parseMediaConditionListWithAnd(e);if(!1!==i)return new MediaCondition(i);const a=parseMediaConditionListWithOr(e);if(!1!==a)return new MediaCondition(a);const r=parseMediaInParens(e);return!1!==r&&new MediaCondition(r)}function parseMediaConditionWithoutOr(e){const t=parseMediaNot(e);if(!1!==t)return new MediaCondition(t);const i=parseMediaConditionListWithAnd(e);if(!1!==i)return new MediaCondition(i);const a=parseMediaInParens(e);return!1!==a&&new MediaCondition(a)}function parseMediaInParens(e){let a=-1;for(let i=0;ie.tokens())),r.startToken],o=[r.endToken,...e.slice(a+1).flatMap((e=>e.tokens()))],s=parseMediaFeature(r,n,o);if(!1!==s)return new MediaInParens(s);const u=parseMediaCondition(r.value);return!1!==u?new MediaInParens(u,n,o):new MediaInParens(new GeneralEnclosed(r),e.slice(0,a).flatMap((e=>e.tokens())),e.slice(a+1).flatMap((e=>e.tokens())))}function parseMediaInParensFromSimpleBlock(e){if(e.startToken[0]!==i.TokenType.OpenParen)return!1;const t=parseMediaFeature(e,[e.startToken],[e.endToken]);if(!1!==t)return new MediaInParens(t);const a=parseMediaCondition(e.value);return!1!==a?new MediaInParens(a,[e.startToken],[e.endToken]):new MediaInParens(new GeneralEnclosed(e))}function parseMediaNot(e){let i=!1,a=null;for(let r=0;re.tokens())),t)}}}return a||!1}function parseMediaOr(e){let i=!1;for(let a=0;ae.tokens())),t)}}return!1}}return!1}function parseMediaAnd(e){let i=!1;for(let a=0;ae.tokens())),t)}}return!1}}return!1}function parseFromTokens(e,i){const a=t.parseCommaSeparatedListOfComponentValues(e,{onParseError:null==i?void 0:i.onParseError});return a.map(((e,t)=>{const r=parseMediaQuery(e);return 0==r&&!0===(null==i?void 0:i.preserveInvalidMediaQueries)?new MediaQueryInvalid(a[t]):r})).filter((e=>!!e))}exports.MediaQueryModifier=void 0,(u=exports.MediaQueryModifier||(exports.MediaQueryModifier={})).Not="not",u.Only="only";class CustomMedia{type=exports.NodeType.CustomMedia;name;mediaQueryList=null;trueOrFalseKeyword=null;constructor(e,t,i){this.name=e,this.mediaQueryList=t,this.trueOrFalseKeyword=i??null}getName(){for(let e=0;ee.toJSON()))}}isCustomMedia(){return CustomMedia.isCustomMedia(this)}static isCustomMedia(e){return!!e&&(e instanceof CustomMedia&&e.type===exports.NodeType.CustomMedia)}}function parseCustomMediaFromTokens(e,t){let a=[],r=e;for(let t=0;tString.fromCharCode(e.charCodeAt(0)+32)))}class MediaCondition{type=exports.NodeType.MediaCondition;media;constructor(e){this.media=e}tokens(){return this.media.tokens()}toString(){return this.media.toString()}indexOf(e){return e===this.media?"media":-1}at(e){if("media"===e)return this.media}walk(e){return!1!==e({node:this.media,parent:this},"media")&&this.media.walk(e)}toJSON(){return{type:this.type,media:this.media.toJSON()}}isMediaCondition(){return MediaCondition.isMediaCondition(this)}static isMediaCondition(e){return!!e&&(e instanceof MediaCondition&&e.type===exports.NodeType.MediaCondition)}}class MediaInParens{type=exports.NodeType.MediaInParens;media;before;after;constructor(e,t=[],i=[]){this.media=e,this.before=t,this.after=i}tokens(){return[...this.before,...this.media.tokens(),...this.after]}toString(){return i.stringify(...this.before)+this.media.toString()+i.stringify(...this.after)}indexOf(e){return e===this.media?"media":-1}at(e){if("media"===e)return this.media}walk(e){return!1!==e({node:this.media,parent:this},"media")&&("walk"in this.media?this.media.walk(e):void 0)}toJSON(){return{type:this.type,media:this.media.toJSON(),before:this.before,after:this.after}}isMediaInParens(){return MediaInParens.isMediaInParens(this)}static isMediaInParens(e){return!!e&&(e instanceof MediaInParens&&e.type===exports.NodeType.MediaInParens)}}class MediaQueryWithType{type=exports.NodeType.MediaQueryWithType;modifier;mediaType;and=void 0;media=void 0;constructor(e,t,i,a){this.modifier=e,this.mediaType=t,i&&a&&(this.and=i,this.media=a)}getModifier(){if(!this.modifier.length)return"";for(let e=0;ee.tokens()))}toString(){return this.media.map((e=>e.toString())).join("")}walk(e){let t=!1;if(this.media.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),t)return!1}toJSON(){return{type:this.type,string:this.toString(),media:this.media}}isMediaQueryInvalid(){return MediaQueryInvalid.isMediaQueryInvalid(this)}static isMediaQueryInvalid(e){return!!e&&(e instanceof MediaQueryInvalid&&e.type===exports.NodeType.MediaQueryInvalid)}}class GeneralEnclosed{type=exports.NodeType.GeneralEnclosed;value;constructor(e){this.value=e}tokens(){return this.value.tokens()}toString(){return this.value.toString()}indexOf(e){return e===this.value?"value":-1}at(e){if("value"===e)return this.value}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,tokens:this.tokens()}}isGeneralEnclosed(){return GeneralEnclosed.isGeneralEnclosed(this)}static isGeneralEnclosed(e){return!!e&&(e instanceof GeneralEnclosed&&e.type===exports.NodeType.GeneralEnclosed)}}class MediaAnd{type=exports.NodeType.MediaAnd;modifier;media;constructor(e,t){this.modifier=e,this.media=t}tokens(){return[...this.modifier,...this.media.tokens()]}toString(){return i.stringify(...this.modifier)+this.media.toString()}indexOf(e){return e===this.media?"media":-1}at(e){return"media"===e?this.media:null}walk(e){return!1!==e({node:this.media,parent:this},"media")&&this.media.walk(e)}toJSON(){return{type:this.type,modifier:this.modifier,media:this.media.toJSON()}}isMediaAnd(){return MediaAnd.isMediaAnd(this)}static isMediaAnd(e){return!!e&&(e instanceof MediaAnd&&e.type===exports.NodeType.MediaAnd)}}class MediaConditionListWithAnd{type=exports.NodeType.MediaConditionListWithAnd;leading;list;before;after;constructor(e,t,i=[],a=[]){this.leading=e,this.list=t,this.before=i,this.after=a}tokens(){return[...this.before,...this.leading.tokens(),...this.list.flatMap((e=>e.tokens())),...this.after]}toString(){return i.stringify(...this.before)+this.leading.toString()+this.list.map((e=>e.toString())).join("")+i.stringify(...this.after)}indexOf(e){return e===this.leading?"leading":"media-and"===e.type?this.list.indexOf(e):-1}at(e){return"leading"===e?this.leading:"number"==typeof e?(e<0&&(e=this.list.length+e),this.list[e]):void 0}walk(e){if(!1===e({node:this.leading,parent:this},"leading"))return!1;if("walk"in this.leading&&!1===this.leading.walk(e))return!1;let t=!1;return this.list.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),!t&&void 0}toJSON(){return{type:this.type,leading:this.leading.toJSON(),list:this.list.map((e=>e.toJSON())),before:this.before,after:this.after}}isMediaConditionListWithAnd(){return MediaConditionListWithAnd.isMediaConditionListWithAnd(this)}static isMediaConditionListWithAnd(e){return!!e&&(e instanceof MediaConditionListWithAnd&&e.type===exports.NodeType.MediaConditionListWithAnd)}}class MediaConditionListWithOr{type=exports.NodeType.MediaConditionListWithOr;leading;list;before;after;constructor(e,t,i=[],a=[]){this.leading=e,this.list=t,this.before=i,this.after=a}tokens(){return[...this.before,...this.leading.tokens(),...this.list.flatMap((e=>e.tokens())),...this.after]}toString(){return i.stringify(...this.before)+this.leading.toString()+this.list.map((e=>e.toString())).join("")+i.stringify(...this.after)}indexOf(e){return e===this.leading?"leading":"media-or"===e.type?this.list.indexOf(e):-1}at(e){return"leading"===e?this.leading:"number"==typeof e?(e<0&&(e=this.list.length+e),this.list[e]):void 0}walk(e){if(!1===e({node:this.leading,parent:this},"leading"))return!1;if("walk"in this.leading&&!1===this.leading.walk(e))return!1;let t=!1;return this.list.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),!t&&void 0}toJSON(){return{type:this.type,leading:this.leading.toJSON(),list:this.list.map((e=>e.toJSON())),before:this.before,after:this.after}}isMediaConditionListWithOr(){return MediaConditionListWithOr.isMediaConditionListWithOr(this)}static isMediaConditionListWithOr(e){return!!e&&(e instanceof MediaConditionListWithOr&&e.type===exports.NodeType.MediaConditionListWithOr)}}function isNumber(e){return!!(e.type===t.ComponentValueType.Token&&e.value[0]===i.TokenType.Number||e.type===t.ComponentValueType.Function&&r.has(toLowerCaseAZ(e.name[4].value)))}const r=new Set(["abs","acos","asin","atan","atan2","calc","clamp","cos","exp","hypot","log","max","min","mod","pow","rem","round","sign","sin","sqrt","tan"]);function isDimension(e){return e.type===t.ComponentValueType.Token&&e.value[0]===i.TokenType.Dimension}function isIdent(e){return e.type===t.ComponentValueType.Token&&e.value[0]===i.TokenType.Ident}function isEnvironmentVariable(e){return e.type===t.ComponentValueType.Function&&"env"===toLowerCaseAZ(e.name[4].value)}class MediaFeatureName{type=exports.NodeType.MediaFeatureName;name;before;after;constructor(e,t=[],i=[]){this.name=e,this.before=t,this.after=i}getName(){return this.name.value[4].value}getNameToken(){return this.name.value}tokens(){return[...this.before,...this.name.tokens(),...this.after]}toString(){return i.stringify(...this.before)+this.name.toString()+i.stringify(...this.after)}indexOf(e){return e===this.name?"name":-1}at(e){if("name"===e)return this.name}toJSON(){return{type:this.type,name:this.getName(),tokens:this.tokens()}}isMediaFeatureName(){return MediaFeatureName.isMediaFeatureName(this)}static isMediaFeatureName(e){return!!e&&(e instanceof MediaFeatureName&&e.type===exports.NodeType.MediaFeatureName)}}function parseMediaFeatureName(e){let i=-1;for(let a=0;ae.tokens())),e.slice(i+1).flatMap((e=>e.tokens())))}class MediaFeatureBoolean{type=exports.NodeType.MediaFeatureBoolean;name;constructor(e){this.name=e}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return this.name.tokens()}toString(){return this.name.toString()}indexOf(e){return e===this.name?"name":-1}at(e){if("name"===e)return this.name}toJSON(){return{type:this.type,name:this.name.toJSON(),tokens:this.tokens()}}isMediaFeatureBoolean(){return MediaFeatureBoolean.isMediaFeatureBoolean(this)}static isMediaFeatureBoolean(e){return!!e&&(e instanceof MediaFeatureBoolean&&e.type===exports.NodeType.MediaFeatureBoolean)}}function parseMediaFeatureBoolean(e){const t=parseMediaFeatureName(e);return!1===t?t:new MediaFeatureBoolean(t)}class MediaFeatureValue{type=exports.NodeType.MediaFeatureValue;value;before;after;constructor(e,t=[],i=[]){Array.isArray(e)&&1===e.length?this.value=e[0]:this.value=e,this.before=t,this.after=i}tokens(){return Array.isArray(this.value)?[...this.before,...this.value.flatMap((e=>e.tokens())),...this.after]:[...this.before,...this.value.tokens(),...this.after]}toString(){return Array.isArray(this.value)?i.stringify(...this.before)+this.value.map((e=>e.toString())).join("")+i.stringify(...this.after):i.stringify(...this.before)+this.value.toString()+i.stringify(...this.after)}indexOf(e){return e===this.value?"value":-1}at(e){if("value"===e)return this.value}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return Array.isArray(this.value)?{type:this.type,value:this.value.map((e=>e.toJSON())),tokens:this.tokens()}:{type:this.type,value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureValue(){return MediaFeatureValue.isMediaFeatureValue(this)}static isMediaFeatureValue(e){return!!e&&(e instanceof MediaFeatureValue&&e.type===exports.NodeType.MediaFeatureValue)}}function parseMediaFeatureValue(e){let i=-1,a=-1;for(let r=0;re.tokens())),e.slice(a+1).flatMap((e=>e.tokens())))}function matchesRatioExactly(e){let t=-1,i=-1;const a=matchesRatio(e);if(-1===a)return-1;t=a[0],i=a[1];for(let t=i+1;t2)return!1;if(e[0][0]!==i.TokenType.Delim)return!1;if(1===e.length)switch(e[0][4].value){case exports.MediaFeatureEQ.EQ:return exports.MediaFeatureEQ.EQ;case exports.MediaFeatureLT.LT:return exports.MediaFeatureLT.LT;case exports.MediaFeatureGT.GT:return exports.MediaFeatureGT.GT;default:return!1}if(e[1][0]!==i.TokenType.Delim)return!1;if(e[1][4].value!==exports.MediaFeatureEQ.EQ)return!1;switch(e[0][4].value){case exports.MediaFeatureLT.LT:return exports.MediaFeatureLT.LT_OR_EQ;case exports.MediaFeatureGT.GT:return exports.MediaFeatureGT.GT_OR_EQ;default:return!1}}exports.MediaFeatureLT=void 0,(n=exports.MediaFeatureLT||(exports.MediaFeatureLT={})).LT="<",n.LT_OR_EQ="<=",exports.MediaFeatureGT=void 0,(o=exports.MediaFeatureGT||(exports.MediaFeatureGT={})).GT=">",o.GT_OR_EQ=">=",exports.MediaFeatureEQ=void 0,(exports.MediaFeatureEQ||(exports.MediaFeatureEQ={})).EQ="=";class MediaFeatureRangeNameValue{type=exports.NodeType.MediaFeatureRangeNameValue;name;operator;value;constructor(e,t,i){this.name=e,this.operator=t,this.value=i}operatorKind(){return comparisonFromTokens(this.operator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.name.tokens(),...this.operator,...this.value.tokens()]}toString(){return this.name.toString()+i.stringify(...this.operator)+this.value.toString()}indexOf(e){return e===this.name?"name":e===this.value?"value":-1}at(e){return"name"===e?this.name:"value"===e?this.value:void 0}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,name:this.name.toJSON(),value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeNameValue(){return MediaFeatureRangeNameValue.isMediaFeatureRangeNameValue(this)}static isMediaFeatureRangeNameValue(e){return!!e&&(e instanceof MediaFeatureRangeNameValue&&e.type===exports.NodeType.MediaFeatureRangeNameValue)}}class MediaFeatureRangeValueName{type=exports.NodeType.MediaFeatureRangeValueName;name;operator;value;constructor(e,t,i){this.name=e,this.operator=t,this.value=i}operatorKind(){return comparisonFromTokens(this.operator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.value.tokens(),...this.operator,...this.name.tokens()]}toString(){return this.value.toString()+i.stringify(...this.operator)+this.name.toString()}indexOf(e){return e===this.name?"name":e===this.value?"value":-1}at(e){return"name"===e?this.name:"value"===e?this.value:void 0}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,name:this.name.toJSON(),value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeValueName(){return MediaFeatureRangeValueName.isMediaFeatureRangeValueName(this)}static isMediaFeatureRangeValueName(e){return!!e&&(e instanceof MediaFeatureRangeValueName&&e.type===exports.NodeType.MediaFeatureRangeValueName)}}class MediaFeatureRangeValueNameValue{type=exports.NodeType.MediaFeatureRangeValueNameValue;name;valueOne;valueOneOperator;valueTwo;valueTwoOperator;constructor(e,t,i,a,r){this.name=e,this.valueOne=t,this.valueOneOperator=i,this.valueTwo=a,this.valueTwoOperator=r}valueOneOperatorKind(){return comparisonFromTokens(this.valueOneOperator)}valueTwoOperatorKind(){return comparisonFromTokens(this.valueTwoOperator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.valueOne.tokens(),...this.valueOneOperator,...this.name.tokens(),...this.valueTwoOperator,...this.valueTwo.tokens()]}toString(){return this.valueOne.toString()+i.stringify(...this.valueOneOperator)+this.name.toString()+i.stringify(...this.valueTwoOperator)+this.valueTwo.toString()}indexOf(e){return e===this.name?"name":e===this.valueOne?"valueOne":e===this.valueTwo?"valueTwo":-1}at(e){return"name"===e?this.name:"valueOne"===e?this.valueOne:"valueTwo"===e?this.valueTwo:void 0}walk(e){return!1!==e({node:this.valueOne,parent:this},"valueOne")&&((!("walk"in this.valueOne)||!1!==this.valueOne.walk(e))&&(!1!==e({node:this.valueTwo,parent:this},"valueTwo")&&((!("walk"in this.valueTwo)||!1!==this.valueTwo.walk(e))&&void 0)))}toJSON(){return{type:this.type,name:this.name.toJSON(),valueOne:this.valueOne.toJSON(),valueTwo:this.valueTwo.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeValueNameValue(){return MediaFeatureRangeValueNameValue.isMediaFeatureRangeValueNameValue(this)}static isMediaFeatureRangeValueNameValue(e){return!!e&&(e instanceof MediaFeatureRangeValueNameValue&&e.type===exports.NodeType.MediaFeatureRangeValueNameValue)}}function parseMediaFeatureRange(e){let a=!1,r=!1;for(let n=0;ne.tokens())),-1!==r&&(s=e.slice(a+1,r+1).flatMap((e=>e.tokens())))):-1!==r&&(s=e.slice(0,r+1).flatMap((e=>e.tokens())));const u=parseMediaConditionWithoutOr(e.slice(Math.max(a,r,n)+1));return!1===u?new MediaQueryWithType(o,[...s,...e.slice(r+1).flatMap((e=>e.tokens()))]):new MediaQueryWithType(o,s,e.slice(r+1,n+1).flatMap((e=>e.tokens())),u)}}function parseMediaConditionListWithOr(e){let i=!1;const a=[];let r=-1,n=-1;for(let o=0;oe.tokens())),e.slice(n+1).flatMap((e=>e.tokens())))}function parseMediaConditionListWithAnd(e){let i=!1;const a=[];let r=-1,n=-1;for(let o=0;oe.tokens())),e.slice(n+1).flatMap((e=>e.tokens())))}function parseMediaCondition(e){const t=parseMediaNot(e);if(!1!==t)return new MediaCondition(t);const i=parseMediaConditionListWithAnd(e);if(!1!==i)return new MediaCondition(i);const a=parseMediaConditionListWithOr(e);if(!1!==a)return new MediaCondition(a);const r=parseMediaInParens(e);return!1!==r&&new MediaCondition(r)}function parseMediaConditionWithoutOr(e){const t=parseMediaNot(e);if(!1!==t)return new MediaCondition(t);const i=parseMediaConditionListWithAnd(e);if(!1!==i)return new MediaCondition(i);const a=parseMediaInParens(e);return!1!==a&&new MediaCondition(a)}function parseMediaInParens(e){let a=-1;for(let i=0;ie.tokens())),r.startToken],o=[r.endToken,...e.slice(a+1).flatMap((e=>e.tokens()))],s=parseMediaFeature(r,n,o);if(!1!==s)return new MediaInParens(s);const u=parseMediaCondition(r.value);return!1!==u?new MediaInParens(u,n,o):new MediaInParens(new GeneralEnclosed(r),e.slice(0,a).flatMap((e=>e.tokens())),e.slice(a+1).flatMap((e=>e.tokens())))}function parseMediaInParensFromSimpleBlock(e){if(e.startToken[0]!==i.TokenType.OpenParen)return!1;const t=parseMediaFeature(e,[e.startToken],[e.endToken]);if(!1!==t)return new MediaInParens(t);const a=parseMediaCondition(e.value);return!1!==a?new MediaInParens(a,[e.startToken],[e.endToken]):new MediaInParens(new GeneralEnclosed(e))}function parseMediaNot(e){let i=!1,a=null;for(let r=0;re.tokens())),t)}}}return a||!1}function parseMediaOr(e){let i=!1;for(let a=0;ae.tokens())),t)}}return!1}}return!1}function parseMediaAnd(e){let i=!1;for(let a=0;ae.tokens())),t)}}return!1}}return!1}function parseFromTokens(e,i){const a=t.parseCommaSeparatedListOfComponentValues(e,{onParseError:null==i?void 0:i.onParseError});return a.map(((e,t)=>{const r=parseMediaQuery(e);return 0==r&&!0===(null==i?void 0:i.preserveInvalidMediaQueries)?new MediaQueryInvalid(a[t]):r})).filter((e=>!!e))}exports.MediaQueryModifier=void 0,(u=exports.MediaQueryModifier||(exports.MediaQueryModifier={})).Not="not",u.Only="only";class CustomMedia{type=exports.NodeType.CustomMedia;name;mediaQueryList=null;trueOrFalseKeyword=null;constructor(e,t,i){this.name=e,this.mediaQueryList=t,this.trueOrFalseKeyword=i??null}getName(){for(let e=0;ee.toJSON()))}}isCustomMedia(){return CustomMedia.isCustomMedia(this)}static isCustomMedia(e){return!!e&&(e instanceof CustomMedia&&e.type===exports.NodeType.CustomMedia)}}function parseCustomMediaFromTokens(e,t){let a=[],r=e;for(let t=0;tString.fromCharCode(e.charCodeAt(0)+32)))}class MediaCondition{type=f.MediaCondition;media;constructor(e){this.media=e}tokens(){return this.media.tokens()}toString(){return this.media.toString()}indexOf(e){return e===this.media?"media":-1}at(e){if("media"===e)return this.media}walk(e){return!1!==e({node:this.media,parent:this},"media")&&this.media.walk(e)}toJSON(){return{type:this.type,media:this.media.toJSON()}}isMediaCondition(){return MediaCondition.isMediaCondition(this)}static isMediaCondition(e){return!!e&&(e instanceof MediaCondition&&e.type===f.MediaCondition)}}class MediaInParens{type=f.MediaInParens;media;before;after;constructor(e,t=[],i=[]){this.media=e,this.before=t,this.after=i}tokens(){return[...this.before,...this.media.tokens(),...this.after]}toString(){return u(...this.before)+this.media.toString()+u(...this.after)}indexOf(e){return e===this.media?"media":-1}at(e){if("media"===e)return this.media}walk(e){return!1!==e({node:this.media,parent:this},"media")&&("walk"in this.media?this.media.walk(e):void 0)}toJSON(){return{type:this.type,media:this.media.toJSON(),before:this.before,after:this.after}}isMediaInParens(){return MediaInParens.isMediaInParens(this)}static isMediaInParens(e){return!!e&&(e instanceof MediaInParens&&e.type===f.MediaInParens)}}class MediaQueryWithType{type=f.MediaQueryWithType;modifier;mediaType;and=void 0;media=void 0;constructor(e,t,i,a){this.modifier=e,this.mediaType=t,i&&a&&(this.and=i,this.media=a)}getModifier(){if(!this.modifier.length)return"";for(let e=0;ee.tokens()))}toString(){return this.media.map((e=>e.toString())).join("")}walk(e){let t=!1;if(this.media.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),t)return!1}toJSON(){return{type:this.type,string:this.toString(),media:this.media}}isMediaQueryInvalid(){return MediaQueryInvalid.isMediaQueryInvalid(this)}static isMediaQueryInvalid(e){return!!e&&(e instanceof MediaQueryInvalid&&e.type===f.MediaQueryInvalid)}}class GeneralEnclosed{type=f.GeneralEnclosed;value;constructor(e){this.value=e}tokens(){return this.value.tokens()}toString(){return this.value.toString()}indexOf(e){return e===this.value?"value":-1}at(e){if("value"===e)return this.value}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,tokens:this.tokens()}}isGeneralEnclosed(){return GeneralEnclosed.isGeneralEnclosed(this)}static isGeneralEnclosed(e){return!!e&&(e instanceof GeneralEnclosed&&e.type===f.GeneralEnclosed)}}class MediaAnd{type=f.MediaAnd;modifier;media;constructor(e,t){this.modifier=e,this.media=t}tokens(){return[...this.modifier,...this.media.tokens()]}toString(){return u(...this.modifier)+this.media.toString()}indexOf(e){return e===this.media?"media":-1}at(e){return"media"===e?this.media:null}walk(e){return!1!==e({node:this.media,parent:this},"media")&&this.media.walk(e)}toJSON(){return{type:this.type,modifier:this.modifier,media:this.media.toJSON()}}isMediaAnd(){return MediaAnd.isMediaAnd(this)}static isMediaAnd(e){return!!e&&(e instanceof MediaAnd&&e.type===f.MediaAnd)}}class MediaConditionListWithAnd{type=f.MediaConditionListWithAnd;leading;list;before;after;constructor(e,t,i=[],a=[]){this.leading=e,this.list=t,this.before=i,this.after=a}tokens(){return[...this.before,...this.leading.tokens(),...this.list.flatMap((e=>e.tokens())),...this.after]}toString(){return u(...this.before)+this.leading.toString()+this.list.map((e=>e.toString())).join("")+u(...this.after)}indexOf(e){return e===this.leading?"leading":"media-and"===e.type?this.list.indexOf(e):-1}at(e){return"leading"===e?this.leading:"number"==typeof e?(e<0&&(e=this.list.length+e),this.list[e]):void 0}walk(e){if(!1===e({node:this.leading,parent:this},"leading"))return!1;if("walk"in this.leading&&!1===this.leading.walk(e))return!1;let t=!1;return this.list.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),!t&&void 0}toJSON(){return{type:this.type,leading:this.leading.toJSON(),list:this.list.map((e=>e.toJSON())),before:this.before,after:this.after}}isMediaConditionListWithAnd(){return MediaConditionListWithAnd.isMediaConditionListWithAnd(this)}static isMediaConditionListWithAnd(e){return!!e&&(e instanceof MediaConditionListWithAnd&&e.type===f.MediaConditionListWithAnd)}}class MediaConditionListWithOr{type=f.MediaConditionListWithOr;leading;list;before;after;constructor(e,t,i=[],a=[]){this.leading=e,this.list=t,this.before=i,this.after=a}tokens(){return[...this.before,...this.leading.tokens(),...this.list.flatMap((e=>e.tokens())),...this.after]}toString(){return u(...this.before)+this.leading.toString()+this.list.map((e=>e.toString())).join("")+u(...this.after)}indexOf(e){return e===this.leading?"leading":"media-or"===e.type?this.list.indexOf(e):-1}at(e){return"leading"===e?this.leading:"number"==typeof e?(e<0&&(e=this.list.length+e),this.list[e]):void 0}walk(e){if(!1===e({node:this.leading,parent:this},"leading"))return!1;if("walk"in this.leading&&!1===this.leading.walk(e))return!1;let t=!1;return this.list.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),!t&&void 0}toJSON(){return{type:this.type,leading:this.leading.toJSON(),list:this.list.map((e=>e.toJSON())),before:this.before,after:this.after}}isMediaConditionListWithOr(){return MediaConditionListWithOr.isMediaConditionListWithOr(this)}static isMediaConditionListWithOr(e){return!!e&&(e instanceof MediaConditionListWithOr&&e.type===f.MediaConditionListWithOr)}}function isNumber(t){return!!(t.type===e.Token&&t.value[0]===d.Number||t.type===e.Function&&M.has(toLowerCaseAZ(t.name[4].value)))}const M=new Set(["abs","acos","asin","atan","atan2","calc","clamp","cos","exp","hypot","log","max","min","mod","pow","rem","round","sign","sin","sqrt","tan"]);function isDimension(t){return t.type===e.Token&&t.value[0]===d.Dimension}function isIdent(t){return t.type===e.Token&&t.value[0]===d.Ident}class MediaFeatureName{type=f.MediaFeatureName;name;before;after;constructor(e,t=[],i=[]){this.name=e,this.before=t,this.after=i}getName(){return this.name.value[4].value}getNameToken(){return this.name.value}tokens(){return[...this.before,...this.name.tokens(),...this.after]}toString(){return u(...this.before)+this.name.toString()+u(...this.after)}indexOf(e){return e===this.name?"name":-1}at(e){if("name"===e)return this.name}toJSON(){return{type:this.type,name:this.getName(),tokens:this.tokens()}}isMediaFeatureName(){return MediaFeatureName.isMediaFeatureName(this)}static isMediaFeatureName(e){return!!e&&(e instanceof MediaFeatureName&&e.type===f.MediaFeatureName)}}function parseMediaFeatureName(t){let i=-1;for(let a=0;ae.tokens())),t.slice(i+1).flatMap((e=>e.tokens())))}class MediaFeatureBoolean{type=f.MediaFeatureBoolean;name;constructor(e){this.name=e}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return this.name.tokens()}toString(){return this.name.toString()}indexOf(e){return e===this.name?"name":-1}at(e){if("name"===e)return this.name}toJSON(){return{type:this.type,name:this.name.toJSON(),tokens:this.tokens()}}isMediaFeatureBoolean(){return MediaFeatureBoolean.isMediaFeatureBoolean(this)}static isMediaFeatureBoolean(e){return!!e&&(e instanceof MediaFeatureBoolean&&e.type===f.MediaFeatureBoolean)}}function parseMediaFeatureBoolean(e){const t=parseMediaFeatureName(e);return!1===t?t:new MediaFeatureBoolean(t)}class MediaFeatureValue{type=f.MediaFeatureValue;value;before;after;constructor(e,t=[],i=[]){Array.isArray(e)&&1===e.length?this.value=e[0]:this.value=e,this.before=t,this.after=i}tokens(){return Array.isArray(this.value)?[...this.before,...this.value.flatMap((e=>e.tokens())),...this.after]:[...this.before,...this.value.tokens(),...this.after]}toString(){return Array.isArray(this.value)?u(...this.before)+this.value.map((e=>e.toString())).join("")+u(...this.after):u(...this.before)+this.value.toString()+u(...this.after)}indexOf(e){return e===this.value?"value":-1}at(e){if("value"===e)return this.value}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return Array.isArray(this.value)?{type:this.type,value:this.value.map((e=>e.toJSON())),tokens:this.tokens()}:{type:this.type,value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureValue(){return MediaFeatureValue.isMediaFeatureValue(this)}static isMediaFeatureValue(e){return!!e&&(e instanceof MediaFeatureValue&&e.type===f.MediaFeatureValue)}}function parseMediaFeatureValue(t){let i=-1,a=-1;for(let n=0;ne.tokens())),t.slice(a+1).flatMap((e=>e.tokens())))}function matchesRatioExactly(e){let t=-1,i=-1;const a=matchesRatio(e);if(-1===a)return-1;t=a[0],i=a[1];for(let t=i+1;t2)return!1;if(e[0][0]!==d.Delim)return!1;if(1===e.length)switch(e[0][4].value){case v.EQ:return v.EQ;case p.LT:return p.LT;case y.GT:return y.GT;default:return!1}if(e[1][0]!==d.Delim)return!1;if(e[1][4].value!==v.EQ)return!1;switch(e[0][4].value){case p.LT:return p.LT_OR_EQ;case y.GT:return y.GT_OR_EQ;default:return!1}}function invertComparison(e){switch(e){case v.EQ:return v.EQ;case p.LT:return y.GT;case p.LT_OR_EQ:return y.GT_OR_EQ;case y.GT:return p.LT;case y.GT_OR_EQ:return p.LT_OR_EQ;default:return!1}}!function(e){e.LT="<",e.LT_OR_EQ="<="}(p||(p={})),function(e){e.GT=">",e.GT_OR_EQ=">="}(y||(y={})),function(e){e.EQ="="}(v||(v={}));class MediaFeatureRangeNameValue{type=f.MediaFeatureRangeNameValue;name;operator;value;constructor(e,t,i){this.name=e,this.operator=t,this.value=i}operatorKind(){return comparisonFromTokens(this.operator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.name.tokens(),...this.operator,...this.value.tokens()]}toString(){return this.name.toString()+u(...this.operator)+this.value.toString()}indexOf(e){return e===this.name?"name":e===this.value?"value":-1}at(e){return"name"===e?this.name:"value"===e?this.value:void 0}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,name:this.name.toJSON(),value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeNameValue(){return MediaFeatureRangeNameValue.isMediaFeatureRangeNameValue(this)}static isMediaFeatureRangeNameValue(e){return!!e&&(e instanceof MediaFeatureRangeNameValue&&e.type===f.MediaFeatureRangeNameValue)}}class MediaFeatureRangeValueName{type=f.MediaFeatureRangeValueName;name;operator;value;constructor(e,t,i){this.name=e,this.operator=t,this.value=i}operatorKind(){return comparisonFromTokens(this.operator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.value.tokens(),...this.operator,...this.name.tokens()]}toString(){return this.value.toString()+u(...this.operator)+this.name.toString()}indexOf(e){return e===this.name?"name":e===this.value?"value":-1}at(e){return"name"===e?this.name:"value"===e?this.value:void 0}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,name:this.name.toJSON(),value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeValueName(){return MediaFeatureRangeValueName.isMediaFeatureRangeValueName(this)}static isMediaFeatureRangeValueName(e){return!!e&&(e instanceof MediaFeatureRangeValueName&&e.type===f.MediaFeatureRangeValueName)}}class MediaFeatureRangeValueNameValue{type=f.MediaFeatureRangeValueNameValue;name;valueOne;valueOneOperator;valueTwo;valueTwoOperator;constructor(e,t,i,a,n){this.name=e,this.valueOne=t,this.valueOneOperator=i,this.valueTwo=a,this.valueTwoOperator=n}valueOneOperatorKind(){return comparisonFromTokens(this.valueOneOperator)}valueTwoOperatorKind(){return comparisonFromTokens(this.valueTwoOperator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.valueOne.tokens(),...this.valueOneOperator,...this.name.tokens(),...this.valueTwoOperator,...this.valueTwo.tokens()]}toString(){return this.valueOne.toString()+u(...this.valueOneOperator)+this.name.toString()+u(...this.valueTwoOperator)+this.valueTwo.toString()}indexOf(e){return e===this.name?"name":e===this.valueOne?"valueOne":e===this.valueTwo?"valueTwo":-1}at(e){return"name"===e?this.name:"valueOne"===e?this.valueOne:"valueTwo"===e?this.valueTwo:void 0}walk(e){return!1!==e({node:this.valueOne,parent:this},"valueOne")&&((!("walk"in this.valueOne)||!1!==this.valueOne.walk(e))&&(!1!==e({node:this.valueTwo,parent:this},"valueTwo")&&((!("walk"in this.valueTwo)||!1!==this.valueTwo.walk(e))&&void 0)))}toJSON(){return{type:this.type,name:this.name.toJSON(),valueOne:this.valueOne.toJSON(),valueTwo:this.valueTwo.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeValueNameValue(){return MediaFeatureRangeValueNameValue.isMediaFeatureRangeValueNameValue(this)}static isMediaFeatureRangeValueNameValue(e){return!!e&&(e instanceof MediaFeatureRangeValueNameValue&&e.type===f.MediaFeatureRangeValueNameValue)}}function parseMediaFeatureRange(t){let i=!1,a=!1;for(let n=0;ne.tokens())),-1!==i&&(u=e.slice(t+1,i+1).flatMap((e=>e.tokens())))):-1!==i&&(u=e.slice(0,i+1).flatMap((e=>e.tokens())));const l=parseMediaConditionWithoutOr(e.slice(Math.max(t,i,s)+1));return!1===l?new MediaQueryWithType(o,[...u,...e.slice(i+1).flatMap((e=>e.tokens()))]):new MediaQueryWithType(o,u,e.slice(i+1,s+1).flatMap((e=>e.tokens())),l)}}function parseMediaConditionListWithOr(t){let i=!1;const a=[];let n=-1,r=-1;for(let o=0;oe.tokens())),t.slice(r+1).flatMap((e=>e.tokens())))}function parseMediaConditionListWithAnd(t){let i=!1;const a=[];let n=-1,r=-1;for(let o=0;oe.tokens())),t.slice(r+1).flatMap((e=>e.tokens())))}function parseMediaCondition(e){const t=parseMediaNot(e);if(!1!==t)return new MediaCondition(t);const i=parseMediaConditionListWithAnd(e);if(!1!==i)return new MediaCondition(i);const a=parseMediaConditionListWithOr(e);if(!1!==a)return new MediaCondition(a);const n=parseMediaInParens(e);return!1!==n&&new MediaCondition(n)}function parseMediaConditionWithoutOr(e){const t=parseMediaNot(e);if(!1!==t)return new MediaCondition(t);const i=parseMediaConditionListWithAnd(e);if(!1!==i)return new MediaCondition(i);const a=parseMediaInParens(e);return!1!==a&&new MediaCondition(a)}function parseMediaInParens(t){let i=-1;for(let a=0;ae.tokens())),a.startToken],r=[a.endToken,...t.slice(i+1).flatMap((e=>e.tokens()))],o=parseMediaFeature(a,n,r);if(!1!==o)return new MediaInParens(o);const u=parseMediaCondition(a.value);return!1!==u?new MediaInParens(u,n,r):new MediaInParens(new GeneralEnclosed(a),t.slice(0,i).flatMap((e=>e.tokens())),t.slice(i+1).flatMap((e=>e.tokens())))}function parseMediaInParensFromSimpleBlock(e){if(e.startToken[0]!==d.OpenParen)return!1;const t=parseMediaFeature(e,[e.startToken],[e.endToken]);if(!1!==t)return new MediaInParens(t);const i=parseMediaCondition(e.value);return!1!==i?new MediaInParens(i,[e.startToken],[e.endToken]):new MediaInParens(new GeneralEnclosed(e))}function parseMediaNot(t){let i=!1,a=null;for(let n=0;ne.tokens())),e)}}}return a||!1}function parseMediaOr(t){let i=!1;for(let a=0;ae.tokens())),e)}}return!1}}return!1}function parseMediaAnd(t){let i=!1;for(let a=0;ae.tokens())),e)}}return!1}}return!1}function parseFromTokens(e,t){const i=o(e,{onParseError:null==t?void 0:t.onParseError});return i.map(((e,a)=>{const n=parseMediaQuery(e);return 0==n&&!0===(null==t?void 0:t.preserveInvalidMediaQueries)?new MediaQueryInvalid(i[a]):n})).filter((e=>!!e))}function parse(e,t){const i=h({css:e},{onParseError:null==t?void 0:t.onParseError}),a=[];for(;!i.endOfFile();)a.push(i.nextToken());return a.push(i.nextToken()),parseFromTokens(a,t)}!function(e){e.Not="not",e.Only="only"}(k||(k={}));class CustomMedia{type=f.CustomMedia;name;mediaQueryList=null;trueOrFalseKeyword=null;constructor(e,t,i){this.name=e,this.mediaQueryList=t,this.trueOrFalseKeyword=i??null}getName(){for(let e=0;ee.toJSON()))}}isCustomMedia(){return CustomMedia.isCustomMedia(this)}static isCustomMedia(e){return!!e&&(e instanceof CustomMedia&&e.type===f.CustomMedia)}}function parseCustomMediaFromTokens(e,t){let i=[],a=e;for(let t=0;tString.fromCharCode(e.charCodeAt(0)+32)))}class MediaCondition{type=f.MediaCondition;media;constructor(e){this.media=e}tokens(){return this.media.tokens()}toString(){return this.media.toString()}indexOf(e){return e===this.media?"media":-1}at(e){if("media"===e)return this.media}walk(e){return!1!==e({node:this.media,parent:this},"media")&&this.media.walk(e)}toJSON(){return{type:this.type,media:this.media.toJSON()}}isMediaCondition(){return MediaCondition.isMediaCondition(this)}static isMediaCondition(e){return!!e&&(e instanceof MediaCondition&&e.type===f.MediaCondition)}}class MediaInParens{type=f.MediaInParens;media;before;after;constructor(e,t=[],i=[]){this.media=e,this.before=t,this.after=i}tokens(){return[...this.before,...this.media.tokens(),...this.after]}toString(){return u(...this.before)+this.media.toString()+u(...this.after)}indexOf(e){return e===this.media?"media":-1}at(e){if("media"===e)return this.media}walk(e){return!1!==e({node:this.media,parent:this},"media")&&("walk"in this.media?this.media.walk(e):void 0)}toJSON(){return{type:this.type,media:this.media.toJSON(),before:this.before,after:this.after}}isMediaInParens(){return MediaInParens.isMediaInParens(this)}static isMediaInParens(e){return!!e&&(e instanceof MediaInParens&&e.type===f.MediaInParens)}}class MediaQueryWithType{type=f.MediaQueryWithType;modifier;mediaType;and=void 0;media=void 0;constructor(e,t,i,a){this.modifier=e,this.mediaType=t,i&&a&&(this.and=i,this.media=a)}getModifier(){if(!this.modifier.length)return"";for(let e=0;ee.tokens()))}toString(){return this.media.map((e=>e.toString())).join("")}walk(e){let t=!1;if(this.media.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),t)return!1}toJSON(){return{type:this.type,string:this.toString(),media:this.media}}isMediaQueryInvalid(){return MediaQueryInvalid.isMediaQueryInvalid(this)}static isMediaQueryInvalid(e){return!!e&&(e instanceof MediaQueryInvalid&&e.type===f.MediaQueryInvalid)}}class GeneralEnclosed{type=f.GeneralEnclosed;value;constructor(e){this.value=e}tokens(){return this.value.tokens()}toString(){return this.value.toString()}indexOf(e){return e===this.value?"value":-1}at(e){if("value"===e)return this.value}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,tokens:this.tokens()}}isGeneralEnclosed(){return GeneralEnclosed.isGeneralEnclosed(this)}static isGeneralEnclosed(e){return!!e&&(e instanceof GeneralEnclosed&&e.type===f.GeneralEnclosed)}}class MediaAnd{type=f.MediaAnd;modifier;media;constructor(e,t){this.modifier=e,this.media=t}tokens(){return[...this.modifier,...this.media.tokens()]}toString(){return u(...this.modifier)+this.media.toString()}indexOf(e){return e===this.media?"media":-1}at(e){return"media"===e?this.media:null}walk(e){return!1!==e({node:this.media,parent:this},"media")&&this.media.walk(e)}toJSON(){return{type:this.type,modifier:this.modifier,media:this.media.toJSON()}}isMediaAnd(){return MediaAnd.isMediaAnd(this)}static isMediaAnd(e){return!!e&&(e instanceof MediaAnd&&e.type===f.MediaAnd)}}class MediaConditionListWithAnd{type=f.MediaConditionListWithAnd;leading;list;before;after;constructor(e,t,i=[],a=[]){this.leading=e,this.list=t,this.before=i,this.after=a}tokens(){return[...this.before,...this.leading.tokens(),...this.list.flatMap((e=>e.tokens())),...this.after]}toString(){return u(...this.before)+this.leading.toString()+this.list.map((e=>e.toString())).join("")+u(...this.after)}indexOf(e){return e===this.leading?"leading":"media-and"===e.type?this.list.indexOf(e):-1}at(e){return"leading"===e?this.leading:"number"==typeof e?(e<0&&(e=this.list.length+e),this.list[e]):void 0}walk(e){if(!1===e({node:this.leading,parent:this},"leading"))return!1;if("walk"in this.leading&&!1===this.leading.walk(e))return!1;let t=!1;return this.list.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),!t&&void 0}toJSON(){return{type:this.type,leading:this.leading.toJSON(),list:this.list.map((e=>e.toJSON())),before:this.before,after:this.after}}isMediaConditionListWithAnd(){return MediaConditionListWithAnd.isMediaConditionListWithAnd(this)}static isMediaConditionListWithAnd(e){return!!e&&(e instanceof MediaConditionListWithAnd&&e.type===f.MediaConditionListWithAnd)}}class MediaConditionListWithOr{type=f.MediaConditionListWithOr;leading;list;before;after;constructor(e,t,i=[],a=[]){this.leading=e,this.list=t,this.before=i,this.after=a}tokens(){return[...this.before,...this.leading.tokens(),...this.list.flatMap((e=>e.tokens())),...this.after]}toString(){return u(...this.before)+this.leading.toString()+this.list.map((e=>e.toString())).join("")+u(...this.after)}indexOf(e){return e===this.leading?"leading":"media-or"===e.type?this.list.indexOf(e):-1}at(e){return"leading"===e?this.leading:"number"==typeof e?(e<0&&(e=this.list.length+e),this.list[e]):void 0}walk(e){if(!1===e({node:this.leading,parent:this},"leading"))return!1;if("walk"in this.leading&&!1===this.leading.walk(e))return!1;let t=!1;return this.list.forEach(((i,a)=>{t||(!1!==e({node:i,parent:this},a)?"walk"in i&&!1===i.walk(e)&&(t=!0):t=!0)})),!t&&void 0}toJSON(){return{type:this.type,leading:this.leading.toJSON(),list:this.list.map((e=>e.toJSON())),before:this.before,after:this.after}}isMediaConditionListWithOr(){return MediaConditionListWithOr.isMediaConditionListWithOr(this)}static isMediaConditionListWithOr(e){return!!e&&(e instanceof MediaConditionListWithOr&&e.type===f.MediaConditionListWithOr)}}function isNumber(t){return!!(t.type===e.Token&&t.value[0]===d.Number||t.type===e.Function&&M.has(toLowerCaseAZ(t.name[4].value)))}const M=new Set(["abs","acos","asin","atan","atan2","calc","clamp","cos","exp","hypot","log","max","min","mod","pow","rem","round","sign","sin","sqrt","tan"]);function isDimension(t){return t.type===e.Token&&t.value[0]===d.Dimension}function isIdent(t){return t.type===e.Token&&t.value[0]===d.Ident}function isEnvironmentVariable(t){return t.type===e.Function&&"env"===toLowerCaseAZ(t.name[4].value)}class MediaFeatureName{type=f.MediaFeatureName;name;before;after;constructor(e,t=[],i=[]){this.name=e,this.before=t,this.after=i}getName(){return this.name.value[4].value}getNameToken(){return this.name.value}tokens(){return[...this.before,...this.name.tokens(),...this.after]}toString(){return u(...this.before)+this.name.toString()+u(...this.after)}indexOf(e){return e===this.name?"name":-1}at(e){if("name"===e)return this.name}toJSON(){return{type:this.type,name:this.getName(),tokens:this.tokens()}}isMediaFeatureName(){return MediaFeatureName.isMediaFeatureName(this)}static isMediaFeatureName(e){return!!e&&(e instanceof MediaFeatureName&&e.type===f.MediaFeatureName)}}function parseMediaFeatureName(t){let i=-1;for(let a=0;ae.tokens())),t.slice(i+1).flatMap((e=>e.tokens())))}class MediaFeatureBoolean{type=f.MediaFeatureBoolean;name;constructor(e){this.name=e}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return this.name.tokens()}toString(){return this.name.toString()}indexOf(e){return e===this.name?"name":-1}at(e){if("name"===e)return this.name}toJSON(){return{type:this.type,name:this.name.toJSON(),tokens:this.tokens()}}isMediaFeatureBoolean(){return MediaFeatureBoolean.isMediaFeatureBoolean(this)}static isMediaFeatureBoolean(e){return!!e&&(e instanceof MediaFeatureBoolean&&e.type===f.MediaFeatureBoolean)}}function parseMediaFeatureBoolean(e){const t=parseMediaFeatureName(e);return!1===t?t:new MediaFeatureBoolean(t)}class MediaFeatureValue{type=f.MediaFeatureValue;value;before;after;constructor(e,t=[],i=[]){Array.isArray(e)&&1===e.length?this.value=e[0]:this.value=e,this.before=t,this.after=i}tokens(){return Array.isArray(this.value)?[...this.before,...this.value.flatMap((e=>e.tokens())),...this.after]:[...this.before,...this.value.tokens(),...this.after]}toString(){return Array.isArray(this.value)?u(...this.before)+this.value.map((e=>e.toString())).join("")+u(...this.after):u(...this.before)+this.value.toString()+u(...this.after)}indexOf(e){return e===this.value?"value":-1}at(e){if("value"===e)return this.value}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return Array.isArray(this.value)?{type:this.type,value:this.value.map((e=>e.toJSON())),tokens:this.tokens()}:{type:this.type,value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureValue(){return MediaFeatureValue.isMediaFeatureValue(this)}static isMediaFeatureValue(e){return!!e&&(e instanceof MediaFeatureValue&&e.type===f.MediaFeatureValue)}}function parseMediaFeatureValue(t){let i=-1,a=-1;for(let n=0;ne.tokens())),t.slice(a+1).flatMap((e=>e.tokens())))}function matchesRatioExactly(e){let t=-1,i=-1;const a=matchesRatio(e);if(-1===a)return-1;t=a[0],i=a[1];for(let t=i+1;t2)return!1;if(e[0][0]!==d.Delim)return!1;if(1===e.length)switch(e[0][4].value){case v.EQ:return v.EQ;case p.LT:return p.LT;case y.GT:return y.GT;default:return!1}if(e[1][0]!==d.Delim)return!1;if(e[1][4].value!==v.EQ)return!1;switch(e[0][4].value){case p.LT:return p.LT_OR_EQ;case y.GT:return y.GT_OR_EQ;default:return!1}}function invertComparison(e){switch(e){case v.EQ:return v.EQ;case p.LT:return y.GT;case p.LT_OR_EQ:return y.GT_OR_EQ;case y.GT:return p.LT;case y.GT_OR_EQ:return p.LT_OR_EQ;default:return!1}}!function(e){e.LT="<",e.LT_OR_EQ="<="}(p||(p={})),function(e){e.GT=">",e.GT_OR_EQ=">="}(y||(y={})),function(e){e.EQ="="}(v||(v={}));class MediaFeatureRangeNameValue{type=f.MediaFeatureRangeNameValue;name;operator;value;constructor(e,t,i){this.name=e,this.operator=t,this.value=i}operatorKind(){return comparisonFromTokens(this.operator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.name.tokens(),...this.operator,...this.value.tokens()]}toString(){return this.name.toString()+u(...this.operator)+this.value.toString()}indexOf(e){return e===this.name?"name":e===this.value?"value":-1}at(e){return"name"===e?this.name:"value"===e?this.value:void 0}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,name:this.name.toJSON(),value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeNameValue(){return MediaFeatureRangeNameValue.isMediaFeatureRangeNameValue(this)}static isMediaFeatureRangeNameValue(e){return!!e&&(e instanceof MediaFeatureRangeNameValue&&e.type===f.MediaFeatureRangeNameValue)}}class MediaFeatureRangeValueName{type=f.MediaFeatureRangeValueName;name;operator;value;constructor(e,t,i){this.name=e,this.operator=t,this.value=i}operatorKind(){return comparisonFromTokens(this.operator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.value.tokens(),...this.operator,...this.name.tokens()]}toString(){return this.value.toString()+u(...this.operator)+this.name.toString()}indexOf(e){return e===this.name?"name":e===this.value?"value":-1}at(e){return"name"===e?this.name:"value"===e?this.value:void 0}walk(e){return!1!==e({node:this.value,parent:this},"value")&&("walk"in this.value?this.value.walk(e):void 0)}toJSON(){return{type:this.type,name:this.name.toJSON(),value:this.value.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeValueName(){return MediaFeatureRangeValueName.isMediaFeatureRangeValueName(this)}static isMediaFeatureRangeValueName(e){return!!e&&(e instanceof MediaFeatureRangeValueName&&e.type===f.MediaFeatureRangeValueName)}}class MediaFeatureRangeValueNameValue{type=f.MediaFeatureRangeValueNameValue;name;valueOne;valueOneOperator;valueTwo;valueTwoOperator;constructor(e,t,i,a,n){this.name=e,this.valueOne=t,this.valueOneOperator=i,this.valueTwo=a,this.valueTwoOperator=n}valueOneOperatorKind(){return comparisonFromTokens(this.valueOneOperator)}valueTwoOperatorKind(){return comparisonFromTokens(this.valueTwoOperator)}getName(){return this.name.getName()}getNameToken(){return this.name.getNameToken()}tokens(){return[...this.valueOne.tokens(),...this.valueOneOperator,...this.name.tokens(),...this.valueTwoOperator,...this.valueTwo.tokens()]}toString(){return this.valueOne.toString()+u(...this.valueOneOperator)+this.name.toString()+u(...this.valueTwoOperator)+this.valueTwo.toString()}indexOf(e){return e===this.name?"name":e===this.valueOne?"valueOne":e===this.valueTwo?"valueTwo":-1}at(e){return"name"===e?this.name:"valueOne"===e?this.valueOne:"valueTwo"===e?this.valueTwo:void 0}walk(e){return!1!==e({node:this.valueOne,parent:this},"valueOne")&&((!("walk"in this.valueOne)||!1!==this.valueOne.walk(e))&&(!1!==e({node:this.valueTwo,parent:this},"valueTwo")&&((!("walk"in this.valueTwo)||!1!==this.valueTwo.walk(e))&&void 0)))}toJSON(){return{type:this.type,name:this.name.toJSON(),valueOne:this.valueOne.toJSON(),valueTwo:this.valueTwo.toJSON(),tokens:this.tokens()}}isMediaFeatureRangeValueNameValue(){return MediaFeatureRangeValueNameValue.isMediaFeatureRangeValueNameValue(this)}static isMediaFeatureRangeValueNameValue(e){return!!e&&(e instanceof MediaFeatureRangeValueNameValue&&e.type===f.MediaFeatureRangeValueNameValue)}}function parseMediaFeatureRange(t){let i=!1,a=!1;for(let n=0;ne.tokens())),-1!==i&&(u=e.slice(t+1,i+1).flatMap((e=>e.tokens())))):-1!==i&&(u=e.slice(0,i+1).flatMap((e=>e.tokens())));const l=parseMediaConditionWithoutOr(e.slice(Math.max(t,i,s)+1));return!1===l?new MediaQueryWithType(o,[...u,...e.slice(i+1).flatMap((e=>e.tokens()))]):new MediaQueryWithType(o,u,e.slice(i+1,s+1).flatMap((e=>e.tokens())),l)}}function parseMediaConditionListWithOr(t){let i=!1;const a=[];let n=-1,r=-1;for(let o=0;oe.tokens())),t.slice(r+1).flatMap((e=>e.tokens())))}function parseMediaConditionListWithAnd(t){let i=!1;const a=[];let n=-1,r=-1;for(let o=0;oe.tokens())),t.slice(r+1).flatMap((e=>e.tokens())))}function parseMediaCondition(e){const t=parseMediaNot(e);if(!1!==t)return new MediaCondition(t);const i=parseMediaConditionListWithAnd(e);if(!1!==i)return new MediaCondition(i);const a=parseMediaConditionListWithOr(e);if(!1!==a)return new MediaCondition(a);const n=parseMediaInParens(e);return!1!==n&&new MediaCondition(n)}function parseMediaConditionWithoutOr(e){const t=parseMediaNot(e);if(!1!==t)return new MediaCondition(t);const i=parseMediaConditionListWithAnd(e);if(!1!==i)return new MediaCondition(i);const a=parseMediaInParens(e);return!1!==a&&new MediaCondition(a)}function parseMediaInParens(t){let i=-1;for(let a=0;ae.tokens())),a.startToken],r=[a.endToken,...t.slice(i+1).flatMap((e=>e.tokens()))],o=parseMediaFeature(a,n,r);if(!1!==o)return new MediaInParens(o);const u=parseMediaCondition(a.value);return!1!==u?new MediaInParens(u,n,r):new MediaInParens(new GeneralEnclosed(a),t.slice(0,i).flatMap((e=>e.tokens())),t.slice(i+1).flatMap((e=>e.tokens())))}function parseMediaInParensFromSimpleBlock(e){if(e.startToken[0]!==d.OpenParen)return!1;const t=parseMediaFeature(e,[e.startToken],[e.endToken]);if(!1!==t)return new MediaInParens(t);const i=parseMediaCondition(e.value);return!1!==i?new MediaInParens(i,[e.startToken],[e.endToken]):new MediaInParens(new GeneralEnclosed(e))}function parseMediaNot(t){let i=!1,a=null;for(let n=0;ne.tokens())),e)}}}return a||!1}function parseMediaOr(t){let i=!1;for(let a=0;ae.tokens())),e)}}return!1}}return!1}function parseMediaAnd(t){let i=!1;for(let a=0;ae.tokens())),e)}}return!1}}return!1}function parseFromTokens(e,t){const i=o(e,{onParseError:null==t?void 0:t.onParseError});return i.map(((e,a)=>{const n=parseMediaQuery(e);return 0==n&&!0===(null==t?void 0:t.preserveInvalidMediaQueries)?new MediaQueryInvalid(i[a]):n})).filter((e=>!!e))}function parse(e,t){const i=h({css:e},{onParseError:null==t?void 0:t.onParseError}),a=[];for(;!i.endOfFile();)a.push(i.nextToken());return a.push(i.nextToken()),parseFromTokens(a,t)}!function(e){e.Not="not",e.Only="only"}(k||(k={}));class CustomMedia{type=f.CustomMedia;name;mediaQueryList=null;trueOrFalseKeyword=null;constructor(e,t,i){this.name=e,this.mediaQueryList=t,this.trueOrFalseKeyword=i??null}getName(){for(let e=0;ee.toJSON()))}}isCustomMedia(){return CustomMedia.isCustomMedia(this)}static isCustomMedia(e){return!!e&&(e instanceof CustomMedia&&e.type===f.CustomMedia)}}function parseCustomMediaFromTokens(e,t){let i=[],a=e;for(let t=0;t): continue; } + if (isEnvironmentVariable(componentValue)) { + candidateIndexStart = i; + candidateIndexEnd = i; + continue; + } + if (isDimension(componentValue)) { candidateIndexStart = i; candidateIndexEnd = i; diff --git a/packages/media-query-list-parser/src/util/component-value-is.ts b/packages/media-query-list-parser/src/util/component-value-is.ts index af36011dd..3ca5eb1af 100644 --- a/packages/media-query-list-parser/src/util/component-value-is.ts +++ b/packages/media-query-list-parser/src/util/component-value-is.ts @@ -52,3 +52,13 @@ export function isIdent(componentValue: ComponentValue) { return false; } + +export function isEnvironmentVariable(componentValue: ComponentValue) { + if (componentValue.type === ComponentValueType.Function && + toLowerCaseAZ(((componentValue as FunctionNode).name as TokenFunction)[4].value) === 'env' + ) { + return true; + } + + return false; +} diff --git a/packages/media-query-list-parser/test/cases/mf-plain/0009.expect.json b/packages/media-query-list-parser/test/cases/mf-plain/0009.expect.json new file mode 100644 index 000000000..edaffe08d --- /dev/null +++ b/packages/media-query-list-parser/test/cases/mf-plain/0009.expect.json @@ -0,0 +1,187 @@ +[ + { + "type": "media-query-without-type", + "string": "(min-width: env(safe-area-inset-top))", + "media": { + "type": "media-condition", + "media": { + "type": "media-in-parens", + "media": { + "type": "media-feature", + "feature": { + "type": "mf-plain", + "name": { + "type": "mf-name", + "name": "min-width", + "tokens": [ + [ + "ident-token", + "min-width", + 1, + 9, + { + "value": "min-width" + } + ] + ] + }, + "value": { + "type": "mf-value", + "value": { + "type": "function", + "name": "env", + "tokens": [ + [ + "function-token", + "env(", + 12, + 15, + { + "value": "env" + } + ], + [ + "ident-token", + "safe-area-inset-top", + 16, + 34, + { + "value": "safe-area-inset-top" + } + ], + [ + ")-token", + ")", + 35, + 35, + null + ] + ], + "value": [ + { + "type": "token", + "tokens": [ + [ + "ident-token", + "safe-area-inset-top", + 16, + 34, + { + "value": "safe-area-inset-top" + } + ] + ] + } + ] + }, + "tokens": [ + [ + "whitespace-token", + " ", + 11, + 11, + null + ], + [ + "function-token", + "env(", + 12, + 15, + { + "value": "env" + } + ], + [ + "ident-token", + "safe-area-inset-top", + 16, + 34, + { + "value": "safe-area-inset-top" + } + ], + [ + ")-token", + ")", + 35, + 35, + null + ] + ] + }, + "tokens": [ + [ + "ident-token", + "min-width", + 1, + 9, + { + "value": "min-width" + } + ], + [ + "colon-token", + ":", + 10, + 10, + null + ], + [ + "whitespace-token", + " ", + 11, + 11, + null + ], + [ + "function-token", + "env(", + 12, + 15, + { + "value": "env" + } + ], + [ + "ident-token", + "safe-area-inset-top", + 16, + 34, + { + "value": "safe-area-inset-top" + } + ], + [ + ")-token", + ")", + 35, + 35, + null + ] + ] + }, + "before": [ + [ + "(-token", + "(", + 0, + 0, + null + ] + ], + "after": [ + [ + ")-token", + ")", + 36, + 36, + null + ] + ] + }, + "before": [], + "after": [] + } + } + } +] \ No newline at end of file diff --git a/packages/media-query-list-parser/test/cases/mf-plain/0009.mjs b/packages/media-query-list-parser/test/cases/mf-plain/0009.mjs new file mode 100644 index 000000000..032c86c56 --- /dev/null +++ b/packages/media-query-list-parser/test/cases/mf-plain/0009.mjs @@ -0,0 +1,13 @@ +import assert from 'assert'; +import { runTest } from '../../util/run-test.mjs'; + +runTest( + '(min-width: env(safe-area-inset-top))', + 'mf-plain/0009', + (actual, expected) => { + assert.deepStrictEqual( + actual, + expected, + ); + }, +); diff --git a/packages/media-query-list-parser/test/cases/mf-range/0017.expect.json b/packages/media-query-list-parser/test/cases/mf-range/0017.expect.json new file mode 100644 index 000000000..97e2b2ea3 --- /dev/null +++ b/packages/media-query-list-parser/test/cases/mf-range/0017.expect.json @@ -0,0 +1,203 @@ +[ + { + "type": "media-query-without-type", + "string": "(height > env(safe-area-inset-top))", + "media": { + "type": "media-condition", + "media": { + "type": "media-in-parens", + "media": { + "type": "media-feature", + "feature": { + "type": "mf-range-name-value", + "name": { + "type": "mf-name", + "name": "height", + "tokens": [ + [ + "ident-token", + "height", + 1, + 6, + { + "value": "height" + } + ], + [ + "whitespace-token", + " ", + 7, + 7, + null + ] + ] + }, + "value": { + "type": "mf-value", + "value": { + "type": "function", + "name": "env", + "tokens": [ + [ + "function-token", + "env(", + 10, + 13, + { + "value": "env" + } + ], + [ + "ident-token", + "safe-area-inset-top", + 14, + 32, + { + "value": "safe-area-inset-top" + } + ], + [ + ")-token", + ")", + 33, + 33, + null + ] + ], + "value": [ + { + "type": "token", + "tokens": [ + [ + "ident-token", + "safe-area-inset-top", + 14, + 32, + { + "value": "safe-area-inset-top" + } + ] + ] + } + ] + }, + "tokens": [ + [ + "whitespace-token", + " ", + 9, + 9, + null + ], + [ + "function-token", + "env(", + 10, + 13, + { + "value": "env" + } + ], + [ + "ident-token", + "safe-area-inset-top", + 14, + 32, + { + "value": "safe-area-inset-top" + } + ], + [ + ")-token", + ")", + 33, + 33, + null + ] + ] + }, + "tokens": [ + [ + "ident-token", + "height", + 1, + 6, + { + "value": "height" + } + ], + [ + "whitespace-token", + " ", + 7, + 7, + null + ], + [ + "delim-token", + ">", + 8, + 8, + { + "value": ">" + } + ], + [ + "whitespace-token", + " ", + 9, + 9, + null + ], + [ + "function-token", + "env(", + 10, + 13, + { + "value": "env" + } + ], + [ + "ident-token", + "safe-area-inset-top", + 14, + 32, + { + "value": "safe-area-inset-top" + } + ], + [ + ")-token", + ")", + 33, + 33, + null + ] + ] + }, + "before": [ + [ + "(-token", + "(", + 0, + 0, + null + ] + ], + "after": [ + [ + ")-token", + ")", + 34, + 34, + null + ] + ] + }, + "before": [], + "after": [] + } + } + } +] \ No newline at end of file diff --git a/packages/media-query-list-parser/test/cases/mf-range/0017.mjs b/packages/media-query-list-parser/test/cases/mf-range/0017.mjs new file mode 100644 index 000000000..330f4a3db --- /dev/null +++ b/packages/media-query-list-parser/test/cases/mf-range/0017.mjs @@ -0,0 +1,14 @@ +import assert from 'assert'; +import { runTest } from '../../util/run-test.mjs'; + +runTest( + '(height > env(safe-area-inset-top))', + 'mf-range/0017', + (actual, expected) => { + assert.deepStrictEqual( + actual, + expected, + ); + }, + 0, +); diff --git a/packages/media-query-list-parser/test/test.mjs b/packages/media-query-list-parser/test/test.mjs index 0ffdcd341..61549a2d9 100644 --- a/packages/media-query-list-parser/test/test.mjs +++ b/packages/media-query-list-parser/test/test.mjs @@ -28,6 +28,7 @@ import './cases/mf-plain/0005.mjs'; import './cases/mf-plain/0006.mjs'; import './cases/mf-plain/0007.mjs'; import './cases/mf-plain/0008.mjs'; +import './cases/mf-plain/0009.mjs'; import './cases/mf-range/0001.mjs'; import './cases/mf-range/0002.mjs'; @@ -45,6 +46,7 @@ import './cases/mf-range/0013.mjs'; import './cases/mf-range/0014.mjs'; import './cases/mf-range/0015.mjs'; import './cases/mf-range/0016.mjs'; +import './cases/mf-range/0017.mjs'; import './cases/query-with-type/0001.mjs'; import './cases/query-with-type/0002.mjs'; diff --git a/plugins/postcss-media-minmax/.tape.mjs b/plugins/postcss-media-minmax/.tape.mjs index 80c338f6b..8fae94bb0 100644 --- a/plugins/postcss-media-minmax/.tape.mjs +++ b/plugins/postcss-media-minmax/.tape.mjs @@ -29,6 +29,9 @@ postcssTape(plugin)({ 'device-width-height': { message: 'device-width-height feature' }, + 'env': { + message: 'handling of `env()` function values', + }, 'line-break': { message: 'line breaks in range notation', }, diff --git a/plugins/postcss-media-minmax/dist/index.cjs b/plugins/postcss-media-minmax/dist/index.cjs index ce64d9182..cd3a79abb 100644 --- a/plugins/postcss-media-minmax/dist/index.cjs +++ b/plugins/postcss-media-minmax/dist/index.cjs @@ -1 +1 @@ -"use strict";var e=require("@csstools/css-parser-algorithms"),i=require("@csstools/css-tokenizer"),n=require("@csstools/media-query-list-parser"),t=require("@csstools/css-calc");const a=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(a,(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(a,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)+a,...T,...c.value.flatMap((e=>e.tokens())),...m):n.newMediaFeaturePlain(featureNamePrefix(l)+a,...T,...c.value.tokens(),...m);let v,f,y=!1;if(Array.isArray(c.value)){if(!n.matchesRatioExactly(c.value))return;if("aspect-ratio"!==a&&"device-aspect-ratio"!==a)return;const e=n.matchesRatio(c.value);if(-1===e)return;y=!0,v=c.value[e[0]],f=[...c.value.slice(e[0]+1).flatMap((e=>e.tokens()))]}else v=c.value,f=[];const h=r[a.toLowerCase()];if(e.isFunctionNode(v)&&t.mathFunctionNames.has(toLowerCaseAZ(v.getName()))){const[[r]]=t.calcFromComponentValues([[v]],{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[a]){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)+a,...T,[i.TokenType.Function,"calc(",-1,-1,{value:"calc("}],[i.TokenType.OpenParen,"(",-1,-1,void 0],...v.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],...f,...m)}v=r}if(!e.isTokenNode(v))return;let k,g=v.value,M="";if(void 0!==h&&g[0]===i.TokenType.Number&&0===g[4].value)k=s[l],M=h;else if(g[0]===i.TokenType.Number&&0===g[4].value)k=s[l],M="";else if(g[0]===i.TokenType.Dimension&&0===g[4].value)k=s[l],M=g[4].unit;else if(g[0]===i.TokenType.Number&&!0===o[a])k=g[4].value+s[l];else if(g[0]===i.TokenType.Dimension&&"px"===g[4].unit&&g[4].type===i.NumberType.Integer)k=Number(Math.round(Number(g[4].value+d*s[l]+"e6"))+"e-6");else{if(g[0]!==i.TokenType.Dimension&&g[0]!==i.TokenType.Number)return;k=Number(Math.round(Number(g[4].value+u*s[l]+"e6"))+"e-6")}return M&&(g=[i.TokenType.Dimension,g[1],g[2],g[3],{value:g[4].value,unit:M,type:g[4].type}]),g[4].value=k,g[0]===i.TokenType.Dimension?g[1]=g[4].value.toString()+g[4].unit:g[1]=g[4].value.toString(),n.newMediaFeaturePlain(featureNamePrefix(l)+a,...T,g,...f,...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(t){return t.map(((t,a)=>{const r=e.gatherNodeAncestry(t);t.walk((e=>{const a=e.node;if(!n.isMediaFeatureRange(a))return;const o=e.parent;if(!n.isMediaFeature(o))return;const s=toLowerCaseAZ(a.name.getName());if(!l.has(s))return;if(n.isMediaFeatureRangeNameValue(a)||n.isMediaFeatureRangeValueName(a)){const e=a.operatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,a.value,n.isMediaFeatureRangeNameValue(a));return void(i&&(o.feature=i.feature))}const u=r.get(o);if(!n.isMediaInParens(u))return;let d=null,c=null;{const e=a.valueOneOperatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,a.valueOne,!1);if(!i)return;e===n.MediaFeatureLT.LT||e===n.MediaFeatureLT.LT_OR_EQ?(d=i,d.before=o.before):(c=i,c.after=o.after)}{const e=a.valueTwoOperatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,a.valueTwo,!0);if(!i)return;e===n.MediaFeatureLT.LT||e===n.MediaFeatureLT.LT_OR_EQ?(c=i,c.before=o.before):(d=i,d.after=o.after)}if(!d||!c)return;const p=new n.MediaInParens(d),T=new n.MediaInParens(c),m=getMediaConditionListWithAndFromAncestry(u,r);if(m)return m.leading===u?(m.leading=p,void(m.list=[new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T),...m.list])):void m.list.splice(m.indexOf(r.get(u)),1,new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],p),new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T));const v=new n.MediaConditionListWithAnd(p,[new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T)],[[i.TokenType.Whitespace," ",-1,-1,void 0]]),f=getMediaConditionInShallowMediaQueryFromAncestry(u,t,r);f?f.media=v:u.media=new n.MediaCondition(new n.MediaInParens(new n.MediaCondition(v),[[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.OpenParen,"(",-1,-1,void 0]],[[i.TokenType.CloseParen,")",-1,-1,void 0]]))}));const o=t.tokens();return i.stringify(...o.filter(((e,n)=>(0!==n||0!==a||e[0]!==i.TokenType.Whitespace)&&(e[0]!==i.TokenType.Whitespace||!o[n+1]||o[n+1][0]!==i.TokenType.Whitespace))))})).join(",")}function getMediaConditionListWithAndFromAncestry(e,i){let t=e;if(t){if(t=i.get(t),n.isMediaConditionListWithAnd(t))return t;if(n.isMediaAnd(t))return t=i.get(t),n.isMediaConditionListWithAnd(t)?t:void 0}}function getMediaConditionInShallowMediaQueryFromAncestry(e,i,t){let a=e;if(!a)return;if(a=t.get(a),!n.isMediaCondition(a))return;const r=a;return a=t.get(a),n.isMediaQuery(a)&&a===i?r:void 0}const creator=()=>({postcssPlugin:"postcss-media-minmax",AtRule:{media:e=>{if(!(e.params.includes("<")||e.params.includes(">")||e.params.includes("=")))return;const i=transform(n.parse(e.params,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw e.error(`Unable to parse media query "${e.params}"`)}}));e.params!==i&&(e.params=i)}}});creator.postcss=!0,module.exports=creator; +"use strict";var e=require("@csstools/css-parser-algorithms"),i=require("@csstools/css-tokenizer"),n=require("@csstools/media-query-list-parser"),t=require("@csstools/css-calc");const a=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(a,(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(a,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)+a,...T,...c.value.flatMap((e=>e.tokens())),...m):n.newMediaFeaturePlain(featureNamePrefix(l)+a,...T,...c.value.tokens(),...m);let v,f,y=!1;if(Array.isArray(c.value)){if(!n.matchesRatioExactly(c.value))return;if("aspect-ratio"!==a&&"device-aspect-ratio"!==a)return;const e=n.matchesRatio(c.value);if(-1===e)return;y=!0,v=c.value[e[0]],f=[...c.value.slice(e[0]+1).flatMap((e=>e.tokens()))]}else v=c.value,f=[];const h=r[a.toLowerCase()];if(e.isFunctionNode(v)){const r=toLowerCaseAZ(v.getName());if(t.mathFunctionNames.has(r)||"env"===r){const[[r]]=t.calcFromComponentValues([[v]],{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[a]){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)+a,...T,[i.TokenType.Function,"calc(",-1,-1,{value:"calc("}],[i.TokenType.OpenParen,"(",-1,-1,void 0],...v.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],...f,...m)}v=r}}if(!e.isTokenNode(v))return;let k,g=v.value,M="";if(void 0!==h&&g[0]===i.TokenType.Number&&0===g[4].value)k=s[l],M=h;else if(g[0]===i.TokenType.Number&&0===g[4].value)k=s[l],M="";else if(g[0]===i.TokenType.Dimension&&0===g[4].value)k=s[l],M=g[4].unit;else if(g[0]===i.TokenType.Number&&!0===o[a])k=g[4].value+s[l];else if(g[0]===i.TokenType.Dimension&&"px"===g[4].unit&&g[4].type===i.NumberType.Integer)k=Number(Math.round(Number(g[4].value+d*s[l]+"e6"))+"e-6");else{if(g[0]!==i.TokenType.Dimension&&g[0]!==i.TokenType.Number)return;k=Number(Math.round(Number(g[4].value+u*s[l]+"e6"))+"e-6")}return M&&(g=[i.TokenType.Dimension,g[1],g[2],g[3],{value:g[4].value,unit:M,type:g[4].type}]),g[4].value=k,g[0]===i.TokenType.Dimension?g[1]=g[4].value.toString()+g[4].unit:g[1]=g[4].value.toString(),n.newMediaFeaturePlain(featureNamePrefix(l)+a,...T,g,...f,...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(t){return t.map(((t,a)=>{const r=e.gatherNodeAncestry(t);t.walk((e=>{const a=e.node;if(!n.isMediaFeatureRange(a))return;const o=e.parent;if(!n.isMediaFeature(o))return;const s=toLowerCaseAZ(a.name.getName());if(!l.has(s))return;if(n.isMediaFeatureRangeNameValue(a)||n.isMediaFeatureRangeValueName(a)){const e=a.operatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,a.value,n.isMediaFeatureRangeNameValue(a));return void(i&&(o.feature=i.feature))}const u=r.get(o);if(!n.isMediaInParens(u))return;let d=null,c=null;{const e=a.valueOneOperatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,a.valueOne,!1);if(!i)return;e===n.MediaFeatureLT.LT||e===n.MediaFeatureLT.LT_OR_EQ?(d=i,d.before=o.before):(c=i,c.after=o.after)}{const e=a.valueTwoOperatorKind();if(!1===e)return;const i=transformSingleNameValuePair(s,e,a.valueTwo,!0);if(!i)return;e===n.MediaFeatureLT.LT||e===n.MediaFeatureLT.LT_OR_EQ?(c=i,c.before=o.before):(d=i,d.after=o.after)}if(!d||!c)return;const p=new n.MediaInParens(d),T=new n.MediaInParens(c),m=getMediaConditionListWithAndFromAncestry(u,r);if(m)return m.leading===u?(m.leading=p,void(m.list=[new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T),...m.list])):void m.list.splice(m.indexOf(r.get(u)),1,new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],p),new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T));const v=new n.MediaConditionListWithAnd(p,[new n.MediaAnd([[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.Ident,"and",-1,-1,{value:"and"}],[i.TokenType.Whitespace," ",-1,-1,void 0]],T)],[[i.TokenType.Whitespace," ",-1,-1,void 0]]),f=getMediaConditionInShallowMediaQueryFromAncestry(u,t,r);f?f.media=v:u.media=new n.MediaCondition(new n.MediaInParens(new n.MediaCondition(v),[[i.TokenType.Whitespace," ",-1,-1,void 0],[i.TokenType.OpenParen,"(",-1,-1,void 0]],[[i.TokenType.CloseParen,")",-1,-1,void 0]]))}));const o=t.tokens();return i.stringify(...o.filter(((e,n)=>(0!==n||0!==a||e[0]!==i.TokenType.Whitespace)&&(e[0]!==i.TokenType.Whitespace||!o[n+1]||o[n+1][0]!==i.TokenType.Whitespace))))})).join(",")}function getMediaConditionListWithAndFromAncestry(e,i){let t=e;if(t){if(t=i.get(t),n.isMediaConditionListWithAnd(t))return t;if(n.isMediaAnd(t))return t=i.get(t),n.isMediaConditionListWithAnd(t)?t:void 0}}function getMediaConditionInShallowMediaQueryFromAncestry(e,i,t){let a=e;if(!a)return;if(a=t.get(a),!n.isMediaCondition(a))return;const r=a;return a=t.get(a),n.isMediaQuery(a)&&a===i?r:void 0}const creator=()=>({postcssPlugin:"postcss-media-minmax",AtRule:{media:e=>{if(!(e.params.includes("<")||e.params.includes(">")||e.params.includes("=")))return;const i=transform(n.parse(e.params,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw e.error(`Unable to parse media query "${e.params}"`)}}));e.params!==i&&(e.params=i)}}});creator.postcss=!0,module.exports=creator; diff --git a/plugins/postcss-media-minmax/dist/index.mjs b/plugins/postcss-media-minmax/dist/index.mjs index fe754e18d..313a00aca 100644 --- a/plugins/postcss-media-minmax/dist/index.mjs +++ b/plugins/postcss-media-minmax/dist/index.mjs @@ -1 +1 @@ -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 f,isMediaFeatureRange as m,isMediaFeature as v,isMediaFeatureRangeNameValue as p,isMediaFeatureRangeValueName as h,isMediaInParens as g,MediaInParens as w,MediaAnd as N,MediaConditionListWithAnd as b,MediaCondition as y,isMediaConditionListWithAnd as x,isMediaAnd as W,isMediaCondition as A,isMediaQuery as P,parse as C}from"@csstools/media-query-list-parser";import{mathFunctionNames as S,calcFromComponentValues as I}from"@csstools/css-calc";const L=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(L,(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"},T={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 _={">":1,"<":-1},M=.001,Q=.02;function transformSingleNameValuePair(r,a,m,v){let p=m.before,h=m.after;if(v||(p=m.after,h=m.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(m.value)?c(featureNamePrefix(a)+r,...p,...m.value.flatMap((e=>e.tokens())),...h):c(featureNamePrefix(a)+r,...p,...m.value.tokens(),...h);let g,w,N=!1;if(Array.isArray(m.value)){if(!d(m.value))return;if("aspect-ratio"!==r&&"device-aspect-ratio"!==r)return;const e=f(m.value);if(-1===e)return;N=!0,g=m.value[e[0]],w=[...m.value.slice(e[0]+1).flatMap((e=>e.tokens()))]}else g=m.value,w=[];const b=O[r.toLowerCase()];if(e(g)&&S.has(toLowerCaseAZ(g.getName()))){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!==b){const t=_[a]*("px"===b?Q:M);e=[i.Dimension,`${t.toString()}${b}`,-1,-1,{value:t,unit:b,type:n.Integer}]}else if(!0===T[r]){const t=_[a];e=[i.Number,t.toString(),-1,-1,{value:t,type:n.Integer}]}else if(N){const t=_[a]*M;e=[i.Number,t.toString(),-1,-1,{value:t,type:n.Integer}]}else{const 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 y,x=g.value,W="";if(void 0!==b&&x[0]===i.Number&&0===x[4].value)y=_[a],W=b;else if(x[0]===i.Number&&0===x[4].value)y=_[a],W="";else if(x[0]===i.Dimension&&0===x[4].value)y=_[a],W=x[4].unit;else if(x[0]===i.Number&&!0===T[r])y=x[4].value+_[a];else if(x[0]===i.Dimension&&"px"===x[4].unit&&x[4].type===n.Integer)y=Number(Math.round(Number(x[4].value+Q*_[a]+"e6"))+"e-6");else{if(x[0]!==i.Dimension&&x[0]!==i.Number)return;y=Number(Math.round(Number(x[4].value+M*_[a]+"e6"))+"e-6")}return W&&(x=[i.Dimension,x[1],x[2],x[3],{value:x[4].value,unit:W,type:x[4].type}]),x[4].value=y,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(!m(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),f=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]],f),...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]],f));const W=new b(d,[new N([[i.Whitespace," ",-1,-1,void 0],[i.Ident,"and",-1,-1,{value:"and"}],[i.Whitespace," ",-1,-1,void 0]],f)],[[i.Whitespace," ",-1,-1,void 0]]),A=getMediaConditionInShallowMediaQueryFromAncestry(s,e,n);A?A.media=W:s.media=new y(new w(new y(W),[[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(W(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),!A(i))return;const n=i;return i=r.get(i),P(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(C(e.params,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw e.error(`Unable to parse media query "${e.params}"`)}}));e.params!==t&&(e.params=t)}}});creator.postcss=!0;export{creator as default}; +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 f,isMediaFeatureRange as v,isMediaFeature as m,isMediaFeatureRangeNameValue as p,isMediaFeatureRangeValueName as h,isMediaInParens as g,MediaInParens as w,MediaAnd as N,MediaConditionListWithAnd as b,MediaCondition as y,isMediaConditionListWithAnd as x,isMediaAnd as W,isMediaCondition as A,isMediaQuery as P,parse as C}from"@csstools/media-query-list-parser";import{mathFunctionNames as S,calcFromComponentValues as I}from"@csstools/css-calc";const L=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(L,(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"},T={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 _={">":1,"<":-1},M=.001,Q=.02;function transformSingleNameValuePair(r,a,v,m){let p=v.before,h=v.after;if(m||(p=v.after,h=v.before),!m){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(v.value)?c(featureNamePrefix(a)+r,...p,...v.value.flatMap((e=>e.tokens())),...h):c(featureNamePrefix(a)+r,...p,...v.value.tokens(),...h);let g,w,N=!1;if(Array.isArray(v.value)){if(!d(v.value))return;if("aspect-ratio"!==r&&"device-aspect-ratio"!==r)return;const e=f(v.value);if(-1===e)return;N=!0,g=v.value[e[0]],w=[...v.value.slice(e[0]+1).flatMap((e=>e.tokens()))]}else g=v.value,w=[];const b=O[r.toLowerCase()];if(e(g)){const e=toLowerCaseAZ(g.getName());if(S.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!==b){const t=_[a]*("px"===b?Q:M);e=[i.Dimension,`${t.toString()}${b}`,-1,-1,{value:t,unit:b,type:n.Integer}]}else if(!0===T[r]){const t=_[a];e=[i.Number,t.toString(),-1,-1,{value:t,type:n.Integer}]}else if(N){const t=_[a]*M;e=[i.Number,t.toString(),-1,-1,{value:t,type:n.Integer}]}else{const 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 y,x=g.value,W="";if(void 0!==b&&x[0]===i.Number&&0===x[4].value)y=_[a],W=b;else if(x[0]===i.Number&&0===x[4].value)y=_[a],W="";else if(x[0]===i.Dimension&&0===x[4].value)y=_[a],W=x[4].unit;else if(x[0]===i.Number&&!0===T[r])y=x[4].value+_[a];else if(x[0]===i.Dimension&&"px"===x[4].unit&&x[4].type===n.Integer)y=Number(Math.round(Number(x[4].value+Q*_[a]+"e6"))+"e-6");else{if(x[0]!==i.Dimension&&x[0]!==i.Number)return;y=Number(Math.round(Number(x[4].value+M*_[a]+"e6"))+"e-6")}return W&&(x=[i.Dimension,x[1],x[2],x[3],{value:x[4].value,unit:W,type:x[4].type}]),x[4].value=y,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(!v(r))return;const a=t.parent;if(!m(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),f=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]],f),...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]],f));const W=new b(d,[new N([[i.Whitespace," ",-1,-1,void 0],[i.Ident,"and",-1,-1,{value:"and"}],[i.Whitespace," ",-1,-1,void 0]],f)],[[i.Whitespace," ",-1,-1,void 0]]),A=getMediaConditionInShallowMediaQueryFromAncestry(s,e,n);A?A.media=W:s.media=new y(new w(new y(W),[[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(W(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),!A(i))return;const n=i;return i=r.get(i),P(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(C(e.params,{preserveInvalidMediaQueries:!0,onParseError:()=>{throw e.error(`Unable to parse media query "${e.params}"`)}}));e.params!==t&&(e.params=t)}}});creator.postcss=!0;export{creator as default}; diff --git a/plugins/postcss-media-minmax/src/transform-single-pair.ts b/plugins/postcss-media-minmax/src/transform-single-pair.ts index 4c7c721b5..b4cbb1562 100644 --- a/plugins/postcss-media-minmax/src/transform-single-pair.ts +++ b/plugins/postcss-media-minmax/src/transform-single-pair.ts @@ -116,60 +116,63 @@ export function transformSingleNameValuePair(name: string, operator: MediaFeatur const featureUnit: string | undefined = unitsForFeature[name.toLowerCase()]; // 1. If the value is a calc() function, try to evaluate it. - if (isFunctionNode(valueNode) && mathFunctionNames.has(toLowerCaseAZ(valueNode.getName()))) { - const [[result]] = calcFromComponentValues([[valueNode]], { precision: 5, toCanonicalUnits: true }); - if ( - result && - isTokenNode(result) && - ( - result.value[0] === TokenType.Number || - result.value[0] === TokenType.Percentage || - result.value[0] === TokenType.Dimension - ) && - Number.isInteger(result.value[4].value) - ) { - // 1.a. If the result is an integer it is safe to use the result as the value and proceed as if there was no calc() function. - - valueNode = result; - } else { - // 1.b. If the result is not an integer, it is not safe to use the result as the value. - // Wrap the value in an additional calc() function and adjust with the appropriate step. - - let valueToken: CSSToken; - - if (typeof featureUnit !== 'undefined') { - const tokenValue = power[operator] * (featureUnit === 'px' ? pixelStep : step); - - valueToken = [TokenType.Dimension, `${tokenValue.toString()}${featureUnit}`, -1, -1, { value: tokenValue, unit: featureUnit, type: NumberType.Integer }]; - } else if (integerFeatures[name] === true) { - const tokenValue = power[operator]; - - valueToken = [TokenType.Number, tokenValue.toString(), -1, -1, { value: tokenValue, type: NumberType.Integer }]; - } else if (isRatio) { - const tokenValue = power[operator] * step; - - valueToken = [TokenType.Number, tokenValue.toString(), -1, -1, { value: tokenValue, type: NumberType.Integer }]; + if (isFunctionNode(valueNode)) { + const functionName = toLowerCaseAZ(valueNode.getName()); + if (mathFunctionNames.has(functionName) || functionName === 'env') { + const [[result]] = calcFromComponentValues([[valueNode]], { precision: 5, toCanonicalUnits: true }); + if ( + result && + isTokenNode(result) && + ( + result.value[0] === TokenType.Number || + result.value[0] === TokenType.Percentage || + result.value[0] === TokenType.Dimension + ) && + Number.isInteger(result.value[4].value) + ) { + // 1.a. If the result is an integer it is safe to use the result as the value and proceed as if there was no calc() function. + + valueNode = result; } else { - const tokenValue = power[operator]; - - valueToken = [TokenType.Number, tokenValue.toString(), -1, -1, { value: tokenValue, type: NumberType.Integer }]; + // 1.b. If the result is not an integer, it is not safe to use the result as the value. + // Wrap the value in an additional calc() function and adjust with the appropriate step. + + let valueToken: CSSToken; + + if (typeof featureUnit !== 'undefined') { + const tokenValue = power[operator] * (featureUnit === 'px' ? pixelStep : step); + + valueToken = [TokenType.Dimension, `${tokenValue.toString()}${featureUnit}`, -1, -1, { value: tokenValue, unit: featureUnit, type: NumberType.Integer }]; + } else if (integerFeatures[name] === true) { + const tokenValue = power[operator]; + + valueToken = [TokenType.Number, tokenValue.toString(), -1, -1, { value: tokenValue, type: NumberType.Integer }]; + } else if (isRatio) { + const tokenValue = power[operator] * step; + + valueToken = [TokenType.Number, tokenValue.toString(), -1, -1, { value: tokenValue, type: NumberType.Integer }]; + } else { + const tokenValue = power[operator]; + + valueToken = [TokenType.Number, tokenValue.toString(), -1, -1, { value: tokenValue, type: NumberType.Integer }]; + } + + // 1.c. return a new media feature with the adjusted value. + return newMediaFeaturePlain( + featureNamePrefix(operator) + name, + ...tokensBefore, + [TokenType.Function, 'calc(', -1, -1, { value: 'calc(' }], + [TokenType.OpenParen, '(', -1, -1, undefined], + ...valueNode.tokens().slice(1), + [TokenType.Whitespace, ' ', -1, -1, undefined], + [TokenType.Delim, '+', -1, -1, { value: '+' }], + [TokenType.Whitespace, ' ', -1, -1, undefined], + valueToken, + [TokenType.CloseParen, ')', -1, -1, undefined], + ...valueRemainder, + ...tokensAfter, + ); } - - // 1.c. return a new media feature with the adjusted value. - return newMediaFeaturePlain( - featureNamePrefix(operator) + name, - ...tokensBefore, - [TokenType.Function, 'calc(', -1, -1, { value: 'calc(' }], - [TokenType.OpenParen, '(', -1, -1, undefined], - ...valueNode.tokens().slice(1), - [TokenType.Whitespace, ' ', -1, -1, undefined], - [TokenType.Delim, '+', -1, -1, { value: '+' }], - [TokenType.Whitespace, ' ', -1, -1, undefined], - valueToken, - [TokenType.CloseParen, ')', -1, -1, undefined], - ...valueRemainder, - ...tokensAfter, - ); } } diff --git a/plugins/postcss-media-minmax/test/env.css b/plugins/postcss-media-minmax/test/env.css new file mode 100644 index 000000000..1f97478e6 --- /dev/null +++ b/plugins/postcss-media-minmax/test/env.css @@ -0,0 +1,7 @@ +@media (height > env(safe-area-inset-top)) {} + +@media (height >= env(safe-area-inset-top)) {} + +@media (height > calc(env(safe-area-inset-top) * 2)) {} + +@media (height >= calc(env(safe-area-inset-top) * 2)) {} diff --git a/plugins/postcss-media-minmax/test/env.expect.css b/plugins/postcss-media-minmax/test/env.expect.css new file mode 100644 index 000000000..b306c9e71 --- /dev/null +++ b/plugins/postcss-media-minmax/test/env.expect.css @@ -0,0 +1,7 @@ +@media (min-height: calc((safe-area-inset-top) + 0.02px)) {} + +@media (min-height: env(safe-area-inset-top)) {} + +@media (min-height: calc((env(safe-area-inset-top) * 2) + 0.02px)) {} + +@media (min-height: calc(env(safe-area-inset-top) * 2)) {}