Skip to content

Commit 80f0e7c

Browse files
Mingundralley
authored andcommitted
Fix trimming of the trailing spaces in read_text when trim_text_start is set
...and the event before the End event is not a Text event
1 parent 4d67ae4 commit 80f0e7c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@
1919

2020
### Bug Fixes
2121

22+
- [#773]: Fixed reporting incorrect end position in `Reader::read_to_end` family
23+
of methods and trimming of the trailing spaces in `Reader::read_text` when
24+
`trim_text_start` is set and the last event is not a `Text` event.
25+
2226
### Misc Changes
2327

2428
[#772]: https://github.com/tafia/quick-xml/pull/772
29+
[#773]: https://github.com/tafia/quick-xml/pull/773
2530

2631

2732
## 0.34.0 -- 2024-06-25

src/reader/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,28 +394,52 @@ macro_rules! read_until_close {
394394
/// Generalization of `read_to_end` method for buffered and borrowed readers
395395
macro_rules! read_to_end {
396396
(
397+
// $self: &mut Reader
397398
$self:expr, $end:expr, $buf:expr,
398399
$read_event:ident,
399400
// Code block that performs clearing of internal buffer after read of each event
400401
$clear:block
401402
$(, $await:ident)?
402403
) => {{
404+
// Because we take position after the event before the End event,
405+
// it is important that this position indicates beginning of the End event.
406+
// If between last event and the End event would be only spaces, then we
407+
// take position before the spaces, but spaces would be skipped without
408+
// generating event if `trim_text_start` is set to `true`. To prevent that
409+
// we temporary disable start text trimming.
410+
//
411+
// We also cannot take position after getting End event, because if
412+
// `trim_markup_names_in_closing_tags` is set to `true` (which is the default),
413+
// we do not known the real size of the End event that it is occupies in
414+
// the source and cannot correct the position after the End event.
415+
// So, we in any case should tweak parser configuration.
416+
let config = $self.config_mut();
417+
let trim = config.trim_text_start;
418+
config.trim_text_start = false;
419+
403420
let start = $self.buffer_position();
404421
let mut depth = 0;
405422
loop {
406423
$clear
407424
let end = $self.buffer_position();
408425
match $self.$read_event($buf) $(.$await)? {
409-
Err(e) => return Err(e),
426+
Err(e) => {
427+
$self.config_mut().trim_text_start = trim;
428+
return Err(e);
429+
}
410430

411431
Ok(Event::Start(e)) if e.name() == $end => depth += 1,
412432
Ok(Event::End(e)) if e.name() == $end => {
413433
if depth == 0 {
434+
$self.config_mut().trim_text_start = trim;
414435
break start..end;
415436
}
416437
depth -= 1;
417438
}
418-
Ok(Event::Eof) => return Err(Error::missed_end($end, $self.decoder())),
439+
Ok(Event::Eof) => {
440+
$self.config_mut().trim_text_start = trim;
441+
return Err(Error::missed_end($end, $self.decoder()));
442+
}
419443
_ => (),
420444
}
421445
}

0 commit comments

Comments
 (0)