From 9b9946c0ee3003bbbb79e379e255e80a0d98719a Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Mon, 17 Aug 2020 03:58:29 -0400 Subject: [PATCH] allow units beginning with a single `-` --- src/parse/value/parse.rs | 16 ++++++++++++++++ tests/units.rs | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/parse/value/parse.rs b/src/parse/value/parse.rs index 47ee279..7bc2f25 100644 --- a/src/parse/value/parse.rs +++ b/src/parse/value/parse.rs @@ -476,6 +476,22 @@ impl<'a> Parser<'a> { span = span.merge(u.span); Unit::from(u.node) } + '-' => { + if let Some(Token { kind, .. }) = self.toks.peek_next().cloned() { + self.toks.reset_cursor(); + if matches!(kind, 'a'..='z' | 'A'..='Z' | '_' | '\\' | '\u{7f}'..=std::char::MAX) + { + let u = self.parse_identifier_no_interpolation(true)?; + span = span.merge(u.span); + Unit::from(u.node) + } else { + Unit::None + } + } else { + self.toks.reset_cursor(); + Unit::None + } + } '%' => { span = span.merge(self.toks.next().unwrap().pos()); Unit::Percent diff --git a/tests/units.rs b/tests/units.rs index 886c5f9..8bb8038 100644 --- a/tests/units.rs +++ b/tests/units.rs @@ -127,6 +127,31 @@ test!( "a {\n color: unit((1 / 1in) * 1in);\n}\n", "a {\n color: \"\";\n}\n" ); +test!( + unit_begins_with_single_hyphen, + "a {\n color: unit(1-em);\n}\n", + "a {\n color: \"-em\";\n}\n" +); +test!( + unit_begins_with_two_hyphens, + "a {\n color: 1--em;\n}\n", + "a {\n color: 1 --em;\n}\n" +); +test!( + unit_begins_with_escape_sequence, + "a {\n color: unit(1\\65);\n}\n", + "a {\n color: \"e\";\n}\n" +); +test!( + unit_begins_with_escape_sequence_followed_by_space_and_hyphen, + "a {\n color: unit(1\\65 -);\n}\n", + "a {\n color: \"e-\";\n}\n" +); +test!( + unit_begins_with_single_hyphen_followed_by_escape_sequence, + "a {\n color: unit(1-\\65);\n}\n", + "a {\n color: \"-e\";\n}\n" +); error!( display_single_div_with_none_numerator, "a {\n color: (1 / 1em);\n}\n", "Error: 1em^-1 isn't a valid CSS value."