@@ -394,28 +394,52 @@ macro_rules! read_until_close {
394394/// Generalization of `read_to_end` method for buffered and borrowed readers
395395macro_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