Skip to content

Commit 085c142

Browse files
committed
Properly normalize EOL characters in Deserializer in elements (but not in attributes yet)
Use `xml_content` instead of `decode` in serde deserializer and tests
1 parent a091bfd commit 085c142

File tree

9 files changed

+19
-15
lines changed

9 files changed

+19
-15
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
### Bug Fixes
3131

32+
- [#806]: Properly normalize EOL characters in `Deserializer`.
33+
3234
### Misc Changes
3335

3436
[#806]: https://github.com/tafia/quick-xml/issues/806

benches/macrobenches.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn parse_document_from_str(doc: &str) -> XmlResult<()> {
5555
}
5656
}
5757
Event::Text(e) => {
58-
black_box(e.decode()?);
58+
black_box(e.xml_content()?);
5959
}
6060
Event::CData(e) => {
6161
black_box(e.into_inner());
@@ -80,7 +80,7 @@ fn parse_document_from_bytes(doc: &[u8]) -> XmlResult<()> {
8080
}
8181
}
8282
Event::Text(e) => {
83-
black_box(e.decode()?);
83+
black_box(e.xml_content()?);
8484
}
8585
Event::CData(e) => {
8686
black_box(e.into_inner());
@@ -106,7 +106,7 @@ fn parse_document_from_str_with_namespaces(doc: &str) -> XmlResult<()> {
106106
}
107107
}
108108
(resolved_ns, Event::Text(e)) => {
109-
black_box(e.decode()?);
109+
black_box(e.xml_content()?);
110110
black_box(resolved_ns);
111111
}
112112
(resolved_ns, Event::CData(e)) => {
@@ -134,7 +134,7 @@ fn parse_document_from_bytes_with_namespaces(doc: &[u8]) -> XmlResult<()> {
134134
}
135135
}
136136
(resolved_ns, Event::Text(e)) => {
137-
black_box(e.decode()?);
137+
black_box(e.xml_content()?);
138138
black_box(resolved_ns);
139139
}
140140
(resolved_ns, Event::CData(e)) => {

benches/microbenches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn one_event(c: &mut Criterion) {
146146
config.trim_text(true);
147147
config.check_end_names = false;
148148
match r.read_event() {
149-
Ok(Event::Comment(e)) => nbtxt += e.decode().unwrap().len(),
149+
Ok(Event::Comment(e)) => nbtxt += e.xml_content().unwrap().len(),
150150
something_else => panic!("Did not expect {:?}", something_else),
151151
};
152152

src/de/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,8 +2439,8 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
24392439
}
24402440

24412441
match self.next_impl()? {
2442-
PayloadEvent::Text(e) => result.to_mut().push_str(&e.decode()?),
2443-
PayloadEvent::CData(e) => result.to_mut().push_str(&e.decode()?),
2442+
PayloadEvent::Text(e) => result.to_mut().push_str(&e.xml_content()?),
2443+
PayloadEvent::CData(e) => result.to_mut().push_str(&e.xml_content()?),
24442444
PayloadEvent::GeneralRef(e) => self.resolve_reference(result.to_mut(), e)?,
24452445

24462446
// SAFETY: current_event_is_last_text checks that event is Text, CData or GeneralRef
@@ -2456,8 +2456,8 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
24562456
return match self.next_impl()? {
24572457
PayloadEvent::Start(e) => Ok(DeEvent::Start(e)),
24582458
PayloadEvent::End(e) => Ok(DeEvent::End(e)),
2459-
PayloadEvent::Text(e) => self.drain_text(e.decode()?),
2460-
PayloadEvent::CData(e) => self.drain_text(e.decode()?),
2459+
PayloadEvent::Text(e) => self.drain_text(e.xml_content()?),
2460+
PayloadEvent::CData(e) => self.drain_text(e.xml_content()?),
24612461
PayloadEvent::DocType(e) => {
24622462
self.entity_resolver
24632463
.capture(e)

tests/encodings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn test_koi8_r_encoding() {
3737
loop {
3838
match r.read_event_into(&mut buf) {
3939
Ok(Text(e)) => {
40-
e.decode().unwrap();
40+
e.xml_content().unwrap();
4141
}
4242
Ok(Eof) => break,
4343
_ => (),

tests/fuzzing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn fuzz_101() {
3838
}
3939
}
4040
Ok(Event::Text(e)) => {
41-
if e.decode().is_err() {
41+
if e.xml_content().is_err() {
4242
break;
4343
}
4444
}

tests/reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn test_escaped_content() {
172172
"content unexpected: expecting 'test', got '{:?}'",
173173
from_utf8(&e)
174174
);
175-
match e.decode() {
175+
match e.xml_content() {
176176
Ok(c) => assert_eq!(c, "test"),
177177
Err(e) => panic!(
178178
"cannot escape content at position {}: {:?}",

tests/roundtrip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn reescape_text() {
236236
match reader.read_event().unwrap() {
237237
Eof => break,
238238
Text(e) => {
239-
let t = e.decode().unwrap();
239+
let t = e.xml_content().unwrap();
240240
assert!(writer.write_event(Text(BytesText::new(&t))).is_ok());
241241
}
242242
e => assert!(writer.write_event(e).is_ok()),

tests/serde-se.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,9 +1897,11 @@ mod with_root {
18971897
<root>3</root>");
18981898
serialize_as!(tuple:
18991899
// Use to_string() to get owned type that is required for deserialization
1900-
("<\"&'>".to_string(), "with\t\r\n spaces", 3usize)
1900+
// NOTE: do not use \r, because it normalized to \n during deserialziation
1901+
// but writes as is during serialization
1902+
("<\"&'>".to_string(), "with\t\n spaces", 3usize)
19011903
=> "<root>&lt;\"&amp;'&gt;</root>\
1902-
<root>with\t\r\n spaces</root>\
1904+
<root>with\t\n spaces</root>\
19031905
<root>3</root>");
19041906
serialize_as!(tuple_struct:
19051907
Tuple(42.0, "answer")

0 commit comments

Comments
 (0)