@@ -1852,7 +1852,7 @@ use std::io::BufRead;
1852
1852
use std:: mem:: replace;
1853
1853
#[ cfg( feature = "overlapped-lists" ) ]
1854
1854
use std:: num:: NonZeroUsize ;
1855
- use std:: ops:: Deref ;
1855
+ use std:: ops:: { Deref , Range } ;
1856
1856
1857
1857
/// Data represented by a text node or a CDATA node. XML markup is not expected
1858
1858
pub ( crate ) const TEXT_KEY : & str = "$text" ;
@@ -1869,7 +1869,24 @@ pub(crate) const VALUE_KEY: &str = "$value";
1869
1869
/// [`PI`]: Event::PI
1870
1870
#[ derive( Debug , PartialEq , Eq ) ]
1871
1871
pub struct Text < ' a > {
1872
+ /// Untrimmed text after concatenating content of all
1873
+ /// [`Text`] and [`CData`] events
1872
1874
text : Cow < ' a , str > ,
1875
+ /// A range into `text` which contains data after trimming
1876
+ content : Range < usize > ,
1877
+ }
1878
+
1879
+ impl < ' a > Text < ' a > {
1880
+ fn new ( text : Cow < ' a , str > ) -> Self {
1881
+ let start = text. find ( |c| !char:: is_whitespace ( c) ) . unwrap_or ( 0 ) ;
1882
+ let end = text
1883
+ . rfind ( |c| !char:: is_whitespace ( c) )
1884
+ . unwrap_or ( text. len ( ) . saturating_sub ( 1 ) ) ;
1885
+ Self {
1886
+ text,
1887
+ content : start..end,
1888
+ }
1889
+ }
1873
1890
}
1874
1891
1875
1892
impl < ' a > Deref for Text < ' a > {
@@ -1884,9 +1901,7 @@ impl<'a> Deref for Text<'a> {
1884
1901
impl < ' a > From < & ' a str > for Text < ' a > {
1885
1902
#[ inline]
1886
1903
fn from ( text : & ' a str ) -> Self {
1887
- Self {
1888
- text : Cow :: Borrowed ( text) ,
1889
- }
1904
+ Self :: new ( Cow :: Borrowed ( text) )
1890
1905
}
1891
1906
}
1892
1907
@@ -2010,7 +2025,7 @@ impl<'i, R: XmlRead<'i>> XmlReader<'i, R> {
2010
2025
_ => break ,
2011
2026
}
2012
2027
}
2013
- Ok ( DeEvent :: Text ( Text { text : result } ) )
2028
+ Ok ( DeEvent :: Text ( Text :: new ( result) ) )
2014
2029
}
2015
2030
2016
2031
/// Read one text event, panics if current event is not a text event
0 commit comments