diff --git a/src/output.rs b/src/output.rs index a36a9a6..fce2fdf 100644 --- a/src/output.rs +++ b/src/output.rs @@ -230,28 +230,48 @@ impl Css { } fn parse_stylesheet(mut self, stmts: Vec) -> SassResult { + #[derive(PartialEq, Eq, Clone, Copy, Debug)] + enum ToplevelKind { + Comment, + Media, + Other, + } + let mut is_first = true; - let mut last_was_comment = false; + let mut last_toplevel = ToplevelKind::Other; + for stmt in stmts { let v = self.parse_stmt(stmt)?; // this is how we print newlines between unrelated styles // it could probably be refactored if !v.is_empty() { - if matches!(v.first(), Some(Toplevel::MultilineComment(..))) { - if !last_was_comment && !is_first { - self.blocks.push(Toplevel::Newline); - } + match v.first() { + Some(Toplevel::MultilineComment(..)) => { + if last_toplevel != ToplevelKind::Comment && !is_first { + self.blocks.push(Toplevel::Newline); + } - last_was_comment = true; - } else if is_first { - last_was_comment = false; - } else { - if !last_was_comment { - self.blocks.push(Toplevel::Newline); + last_toplevel = ToplevelKind::Comment; } + Some(Toplevel::Media { .. }) => { + if last_toplevel != ToplevelKind::Media && !is_first { + self.blocks.push(Toplevel::Newline); + } - last_was_comment = false; + last_toplevel = ToplevelKind::Media; + } + _ if is_first => { + last_toplevel = ToplevelKind::Other; + } + _ => { + if last_toplevel != ToplevelKind::Comment { + self.blocks.push(Toplevel::Newline); + } + + last_toplevel = ToplevelKind::Other; + } } + is_first = false; self.blocks.extend(v); } diff --git a/tests/media.rs b/tests/media.rs index a05573a..b4dea5f 100644 --- a/tests/media.rs +++ b/tests/media.rs @@ -170,6 +170,25 @@ test!( }", "@media (true) {\n a {\n color: red;\n }\n}\n" ); +test!( + no_newline_between_two_media_following_ruleset, + "a { + color: red; + } + + @media (min-width: 0px) { + a { + color: red; + } + } + + @media (min-width: 0px) { + a { + color: red; + } + }", + "a {\n color: red;\n}\n\n@media (min-width: 0px) {\n a {\n color: red;\n }\n}\n@media (min-width: 0px) {\n a {\n color: red;\n }\n}\n" +); error!( media_feature_missing_closing_paren,