Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
- `Deserializer::buffering_with_resolver`
- [#878]: Add ability to serialize structs in `$value` fields. The struct name will
be used as a tag name. Previously only enums was allowed there.
- [#806]: Add `BytesText::xml_content`, `BytesCData::xml_content` and `BytesRef::xml_content`
methods which returns XML EOL normalized strings.
- [#806]: Add `BytesText::html_content`, `BytesCData::html_content` and `BytesRef::html_content`
methods which returns HTML EOL normalized strings.

### Bug Fixes

- [#806]: Properly normalize EOL characters in `Deserializer`.

### Misc Changes

[#806]: https://github.com/tafia/quick-xml/issues/806
[#878]: https://github.com/tafia/quick-xml/pull/878
[#882]: https://github.com/tafia/quick-xml/pull/882

Expand Down
8 changes: 4 additions & 4 deletions benches/macrobenches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn parse_document_from_str(doc: &str) -> XmlResult<()> {
}
}
Event::Text(e) => {
black_box(e.decode()?);
black_box(e.xml_content()?);
}
Event::CData(e) => {
black_box(e.into_inner());
Expand All @@ -80,7 +80,7 @@ fn parse_document_from_bytes(doc: &[u8]) -> XmlResult<()> {
}
}
Event::Text(e) => {
black_box(e.decode()?);
black_box(e.xml_content()?);
}
Event::CData(e) => {
black_box(e.into_inner());
Expand All @@ -106,7 +106,7 @@ fn parse_document_from_str_with_namespaces(doc: &str) -> XmlResult<()> {
}
}
(resolved_ns, Event::Text(e)) => {
black_box(e.decode()?);
black_box(e.xml_content()?);
black_box(resolved_ns);
}
(resolved_ns, Event::CData(e)) => {
Expand Down Expand Up @@ -134,7 +134,7 @@ fn parse_document_from_bytes_with_namespaces(doc: &[u8]) -> XmlResult<()> {
}
}
(resolved_ns, Event::Text(e)) => {
black_box(e.decode()?);
black_box(e.xml_content()?);
black_box(resolved_ns);
}
(resolved_ns, Event::CData(e)) => {
Expand Down
2 changes: 1 addition & 1 deletion benches/microbenches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn one_event(c: &mut Criterion) {
config.trim_text(true);
config.check_end_names = false;
match r.read_event() {
Ok(Event::Comment(e)) => nbtxt += e.decode().unwrap().len(),
Ok(Event::Comment(e)) => nbtxt += e.xml_content().unwrap().len(),
something_else => panic!("Did not expect {:?}", something_else),
};

Expand Down
8 changes: 4 additions & 4 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2439,8 +2439,8 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
}

match self.next_impl()? {
PayloadEvent::Text(e) => result.to_mut().push_str(&e.decode()?),
PayloadEvent::CData(e) => result.to_mut().push_str(&e.decode()?),
PayloadEvent::Text(e) => result.to_mut().push_str(&e.xml_content()?),
PayloadEvent::CData(e) => result.to_mut().push_str(&e.xml_content()?),
PayloadEvent::GeneralRef(e) => self.resolve_reference(result.to_mut(), e)?,

// SAFETY: current_event_is_last_text checks that event is Text, CData or GeneralRef
Expand All @@ -2456,8 +2456,8 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
return match self.next_impl()? {
PayloadEvent::Start(e) => Ok(DeEvent::Start(e)),
PayloadEvent::End(e) => Ok(DeEvent::End(e)),
PayloadEvent::Text(e) => self.drain_text(e.decode()?),
PayloadEvent::CData(e) => self.drain_text(e.decode()?),
PayloadEvent::Text(e) => self.drain_text(e.xml_content()?),
PayloadEvent::CData(e) => self.drain_text(e.xml_content()?),
PayloadEvent::DocType(e) => {
self.entity_resolver
.capture(e)
Expand Down
24 changes: 24 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ impl Decoder {
Cow::Owned(bytes) => Ok(self.decode(bytes)?.into_owned().into()),
}
}

/// Decodes the `Cow` buffer, normalizes XML EOLs, preserves the lifetime
pub(crate) fn content<'b>(
&self,
bytes: &Cow<'b, [u8]>,
normalize_eol: impl Fn(&str) -> Cow<str>,
) -> Result<Cow<'b, str>, EncodingError> {
match bytes {
Cow::Borrowed(bytes) => {
let text = self.decode(bytes)?;
match normalize_eol(&text) {
// If text borrowed after normalization that means that it's not changed
Cow::Borrowed(_) => Ok(text),
Cow::Owned(s) => Ok(Cow::Owned(s)),
}
}
Cow::Owned(bytes) => {
let text = self.decode(bytes)?;
let text = normalize_eol(&text);
// Convert to owned, because otherwise Cow will be bound with wrong lifetime
Ok(text.into_owned().into())
}
}
}
}

/// Decodes the provided bytes using the specified encoding.
Expand Down
Loading