newline after @supports when nested inside style rule

This commit is contained in:
Connor Skees 2021-07-24 15:04:40 -04:00
parent fccf93cd96
commit 3ea5dd48b3
2 changed files with 49 additions and 5 deletions

View File

@ -43,6 +43,8 @@ enum Toplevel {
Supports { Supports {
params: String, params: String,
body: Vec<Stmt>, body: Vec<Stmt>,
inside_rule: bool,
is_group_end: bool,
}, },
// todo: do we actually need a toplevel style variant? // todo: do we actually need a toplevel style variant?
Style(Style), Style(Style),
@ -68,6 +70,11 @@ impl Toplevel {
is_group_end, is_group_end,
.. ..
} => *inside_rule && *is_group_end, } => *inside_rule && *is_group_end,
Toplevel::Supports {
inside_rule,
is_group_end,
..
} => *inside_rule && *is_group_end,
_ => false, _ => false,
} }
} }
@ -179,7 +186,12 @@ impl Css {
} }
Stmt::Supports(s) => { Stmt::Supports(s) => {
let SupportsRule { params, body } = *s; let SupportsRule { params, body } = *s;
vals.push(Toplevel::Supports { params, body }); vals.push(Toplevel::Supports {
params,
body,
inside_rule: true,
is_group_end: false,
});
} }
Stmt::UnknownAtRule(u) => { Stmt::UnknownAtRule(u) => {
let UnknownAtRule { let UnknownAtRule {
@ -231,7 +243,12 @@ impl Css {
} }
Stmt::Supports(s) => { Stmt::Supports(s) => {
let SupportsRule { params, body } = *s; let SupportsRule { params, body } = *s;
vec![Toplevel::Supports { params, body }] vec![Toplevel::Supports {
params,
body,
inside_rule: false,
is_group_end: false,
}]
} }
Stmt::UnknownAtRule(u) => { Stmt::UnknownAtRule(u) => {
let UnknownAtRule { let UnknownAtRule {
@ -270,6 +287,7 @@ impl Css {
match v.last_mut() { match v.last_mut() {
Some(Toplevel::RuleSet { is_group_end, .. }) Some(Toplevel::RuleSet { is_group_end, .. })
| Some(Toplevel::Supports { is_group_end, .. })
| Some(Toplevel::Media { is_group_end, .. }) => { | Some(Toplevel::Media { is_group_end, .. }) => {
*is_group_end = true; *is_group_end = true;
} }
@ -404,7 +422,7 @@ impl Formatter for CompressedFormatter {
self.write_css(buf, css, map)?; self.write_css(buf, css, map)?;
write!(buf, "}}")?; write!(buf, "}}")?;
} }
Toplevel::Supports { params, body } => { Toplevel::Supports { params, body, .. } => {
if params.is_empty() { if params.is_empty() {
write!(buf, "@supports")?; write!(buf, "@supports")?;
} else { } else {
@ -606,7 +624,12 @@ impl Formatter for ExpandedFormatter {
self.write_css(buf, css, map)?; self.write_css(buf, css, map)?;
write!(buf, "\n{}}}", padding)?; write!(buf, "\n{}}}", padding)?;
} }
Toplevel::Supports { params, body } => { Toplevel::Supports {
params,
body,
inside_rule,
..
} => {
if params.is_empty() { if params.is_empty() {
write!(buf, "{}@supports", padding)?; write!(buf, "{}@supports", padding)?;
} else { } else {
@ -620,7 +643,15 @@ impl Formatter for ExpandedFormatter {
} }
writeln!(buf, " {{")?; writeln!(buf, " {{")?;
let css = Css::from_stmts(body, AtRuleContext::None, css.allows_charset)?; let css = Css::from_stmts(
body,
if inside_rule {
AtRuleContext::Supports
} else {
AtRuleContext::None
},
css.allows_charset,
)?;
self.write_css(buf, css, map)?; self.write_css(buf, css, map)?;
write!(buf, "\n{}}}", padding)?; write!(buf, "\n{}}}", padding)?;
} }

View File

@ -50,3 +50,16 @@ test!(
}", }",
"@supports (position: sticky) {\n a {\n color: red;\n }\n\n @media (min-width: 576px) {\n a {\n color: red;\n }\n\n a {\n color: red;\n }\n }\n a {\n color: red;\n }\n}\n" "@supports (position: sticky) {\n a {\n color: red;\n }\n\n @media (min-width: 576px) {\n a {\n color: red;\n }\n\n a {\n color: red;\n }\n }\n a {\n color: red;\n }\n}\n"
); );
test!(
newline_after_supports_when_inside_style_rule,
"a {
@supports (position: sticky) {
color: red;
}
}
a {
color: red;
}",
"@supports (position: sticky) {\n a {\n color: red;\n }\n}\n\na {\n color: red;\n}\n"
);