From 548a5921e665f8b32490981bbbb25ee95bf51c2c Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Fri, 23 Jul 2021 03:28:41 -0400 Subject: [PATCH] encountering placeholder selector resets has_written --- src/output.rs | 7 ++++++- tests/comments.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/selectors.rs | 15 +++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/output.rs b/src/output.rs index 3f9f848..7a73f32 100644 --- a/src/output.rs +++ b/src/output.rs @@ -125,7 +125,7 @@ impl Css { let selector = selector.into_selector().remove_placeholders(); if selector.is_empty() { - return Ok(Vec::new()); + return Ok(vec![Toplevel::new_rule(selector)]); } let mut vals = vec![Toplevel::new_rule(selector)]; @@ -494,6 +494,11 @@ impl Formatter for ExpandedFormatter { for block in css.blocks { match block { Toplevel::RuleSet(selector, styles) => { + if selector.is_empty() { + has_written = false; + continue; + } + if styles.is_empty() { continue; } diff --git a/tests/comments.rs b/tests/comments.rs index 88a84ba..1cd763d 100644 --- a/tests/comments.rs +++ b/tests/comments.rs @@ -98,3 +98,45 @@ test!( /**/", "/**/\n/**/\n" ); +test!( + no_extra_newline_when_after_placeholder_selector, + r"a { + color: red; + + %b { + color: red; + } + } + + /**/ + + c { + color: red; + } + + /**/", + "a {\n color: red;\n}\n/**/\nc {\n color: red;\n}\n\n/**/\n" +); +test!( + extra_newline_when_after_regular_selector_after_placeholder_selector, + r"a { + color: red; + + %b { + color: red; + } + + d { + color: red; + } + } + + /**/ + + c { + color: red; + } + + /**/", + "a {\n color: red;\n}\na d {\n color: red;\n}\n\n/**/\nc {\n color: red;\n}\n\n/**/\n" +); diff --git a/tests/selectors.rs b/tests/selectors.rs index 4efdf50..a0600e3 100644 --- a/tests/selectors.rs +++ b/tests/selectors.rs @@ -829,6 +829,21 @@ test!( "[a=\"a\\\\66 \"] {\n color: &;\n}\n", "[a=\"a\\\\66 \"] {\n color: [a=\"a\\\\66 \"];\n}\n" ); +test!( + no_newline_between_styles_when_last_style_was_placeholder, + "a { + color: red; + + %b { + color: red; + } + } + + c { + color: red; + }", + "a {\n color: red;\n}\nc {\n color: red;\n}\n" +); error!( a_n_plus_b_n_invalid_odd, ":nth-child(ofdd) {\n color: &;\n}\n", "Error: Expected \"odd\"."