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); devour_whitespace_or_comment(toks);
if let Some(tok) = toks.next() { if let Some(tok) = toks.next() {
match tok.kind { match tok.kind {
TokenKind::Keyword(Keyword::From) => {} TokenKind::Keyword(Keyword::From(..)) => {}
_ => return Err("Expected \"from\".".into()), _ => return Err("Expected \"from\".".into()),
} }
} else { } else {
@ -132,11 +132,11 @@ impl AtRule {
let mut through = 0; let mut through = 0;
while let Some(tok) = toks.next() { while let Some(tok) = toks.next() {
match tok.kind { match tok.kind {
TokenKind::Keyword(Keyword::Through) => { TokenKind::Keyword(Keyword::Through(..)) => {
through = 1; through = 1;
break; break;
} }
TokenKind::Keyword(Keyword::To) => break, TokenKind::Keyword(Keyword::To(..)) => break,
_ => from_toks.push(tok), _ => 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 { pub enum Keyword {
Important, Important,
True, True,
False, False,
Null, Null,
Default, Default,
From, From(String),
To, To(String),
Through, Through(String),
// Infinity, // Infinity,
// NaN, // NaN,
// Auto, // Auto,
@ -244,9 +244,9 @@ impl Display for Keyword {
Self::Null => write!(f, "null"), Self::Null => write!(f, "null"),
Self::Default => write!(f, "!default"), Self::Default => write!(f, "!default"),
// todo!(maintain casing for keywords) // todo!(maintain casing for keywords)
Self::From => write!(f, "from"), Self::From(s) => write!(f, "{}", s),
Self::To => write!(f, "to"), Self::To(s) => write!(f, "{}", s),
Self::Through => write!(f, "through"), Self::Through(s) => write!(f, "{}", s),
// Self::Infinity => write!(f, "Infinity"), // Self::Infinity => write!(f, "Infinity"),
// Self::NaN => write!(f, "NaN"), // Self::NaN => write!(f, "NaN"),
// Self::Auto => write!(f, "auto"), // Self::Auto => write!(f, "auto"),
@ -269,9 +269,9 @@ impl Into<&'static str> for Keyword {
Self::False => "false", Self::False => "false",
Self::Null => "null", Self::Null => "null",
Self::Default => "!default", Self::Default => "!default",
Self::From => "from", Self::From(_) => "from",
Self::To => "to", Self::To(_) => "to",
Self::Through => "through", Self::Through(_) => "through",
// Self::Infinity => "Infinity", // Self::Infinity => "Infinity",
// Self::NaN => "NaN", // Self::NaN => "NaN",
// Self::Auto => "auto", // Self::Auto => "auto",
@ -296,9 +296,9 @@ impl TryFrom<&str> for Keyword {
"false" => Ok(Self::False), "false" => Ok(Self::False),
"null" => Ok(Self::Null), "null" => Ok(Self::Null),
"default" => Ok(Self::Default), "default" => Ok(Self::Default),
"from" => Ok(Self::From), "from" => Ok(Self::From(kw.to_owned())),
"to" => Ok(Self::To), "to" => Ok(Self::To(kw.to_owned())),
"through" => Ok(Self::Through), "through" => Ok(Self::Through(kw.to_owned())),
// "infinity" => Ok(Self::Infinity), // "infinity" => Ok(Self::Infinity),
// "nan" => Ok(Self::NaN), // "nan" => Ok(Self::NaN),
// "auto" => Ok(Self::Auto), // "auto" => Ok(Self::Auto),

View File

@ -314,6 +314,9 @@ impl Value {
TokenKind::Keyword(Keyword::True) => Ok(Value::True), TokenKind::Keyword(Keyword::True) => Ok(Value::True),
TokenKind::Keyword(Keyword::False) => Ok(Value::False), TokenKind::Keyword(Keyword::False) => Ok(Value::False),
TokenKind::Keyword(Keyword::Null) => Ok(Value::Null), 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)), TokenKind::Unknown(c) => Ok(Value::Ident(c.to_string(), QuoteKind::None)),
_ => Err("Unexpected token in value parsing".into()), _ => 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_or, "a {\n color: or;\n}\n");
test!(preserves_keyword_unset, "a {\n color: unset;\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_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!( test!(
preserves_quotes, preserves_quotes,
"a {\n color: \"'foo' \\\"bar\\\"\";\n}\n" "a {\n color: \"'foo' \\\"bar\\\"\";\n}\n"