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): bugfixes and tests #6566

Merged
merged 23 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions crates/swc_xml_codegen/src/lib.rs
Expand Up @@ -352,7 +352,7 @@ fn normalize_attribute_value(value: &str) -> String {
let mut normalized = String::with_capacity(value.len() + 2);

normalized.push('"');
normalized.push_str(value);
normalized.push_str(&escape_string(value, true));
normalized.push('"');

normalized
Expand Down Expand Up @@ -463,7 +463,7 @@ fn escape_string(value: &str, is_attribute_mode: bool) -> String {
result.push_str("&");
}
'"' if is_attribute_mode => result.push_str("""),
'<' if !is_attribute_mode => {
'<' if is_attribute_mode => {
result.push_str("&lt;");
}
'>' if !is_attribute_mode => {
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_xml_codegen/tests/fixture/base/input.xml
Expand Up @@ -4,4 +4,10 @@
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<foo attributeName="He said &quot;OK&quot;"></foo>
<foo attributeName="He said &lt;OK&lt;"></foo>
<foo attributeName="He said &gt;OK&gt;"></foo>
<foo attributeName="He said >OK>"></foo>
<foo attributeName="He said &apos;OK&apos;"></foo>
<foo attributeName="He said &amp;OK&amp;"></foo>
</note>
6 changes: 6 additions & 0 deletions crates/swc_xml_codegen/tests/fixture/base/output.min.xml
Expand Up @@ -3,4 +3,10 @@
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<foo attributeName="He said &quot;OK&quot;"/>
<foo attributeName="He said &lt;OK&lt;"/>
<foo attributeName="He said >OK>"/>
<foo attributeName="He said >OK>"/>
<foo attributeName="He said 'OK'"/>
<foo attributeName="He said &amp;OK&amp;"/>
</note>
6 changes: 6 additions & 0 deletions crates/swc_xml_codegen/tests/fixture/base/output.xml
Expand Up @@ -3,4 +3,10 @@
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<foo attributeName="He said &quot;OK&quot;" />
<foo attributeName="He said &lt;OK&lt;" />
<foo attributeName="He said >OK>" />
<foo attributeName="He said >OK>" />
<foo attributeName="He said 'OK'" />
<foo attributeName="He said &amp;OK&amp;" />
</note>
16 changes: 16 additions & 0 deletions crates/swc_xml_parser/src/error.rs
Expand Up @@ -57,6 +57,7 @@ impl Error {
"Invalid character of processing instruction".into()
}
ErrorKind::InvalidCharacterInTag => "Invalid character in tag".into(),
ErrorKind::InvalidEntityCharacter => "Invalid entity character".into(),
ErrorKind::MissingDoctypeName => "Missing doctype name".into(),
ErrorKind::MissingDoctypePublicIdentifier => "Missing doctype public identifier".into(),
ErrorKind::MissingQuoteBeforeDoctypePublicIdentifier => {
Expand All @@ -81,6 +82,13 @@ impl Error {
"Missing whitespace between doctype public and system identifiers".into()
}
ErrorKind::MissingEndTagName => "Missing end tag name".into(),
ErrorKind::MissingQuoteBeforeAttributeValue => {
"Missing quote before attribute value".into()
}
ErrorKind::MissingEqualAfterAttributeName => {
"Missing equal after attribute name".into()
}
ErrorKind::MissingSpaceBetweenAttributes => "Missing space between attributes".into(),
ErrorKind::NestedComment => "Nested comment".into(),
ErrorKind::DoubleHyphenWithInComment => "Double hyper within comment".into(),
ErrorKind::NoncharacterInInputStream => "Noncharacter in input stream".into(),
Expand All @@ -97,6 +105,9 @@ impl Error {
ErrorKind::MissingWhitespaceBeforeQuestionInProcessingInstruction => {
"Missing whitespace before '?'".into()
}
ErrorKind::UnescapedCharacterInAttributeValue(c) => {
format!("Unescaped \"{}\" not allowed in attribute values", c).into()
}

// Parser errors
ErrorKind::UnexpectedTokenInStartPhase => "Unexpected token in start phase".into(),
Expand Down Expand Up @@ -142,6 +153,7 @@ pub enum ErrorKind {
InvalidFirstCharacterOfTagName,
InvalidCharacterOfProcessingInstruction,
InvalidCharacterInTag,
InvalidEntityCharacter,
MissingDoctypeName,
MissingDoctypePublicIdentifier,
MissingQuoteBeforeDoctypePublicIdentifier,
Expand All @@ -152,6 +164,9 @@ pub enum ErrorKind {
MissingWhitespaceBeforeDoctypeName,
MissingWhitespaceBetweenDoctypePublicAndSystemIdentifiers,
MissingEndTagName,
MissingQuoteBeforeAttributeValue,
MissingEqualAfterAttributeName,
MissingSpaceBetweenAttributes,
NestedComment,
DoubleHyphenWithInComment,
NoncharacterInInputStream,
Expand All @@ -162,6 +177,7 @@ pub enum ErrorKind {
UnexpectedSolidusInTag,
NoTargetNameInProcessingInstruction,
MissingWhitespaceBeforeQuestionInProcessingInstruction,
UnescapedCharacterInAttributeValue(char),

// Parser errors
UnexpectedTokenInStartPhase,
Expand Down