Support @layer at rules

This commit is contained in:
Mario Carbajal 2024-01-25 17:21:32 -03:00
parent d1796f6f50
commit ac3dc8cb6b
3 changed files with 67 additions and 6 deletions

View File

@ -204,12 +204,19 @@ fn at_rule<'s>(input: &mut &'s str) -> PResult<Vec<CssFragment<'s>>> {
return Ok(vec![]);
}
if identifier == "media" {
cut_err(terminated(style_rule_block_contents, '}')).parse_next(input)
} else {
cut_err(terminated(unknown_block_contents, '}')).parse_next(input)?;
Ok(vec![])
match identifier {
"media" | "layer" => cut_err(terminated(style_rule_block_contents, '}')).parse_next(input),
_ => {
cut_err(terminated(unknown_block_contents, '}')).parse_next(input)?;
Ok(vec![])
}
}
// if identifier == "media" {
// cut_err(terminated(style_rule_block_contents, '}')).parse_next(input)
// } else {
// cut_err(terminated(unknown_block_contents, '}')).parse_next(input)?;
// Ok(vec![])
// }
}
fn unknown_block_contents<'s>(input: &mut &'s str) -> PResult<&'s str> {
@ -359,6 +366,35 @@ mod tests {
assert!(input.is_empty());
}
#[test]
fn test_at_rule_layer() {
let mut input = "@layer test {
.foo {
background-color: red;
}
.bar {
color: blue;
.baz {
color: green;
}
}
}";
let r = at_rule.parse_next(&mut input);
assert_eq!(
r,
Ok(vec![
CssFragment::Class("foo"),
CssFragment::Class("bar"),
CssFragment::Class("baz")
])
);
assert!(input.is_empty());
}
#[test]
fn test_top_level() {
let mut input = "// tool.module.scss
@ -376,6 +412,14 @@ mod tests {
}
}
@layer {
.foo {
color: blue;
}
}
@layer foo;
@debug 1+2 * 3==1+(2 * 3); // true
.container {
@ -401,6 +445,7 @@ mod tests {
Ok(vec![
CssFragment::Class("default_border"),
CssFragment::Class("foo"),
CssFragment::Class("foo"),
CssFragment::Class("container"),
CssFragment::Class("bar"),
])

View File

@ -67,4 +67,16 @@ $some-scss-variable: 10px; // Scss variable declarations in top scope.
// scss placeholder class should parse
%scss-class {
color: red;
}
}
@layer test-layer {
.style8 {
color: red;
}
}
@layer test-layer2;
@layer;
//eof

View File

@ -10,6 +10,8 @@ fn test_import_crate_style() {
assert_eq!(style::style4, "style4-a331da9");
assert_eq!(style::style5, "style5-a331da9");
assert_eq!(style::style6, "style6-a331da9");
assert_eq!(style::style7, "style7-a331da9");
assert_eq!(style::style8, "style8-a331da9");
assert_eq!(style::style_with_dashes, "style-with-dashes-a331da9");
assert_eq!(style::nested_style, "nested-style-a331da9");
@ -36,6 +38,8 @@ fn test_import_style() {
assert_eq!(style::style4, "style4-a331da9");
assert_eq!(style::style5, "style5-a331da9");
assert_eq!(style::style6, "style6-a331da9");
assert_eq!(style::style7, "style7-a331da9");
assert_eq!(style::style8, "style8-a331da9");
assert_eq!(style::style_with_dashes, "style-with-dashes-a331da9");
assert_eq!(style::nested_style, "nested-style-a331da9");