diff --git a/src/lib.rs b/src/lib.rs index 30270f6..4a7916d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -160,7 +160,7 @@ pub fn from_path(p: &str) -> Result { .parse() .map_err(|e| raw_to_parse_error(&map, *e))?; - Css::from_stmts(stmts) + Css::from_stmts(stmts, false) .map_err(|e| raw_to_parse_error(&map, *e))? .pretty_print(&map) .map_err(|e| raw_to_parse_error(&map, *e)) @@ -205,7 +205,7 @@ pub fn from_string(p: String) -> Result { .parse() .map_err(|e| raw_to_parse_error(&map, *e))?; - Css::from_stmts(stmts) + Css::from_stmts(stmts, false) .map_err(|e| raw_to_parse_error(&map, *e))? .pretty_print(&map) .map_err(|e| raw_to_parse_error(&map, *e)) @@ -241,7 +241,7 @@ pub fn from_string(p: String) -> std::result::Result { .parse() .map_err(|e| raw_to_parse_error(&map, *e).to_string())?; - Ok(Css::from_stmts(stmts) + Ok(Css::from_stmts(stmts, false) .map_err(|e| raw_to_parse_error(&map, *e).to_string())? .pretty_print(&map) .map_err(|e| raw_to_parse_error(&map, *e).to_string())?) diff --git a/src/output.rs b/src/output.rs index af861e7..8c173b0 100644 --- a/src/output.rs +++ b/src/output.rs @@ -82,15 +82,19 @@ impl Toplevel { #[derive(Debug, Clone)] pub(crate) struct Css { blocks: Vec, + in_at_rule: bool, } impl Css { - pub const fn new() -> Self { - Css { blocks: Vec::new() } + pub const fn new(in_at_rule: bool) -> Self { + Css { + blocks: Vec::new(), + in_at_rule, + } } - pub(crate) fn from_stmts(s: Vec) -> SassResult { - Css::new().parse_stylesheet(s) + pub(crate) fn from_stmts(s: Vec, in_at_rule: bool) -> SassResult { + Css::new(in_at_rule).parse_stylesheet(s) } fn parse_stmt(&mut self, stmt: Stmt) -> SassResult> { @@ -232,7 +236,7 @@ impl Css { continue; } has_written = true; - if should_emit_newline { + if should_emit_newline && !self.in_at_rule { should_emit_newline = false; writeln!(buf)?; } @@ -247,10 +251,7 @@ impl Css { continue; } has_written = true; - if should_emit_newline { - should_emit_newline = false; - writeln!(buf)?; - } + writeln!( buf, "{}{} {{", @@ -290,7 +291,7 @@ impl Css { writeln!(buf, " {{")?; } - Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?; + Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?; writeln!(buf, "{}}}", padding)?; } Toplevel::Keyframes(k) => { @@ -313,7 +314,7 @@ impl Css { writeln!(buf, " {{")?; } - Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?; + Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?; writeln!(buf, "{}}}", padding)?; } Toplevel::Supports { params, body } => { @@ -335,19 +336,16 @@ impl Css { writeln!(buf, " {{")?; } - Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?; + Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?; writeln!(buf, "{}}}", padding)?; } Toplevel::Media { query, body } => { if body.is_empty() { continue; } - if should_emit_newline { - should_emit_newline = false; - writeln!(buf)?; - } + writeln!(buf, "{}@media {} {{", padding, query)?; - Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?; + Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?; writeln!(buf, "{}}}", padding)?; } Toplevel::Style(s) => { diff --git a/tests/keyframes.rs b/tests/keyframes.rs index 80b99ff..ea22d8b 100644 --- a/tests/keyframes.rs +++ b/tests/keyframes.rs @@ -109,3 +109,15 @@ test!( "@keyframes foo {/**/}", "@keyframes foo {\n /**/\n}\n" ); +test!( + keyframes_multiple_rulesets, + "@keyframes { + to { + color: red; + } + from { + color: green; + } + }", + "@keyframes {\n to {\n color: red;\n }\n from {\n color: green;\n }\n}\n" +); diff --git a/tests/media.rs b/tests/media.rs index 10a4e92..8d69c68 100644 --- a/tests/media.rs +++ b/tests/media.rs @@ -17,3 +17,17 @@ test!( "@media foo {\n a {\n color: red;\n }\n}\n" ); test!(empty_body, "@media (min-width: 2px) {}", ""); +test!( + newlines_are_not_emitted_for_child_styles, + "a { + @media screen { + b { + color: red; + } + c { + color: green; + } + } + }", + "@media screen {\n a b {\n color: red;\n }\n a c {\n color: green;\n }\n}\n" +);