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): parse legacy document type #6555

Merged
merged 2 commits into from
Dec 2, 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
41 changes: 41 additions & 0 deletions crates/swc_xml_parser/src/lexer/mod.rs
Expand Up @@ -63,6 +63,7 @@ pub enum State {
DoctypeSystemIdentifierSingleQuoted,
DoctypeSystemIdentifierDoubleQuoted,
AfterDoctypeSystemIdentifier,
DoctypeTypeInternalSubSet,
BogusDoctype,
}

Expand Down Expand Up @@ -2403,6 +2404,12 @@ where
self.state = State::Data;
self.emit_doctype_token();
}
// U+005B LEFT SQUARE BRACKET ([)
// Switch to the doctype internal subset state.
Some(c @ '[') => {
self.append_raw_to_doctype_token(c);
self.state = State::DoctypeTypeInternalSubSet;
}
// EOF
// Parse error. Switch to the data state. Emit DOCTYPE token. Reconsume the EOF
// character.
Expand Down Expand Up @@ -2983,6 +2990,12 @@ where
self.state = State::Data;
self.emit_doctype_token();
}
// U+005B LEFT SQUARE BRACKET ([)
// Switch to the doctype internal subset state.
Some(c @ '[') => {
self.append_raw_to_doctype_token(c);
self.state = State::DoctypeTypeInternalSubSet;
}
// EOF
// Parse error. Switch to the data state. Emit DOCTYPE token. Reconsume the EOF
// character.
Expand All @@ -3001,6 +3014,34 @@ where
}
}
}
State::DoctypeTypeInternalSubSet => {
// Consume the next input character:
match self.consume_next_char() {
// U+005D RIGHT SQUARE BRACKET (])
// Switch to the CDATA bracket state.
Some(c @ ']') => {
self.append_raw_to_doctype_token(c);
self.state = State::AfterDoctypeName;
}
// EOF
// Parse error. Switch to the data state. Emit DOCTYPE token. Reconsume the EOF
// character.
None => {
self.emit_error(ErrorKind::EofInDoctype);
self.state = State::Data;
self.emit_doctype_token();
self.reconsume();
}
// Anything else
// Append the current input character to the current DOCTYPE token's system
// identifier.
Some(c) => {
// TODO improve parse legacy declarations
self.validate_input_stream_character(c);
self.append_raw_to_doctype_token(c);
}
}
}
State::BogusDoctype => {
// Consume the next input character:
match self.consume_next_char() {
Expand Down
@@ -0,0 +1,3 @@
| <!DOCTYPE svg>
| <root>
| "test"
@@ -0,0 +1,5 @@
<!DOCTYPE svg [
<!ENTITY Pub-Status "This is a pre-release of the
specification.">
]>
<root>test</root>
@@ -0,0 +1,44 @@
{
"type": "Document",
"span": {
"start": 1,
"end": 112,
"ctxt": 0
},
"children": [
{
"type": "DocumentType",
"span": {
"start": 1,
"end": 94,
"ctxt": 0
},
"name": "svg",
"publicId": null,
"systemId": null,
"raw": "<!DOCTYPE svg [\n <!ENTITY Pub-Status \"This is a pre-release of the\nspecification.\">\n]>"
},
{
"type": "Element",
"span": {
"start": 95,
"end": 112,
"ctxt": 0
},
"tagName": "root",
"attributes": [],
"children": [
{
"type": "Text",
"span": {
"start": 101,
"end": 105,
"ctxt": 0
},
"data": "test",
"raw": "test"
}
]
}
]
}
@@ -0,0 +1,55 @@

x Document
,-[$DIR/tests/fixture/document_type/internal_subset-1/input.xml:1:1]
1 | ,-> <!DOCTYPE svg [
2 | | <!ENTITY Pub-Status "This is a pre-release of the
3 | | specification.">
4 | | ]>
5 | `-> <root>test</root>
`----

x Child
,-[$DIR/tests/fixture/document_type/internal_subset-1/input.xml:1:1]
1 | ,-> <!DOCTYPE svg [
2 | | <!ENTITY Pub-Status "This is a pre-release of the
3 | | specification.">
4 | `-> ]>
5 | <root>test</root>
`----

x DocumentType
,-[$DIR/tests/fixture/document_type/internal_subset-1/input.xml:1:1]
1 | ,-> <!DOCTYPE svg [
2 | | <!ENTITY Pub-Status "This is a pre-release of the
3 | | specification.">
4 | `-> ]>
5 | <root>test</root>
`----

x Child
,-[$DIR/tests/fixture/document_type/internal_subset-1/input.xml:4:1]
4 | ]>
5 | <root>test</root>
: ^^^^^^^^^^^^^^^^^
`----

x Element
,-[$DIR/tests/fixture/document_type/internal_subset-1/input.xml:4:1]
4 | ]>
5 | <root>test</root>
: ^^^^^^^^^^^^^^^^^
`----

x Child
,-[$DIR/tests/fixture/document_type/internal_subset-1/input.xml:4:1]
4 | ]>
5 | <root>test</root>
: ^^^^
`----

x Text
,-[$DIR/tests/fixture/document_type/internal_subset-1/input.xml:4:1]
4 | ]>
5 | <root>test</root>
: ^^^^
`----
@@ -0,0 +1,3 @@
| <!DOCTYPE svg>
| <root>
| "test"
@@ -0,0 +1,4 @@
<!DOCTYPE svg [
<!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
]>
<root>test</root>
@@ -0,0 +1,44 @@
{
"type": "Document",
"span": {
"start": 1,
"end": 127,
"ctxt": 0
},
"children": [
{
"type": "DocumentType",
"span": {
"start": 1,
"end": 109,
"ctxt": 0
},
"name": "svg",
"publicId": null,
"systemId": null,
"raw": "<!DOCTYPE svg [\n <!ENTITY open-hatch SYSTEM \"http://www.textuality.com/boilerplate/OpenHatch.xml\">\n]>"
},
{
"type": "Element",
"span": {
"start": 110,
"end": 127,
"ctxt": 0
},
"tagName": "root",
"attributes": [],
"children": [
{
"type": "Text",
"span": {
"start": 116,
"end": 120,
"ctxt": 0
},
"data": "test",
"raw": "test"
}
]
}
]
}
@@ -0,0 +1,52 @@

x Document
,-[$DIR/tests/fixture/document_type/internal_subset-2/input.xml:1:1]
1 | ,-> <!DOCTYPE svg [
2 | | <!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
3 | | ]>
4 | `-> <root>test</root>
`----

x Child
,-[$DIR/tests/fixture/document_type/internal_subset-2/input.xml:1:1]
1 | ,-> <!DOCTYPE svg [
2 | | <!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
3 | `-> ]>
4 | <root>test</root>
`----

x DocumentType
,-[$DIR/tests/fixture/document_type/internal_subset-2/input.xml:1:1]
1 | ,-> <!DOCTYPE svg [
2 | | <!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
3 | `-> ]>
4 | <root>test</root>
`----

x Child
,-[$DIR/tests/fixture/document_type/internal_subset-2/input.xml:3:1]
3 | ]>
4 | <root>test</root>
: ^^^^^^^^^^^^^^^^^
`----

x Element
,-[$DIR/tests/fixture/document_type/internal_subset-2/input.xml:3:1]
3 | ]>
4 | <root>test</root>
: ^^^^^^^^^^^^^^^^^
`----

x Child
,-[$DIR/tests/fixture/document_type/internal_subset-2/input.xml:3:1]
3 | ]>
4 | <root>test</root>
: ^^^^
`----

x Text
,-[$DIR/tests/fixture/document_type/internal_subset-2/input.xml:3:1]
3 | ]>
4 | <root>test</root>
: ^^^^
`----
@@ -0,0 +1,3 @@
| <!DOCTYPE svg>
| <root>
| "test"
@@ -0,0 +1,6 @@
<!DOCTYPE svg [
<!ENTITY open-hatch
PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN"
"http://www.textuality.com/boilerplate/OpenHatch.xml">
]>
<root>test</root>
@@ -0,0 +1,44 @@
{
"type": "Document",
"span": {
"start": 1,
"end": 205,
"ctxt": 0
},
"children": [
{
"type": "DocumentType",
"span": {
"start": 1,
"end": 187,
"ctxt": 0
},
"name": "svg",
"publicId": null,
"systemId": null,
"raw": "<!DOCTYPE svg [\n <!ENTITY open-hatch\n PUBLIC \"-//Textuality//TEXT Standard open-hatch boilerplate//EN\"\n \"http://www.textuality.com/boilerplate/OpenHatch.xml\">\n]>"
},
{
"type": "Element",
"span": {
"start": 188,
"end": 205,
"ctxt": 0
},
"tagName": "root",
"attributes": [],
"children": [
{
"type": "Text",
"span": {
"start": 194,
"end": 198,
"ctxt": 0
},
"data": "test",
"raw": "test"
}
]
}
]
}