Skip to content

Commit

Permalink
Parse media query features according to specified type
Browse files Browse the repository at this point in the history
Fixes #454
  • Loading branch information
devongovett committed Apr 15, 2023
1 parent 7413eac commit a84fbdb
Show file tree
Hide file tree
Showing 6 changed files with 569 additions and 76 deletions.
61 changes: 57 additions & 4 deletions node/ast.d.ts
Expand Up @@ -123,7 +123,7 @@ export type MediaFeature =
/**
* The name of the feature.
*/
name: String;
name: MediaFeatureName;
type: "plain";
/**
* The feature value.
Expand All @@ -134,14 +134,14 @@ export type MediaFeature =
/**
* The name of the feature.
*/
name: String;
name: MediaFeatureName;
type: "boolean";
}
| {
/**
* The name of the feature.
*/
name: String;
name: MediaFeatureName;
/**
* A comparator.
*/
Expand All @@ -164,7 +164,7 @@ export type MediaFeature =
/**
* The name of the feature.
*/
name: String;
name: MediaFeatureName;
/**
* A start value.
*/
Expand All @@ -175,6 +175,51 @@ export type MediaFeature =
startOperator: MediaFeatureComparison;
type: "interval";
};
/**
* A media feature name.
*/
export type MediaFeatureName = MediaFeatureId | String | String;
/**
* A media query feature identifier.
*/
export type MediaFeatureId =
| "width"
| "height"
| "aspect-ratio"
| "orientation"
| "overflow-block"
| "overflow-inline"
| "horizontal-viewport-segments"
| "vertical-viewport-segments"
| "display-mode"
| "resolution"
| "scan"
| "grid"
| "update"
| "environment-blending"
| "color"
| "color-index"
| "monochrome"
| "color-gamut"
| "dynamic-range"
| "inverted-colors"
| "pointer"
| "hover"
| "any-pointer"
| "any-hover"
| "nav-controls"
| "video-color-gamut"
| "video-dynamic-range"
| "scripting"
| "prefers-reduced-motion"
| "prefers-reduced-transparency"
| "prefers-contrast"
| "forced-colors"
| "prefers-color-scheme"
| "prefers-reduced-data"
| "device-width"
| "device-height"
| "device-aspect-ratio";
export type String = string;
/**
* [media feature value](https://drafts.csswg.org/mediaqueries/#typedef-mf-value) within a media query.
Expand All @@ -190,6 +235,14 @@ export type MediaFeatureValue =
type: "number";
value: number;
}
| {
type: "integer";
value: number;
}
| {
type: "boolean";
value: boolean;
}
| {
type: "resolution";
value: Resolution;
Expand Down
2 changes: 1 addition & 1 deletion node/test/composeVisitors.test.mjs
Expand Up @@ -616,7 +616,7 @@ test('environment variables', () => {
])
});

assert.equal(res.code.toString(), '@media (max-width:600px){body{padding:20px}}');
assert.equal(res.code.toString(), '@media (width<=600px){body{padding:20px}}');
});

test('variables', () => {
Expand Down
4 changes: 2 additions & 2 deletions node/test/visitor.test.mjs
Expand Up @@ -170,7 +170,7 @@ test('env function', () => {
}
});

assert.equal(res.code.toString(), '@media (max-width:600px){body{padding:20px}}');
assert.equal(res.code.toString(), '@media (width<=600px){body{padding:20px}}');
});

test('specific environment variables', () => {
Expand Down Expand Up @@ -212,7 +212,7 @@ test('specific environment variables', () => {
}
});

assert.equal(res.code.toString(), '@media (max-width:600px){body{padding:20px}}');
assert.equal(res.code.toString(), '@media (width<=600px){body{padding:20px}}');
});

test('url', () => {
Expand Down
48 changes: 24 additions & 24 deletions src/bundler.rs
Expand Up @@ -957,7 +957,7 @@ mod tests {
.b {
color: green;
}
.a {
color: red;
}
Expand Down Expand Up @@ -986,7 +986,7 @@ mod tests {
color: green;
}
}
.a {
color: red;
}
Expand Down Expand Up @@ -1015,7 +1015,7 @@ mod tests {
color: green;
}
}
.a {
color: red;
}
Expand Down Expand Up @@ -1046,7 +1046,7 @@ mod tests {
}
}
}
.a {
color: red;
}
Expand Down Expand Up @@ -1076,7 +1076,7 @@ mod tests {
color: green;
}
}
.a {
color: red;
}
Expand Down Expand Up @@ -1106,7 +1106,7 @@ mod tests {
color: green;
}
}
.a {
color: red;
}
Expand Down Expand Up @@ -1139,7 +1139,7 @@ mod tests {
color: green;
}
}
@media print {
.b {
color: #ff0;
Expand Down Expand Up @@ -1203,7 +1203,7 @@ mod tests {
.b {
color: green;
}
.a {
color: red;
}
Expand All @@ -1230,7 +1230,7 @@ mod tests {
.b {
color: green;
}
.a {
color: red;
}
Expand Down Expand Up @@ -1294,7 +1294,7 @@ mod tests {
color: green;
}
}
.a {
color: red;
}
Expand Down Expand Up @@ -1323,7 +1323,7 @@ mod tests {
color: green;
}
}
.a {
color: red;
}
Expand Down Expand Up @@ -1362,7 +1362,7 @@ mod tests {
color: green;
}
}
.a {
color: red;
}
Expand Down Expand Up @@ -1400,7 +1400,7 @@ mod tests {
"/a.css": r#"
@layer bar, foo;
@import "b.css" layer(foo);
@layer bar {
div {
background: red;
Expand All @@ -1410,7 +1410,7 @@ mod tests {
"/b.css": r#"
@layer qux, baz;
@import "c.css" layer(baz);
@layer qux {
div {
background: green;
Expand All @@ -1420,7 +1420,7 @@ mod tests {
"/c.css": r#"
div {
background: yellow;
}
}
"#
},
},
Expand All @@ -1445,7 +1445,7 @@ mod tests {
}
}
}
@layer bar {
div {
background: red;
Expand Down Expand Up @@ -1479,20 +1479,20 @@ mod tests {
assert_eq!(
res,
indoc! { r#"
@media (min-width: 1000px) {
@media (width >= 1000px) {
@layer bar {
#box {
background: green;
}
}
}
@layer baz {
#box {
background: purple;
}
}
@layer bar {
#box {
background: #ff0;
Expand Down Expand Up @@ -1599,7 +1599,7 @@ mod tests {
"/c.css": r#"
body {
background: white;
color: black;
color: black;
}
"#
},
Expand Down Expand Up @@ -1886,7 +1886,7 @@ mod tests {
--_8Cs9ZG_fallback: yellow;
--_8Cs9ZG_opacity: .5;
}
.GbJUva_env {
--GbJUva_env-fallback: 20px;
}
Expand Down Expand Up @@ -1965,16 +1965,16 @@ mod tests {
let source = r#".imported {
content: "yay, file support!";
}
.selector {
margin: 1em;
background-color: #f60;
}
.selector .nested {
margin: 0.5em;
}
/*# sourceMappingURL=data:application/json;base64,ewoJInZlcnNpb24iOiAzLAoJInNvdXJjZVJvb3QiOiAicm9vdCIsCgkiZmlsZSI6ICJzdGRvdXQiLAoJInNvdXJjZXMiOiBbCgkJInN0ZGluIiwKCQkic2Fzcy9fdmFyaWFibGVzLnNjc3MiLAoJCSJzYXNzL19kZW1vLnNjc3MiCgldLAoJInNvdXJjZXNDb250ZW50IjogWwoJCSJAaW1wb3J0IFwiX3ZhcmlhYmxlc1wiO1xuQGltcG9ydCBcIl9kZW1vXCI7XG5cbi5zZWxlY3RvciB7XG4gIG1hcmdpbjogJHNpemU7XG4gIGJhY2tncm91bmQtY29sb3I6ICRicmFuZENvbG9yO1xuXG4gIC5uZXN0ZWQge1xuICAgIG1hcmdpbjogJHNpemUgLyAyO1xuICB9XG59IiwKCQkiJGJyYW5kQ29sb3I6ICNmNjA7XG4kc2l6ZTogMWVtOyIsCgkJIi5pbXBvcnRlZCB7XG4gIGNvbnRlbnQ6IFwieWF5LCBmaWxlIHN1cHBvcnQhXCI7XG59IgoJXSwKCSJtYXBwaW5ncyI6ICJBRUFBLFNBQVMsQ0FBQztFQUNSLE9BQU8sRUFBRSxvQkFBcUI7Q0FDL0I7O0FGQ0QsU0FBUyxDQUFDO0VBQ1IsTUFBTSxFQ0hELEdBQUc7RURJUixnQkFBZ0IsRUNMTCxJQUFJO0NEVWhCOztBQVBELFNBQVMsQ0FJUCxPQUFPLENBQUM7RUFDTixNQUFNLEVDUEgsS0FBRztDRFFQIiwKCSJuYW1lcyI6IFtdCn0= */"#;

let fs = TestProvider {
Expand Down

0 comments on commit a84fbdb

Please sign in to comment.