Skip to content

Fix serialization of bad-string and bad-url tokens #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.20.0"
version = "0.20.1"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
16 changes: 14 additions & 2 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,20 @@ impl<'a> ToCss for Token<'a> {
Token::SquareBracketBlock => dest.write_str("[")?,
Token::CurlyBracketBlock => dest.write_str("{")?,

Token::BadUrl(_) => dest.write_str("url(<bad url>)")?,
Token::BadString(_) => dest.write_str("\"<bad string>\n")?,
Token::BadUrl(ref contents) => {
dest.write_str("url(")?;
dest.write_str(contents)?;
dest.write_char(')')?;
}
Token::BadString(ref value) => {
// During tokenization, an unescaped newline after a quote causes
// the token to be a BadString instead of a QuotedString.
// The BadString token ends just before the newline
// (which is in a separate WhiteSpace token),
// and therefore does not have a closing quote.
dest.write_char('"')?;
CssStringWriter::new(dest).write_str(value)?;
},
Token::CloseParenthesis => dest.write_str(")")?,
Token::CloseSquareBracket => dest.write_str("]")?,
Token::CloseCurlyBracket => dest.write_str("}")?,
Expand Down
20 changes: 20 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,26 @@ fn serializer(preserve_comments: bool) {
});
}

#[test]
fn serialize_bad_tokens() {
let mut input = ParserInput::new("url(foo\\) b\\)ar)'ba\\'\"z\n4");
let mut parser = Parser::new(&mut input);

let token = parser.next().unwrap().clone();
assert!(matches!(token, Token::BadUrl(_)));
assert_eq!(token.to_css_string(), "url(foo\\) b\\)ar)");

let token = parser.next().unwrap().clone();
assert!(matches!(token, Token::BadString(_)));
assert_eq!(token.to_css_string(), "\"ba'\\\"z");

let token = parser.next().unwrap().clone();
assert!(matches!(token, Token::Number { .. }));
assert_eq!(token.to_css_string(), "4");

assert!(parser.next().is_err());
}

#[test]
fn serialize_current_color() {
let c = Color::CurrentColor;
Expand Down
3 changes: 2 additions & 1 deletion src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,8 +1289,9 @@ fn consume_unquoted_url<'a>(tokenizer: &mut Tokenizer<'a>) -> Result<Token<'a>,
while !tokenizer.is_eof() {
match_byte! { tokenizer.next_byte_unchecked(),
b')' => {
let contents = tokenizer.slice_from(start_pos).into();
tokenizer.advance(1);
break
return BadUrl(contents)
}
b'\\' => {
tokenizer.advance(1);
Expand Down