Skip to content
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

fix(xml/parser): fix comment parsing #6449

Merged
merged 2 commits into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/swc_xml_parser/src/error.rs
Expand Up @@ -81,6 +81,7 @@ impl Error {
"Missing whitespace between doctype public and system identifiers".into()
}
ErrorKind::NestedComment => "Nested comment".into(),
ErrorKind::DoubleHyphenWithInComment => "Double hyper within comment".into(),
ErrorKind::NoncharacterInInputStream => "Noncharacter in input stream".into(),
ErrorKind::SurrogateInInputStream => "Surrogate in input stream".into(),
ErrorKind::SurrogateCharacterReference => "Surrogate character reference".into(),
Expand Down Expand Up @@ -146,6 +147,7 @@ pub enum ErrorKind {
MissingWhitespaceBeforeDoctypeName,
MissingWhitespaceBetweenDoctypePublicAndSystemIdentifiers,
NestedComment,
DoubleHyphenWithInComment,
NoncharacterInInputStream,
SurrogateInInputStream,
SurrogateCharacterReference,
Expand Down
1 change: 1 addition & 0 deletions crates/swc_xml_parser/src/lexer/mod.rs
Expand Up @@ -1620,6 +1620,7 @@ where
// Append a U+002D HYPHEN-MINUS character (-) to the comment token's data.
Some(c @ '-') => {
self.append_to_comment_token(c, c);
self.emit_error(ErrorKind::DoubleHyphenWithInComment);
}
// EOF
// This is an eof-in-comment parse error. Emit the current comment token.
Expand Down
43 changes: 43 additions & 0 deletions crates/swc_xml_parser/tests/fixture/comments/dom.rust-debug
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8">
| <root>
| "
"
| <!-- comment -->
| "
"
| <!-- <head> -->
| "
"
| <!-- <!-x -->
| "
"
| <!-- <!x -->
| "
"
| <!-- <<!x -->
| "
"
| <!-- <<!-x -->
| "
"
| <!-- <x -->
| "
"
| <!-- <> -->
| "
"
| <!-- < -->
| "
"
| <!-- <! -->
| "
"
| <!-- -->
| "
"
| <!-- -a->-a -->
| "
"
| <!-- -<!-test-> -->
| "
"
17 changes: 17 additions & 0 deletions crates/swc_xml_parser/tests/fixture/comments/input.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<!--comment-->
<!--<head>-->
<!--<!-x-->
<!--<!x-->
<!--<<!x-->
<!--<<!-x-->
<!--<x-->
<!--<>-->
<!--<-->
<!--<!-->
<!---->
<!---a->-a-->
<!---<!-test->-->
</root>

302 changes: 302 additions & 0 deletions crates/swc_xml_parser/tests/fixture/comments/output.json
@@ -0,0 +1,302 @@
{
"type": "Document",
"span": {
"start": 1,
"end": 264,
"ctxt": 0
},
"children": [
{
"type": "ProcessingInstruction",
"span": {
"start": 1,
"end": 39,
"ctxt": 0
},
"target": "xml",
"data": "version=\"1.0\" encoding=\"UTF-8\""
},
{
"type": "Element",
"span": {
"start": 40,
"end": 262,
"ctxt": 0
},
"tagName": "root",
"attributes": [],
"children": [
{
"type": "Text",
"span": {
"start": 46,
"end": 51,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 51,
"end": 65,
"ctxt": 0
},
"data": "comment",
"raw": "<!--comment-->"
},
{
"type": "Text",
"span": {
"start": 65,
"end": 70,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 70,
"end": 83,
"ctxt": 0
},
"data": "<head>",
"raw": "<!--<head>-->"
},
{
"type": "Text",
"span": {
"start": 83,
"end": 88,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 88,
"end": 99,
"ctxt": 0
},
"data": "<!-x",
"raw": "<!--<!-x-->"
},
{
"type": "Text",
"span": {
"start": 99,
"end": 104,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 104,
"end": 114,
"ctxt": 0
},
"data": "<!x",
"raw": "<!--<!x-->"
},
{
"type": "Text",
"span": {
"start": 114,
"end": 119,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 119,
"end": 130,
"ctxt": 0
},
"data": "<<!x",
"raw": "<!--<<!x-->"
},
{
"type": "Text",
"span": {
"start": 130,
"end": 135,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 135,
"end": 147,
"ctxt": 0
},
"data": "<<!-x",
"raw": "<!--<<!-x-->"
},
{
"type": "Text",
"span": {
"start": 147,
"end": 152,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 152,
"end": 161,
"ctxt": 0
},
"data": "<x",
"raw": "<!--<x-->"
},
{
"type": "Text",
"span": {
"start": 161,
"end": 166,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 166,
"end": 175,
"ctxt": 0
},
"data": "<>",
"raw": "<!--<>-->"
},
{
"type": "Text",
"span": {
"start": 175,
"end": 180,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 180,
"end": 188,
"ctxt": 0
},
"data": "<",
"raw": "<!--<-->"
},
{
"type": "Text",
"span": {
"start": 188,
"end": 193,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 193,
"end": 202,
"ctxt": 0
},
"data": "<!",
"raw": "<!--<!-->"
},
{
"type": "Text",
"span": {
"start": 202,
"end": 207,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 207,
"end": 214,
"ctxt": 0
},
"data": "",
"raw": "<!---->"
},
{
"type": "Text",
"span": {
"start": 214,
"end": 219,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 219,
"end": 232,
"ctxt": 0
},
"data": "-a->-a",
"raw": "<!---a->-a-->"
},
{
"type": "Text",
"span": {
"start": 232,
"end": 237,
"ctxt": 0
},
"data": "\n ",
"raw": "\n "
},
{
"type": "Comment",
"span": {
"start": 237,
"end": 254,
"ctxt": 0
},
"data": "-<!-test->",
"raw": "<!---<!-test->-->"
},
{
"type": "Text",
"span": {
"start": 254,
"end": 255,
"ctxt": 0
},
"data": "\n",
"raw": "\n"
}
]
}
]
}