Skip to content

Commit

Permalink
Support escaped characters
Browse files Browse the repository at this point in the history
  • Loading branch information
nifouprog committed Aug 24, 2024
1 parent 33d05d7 commit 2605732
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/css/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ fn check_slash_slash() {
assert_eq!(minify(s).expect("minify failed").to_string(), expected);
}

#[test]
fn check_escaped_characters() {
let s = r#".before\:prose-headings\:content-\[\'\#\'\] :is(:where(h1,h2,h3,h4,h5,h6,th):not(:where([class~="not-prose"] *)))::before{
--en-content: '#';
content: var(--en-content);
}"#;
let expected = r#".before\:prose-headings\:content-\[\'\#\'\] :is(:where(h1,h2,h3,h4,h5,h6,th):not(:where([class~="not-prose"] *)))::before{--en-content:'#';content:var(--en-content);}"#;
assert_eq!(minify(s).expect("minify failed").to_string(), expected);
}

#[test]
fn issue_80() {
assert_eq!(
Expand Down
12 changes: 12 additions & 0 deletions src/css/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum ReservedChar {
Tilde,
Dollar,
Circumflex,
Backslash,
}

impl fmt::Display for ReservedChar {
Expand Down Expand Up @@ -61,6 +62,7 @@ impl fmt::Display for ReservedChar {
ReservedChar::Tilde => '~',
ReservedChar::Dollar => '$',
ReservedChar::Circumflex => '^',
ReservedChar::Backslash => '\\',
}
)
}
Expand Down Expand Up @@ -94,6 +96,7 @@ impl TryFrom<char> for ReservedChar {
'~' => Ok(ReservedChar::Tilde),
'$' => Ok(ReservedChar::Dollar),
'^' => Ok(ReservedChar::Circumflex),
'\\' => Ok(ReservedChar::Backslash),
_ => Err("Unknown reserved char"),
}
}
Expand Down Expand Up @@ -465,7 +468,16 @@ pub(super) fn tokenize(source: &str) -> Result<Tokens<'_>, &'static str> {
|| v.last()
.unwrap_or(&Token::Char(ReservedChar::Space))
.is_a_media();

match c {
ReservedChar::Backslash => {
v.push(Token::Char(ReservedChar::Backslash));

if iterator.next().is_some() {
pos += 1;
v.push(Token::Other(&source[pos..pos + 1]));
}
}
ReservedChar::Quote | ReservedChar::DoubleQuote => {
if let Some(s) = get_string(source, &mut iterator, &mut pos, c) {
v.push(s);
Expand Down

0 comments on commit 2605732

Please sign in to comment.