@@ -10,7 +10,7 @@ use memchr;
1010use crate :: errors:: { Error , Result } ;
1111use crate :: events:: Event ;
1212use crate :: name:: QName ;
13- use crate :: reader:: { is_whitespace, BangType , ReadElementState , Reader , XmlSource } ;
13+ use crate :: reader:: { is_whitespace, BangType , ReadElementState , Reader , Span , XmlSource } ;
1414
1515macro_rules! impl_buffered_source {
1616 ( $( $lf: lifetime, $reader: tt, $async: ident, $await: ident) ?) => {
@@ -277,6 +277,10 @@ impl<R: BufRead> Reader<R> {
277277 /// storage for events content. This function is supposed to be called after
278278 /// you already read a [`Start`] event.
279279 ///
280+ /// Returns a span that cover content between `>` of an opening tag and `<` of
281+ /// a closing tag or an empty slice, if [`expand_empty_elements`] is set and
282+ /// this method was called after reading expanded [`Start`] event.
283+ ///
280284 /// Manages nested cases where parent and child elements have the same name.
281285 ///
282286 /// If corresponding [`End`] event will not be found, the [`Error::UnexpectedEof`]
@@ -340,7 +344,7 @@ impl<R: BufRead> Reader<R> {
340344 /// // First, we read a start event...
341345 /// assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Start(start));
342346 ///
343- /// //...then, we could skip all events to the corresponding end event.
347+ /// // ...then, we could skip all events to the corresponding end event.
344348 /// // This call will correctly handle nested <outer> elements.
345349 /// // Note, however, that this method does not handle namespaces.
346350 /// reader.read_to_end_into(end.name(), &mut buf).unwrap();
@@ -353,60 +357,13 @@ impl<R: BufRead> Reader<R> {
353357 /// [`End`]: Event::End
354358 /// [`BytesStart::to_end()`]: crate::events::BytesStart::to_end
355359 /// [`read_to_end()`]: Self::read_to_end
360+ /// [`expand_empty_elements`]: Self::expand_empty_elements
356361 /// [`check_end_names`]: Self::check_end_names
357362 /// [the specification]: https://www.w3.org/TR/xml11/#dt-etag
358- pub fn read_to_end_into ( & mut self , end : QName , buf : & mut Vec < u8 > ) -> Result < ( ) > {
359- read_to_end ! ( self , end, buf, read_event_impl, {
363+ pub fn read_to_end_into ( & mut self , end : QName , buf : & mut Vec < u8 > ) -> Result < Span > {
364+ Ok ( read_to_end ! ( self , end, buf, read_event_impl, {
360365 buf. clear( ) ;
361- } )
362- }
363-
364- /// Reads optional text between start and end tags.
365- ///
366- /// If the next event is a [`Text`] event, returns the decoded and unescaped content as a
367- /// `String`. If the next event is an [`End`] event, returns the empty string. In all other
368- /// cases, returns an error.
369- ///
370- /// Any text will be decoded using the XML encoding specified in the XML declaration (or UTF-8
371- /// if none is specified).
372- ///
373- /// # Examples
374- ///
375- /// ```
376- /// # use pretty_assertions::assert_eq;
377- /// use quick_xml::Reader;
378- /// use quick_xml::events::Event;
379- ///
380- /// let mut xml = Reader::from_reader(b"
381- /// <a><b></a>
382- /// <a></a>
383- /// " as &[u8]);
384- /// xml.trim_text(true);
385- ///
386- /// let expected = ["<b>", ""];
387- /// for &content in expected.iter() {
388- /// match xml.read_event_into(&mut Vec::new()) {
389- /// Ok(Event::Start(ref e)) => {
390- /// assert_eq!(&xml.read_text_into(e.name(), &mut Vec::new()).unwrap(), content);
391- /// },
392- /// e => panic!("Expecting Start event, found {:?}", e),
393- /// }
394- /// }
395- /// ```
396- ///
397- /// [`Text`]: Event::Text
398- /// [`End`]: Event::End
399- pub fn read_text_into ( & mut self , end : QName , buf : & mut Vec < u8 > ) -> Result < String > {
400- let s = match self . read_event_into ( buf) {
401- Err ( e) => return Err ( e) ,
402-
403- Ok ( Event :: Text ( e) ) => e. unescape ( ) ?. into_owned ( ) ,
404- Ok ( Event :: End ( e) ) if e. name ( ) == end => return Ok ( "" . to_string ( ) ) ,
405- Ok ( Event :: Eof ) => return Err ( Error :: UnexpectedEof ( "Text" . to_string ( ) ) ) ,
406- _ => return Err ( Error :: TextNotFound ) ,
407- } ;
408- self . read_to_end_into ( end, buf) ?;
409- Ok ( s)
366+ } ) )
410367 }
411368}
412369
0 commit comments