From 105a0cccf7dec630824e2e0f8ee77bf31645aa28 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Thu, 7 Apr 2022 08:12:54 +0300 Subject: [PATCH] fix(html/parser): Fix bugs (#4265) --- Cargo.lock | 1 + crates/swc_html_ast/src/token.rs | 1 + crates/swc_html_codegen/src/lib.rs | 4 +- .../tests/fixture/attributes/input.html | 50 + .../tests/fixture/attributes/output.html | 48 +- .../tests/fixture/attributes/output.min.html | 48 +- .../tests/fixture/html-entity/input.html | 40 + .../tests/fixture/html-entity/output.html | 40 + .../tests/fixture/html-entity/output.min.html | 40 + .../tests/fixture/ng-tag/input.html | 3 + .../tests/fixture/ng-tag/output.html | 3 + .../tests/fixture/ng-tag/output.min.html | 3 + crates/swc_html_parser/Cargo.toml | 3 + .../swc_html_parser/src/lexer/entities.json | 2233 ++ crates/swc_html_parser/src/lexer/mod.rs | 446 +- .../tests/fixture/attributes/input.html | 10 + .../tests/fixture/attributes/output.json | 433 + .../tests/fixture/attributes/span.rust-debug | 136 + .../tests/fixture/bogus-comment-1/input.html | 1 + .../tests/fixture/bogus-comment-1/output.json | 23 + .../fixture/bogus-comment-1/span.rust-debug | 12 + .../tests/fixture/bogus-comment/input.html | 1 + .../tests/fixture/bogus-comment/output.json | 23 + .../fixture/bogus-comment/span.rust-debug | 12 + .../tests/fixture/broken-doctype/input.html | 8 + .../tests/fixture/broken-doctype/output.json | 407 + .../fixture/broken-doctype/span.rust-debug | 191 + .../tests/fixture/class-attribute/input.html | 10 + .../tests/fixture/class-attribute/output.json | 477 + .../fixture/class-attribute/span.rust-debug | 200 + .../tests/fixture/comments/input.html | 9 + .../tests/fixture/comments/output.json | 572 + .../tests/fixture/comments/span.rust-debug | 273 + .../cr-charref-novalid/html/input.html | 1 + .../cr-charref-novalid/html/output.json | 507 + .../cr-charref-novalid/html/span.rust-debug | 228 + .../tests/fixture/html-entity/input.html | 105 + .../tests/fixture/html-entity/output.json | 24774 ++++++++++++++++ .../tests/fixture/html-entity/span.rust-debug | 11519 +++++++ .../fixture/missing-doctype-name/input.html | 8 + .../fixture/missing-doctype-name/output.json | 394 + .../missing-doctype-name/span.rust-debug | 185 + .../range-charref-novalid/html/input.html | 1 + .../range-charref-novalid/html/output.json | 559 + .../html/span.rust-debug | 252 + .../tests/fixture/tag/basic/input.html | 26 + .../tests/fixture/tag/basic/output.json | 1181 + .../tests/fixture/tag/basic/span.rust-debug | 536 + .../fixture/tag/custom-element/input.html | 24 + .../fixture/tag/custom-element/output.json | 2823 ++ .../tag/custom-element/span.rust-debug | 1292 + .../tests/fixture/tag/template/input.html | 27 + .../tests/fixture/tag/template/output.json | 8792 ++++++ .../fixture/tag/template/span.rust-debug | 4071 +++ .../u000b-charref-novalid/html/input.html | 1 + .../u000b-charref-novalid/html/output.json | 533 + .../html/span.rust-debug | 240 + .../unassigned-charref-novalid/input.html | 1 + .../unassigned-charref-novalid/output.json | 533 + .../span.rust-debug | 240 + 60 files changed, 64565 insertions(+), 49 deletions(-) create mode 100644 crates/swc_html_codegen/tests/fixture/html-entity/input.html create mode 100644 crates/swc_html_codegen/tests/fixture/html-entity/output.html create mode 100644 crates/swc_html_codegen/tests/fixture/html-entity/output.min.html create mode 100644 crates/swc_html_codegen/tests/fixture/ng-tag/input.html create mode 100644 crates/swc_html_codegen/tests/fixture/ng-tag/output.html create mode 100644 crates/swc_html_codegen/tests/fixture/ng-tag/output.min.html create mode 100644 crates/swc_html_parser/src/lexer/entities.json create mode 100644 crates/swc_html_parser/tests/fixture/attributes/input.html create mode 100644 crates/swc_html_parser/tests/fixture/attributes/output.json create mode 100644 crates/swc_html_parser/tests/fixture/attributes/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/bogus-comment-1/input.html create mode 100644 crates/swc_html_parser/tests/fixture/bogus-comment-1/output.json create mode 100644 crates/swc_html_parser/tests/fixture/bogus-comment-1/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/bogus-comment/input.html create mode 100644 crates/swc_html_parser/tests/fixture/bogus-comment/output.json create mode 100644 crates/swc_html_parser/tests/fixture/bogus-comment/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/broken-doctype/input.html create mode 100644 crates/swc_html_parser/tests/fixture/broken-doctype/output.json create mode 100644 crates/swc_html_parser/tests/fixture/broken-doctype/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/class-attribute/input.html create mode 100644 crates/swc_html_parser/tests/fixture/class-attribute/output.json create mode 100644 crates/swc_html_parser/tests/fixture/class-attribute/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/comments/input.html create mode 100644 crates/swc_html_parser/tests/fixture/comments/output.json create mode 100644 crates/swc_html_parser/tests/fixture/comments/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/cr-charref-novalid/html/input.html create mode 100644 crates/swc_html_parser/tests/fixture/cr-charref-novalid/html/output.json create mode 100644 crates/swc_html_parser/tests/fixture/cr-charref-novalid/html/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/html-entity/input.html create mode 100644 crates/swc_html_parser/tests/fixture/html-entity/output.json create mode 100644 crates/swc_html_parser/tests/fixture/html-entity/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/missing-doctype-name/input.html create mode 100644 crates/swc_html_parser/tests/fixture/missing-doctype-name/output.json create mode 100644 crates/swc_html_parser/tests/fixture/missing-doctype-name/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/range-charref-novalid/html/input.html create mode 100644 crates/swc_html_parser/tests/fixture/range-charref-novalid/html/output.json create mode 100644 crates/swc_html_parser/tests/fixture/range-charref-novalid/html/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/tag/basic/input.html create mode 100644 crates/swc_html_parser/tests/fixture/tag/basic/output.json create mode 100644 crates/swc_html_parser/tests/fixture/tag/basic/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/tag/custom-element/input.html create mode 100644 crates/swc_html_parser/tests/fixture/tag/custom-element/output.json create mode 100644 crates/swc_html_parser/tests/fixture/tag/custom-element/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/tag/template/input.html create mode 100644 crates/swc_html_parser/tests/fixture/tag/template/output.json create mode 100644 crates/swc_html_parser/tests/fixture/tag/template/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/u000b-charref-novalid/html/input.html create mode 100644 crates/swc_html_parser/tests/fixture/u000b-charref-novalid/html/output.json create mode 100644 crates/swc_html_parser/tests/fixture/u000b-charref-novalid/html/span.rust-debug create mode 100644 crates/swc_html_parser/tests/fixture/unassigned-charref-novalid/input.html create mode 100644 crates/swc_html_parser/tests/fixture/unassigned-charref-novalid/output.json create mode 100644 crates/swc_html_parser/tests/fixture/unassigned-charref-novalid/span.rust-debug diff --git a/Cargo.lock b/Cargo.lock index 75f72d16138..adbce4abbe2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3770,6 +3770,7 @@ version = "0.2.1" dependencies = [ "bitflags", "lexical", + "once_cell", "serde", "serde_json", "swc_atoms", diff --git a/crates/swc_html_ast/src/token.rs b/crates/swc_html_ast/src/token.rs index 17fff0e4a8a..b8a727f807a 100644 --- a/crates/swc_html_ast/src/token.rs +++ b/crates/swc_html_ast/src/token.rs @@ -35,6 +35,7 @@ pub enum Token { Comment { data: JsWord, }, + // TODO add `raw` to get real character from code Character { value: char, }, diff --git a/crates/swc_html_codegen/src/lib.rs b/crates/swc_html_codegen/src/lib.rs index 3e617555f14..ccbb6355f99 100644 --- a/crates/swc_html_codegen/src/lib.rs +++ b/crates/swc_html_codegen/src/lib.rs @@ -116,7 +116,7 @@ where start_tag.push('>'); - write_raw!(self, span, &start_tag); + write_str!(self, span, &start_tag); } Token::EndTag { tag_name, @@ -145,7 +145,7 @@ where start_tag.push('>'); - write_raw!(self, span, &start_tag); + write_str!(self, span, &start_tag); } Token::Comment { data } => { let mut comment = String::new(); diff --git a/crates/swc_html_codegen/tests/fixture/attributes/input.html b/crates/swc_html_codegen/tests/fixture/attributes/input.html index 0edbdc55f8b..014ccba86aa 100644 --- a/crates/swc_html_codegen/tests/fixture/attributes/input.html +++ b/crates/swc_html_codegen/tests/fixture/attributes/input.html @@ -8,5 +8,55 @@ + + +
x
+x
+ +x
+ + + + + + + +foo bar baz
+foo bar baz
+ + + +x
+x
+x
+xxx
+xxx
+xxx
+ +x
+x
+x
+Click me
+ + +