Skip to content

Commit 1e6b1ef

Browse files
authored
Do not expand predefined references in Stream::consume_reference
1 parent 395dc58 commit 1e6b1ef

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/stream.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ impl<'a> Stream<'a> {
349349
/// Consumes an XML character reference if there is one.
350350
///
351351
/// On error will reset the position to the original.
352+
///
353+
/// See [`consume_reference()`] for details.
354+
///
355+
/// [`consume_reference()`]: #method.consume_reference
352356
pub fn try_consume_reference(&mut self) -> Option<Reference<'a>> {
353357
let start = self.pos();
354358

@@ -374,6 +378,35 @@ impl<'a> Stream<'a> {
374378
/// # Errors
375379
///
376380
/// - `InvalidReference`
381+
///
382+
/// # Examples
383+
///
384+
/// Escaped character:
385+
///
386+
/// ```
387+
/// use xmlparser::{Stream, Reference};
388+
///
389+
/// let mut stream = Stream::from("&#xe13;");
390+
/// assert_eq!(stream.consume_reference().unwrap(), Reference::Char('\u{e13}'));
391+
/// ```
392+
///
393+
/// Predefined references will not be expanded.
394+
/// [Details.]( https://www.w3.org/TR/xml/#sec-entexpand)
395+
///
396+
/// ```
397+
/// use xmlparser::{Stream, Reference};
398+
///
399+
/// let mut stream = Stream::from("&le;");
400+
/// assert_eq!(stream.consume_reference().unwrap(), Reference::Entity("le"));
401+
/// ```
402+
///
403+
/// Named reference:
404+
///
405+
/// ```
406+
/// use xmlparser::{Stream, Reference};
407+
///
408+
/// let mut stream = Stream::from("&other;");
409+
/// assert_eq!(stream.consume_reference().unwrap(), Reference::Entity("other"));
377410
pub fn consume_reference(&mut self) -> Result<Reference<'a>> {
378411
self._consume_reference().map_err(|_| StreamError::InvalidReference)
379412
}
@@ -403,14 +436,7 @@ impl<'a> Stream<'a> {
403436
Reference::Char(c)
404437
} else {
405438
let name = self.consume_name()?;
406-
match name.as_str() {
407-
"quot" => Reference::Char('"'),
408-
"amp" => Reference::Char('&'),
409-
"apos" => Reference::Char('\''),
410-
"lt" => Reference::Char('<'),
411-
"gt" => Reference::Char('>'),
412-
_ => Reference::Entity(name.as_str()),
413-
}
439+
Reference::Entity(name.as_str())
414440
};
415441

416442
self.consume_byte(b';')?;

0 commit comments

Comments
 (0)