Skip to content

Commit

Permalink
Disallow { in standard properties to avoid ambiguity in nested rules
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Sep 17, 2023
1 parent a8b7ea0 commit e674bc5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/declaration.rs
Expand Up @@ -8,6 +8,7 @@ use crate::error::{ParserError, PrinterError};
use crate::parser::ParserOptions;
use crate::printer::Printer;
use crate::properties::box_shadow::BoxShadowHandler;
use crate::properties::custom::CustomPropertyName;
use crate::properties::masking::MaskHandler;
use crate::properties::{
align::AlignHandler,
Expand Down Expand Up @@ -456,15 +457,22 @@ pub(crate) fn parse_declaration<'i, 't>(
important_declarations: &mut DeclarationList<'i>,
options: &ParserOptions<'_, 'i>,
) -> Result<(), cssparser::ParseError<'i, ParserError<'i>>> {
let property = input.parse_until_before(Delimiter::Bang, |input| {
Property::parse(PropertyId::from(CowArcStr::from(name)), input, options)
})?;
// Stop if we hit a `{` token in a non-custom property to
// avoid ambiguity between nested rules and declarations.
// https://github.com/w3c/csswg-drafts/issues/9317
let property_id = PropertyId::from(CowArcStr::from(name));
let mut delimiters = Delimiter::Bang;
if !matches!(property_id, PropertyId::Custom(CustomPropertyName::Custom(..))) {
delimiters = delimiters | Delimiter::CurlyBracketBlock;
}
let property = input.parse_until_before(delimiters, |input| Property::parse(property_id, input, options))?;
let important = input
.try_parse(|input| {
input.expect_delim('!')?;
input.expect_ident_matching("important")
})
.is_ok();
input.expect_exhausted()?;
if important {
important_declarations.push(property);
} else {
Expand Down
38 changes: 38 additions & 0 deletions src/lib.rs
Expand Up @@ -22428,6 +22428,44 @@ mod tests {
"#},
);

nesting_test(
r#"
div {
color: blue;

button:focus {
color: red;
}
}
"#,
indoc! {r#"
div {
color: #00f;
}

div button:focus {
color: red;
}
"#},
);
nesting_test(
r#"
div {
color: blue;

--button:focus {
color: red;
}
}
"#,
indoc! {r#"
div {
color: #00f;
--button: focus { color: red; };
}
"#},
);

nesting_test_no_targets(
r#"
.foo {
Expand Down

0 comments on commit e674bc5

Please sign in to comment.