@@ -394,28 +394,52 @@ macro_rules! read_until_close {
394
394
/// Generalization of `read_to_end` method for buffered and borrowed readers
395
395
macro_rules! read_to_end {
396
396
(
397
+ // $self: &mut Reader
397
398
$self: expr, $end: expr, $buf: expr,
398
399
$read_event: ident,
399
400
// Code block that performs clearing of internal buffer after read of each event
400
401
$clear: block
401
402
$( , $await: ident) ?
402
403
) => { {
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
+
403
420
let start = $self. buffer_position( ) ;
404
421
let mut depth = 0 ;
405
422
loop {
406
423
$clear
407
424
let end = $self. buffer_position( ) ;
408
425
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
+ }
410
430
411
431
Ok ( Event :: Start ( e) ) if e. name( ) == $end => depth += 1 ,
412
432
Ok ( Event :: End ( e) ) if e. name( ) == $end => {
413
433
if depth == 0 {
434
+ $self. config_mut( ) . trim_text_start = trim;
414
435
break start..end;
415
436
}
416
437
depth -= 1 ;
417
438
}
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
+ }
419
443
_ => ( ) ,
420
444
}
421
445
}
0 commit comments