Skip to content

Commit 7413eac

Browse files
committed
Implement style container queries
Closes parcel-bundler#461
1 parent 81cd955 commit 7413eac

File tree

5 files changed

+527
-90
lines changed

5 files changed

+527
-90
lines changed

node/ast.d.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6835,6 +6835,56 @@ export type SyntaxComponentKind =
68356835
type: "literal";
68366836
value: string;
68376837
};
6838+
/**
6839+
* Represents a container condition.
6840+
*/
6841+
export type ContainerCondition =
6842+
| {
6843+
type: "feature";
6844+
value: MediaFeature;
6845+
}
6846+
| {
6847+
type: "not";
6848+
value: ContainerCondition;
6849+
}
6850+
| {
6851+
/**
6852+
* The conditions for the operator.
6853+
*/
6854+
conditions: ContainerCondition[];
6855+
/**
6856+
* The operator for the conditions.
6857+
*/
6858+
operator: Operator;
6859+
type: "operation";
6860+
}
6861+
| {
6862+
type: "style";
6863+
value: StyleQuery;
6864+
};
6865+
/**
6866+
* Represents a style query within a container condition.
6867+
*/
6868+
export type StyleQuery =
6869+
| {
6870+
type: "feature";
6871+
value: Declaration;
6872+
}
6873+
| {
6874+
type: "not";
6875+
value: StyleQuery;
6876+
}
6877+
| {
6878+
/**
6879+
* The conditions for the operator.
6880+
*/
6881+
conditions: StyleQuery[];
6882+
/**
6883+
* The operator for the conditions.
6884+
*/
6885+
operator: Operator;
6886+
type: "operation";
6887+
};
68386888
export type DefaultAtRule = null;
68396889

68406890
/**
@@ -8826,7 +8876,7 @@ export interface ContainerRule<D = Declaration> {
88268876
/**
88278877
* The container condition.
88288878
*/
8829-
condition: MediaCondition;
8879+
condition: ContainerCondition;
88308880
/**
88318881
* The location of the rule in the source file.
88328882
*/

src/lib.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23689,6 +23689,107 @@ mod tests {
2368923689
},
2369023690
);
2369123691

23692+
minify_test(
23693+
r#"
23694+
@container style(--responsive: true) {
23695+
.foo {
23696+
color: red;
23697+
}
23698+
}
23699+
"#,
23700+
"@container style(--responsive:true){.foo{color:red}}",
23701+
);
23702+
minify_test(
23703+
r#"
23704+
@container style(--responsive: true) and style(color: yellow) {
23705+
.foo {
23706+
color: red;
23707+
}
23708+
}
23709+
"#,
23710+
"@container style(--responsive:true) and style(color:#ff0){.foo{color:red}}",
23711+
);
23712+
minify_test(
23713+
r#"
23714+
@container not style(--responsive: true) {
23715+
.foo {
23716+
color: red;
23717+
}
23718+
}
23719+
"#,
23720+
"@container not style(--responsive:true){.foo{color:red}}",
23721+
);
23722+
minify_test(
23723+
r#"
23724+
@container (inline-size > 45em) and style(--responsive: true) {
23725+
.foo {
23726+
color: red;
23727+
}
23728+
}
23729+
"#,
23730+
"@container (inline-size>45em) and style(--responsive:true){.foo{color:red}}",
23731+
);
23732+
minify_test(
23733+
r#"
23734+
@container style((accent-color: yellow) or (--bar: 10px)) {
23735+
.foo {
23736+
color: red;
23737+
}
23738+
}
23739+
"#,
23740+
"@container style((accent-color:#ff0) or (--bar:10px)){.foo{color:red}}",
23741+
);
23742+
minify_test(
23743+
r#"
23744+
@container style(not ((width: calc(10px + 20px)) and ((--bar: url(x))))) {
23745+
.foo {
23746+
color: red;
23747+
}
23748+
}
23749+
"#,
23750+
"@container style(not ((width:30px) and (--bar:url(x)))){.foo{color:red}}",
23751+
);
23752+
minify_test(
23753+
r#"
23754+
@container style(color: yellow !important) {
23755+
.foo {
23756+
color: red;
23757+
}
23758+
}
23759+
"#,
23760+
"@container style(color:yellow){.foo{color:red}}",
23761+
);
23762+
minify_test(
23763+
r#"
23764+
@container style(--foo:) {
23765+
.foo {
23766+
color: red;
23767+
}
23768+
}
23769+
"#,
23770+
"@container style(--foo:){.foo{color:red}}",
23771+
);
23772+
minify_test(
23773+
r#"
23774+
@container style(--foo: ) {
23775+
.foo {
23776+
color: red;
23777+
}
23778+
}
23779+
"#,
23780+
"@container style(--foo:){.foo{color:red}}",
23781+
);
23782+
minify_test(
23783+
r#"
23784+
@container style(--my-prop: foo - bar ()) {
23785+
.foo {
23786+
color: red;
23787+
}
23788+
}
23789+
"#,
23790+
"@container style(--my-prop:foo - bar ()){.foo{color:red}}",
23791+
);
23792+
2369223793
// Disallow 'none', 'not', 'and', 'or' as a `<container-name>`
2369323794
// https://github.com/w3c/csswg-drafts/issues/7203#issuecomment-1144257312
2369423795
// https://chromium-review.googlesource.com/c/chromium/src/+/3698402
@@ -23724,6 +23825,12 @@ mod tests {
2372423825
"@container foo bar (width < 100vw) {}",
2372523826
ParserError::UnexpectedToken(crate::properties::custom::Token::Ident("bar".into())),
2372623827
);
23828+
23829+
error_test("@container style(width) {}", ParserError::EndOfInput);
23830+
error_test(
23831+
"@container style(style(--foo: bar)) {}",
23832+
ParserError::UnexpectedToken(crate::properties::custom::Token::Function("style".into())),
23833+
);
2372723834
}
2372823835

2372923836
#[test]

0 commit comments

Comments
 (0)