From a6eb1fb88a5cdf0be6ebf3aa50fc3247ed63df6c Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sun, 19 Sep 2021 16:31:34 -0400 Subject: [PATCH] properly emit 0 in compressed mode --- src/value/number/mod.rs | 15 ++++++++++++--- tests/compressed.rs | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/value/number/mod.rs b/src/value/number/mod.rs index f0e7bee..7a38f4e 100644 --- a/src/value/number/mod.rs +++ b/src/value/number/mod.rs @@ -444,15 +444,24 @@ impl Number { } } - if self.is_negative() && (!whole.is_zero() || !dec.is_empty()) { + let has_decimal = !dec.is_empty(); + + if self.is_negative() && (!whole.is_zero() || has_decimal) { buf.push('-'); } - if !(whole.is_zero() && is_compressed) { + // if the entire number is just zero, we always want to emit it + if whole.is_zero() && !has_decimal { + return "0".to_owned(); + + // otherwise, if the number is not 0, or the number before the decimal + // _is_ 0 and we aren't in compressed mode, emit the number before the + // decimal + } else if !(whole.is_zero() && is_compressed) { buf.push_str(&whole.to_string()); } - if !dec.is_empty() { + if has_decimal { buf.push('.'); buf.push_str(&dec); } diff --git a/tests/compressed.rs b/tests/compressed.rs index e8fc259..de16c72 100644 --- a/tests/compressed.rs +++ b/tests/compressed.rs @@ -116,3 +116,9 @@ test!( "a{color:grayscale(0.5)}", grass::Options::default().style(grass::OutputStyle::Compressed) ); +test!( + retains_zero_without_decimal, + "a {\n color: 0.0;\n color: 0;\n}\n", + "a{color:0;color:0}", + grass::Options::default().style(grass::OutputStyle::Compressed) +);