Skip to content

Commit 2284653

Browse files
committed
Fix parsing style container queries without a value
Closes #915
1 parent b153413 commit 2284653

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

node/ast.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -7453,9 +7453,13 @@ export type ContainerSizeFeatureId = "width" | "height" | "inline-size" | "block
74537453
* Represents a style query within a container condition.
74547454
*/
74557455
export type StyleQuery<D = Declaration> = | {
7456-
type: "feature";
7456+
type: "declaration";
74577457
value: D;
74587458
}
7459+
| {
7460+
type: "property";
7461+
value: PropertyId;
7462+
}
74597463
| {
74607464
type: "not";
74617465
value: StyleQuery<D>;

src/lib.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -28270,6 +28270,26 @@ mod tests {
2827028270
"#,
2827128271
"@container style(--my-prop:foo - bar ()){.foo{color:red}}",
2827228272
);
28273+
minify_test(
28274+
r#"
28275+
@container style(--test) {
28276+
.foo {
28277+
color: red;
28278+
}
28279+
}
28280+
"#,
28281+
"@container style(--test){.foo{color:red}}",
28282+
);
28283+
minify_test(
28284+
r#"
28285+
@container style(width) {
28286+
.foo {
28287+
color: red;
28288+
}
28289+
}
28290+
"#,
28291+
"@container style(width){.foo{color:red}}",
28292+
);
2827328293

2827428294
// Disallow 'none', 'not', 'and', 'or' as a `<container-name>`
2827528295
// https://github.com/w3c/csswg-drafts/issues/7203#issuecomment-1144257312
@@ -28310,7 +28330,6 @@ mod tests {
2831028330
error_test("@container (inline-size <= foo) {}", ParserError::InvalidMediaQuery);
2831128331
error_test("@container (orientation <= 10px) {}", ParserError::InvalidMediaQuery);
2831228332

28313-
error_test("@container style(width) {}", ParserError::EndOfInput);
2831428333
error_test(
2831528334
"@container style(style(--foo: bar)) {}",
2831628335
ParserError::UnexpectedToken(crate::properties::custom::Token::Function("style".into())),

src/rules/container.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ impl FeatureToCss for ContainerSizeFeatureId {
112112
)]
113113
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
114114
pub enum StyleQuery<'i> {
115-
/// A style feature, implicitly parenthesized.
115+
/// A property declaration.
116116
#[cfg_attr(feature = "serde", serde(borrow, with = "ValueWrapper::<Property>"))]
117-
Feature(Property<'i>),
117+
Declaration(Property<'i>),
118+
/// A property name, without a value.
119+
/// This matches if the property value is different from the initial value.
120+
#[cfg_attr(feature = "serde", serde(with = "ValueWrapper::<PropertyId>"))]
121+
Property(PropertyId<'i>),
118122
/// A negation of a condition.
119123
#[cfg_attr(feature = "visitor", skip_type)]
120124
#[cfg_attr(feature = "serde", serde(with = "ValueWrapper::<Box<StyleQuery>>"))]
@@ -170,11 +174,14 @@ impl<'i> QueryCondition<'i> for StyleQuery<'i> {
170174
#[inline]
171175
fn parse_feature<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
172176
let property_id = PropertyId::parse(input)?;
173-
input.expect_colon()?;
174-
input.skip_whitespace();
175-
let feature = Self::Feature(Property::parse(property_id, input, &Default::default())?);
176-
let _ = input.try_parse(|input| parse_important(input));
177-
Ok(feature)
177+
if input.try_parse(|input| input.expect_colon()).is_ok() {
178+
input.skip_whitespace();
179+
let feature = Self::Declaration(Property::parse(property_id, input, &Default::default())?);
180+
let _ = input.try_parse(|input| parse_important(input));
181+
Ok(feature)
182+
} else {
183+
Ok(Self::Property(property_id))
184+
}
178185
}
179186

180187
#[inline]
@@ -191,7 +198,7 @@ impl<'i> QueryCondition<'i> for StyleQuery<'i> {
191198
match self {
192199
StyleQuery::Not(_) => true,
193200
StyleQuery::Operation { operator, .. } => Some(*operator) != parent_operator,
194-
StyleQuery::Feature(_) => true,
201+
StyleQuery::Declaration(_) | StyleQuery::Property(_) => true,
195202
}
196203
}
197204
}
@@ -232,7 +239,8 @@ impl<'i> ToCss for StyleQuery<'i> {
232239
W: std::fmt::Write,
233240
{
234241
match *self {
235-
StyleQuery::Feature(ref f) => f.to_css(dest, false),
242+
StyleQuery::Declaration(ref f) => f.to_css(dest, false),
243+
StyleQuery::Property(ref f) => f.to_css(dest),
236244
StyleQuery::Not(ref c) => {
237245
dest.write_str("not ")?;
238246
to_css_with_parens_if_needed(&**c, dest, c.needs_parens(None, &dest.targets))

0 commit comments

Comments
 (0)