Skip to content

Commit

Permalink
Fix ]]> escaping in CDATA sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 authored and andy128k committed Nov 20, 2024
1 parent 95a25ee commit bc38dfb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ validation = ["chrono", "chrono/std", "url", "mime"]
with-serde = ["serde", "atom_syndication/with-serde"]

[dependencies]
quick-xml = { version = "0.37", features = ["encoding"] }
quick-xml = { version = "0.37.1", features = ["encoding"] }
atom_syndication = { version = "0.12", optional = true }
chrono = { version = "0.4.31", optional = true, default-features = false, features = ["alloc"] }
derive_builder = { version = "0.20", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion src/toxml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ impl<W: Write> WriterExt for Writer<W> {
{
let name = name.as_ref();
self.write_event(Event::Start(BytesStart::new(name)))?;
self.write_event(Event::CData(BytesCData::new(text.as_ref())))?;
BytesCData::escaped(text.as_ref())
.try_for_each(|event| self.write_event(Event::CData(event)))?;
self.write_event(Event::End(BytesEnd::new(name)))?;
Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions tests/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ fn test_escape() {
.value("51ed8fb6-e7db-4b1d-a75a-0d1621e895b4")
.build(),
)
.description("let's try & break this <item> ]]>, shall we?".to_owned())
.content("Lorem ipsum dolor sit amet".to_owned())
.enclosure(
EnclosureBuilder::default()
Expand Down Expand Up @@ -290,6 +291,7 @@ fn test_escape() {
assert!(xml.contains("http://example.com?test=1&amp;another=true"));
assert!(xml.contains("http://example.com?test=2&amp;another=false"));
assert!(xml.contains("&lt;title&gt;"));
assert!(xml.contains("<![CDATA[let's try & break this <item> ]]]]><![CDATA[>, shall we?]]>"));

let channel = rss::Channel::read_from(xml.as_bytes()).unwrap();

Expand Down Expand Up @@ -323,6 +325,10 @@ fn test_escape() {
.unwrap(),
"<title>"
);
assert_eq!(
channel.items[0].description.as_ref().unwrap(),
"let's try & break this <item> ]]>, shall we?"
);
}

#[test]
Expand Down

0 comments on commit bc38dfb

Please sign in to comment.