From 3ea5dd48b3b45267b452bae93f795bfb58efa214 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sat, 24 Jul 2021 15:04:40 -0400 Subject: [PATCH] newline after `@supports` when nested inside style rule --- src/output.rs | 41 ++++++++++++++++++++++++++++++++++++----- tests/supports.rs | 13 +++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/output.rs b/src/output.rs index 21a6c74..bde2e71 100644 --- a/src/output.rs +++ b/src/output.rs @@ -43,6 +43,8 @@ enum Toplevel { Supports { params: String, body: Vec, + inside_rule: bool, + is_group_end: bool, }, // todo: do we actually need a toplevel style variant? Style(Style), @@ -68,6 +70,11 @@ impl Toplevel { is_group_end, .. } => *inside_rule && *is_group_end, + Toplevel::Supports { + inside_rule, + is_group_end, + .. + } => *inside_rule && *is_group_end, _ => false, } } @@ -179,7 +186,12 @@ impl Css { } Stmt::Supports(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) => { let UnknownAtRule { @@ -231,7 +243,12 @@ impl Css { } Stmt::Supports(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) => { let UnknownAtRule { @@ -270,6 +287,7 @@ impl Css { match v.last_mut() { Some(Toplevel::RuleSet { is_group_end, .. }) + | Some(Toplevel::Supports { is_group_end, .. }) | Some(Toplevel::Media { is_group_end, .. }) => { *is_group_end = true; } @@ -404,7 +422,7 @@ impl Formatter for CompressedFormatter { self.write_css(buf, css, map)?; write!(buf, "}}")?; } - Toplevel::Supports { params, body } => { + Toplevel::Supports { params, body, .. } => { if params.is_empty() { write!(buf, "@supports")?; } else { @@ -606,7 +624,12 @@ impl Formatter for ExpandedFormatter { self.write_css(buf, css, map)?; write!(buf, "\n{}}}", padding)?; } - Toplevel::Supports { params, body } => { + Toplevel::Supports { + params, + body, + inside_rule, + .. + } => { if params.is_empty() { write!(buf, "{}@supports", padding)?; } else { @@ -620,7 +643,15 @@ impl Formatter for ExpandedFormatter { } 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)?; write!(buf, "\n{}}}", padding)?; } diff --git a/tests/supports.rs b/tests/supports.rs index 5b091e6..b17ef55 100644 --- a/tests/supports.rs +++ b/tests/supports.rs @@ -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" ); +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" +);