Skip to content

Commit

Permalink
Fix trimming of the trailing spaces in read_text when `trim_text_st…
Browse files Browse the repository at this point in the history
…art` is set

...and the event before the End event is not a Text event
  • Loading branch information
Mingun authored and dralley committed Jun 27, 2024
1 parent 4d67ae4 commit 80f0e7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@

### Bug Fixes

- [#773]: Fixed reporting incorrect end position in `Reader::read_to_end` family
of methods and trimming of the trailing spaces in `Reader::read_text` when
`trim_text_start` is set and the last event is not a `Text` event.

### Misc Changes

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


## 0.34.0 -- 2024-06-25
Expand Down
28 changes: 26 additions & 2 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,28 +394,52 @@ macro_rules! read_until_close {
/// Generalization of `read_to_end` method for buffered and borrowed readers
macro_rules! read_to_end {
(
// $self: &mut Reader
$self:expr, $end:expr, $buf:expr,
$read_event:ident,
// Code block that performs clearing of internal buffer after read of each event
$clear:block
$(, $await:ident)?
) => {{
// Because we take position after the event before the End event,
// it is important that this position indicates beginning of the End event.
// If between last event and the End event would be only spaces, then we
// take position before the spaces, but spaces would be skipped without
// generating event if `trim_text_start` is set to `true`. To prevent that
// we temporary disable start text trimming.
//
// We also cannot take position after getting End event, because if
// `trim_markup_names_in_closing_tags` is set to `true` (which is the default),
// we do not known the real size of the End event that it is occupies in
// the source and cannot correct the position after the End event.
// So, we in any case should tweak parser configuration.
let config = $self.config_mut();
let trim = config.trim_text_start;
config.trim_text_start = false;

let start = $self.buffer_position();
let mut depth = 0;
loop {
$clear
let end = $self.buffer_position();
match $self.$read_event($buf) $(.$await)? {
Err(e) => return Err(e),
Err(e) => {
$self.config_mut().trim_text_start = trim;
return Err(e);
}

Ok(Event::Start(e)) if e.name() == $end => depth += 1,
Ok(Event::End(e)) if e.name() == $end => {
if depth == 0 {
$self.config_mut().trim_text_start = trim;
break start..end;
}
depth -= 1;
}
Ok(Event::Eof) => return Err(Error::missed_end($end, $self.decoder())),
Ok(Event::Eof) => {
$self.config_mut().trim_text_start = trim;
return Err(Error::missed_end($end, $self.decoder()));
}
_ => (),
}
}
Expand Down

0 comments on commit 80f0e7c

Please sign in to comment.