Skip to content

Commit

Permalink
fix(xml/parser): Fix various bugs (#6566)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 3, 2022
1 parent cffa111 commit 01232f0
Show file tree
Hide file tree
Showing 74 changed files with 6,366 additions and 1,114 deletions.
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

1 comment on commit 01232f0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 01232f0 Previous: b3ea52f Ratio
es/full/bugs-1 360532 ns/iter (± 22667) 343801 ns/iter (± 20816) 1.05
es/full/minify/libraries/antd 2059896790 ns/iter (± 21826036) 2074193534 ns/iter (± 30405152) 0.99
es/full/minify/libraries/d3 472027800 ns/iter (± 23829735) 464031120 ns/iter (± 7391889) 1.02
es/full/minify/libraries/echarts 1741553955 ns/iter (± 49207818) 1759691063 ns/iter (± 28454434) 0.99
es/full/minify/libraries/jquery 109253087 ns/iter (± 2209958) 117025630 ns/iter (± 1086192) 0.93
es/full/minify/libraries/lodash 125198057 ns/iter (± 3714508) 134996226 ns/iter (± 5100567) 0.93
es/full/minify/libraries/moment 64127061 ns/iter (± 7665962) 67771633 ns/iter (± 2414796) 0.95
es/full/minify/libraries/react 21947713 ns/iter (± 460113) 22860831 ns/iter (± 926675) 0.96
es/full/minify/libraries/terser 331681260 ns/iter (± 15671794) 361358001 ns/iter (± 9156009) 0.92
es/full/minify/libraries/three 612215724 ns/iter (± 23303045) 640193589 ns/iter (± 16087997) 0.96
es/full/minify/libraries/typescript 3793185807 ns/iter (± 58044728) 3747256017 ns/iter (± 43887440) 1.01
es/full/minify/libraries/victory 982142784 ns/iter (± 84583244) 962472607 ns/iter (± 26793385) 1.02
es/full/minify/libraries/vue 203116835 ns/iter (± 19896332) 175548395 ns/iter (± 9521210) 1.16
es/full/codegen/es3 35936 ns/iter (± 5000) 34843 ns/iter (± 4262) 1.03
es/full/codegen/es5 34247 ns/iter (± 2453) 37205 ns/iter (± 5140) 0.92
es/full/codegen/es2015 34262 ns/iter (± 1185) 34240 ns/iter (± 3652) 1.00
es/full/codegen/es2016 34741 ns/iter (± 1521) 34501 ns/iter (± 3868) 1.01
es/full/codegen/es2017 34367 ns/iter (± 1609) 34396 ns/iter (± 1523) 1.00
es/full/codegen/es2018 34532 ns/iter (± 2530) 34174 ns/iter (± 1919) 1.01
es/full/codegen/es2019 34565 ns/iter (± 1558) 34464 ns/iter (± 2896) 1.00
es/full/codegen/es2020 34832 ns/iter (± 3878) 34143 ns/iter (± 4401) 1.02
es/full/all/es3 237315028 ns/iter (± 22871198) 200427698 ns/iter (± 10403125) 1.18
es/full/all/es5 225492813 ns/iter (± 19749924) 195509378 ns/iter (± 13780140) 1.15
es/full/all/es2015 181221843 ns/iter (± 15816575) 149577455 ns/iter (± 14144437) 1.21
es/full/all/es2016 178068147 ns/iter (± 19930294) 145033761 ns/iter (± 7533543) 1.23
es/full/all/es2017 174244091 ns/iter (± 17486793) 145422970 ns/iter (± 6738295) 1.20
es/full/all/es2018 172144942 ns/iter (± 14584821) 145488546 ns/iter (± 5955845) 1.18
es/full/all/es2019 169395980 ns/iter (± 21142491) 142097566 ns/iter (± 4973738) 1.19
es/full/all/es2020 167952429 ns/iter (± 16215571) 136534753 ns/iter (± 3921701) 1.23
es/full/parser 765715 ns/iter (± 78211) 734155 ns/iter (± 92517) 1.04
es/full/base/fixer 28320 ns/iter (± 5716) 26396 ns/iter (± 1588) 1.07
es/full/base/resolver_and_hygiene 98593 ns/iter (± 21215) 91086 ns/iter (± 5520) 1.08
serialization of ast node 208 ns/iter (± 7) 220 ns/iter (± 38) 0.95
serialization of serde 222 ns/iter (± 6) 224 ns/iter (± 28) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.