Skip to content

Commit a84fbdb

Browse files
committed
Parse media query features according to specified type
Fixes parcel-bundler#454
1 parent 7413eac commit a84fbdb

File tree

6 files changed

+569
-76
lines changed

6 files changed

+569
-76
lines changed

node/ast.d.ts

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export type MediaFeature =
123123
/**
124124
* The name of the feature.
125125
*/
126-
name: String;
126+
name: MediaFeatureName;
127127
type: "plain";
128128
/**
129129
* The feature value.
@@ -134,14 +134,14 @@ export type MediaFeature =
134134
/**
135135
* The name of the feature.
136136
*/
137-
name: String;
137+
name: MediaFeatureName;
138138
type: "boolean";
139139
}
140140
| {
141141
/**
142142
* The name of the feature.
143143
*/
144-
name: String;
144+
name: MediaFeatureName;
145145
/**
146146
* A comparator.
147147
*/
@@ -164,7 +164,7 @@ export type MediaFeature =
164164
/**
165165
* The name of the feature.
166166
*/
167-
name: String;
167+
name: MediaFeatureName;
168168
/**
169169
* A start value.
170170
*/
@@ -175,6 +175,51 @@ export type MediaFeature =
175175
startOperator: MediaFeatureComparison;
176176
type: "interval";
177177
};
178+
/**
179+
* A media feature name.
180+
*/
181+
export type MediaFeatureName = MediaFeatureId | String | String;
182+
/**
183+
* A media query feature identifier.
184+
*/
185+
export type MediaFeatureId =
186+
| "width"
187+
| "height"
188+
| "aspect-ratio"
189+
| "orientation"
190+
| "overflow-block"
191+
| "overflow-inline"
192+
| "horizontal-viewport-segments"
193+
| "vertical-viewport-segments"
194+
| "display-mode"
195+
| "resolution"
196+
| "scan"
197+
| "grid"
198+
| "update"
199+
| "environment-blending"
200+
| "color"
201+
| "color-index"
202+
| "monochrome"
203+
| "color-gamut"
204+
| "dynamic-range"
205+
| "inverted-colors"
206+
| "pointer"
207+
| "hover"
208+
| "any-pointer"
209+
| "any-hover"
210+
| "nav-controls"
211+
| "video-color-gamut"
212+
| "video-dynamic-range"
213+
| "scripting"
214+
| "prefers-reduced-motion"
215+
| "prefers-reduced-transparency"
216+
| "prefers-contrast"
217+
| "forced-colors"
218+
| "prefers-color-scheme"
219+
| "prefers-reduced-data"
220+
| "device-width"
221+
| "device-height"
222+
| "device-aspect-ratio";
178223
export type String = string;
179224
/**
180225
* [media feature value](https://drafts.csswg.org/mediaqueries/#typedef-mf-value) within a media query.
@@ -190,6 +235,14 @@ export type MediaFeatureValue =
190235
type: "number";
191236
value: number;
192237
}
238+
| {
239+
type: "integer";
240+
value: number;
241+
}
242+
| {
243+
type: "boolean";
244+
value: boolean;
245+
}
193246
| {
194247
type: "resolution";
195248
value: Resolution;

node/test/composeVisitors.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ test('environment variables', () => {
616616
])
617617
});
618618

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

622622
test('variables', () => {

node/test/visitor.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ test('env function', () => {
170170
}
171171
});
172172

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

176176
test('specific environment variables', () => {
@@ -212,7 +212,7 @@ test('specific environment variables', () => {
212212
}
213213
});
214214

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

218218
test('url', () => {

src/bundler.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ mod tests {
957957
.b {
958958
color: green;
959959
}
960-
960+
961961
.a {
962962
color: red;
963963
}
@@ -986,7 +986,7 @@ mod tests {
986986
color: green;
987987
}
988988
}
989-
989+
990990
.a {
991991
color: red;
992992
}
@@ -1015,7 +1015,7 @@ mod tests {
10151015
color: green;
10161016
}
10171017
}
1018-
1018+
10191019
.a {
10201020
color: red;
10211021
}
@@ -1046,7 +1046,7 @@ mod tests {
10461046
}
10471047
}
10481048
}
1049-
1049+
10501050
.a {
10511051
color: red;
10521052
}
@@ -1076,7 +1076,7 @@ mod tests {
10761076
color: green;
10771077
}
10781078
}
1079-
1079+
10801080
.a {
10811081
color: red;
10821082
}
@@ -1106,7 +1106,7 @@ mod tests {
11061106
color: green;
11071107
}
11081108
}
1109-
1109+
11101110
.a {
11111111
color: red;
11121112
}
@@ -1139,7 +1139,7 @@ mod tests {
11391139
color: green;
11401140
}
11411141
}
1142-
1142+
11431143
@media print {
11441144
.b {
11451145
color: #ff0;
@@ -1203,7 +1203,7 @@ mod tests {
12031203
.b {
12041204
color: green;
12051205
}
1206-
1206+
12071207
.a {
12081208
color: red;
12091209
}
@@ -1230,7 +1230,7 @@ mod tests {
12301230
.b {
12311231
color: green;
12321232
}
1233-
1233+
12341234
.a {
12351235
color: red;
12361236
}
@@ -1294,7 +1294,7 @@ mod tests {
12941294
color: green;
12951295
}
12961296
}
1297-
1297+
12981298
.a {
12991299
color: red;
13001300
}
@@ -1323,7 +1323,7 @@ mod tests {
13231323
color: green;
13241324
}
13251325
}
1326-
1326+
13271327
.a {
13281328
color: red;
13291329
}
@@ -1362,7 +1362,7 @@ mod tests {
13621362
color: green;
13631363
}
13641364
}
1365-
1365+
13661366
.a {
13671367
color: red;
13681368
}
@@ -1400,7 +1400,7 @@ mod tests {
14001400
"/a.css": r#"
14011401
@layer bar, foo;
14021402
@import "b.css" layer(foo);
1403-
1403+
14041404
@layer bar {
14051405
div {
14061406
background: red;
@@ -1410,7 +1410,7 @@ mod tests {
14101410
"/b.css": r#"
14111411
@layer qux, baz;
14121412
@import "c.css" layer(baz);
1413-
1413+
14141414
@layer qux {
14151415
div {
14161416
background: green;
@@ -1420,7 +1420,7 @@ mod tests {
14201420
"/c.css": r#"
14211421
div {
14221422
background: yellow;
1423-
}
1423+
}
14241424
"#
14251425
},
14261426
},
@@ -1445,7 +1445,7 @@ mod tests {
14451445
}
14461446
}
14471447
}
1448-
1448+
14491449
@layer bar {
14501450
div {
14511451
background: red;
@@ -1479,20 +1479,20 @@ mod tests {
14791479
assert_eq!(
14801480
res,
14811481
indoc! { r#"
1482-
@media (min-width: 1000px) {
1482+
@media (width >= 1000px) {
14831483
@layer bar {
14841484
#box {
14851485
background: green;
14861486
}
14871487
}
14881488
}
1489-
1489+
14901490
@layer baz {
14911491
#box {
14921492
background: purple;
14931493
}
14941494
}
1495-
1495+
14961496
@layer bar {
14971497
#box {
14981498
background: #ff0;
@@ -1599,7 +1599,7 @@ mod tests {
15991599
"/c.css": r#"
16001600
body {
16011601
background: white;
1602-
color: black;
1602+
color: black;
16031603
}
16041604
"#
16051605
},
@@ -1886,7 +1886,7 @@ mod tests {
18861886
--_8Cs9ZG_fallback: yellow;
18871887
--_8Cs9ZG_opacity: .5;
18881888
}
1889-
1889+
18901890
.GbJUva_env {
18911891
--GbJUva_env-fallback: 20px;
18921892
}
@@ -1965,16 +1965,16 @@ mod tests {
19651965
let source = r#".imported {
19661966
content: "yay, file support!";
19671967
}
1968-
1968+
19691969
.selector {
19701970
margin: 1em;
19711971
background-color: #f60;
19721972
}
1973-
1973+
19741974
.selector .nested {
19751975
margin: 0.5em;
19761976
}
1977-
1977+
19781978
/*# sourceMappingURL=data:application/json;base64,ewoJInZlcnNpb24iOiAzLAoJInNvdXJjZVJvb3QiOiAicm9vdCIsCgkiZmlsZSI6ICJzdGRvdXQiLAoJInNvdXJjZXMiOiBbCgkJInN0ZGluIiwKCQkic2Fzcy9fdmFyaWFibGVzLnNjc3MiLAoJCSJzYXNzL19kZW1vLnNjc3MiCgldLAoJInNvdXJjZXNDb250ZW50IjogWwoJCSJAaW1wb3J0IFwiX3ZhcmlhYmxlc1wiO1xuQGltcG9ydCBcIl9kZW1vXCI7XG5cbi5zZWxlY3RvciB7XG4gIG1hcmdpbjogJHNpemU7XG4gIGJhY2tncm91bmQtY29sb3I6ICRicmFuZENvbG9yO1xuXG4gIC5uZXN0ZWQge1xuICAgIG1hcmdpbjogJHNpemUgLyAyO1xuICB9XG59IiwKCQkiJGJyYW5kQ29sb3I6ICNmNjA7XG4kc2l6ZTogMWVtOyIsCgkJIi5pbXBvcnRlZCB7XG4gIGNvbnRlbnQ6IFwieWF5LCBmaWxlIHN1cHBvcnQhXCI7XG59IgoJXSwKCSJtYXBwaW5ncyI6ICJBRUFBLFNBQVMsQ0FBQztFQUNSLE9BQU8sRUFBRSxvQkFBcUI7Q0FDL0I7O0FGQ0QsU0FBUyxDQUFDO0VBQ1IsTUFBTSxFQ0hELEdBQUc7RURJUixnQkFBZ0IsRUNMTCxJQUFJO0NEVWhCOztBQVBELFNBQVMsQ0FJUCxPQUFPLENBQUM7RUFDTixNQUFNLEVDUEgsS0FBRztDRFFQIiwKCSJuYW1lcyI6IFtdCn0= */"#;
19791979

19801980
let fs = TestProvider {

0 commit comments

Comments
 (0)