Skip to content

Commit

Permalink
add detection for env functions (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke authored Apr 9, 2023
1 parent 51b3bc6 commit 571f800
Show file tree
Hide file tree
Showing 17 changed files with 515 additions and 58 deletions.
3 changes: 2 additions & 1 deletion packages/media-query-list-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion packages/media-query-list-parser/dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/media-query-list-parser/dist/index.mjs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import { ComponentValue } from '@csstools/css-parser-algorithms';
export declare function isNumber(componentValue: ComponentValue): boolean;
export declare function isDimension(componentValue: ComponentValue): boolean;
export declare function isIdent(componentValue: ComponentValue): boolean;
export declare function isEnvironmentVariable(componentValue: ComponentValue): boolean;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentValue, ComponentValueType, ContainerNode } from '@csstools/css-parser-algorithms';
import { CSSToken, stringify, TokenType } from '@csstools/css-tokenizer';
import { isDimension, isIdent, isNumber } from '../util/component-value-is';
import { isDimension, isEnvironmentVariable, isIdent, isNumber } from '../util/component-value-is';
import { NodeType } from '../util/node-type';

export class MediaFeatureValue {
Expand Down Expand Up @@ -137,6 +137,12 @@ export function parseMediaFeatureValue(componentValues: Array<ComponentValue>):
continue;
}

if (isEnvironmentVariable(componentValue)) {
candidateIndexStart = i;
candidateIndexEnd = i;
continue;
}

if (isDimension(componentValue)) {
candidateIndexStart = i;
candidateIndexEnd = i;
Expand Down
10 changes: 10 additions & 0 deletions packages/media-query-list-parser/src/util/component-value-is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
187 changes: 187 additions & 0 deletions packages/media-query-list-parser/test/cases/mf-plain/0009.expect.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
}
]
13 changes: 13 additions & 0 deletions packages/media-query-list-parser/test/cases/mf-plain/0009.mjs
Original file line number Diff line number Diff line change
@@ -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,
);
},
);
Loading

0 comments on commit 571f800

Please sign in to comment.