properly handle whitespace and start and end of url()

This commit is contained in:
ConnorSkees 2020-04-24 19:00:06 -04:00
parent 1a18955f0b
commit a5cd335318
3 changed files with 50 additions and 3 deletions

View File

@ -34,6 +34,20 @@ pub(crate) fn devour_whitespace<I: Iterator<Item = W>, W: IsWhitespace>(
found_whitespace
}
pub(crate) fn peek_whitespace<I: Iterator<Item = W>, W: IsWhitespace>(
s: &mut PeekMoreIterator<I>,
) -> usize {
let mut peek_counter = 0;
while let Some(w) = s.peek() {
if !w.is_whitespace() {
break;
}
peek_counter += 1;
s.peek_forward(1);
}
peek_counter
}
pub(crate) fn devour_whitespace_or_comment<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>,
) -> SassResult<bool> {

View File

@ -5,6 +5,7 @@ use crate::scope::Scope;
use crate::selector::Selector;
use crate::utils::{
devour_whitespace, parse_interpolation, peek_escape, peek_until_closing_curly_brace,
peek_whitespace,
};
use crate::Token;
@ -88,6 +89,7 @@ pub(crate) fn try_eat_url<I: Iterator<Item = Token>>(
) -> SassResult<Option<String>> {
let mut buf = String::from("url(");
let mut peek_counter = 0;
peek_counter += peek_whitespace(toks);
while let Some(tok) = toks.peek() {
let kind = tok.kind;
toks.move_forward(1);
@ -117,10 +119,21 @@ pub(crate) fn try_eat_url<I: Iterator<Item = Token>>(
}
} else if kind == ')' {
buf.push(')');
for _ in 0..=peek_counter {
toks.next();
}
toks.take(peek_counter).for_each(drop);
return Ok(Some(buf));
} else if kind.is_whitespace() {
peek_counter += peek_whitespace(toks);
let next = match toks.peek() {
Some(v) => v,
None => break,
};
if next.kind == ')' {
buf.push(')');
toks.take(peek_counter + 1).for_each(drop);
return Ok(Some(buf));
} else {
break;
}
} else {
break;
}

View File

@ -23,6 +23,26 @@ test!(
"a {\n color: url(1+2);\n}\n",
"a {\n color: url(1+2);\n}\n"
);
test!(
arithmetic_space_start_of_url,
"a {\n color: url( 1+2);\n}\n",
"a {\n color: url(1+2);\n}\n"
);
test!(
arithmetic_space_end_of_url,
"a {\n color: url(1+2 );\n}\n",
"a {\n color: url(1+2);\n}\n"
);
test!(
arithmetic_space_start_end_of_url,
"a {\n color: url( 1+2 );\n}\n",
"a {\n color: url(1+2);\n}\n"
);
test!(
arithmetic_space_start_end_of_url_and_operands,
"a {\n color: url( 1 + 2 );\n}\n",
"a {\n color: url(3);\n}\n"
);
test!(
silent_comment,
"a {\n color: url(//some/absolute/path);\n}\n"