fix(css/lexer): Fix some edge cases (#2612)

This commit is contained in:
Alexander Akait 2021-11-02 08:28:19 +03:00 committed by GitHub
parent 6644a06da6
commit c89a9ea171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 92 additions and 26 deletions

2
Cargo.lock generated
View File

@ -2568,7 +2568,7 @@ dependencies = [
[[package]]
name = "swc_css_parser"
version = "0.26.1"
version = "0.26.2"
dependencies = [
"bitflags",
"lexical",

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_css_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.26.1"
version = "0.26.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]

View File

@ -155,26 +155,57 @@ where
return self.read_str(None);
}
// U+0023 NUMBER SIGN (#)
// If the next input code point is a name code point or the next two input code points
// are a valid escape, then:
Some(c) if c == '#' => {
// TODO: If the next input code point is a name code point or the next two input
// code points are a valid escape, then:
self.input.bump();
let first = self.input.cur();
let second = self.input.peek();
if is_name(first.unwrap()) || self.is_valid_escape(first, second)? {
// If the next input code point is a name code point or the next two input code
// points are a valid escape, then:
if (first.is_some() && is_name(first.unwrap()))
|| self.is_valid_escape(first, second)?
{
// Create a <hash-token>.
let mut hash_token = Token::Hash {
is_id: Default::default(),
value: Default::default(),
raw: Default::default(),
};
// If the next 3 input code points would start an identifier, set the
// <hash-token>s type flag to "id".
let third = self.input.peek_ahead();
let is_id = self.would_start_ident(first, second, third)?;
let is_would_start_ident = self.would_start_ident(first, second, third)?;
match hash_token {
Token::Hash { ref mut is_id, .. } => {
*is_id = is_would_start_ident;
}
_ => {
unreachable!();
}
}
// Consume a name, and set the <hash-token>s value to the returned string.
let name = self.read_name()?;
return Ok(Token::Hash {
is_id,
value: name.0,
raw: name.1,
});
match hash_token {
Token::Hash {
ref mut value,
ref mut raw,
..
} => {
*value = name.0;
*raw = name.1;
}
_ => {
unreachable!();
}
}
// Return the <hash-token>.
return Ok(hash_token);
}
return Ok(Token::Delim { value: c });
@ -590,7 +621,8 @@ where
continue;
}
// Otherwise, if the next input code point is a newline, consume it.
else if is_newline(self.input.peek().unwrap()) {
else if self.input.peek().is_some() && is_newline(self.input.peek().unwrap())
{
raw.push(c);
self.input.bump();
raw.push(self.input.cur().unwrap());
@ -790,12 +822,12 @@ where
match next {
// hex digit
Some(c) if is_hex_digit(c) => {
self.input.bump();
let mut hex = c.to_digit(16).unwrap();
raw.push(next.unwrap());
self.input.bump();
// Consume as many hex digits as possible, but no more than 5.
// Note that this means 1-6 hex digits have been consumed in total.
for _ in 0..5 {
@ -805,20 +837,21 @@ where
None => break,
};
raw.push(next.unwrap());
self.input.bump();
raw.push(next.unwrap());
hex = hex * 16 + digit;
}
self.last_pos = Some(self.input.cur_pos());
// If the next input code point is whitespace, consume it as well.
if is_whitespace(self.input.cur().unwrap()) {
raw.push(self.input.cur().unwrap());
let next = self.input.cur();
if next.is_some() && is_whitespace(next.unwrap()) {
self.input.bump();
raw.push(next.unwrap());
}
// Interpret the hex digits as a hexadecimal number. If this number is zero, or
@ -841,11 +874,11 @@ where
}
// anything else
// Return the current input code point.
_ => {
raw.push(next.unwrap());
Some(_) => {
self.input.bump();
raw.push(next.unwrap());
return Ok((next.unwrap(), raw));
}
}
@ -1052,11 +1085,11 @@ where
let mut digits = String::new();
loop {
let code = self.input.cur().unwrap();
let next = self.input.cur();
if code.is_digit(10) {
if next.is_some() && next.unwrap().is_digit(10) {
self.input.bump();
digits.push(code);
digits.push(next.unwrap());
} else {
break;
}

View File

@ -0,0 +1,3 @@
a {
prop: "\a
}

View File

@ -0,0 +1,2 @@
error: Expected "}"

View File

@ -0,0 +1,2 @@
a {
prop: "test\\

View File

@ -0,0 +1,2 @@
error: Expected "}"

View File

@ -0,0 +1,2 @@
a {
prop: #

View File

@ -0,0 +1,8 @@
error: Expected "}"
error: Expected Declaration value
--> $DIR/tests/errors/hash/input.css:2:11
|
2 | prop: #
| ^

View File

@ -0,0 +1,2 @@
a {
prop: 12

View File

@ -0,0 +1,2 @@
error: Expected "}"

View File

@ -0,0 +1,2 @@
a {
prop: "

View File

@ -0,0 +1,8 @@
error: Expected "}"
error: Expected Declaration value
--> $DIR/tests/errors/strings/newline/input.css:2:11
|
2 | prop: "
| ^