Skip to content

Commit

Permalink
fix(parser/html): fix parsing html keyword in doctype (#3776)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 authored Sep 4, 2024
1 parent 3180697 commit b835e0b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
5 changes: 3 additions & 2 deletions crates/biome_html_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ mod tests;

use crate::token_source::HtmlLexContext;
use biome_html_syntax::HtmlSyntaxKind::{
COMMENT, DOCTYPE_KW, EOF, ERROR_TOKEN, HTML_LITERAL, HTML_STRING_LITERAL, NEWLINE, TOMBSTONE,
UNICODE_BOM, WHITESPACE,
COMMENT, DOCTYPE_KW, EOF, ERROR_TOKEN, HTML_KW, HTML_LITERAL, HTML_STRING_LITERAL, NEWLINE,
TOMBSTONE, UNICODE_BOM, WHITESPACE,
};
use biome_html_syntax::{HtmlSyntaxKind, TextLen, TextSize, T};
use biome_parser::diagnostic::ParseDiagnostic;
Expand Down Expand Up @@ -149,6 +149,7 @@ impl<'src> HtmlLexer<'src> {

match &buffer[..len] {
b"doctype" | b"DOCTYPE" => DOCTYPE_KW,
b"html" | b"HTML" => HTML_KW,
_ => HTML_LITERAL,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_html_parser/src/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn doctype_with_quirk() {
BANG: 1,
DOCTYPE_KW: 7,
WHITESPACE: 1,
HTML_LITERAL: 4,
HTML_KW: 4,
R_ANGLE: 1,
}
}
Expand All @@ -170,7 +170,7 @@ fn doctype_with_quirk_and_system() {
BANG: 1,
DOCTYPE_KW: 7,
WHITESPACE: 1,
HTML_LITERAL: 4,
HTML_KW: 4,
WHITESPACE: 1,
HTML_STRING_LITERAL: 44,
R_ANGLE: 1,
Expand Down
4 changes: 4 additions & 0 deletions crates/biome_html_parser/src/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ fn parse_doc_type(p: &mut HtmlParser) -> ParsedSyntax {
p.eat(T![doctype]);
}

if p.at(T![html]) {
p.eat(T![html]);
}

p.eat(T![>]);

Present(m.complete(p, HTML_DIRECTIVE))
Expand Down
1 change: 1 addition & 0 deletions crates/biome_html_parser/tests/html_specs/ok/ok2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html>
50 changes: 50 additions & 0 deletions crates/biome_html_parser/tests/html_specs/ok/ok2.html.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
source: crates/biome_html_parser/tests/spec_test.rs
expression: snapshot
---
## Input

```css
<!doctype html>
```


## AST

```
HtmlRoot {
bom_token: missing (optional),
directive: HtmlDirective {
l_angle_token: L_ANGLE@0..1 "<" [] [],
excl_token: BANG@1..2 "!" [] [],
doctype_token: DOCTYPE_KW@2..10 "doctype" [] [Whitespace(" ")],
html_token: HTML_KW@10..14 "html" [] [],
quirk_token: missing (optional),
public_id_token: missing (optional),
system_id_token: missing (optional),
r_angle_token: R_ANGLE@14..15 ">" [] [],
},
html: missing (optional),
eof_token: EOF@15..16 "" [Newline("\n")] [],
}
```

## CST

```
0: [email protected]
0: (empty)
1: [email protected]
0: [email protected] "<" [] []
1: [email protected] "!" [] []
2: [email protected] "doctype" [] [Whitespace(" ")]
3: [email protected] "html" [] []
4: (empty)
5: (empty)
6: (empty)
7: [email protected] ">" [] []
2: (empty)
3: [email protected] "" [Newline("\n")] []
```
2 changes: 1 addition & 1 deletion xtask/codegen/html.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ HtmlAttribute =


// <a href="">
// ^^^^
// ^^^
HtmlAttributeInitializerClause =
'='
value: HtmlString
Expand Down

0 comments on commit b835e0b

Please sign in to comment.