Preserve case of keywords from, to, and through

This commit is contained in:
ConnorSkees 2020-02-29 12:11:40 -05:00
parent 0adc6f90e3
commit b85b122e3d
4 changed files with 22 additions and 16 deletions

View File

@ -121,7 +121,7 @@ impl AtRule {
devour_whitespace_or_comment(toks);
if let Some(tok) = toks.next() {
match tok.kind {
TokenKind::Keyword(Keyword::From) => {}
TokenKind::Keyword(Keyword::From(..)) => {}
_ => return Err("Expected \"from\".".into()),
}
} else {
@ -132,11 +132,11 @@ impl AtRule {
let mut through = 0;
while let Some(tok) = toks.next() {
match tok.kind {
TokenKind::Keyword(Keyword::Through) => {
TokenKind::Keyword(Keyword::Through(..)) => {
through = 1;
break;
}
TokenKind::Keyword(Keyword::To) => break,
TokenKind::Keyword(Keyword::To(..)) => break,
_ => from_toks.push(tok),
}
}

View File

@ -213,16 +213,16 @@ impl Display for Op {
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Keyword {
Important,
True,
False,
Null,
Default,
From,
To,
Through,
From(String),
To(String),
Through(String),
// Infinity,
// NaN,
// Auto,
@ -244,9 +244,9 @@ impl Display for Keyword {
Self::Null => write!(f, "null"),
Self::Default => write!(f, "!default"),
// todo!(maintain casing for keywords)
Self::From => write!(f, "from"),
Self::To => write!(f, "to"),
Self::Through => write!(f, "through"),
Self::From(s) => write!(f, "{}", s),
Self::To(s) => write!(f, "{}", s),
Self::Through(s) => write!(f, "{}", s),
// Self::Infinity => write!(f, "Infinity"),
// Self::NaN => write!(f, "NaN"),
// Self::Auto => write!(f, "auto"),
@ -269,9 +269,9 @@ impl Into<&'static str> for Keyword {
Self::False => "false",
Self::Null => "null",
Self::Default => "!default",
Self::From => "from",
Self::To => "to",
Self::Through => "through",
Self::From(_) => "from",
Self::To(_) => "to",
Self::Through(_) => "through",
// Self::Infinity => "Infinity",
// Self::NaN => "NaN",
// Self::Auto => "auto",
@ -296,9 +296,9 @@ impl TryFrom<&str> for Keyword {
"false" => Ok(Self::False),
"null" => Ok(Self::Null),
"default" => Ok(Self::Default),
"from" => Ok(Self::From),
"to" => Ok(Self::To),
"through" => Ok(Self::Through),
"from" => Ok(Self::From(kw.to_owned())),
"to" => Ok(Self::To(kw.to_owned())),
"through" => Ok(Self::Through(kw.to_owned())),
// "infinity" => Ok(Self::Infinity),
// "nan" => Ok(Self::NaN),
// "auto" => Ok(Self::Auto),

View File

@ -314,6 +314,9 @@ impl Value {
TokenKind::Keyword(Keyword::True) => Ok(Value::True),
TokenKind::Keyword(Keyword::False) => Ok(Value::False),
TokenKind::Keyword(Keyword::Null) => Ok(Value::Null),
TokenKind::Keyword(Keyword::From(s)) => Ok(Value::Ident(s, QuoteKind::None)),
TokenKind::Keyword(Keyword::Through(s)) => Ok(Value::Ident(s, QuoteKind::None)),
TokenKind::Keyword(Keyword::To(s)) => Ok(Value::Ident(s, QuoteKind::None)),
TokenKind::Unknown(c) => Ok(Value::Ident(c.to_string(), QuoteKind::None)),
_ => Err("Unexpected token in value parsing".into()),
}

View File

@ -29,6 +29,9 @@ test!(preserves_keyword_and, "a {\n color: and;\n}\n");
test!(preserves_keyword_or, "a {\n color: or;\n}\n");
test!(preserves_keyword_unset, "a {\n color: unset;\n}\n");
test!(preserves_keyword_nan, "a {\n color: NaN;\n}\n");
test!(preserves_keyword_from, "a {\n color: FRoM;\n}\n");
test!(preserves_keyword_to, "a {\n color: To;\n}\n");
test!(preserves_keyword_through, "a {\n color: ThRouGh;\n}\n");
test!(
preserves_quotes,
"a {\n color: \"'foo' \\\"bar\\\"\";\n}\n"