Skip to content

Commit bdf9f46

Browse files
committed
Fix #363: Do not generate empty Text events
1 parent 5cf2ed4 commit bdf9f46

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [#393]: Now `event_namespace`, `attribute_namespace` and `read_event_namespaced`
3030
returns `ResolveResult::Unknown` if prefix was not registered in namespace buffer
3131
- [#393]: Fix breaking processing after encounter an attribute with a reserved name (started with "xmlns")
32+
- [#363]: Do not generate empty `Event::Text` events
3233

3334
### Misc Changes
3435

src/reader.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,11 @@ impl<R: BufRead> Reader<R> {
266266

267267
if self.trim_text_start {
268268
self.reader.skip_whitespace(&mut self.buf_position)?;
269-
if self.reader.skip_one(b'<', &mut self.buf_position)? {
270-
return self.read_event_buffered(buf);
271-
}
269+
}
270+
271+
// If we already at the `<` symbol, do not try to return an empty Text event
272+
if self.reader.skip_one(b'<', &mut self.buf_position)? {
273+
return self.read_event_buffered(buf);
272274
}
273275

274276
match self

tests/unit_tests.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,7 @@ fn test_trim_test() {
168168

169169
let mut r = Reader::from_str(txt);
170170
r.trim_text(false);
171-
next_eq!(
172-
r, Text, b"", Start, b"a", Text, b"", Start, b"b", Text, b" ", End, b"b", Text, b"", End,
173-
b"a"
174-
);
171+
next_eq!(r, Start, b"a", Start, b"b", Text, b" ", End, b"b", End, b"a");
175172
}
176173

177174
#[test]

tests/xmlrs_reader_tests.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,7 @@ fn issue_98_cdata_ending_with_right_bracket() {
257257
r#"<hello><![CDATA[Foo [Bar]]]></hello>"#,
258258
r#"
259259
|StartElement(hello)
260-
|Characters()
261260
|CData(Foo [Bar])
262-
|Characters()
263261
|EndElement(hello)
264262
|EndDocument
265263
"#,
@@ -306,9 +304,7 @@ fn issue_105_unexpected_double_dash() {
306304
r#"<hello><![CDATA[--]]></hello>"#,
307305
r#"
308306
|StartElement(hello)
309-
|Characters()
310307
|CData(--)
311-
|Characters()
312308
|EndElement(hello)
313309
|EndDocument
314310
"#,
@@ -359,10 +355,12 @@ fn default_namespace_applies_to_end_elem() {
359355
);
360356
}
361357

358+
#[track_caller]
362359
fn test(input: &str, output: &str, is_short: bool) {
363360
test_bytes(input.as_bytes(), output.as_bytes(), is_short);
364361
}
365362

363+
#[track_caller]
366364
fn test_bytes(input: &[u8], output: &[u8], is_short: bool) {
367365
// Normalize newlines on Windows to just \n, which is what the reader and
368366
// writer use.
@@ -380,11 +378,6 @@ fn test_bytes(input: &[u8], output: &[u8], is_short: bool) {
380378
let mut buf = Vec::new();
381379
let mut ns_buffer = Vec::new();
382380

383-
if !is_short {
384-
// discard first whitespace
385-
reader.read_event(&mut buf).unwrap();
386-
}
387-
388381
loop {
389382
buf.clear();
390383
let event = reader.read_namespaced_event(&mut buf, &mut ns_buffer);

0 commit comments

Comments
 (0)