From 7164728c697f3478ac355a28512269d9c572dade Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 30 Mar 2020 02:54:11 -0400 Subject: [PATCH] properly handle hash in string --- src/utils.rs | 49 +++++++++------------------------------------- tests/selectors.rs | 5 +++++ tests/strings.rs | 1 + 3 files changed, 15 insertions(+), 40 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index b6b7a7f..31a1107 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -56,49 +56,17 @@ pub(crate) fn devour_whitespace_or_comment>( } pub(crate) fn parse_interpolation>( - tokens: &mut Peekable, + toks: &mut Peekable, scope: &Scope, super_selector: &Selector, ) -> SassResult { - let mut val = String::new(); - while let Some(tok) = tokens.next() { - match tok.kind { - '}' => break, - '{' => todo!("invalid character in interpolation"), - q @ '"' | q @ '\'' => { - val.push_str(&parse_quoted_string(tokens, scope, q, super_selector)?.to_string()) - } - '$' => val.push_str( - &scope - .get_var(&eat_ident(tokens, scope, super_selector)?)? - .clone() - .unquote() - .to_string(), - ), - '#' => { - if tokens.next().unwrap().kind == '{' { - val.push_str( - &Lexer::new( - &parse_interpolation(tokens, scope, super_selector)?.to_string(), - ) - .map(|x| x.kind.to_string()) - .collect::(), - ) - } else { - return Err("Expected identifier.".into()); - } - } - _ => val.push_str(&tok.kind.to_string()), - } - } - if val.trim().is_empty() { - return Ok(Value::Ident(String::new(), QuoteKind::None)); - } - Ok( - Value::from_tokens(&mut Lexer::new(&val).peekable(), scope, super_selector)? - .eval()? - .unquote(), - ) + let val = Value::from_tokens( + &mut read_until_closing_curly_brace(toks).into_iter().peekable(), + scope, + super_selector, + )?; + toks.next(); + Ok(val.eval()?.unquote()) } pub(crate) struct VariableDecl { @@ -598,6 +566,7 @@ pub(crate) fn parse_quoted_string>( continue; } else { s.push('#'); + continue; } } '\n' => return Err("Expected \".".into()), diff --git a/tests/selectors.rs b/tests/selectors.rs index a910d7d..79b1dd2 100644 --- a/tests/selectors.rs +++ b/tests/selectors.rs @@ -310,3 +310,8 @@ test!( "$zzz: zzz;\n##{$zzz} {\n a: b;\n}\n", "#zzz {\n a: b;\n}\n" ); +test!( + interpolate_id_selector, + "$bar: \"#foo\";\nul li#{$bar} {\n foo: bar;\n}\n", + "ul li#foo {\n foo: bar;\n}\n" +); diff --git a/tests/strings.rs b/tests/strings.rs index 84e4fec..aa90df5 100644 --- a/tests/strings.rs +++ b/tests/strings.rs @@ -230,3 +230,4 @@ test!( "a {\n color: str-insert($string: \"foo\", $insert: \"X\", $index: -99999999999999999999);\n}\n", "a {\n color: \"Xfoo\";\n}\n" ); +test!(hash_in_string, "a {\n color: \"#foo\";\n}\n");