encoding.xml: fix parsing of single letter tag names (#19815)

This commit is contained in:
Subhomoy Haldar 2023-11-09 11:02:05 +00:00 committed by GitHub
parent 10160c0cae
commit cd2e36a44d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 3 deletions

View File

@ -534,9 +534,12 @@ fn parse_single_node(first_char u8, mut reader io.Reader) !XMLNode {
if ch == `/` { if ch == `/` {
return error('XML node cannot start with "</".') return error('XML node cannot start with "</".')
} }
contents.write_u8(ch)
for { if ch != `>` {
contents.write_u8(ch)
}
for ch != `>` {
ch = next_char(mut reader, mut local_buf)! ch = next_char(mut reader, mut local_buf)!
if ch == `>` { if ch == `>` {
break break
@ -547,7 +550,7 @@ fn parse_single_node(first_char u8, mut reader io.Reader) !XMLNode {
tag_contents := contents.str().trim_space() tag_contents := contents.str().trim_space()
parts := tag_contents.split_any(' \t\n') parts := tag_contents.split_any(' \t\n')
name := first_char.ascii_str() + parts[0] name := if parts.len > 0 { first_char.ascii_str() + parts[0] } else { first_char.ascii_str() }
// Check if it is a self-closing tag // Check if it is a self-closing tag
if tag_contents.ends_with('/') { if tag_contents.ends_with('/') {

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst count="5" uniqueCount="5">
<si>
<t>Item 1</t>
</si>
<si>
<t>Item 2</t>
</si>
<si>
<t>Item 3</t>
</si>
<si>
<t>Item 4</t>
</si>
<si>
<t>Item 5</t>
</si>
</sst>

View File

@ -0,0 +1,68 @@
module main
import os
import encoding.xml
fn test_valid_parsing() {
path := os.join_path(os.dir(@FILE), 'shared.xml')
expected := xml.XMLDocument{
root: xml.XMLNode{
name: 'sst'
attributes: {
'count': '5'
'uniqueCount': '5'
}
children: [
xml.XMLNode{
name: 'si'
children: [
xml.XMLNode{
name: 't'
children: ['Item 1']
},
]
},
xml.XMLNode{
name: 'si'
children: [
xml.XMLNode{
name: 't'
children: ['Item 2']
},
]
},
xml.XMLNode{
name: 'si'
children: [
xml.XMLNode{
name: 't'
children: ['Item 3']
},
]
},
xml.XMLNode{
name: 'si'
children: [
xml.XMLNode{
name: 't'
children: ['Item 4']
},
]
},
xml.XMLNode{
name: 'si'
children: [
xml.XMLNode{
name: 't'
children: ['Item 5']
},
]
},
]
}
}
actual := xml.XMLDocument.from_file(path)!
assert expected == actual, 'Parsed XML document should be equal to expected XML document'
}