Skip to content

Commit

Permalink
Merge pull request #20 from mautier/escape_gt_in_writer
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski authored Mar 31, 2024
2 parents 4cd3b5a + 59d6294 commit d8216c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ escapes!(
escapes!(
PcDataEscapes,
b'<' => "&lt;",
b'>' => "&gt;",
b'&' => "&amp;",
);

Expand Down Expand Up @@ -150,7 +151,7 @@ mod tests {

#[test]
fn test_escape_str_pcdata() {
assert_eq!(escape_str_pcdata("<&"), "&lt;&amp;");
assert_eq!(escape_str_pcdata("<>&"), "&lt;&gt;&amp;");
assert_eq!(escape_str_pcdata("no_escapes"), "no_escapes");
}

Expand Down
27 changes: 27 additions & 0 deletions tests/event_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,30 @@ fn attribute_escaping() {
<hello testNl=\"\\n\" testCr=\"\\r\" />"
);
}

#[test]
fn accidental_cdata_suffix_in_characters_is_escaped() {
let mut b = Vec::new();
{
use xml::writer::XmlEvent;
let mut w = EmitterConfig::new()
.write_document_declaration(false)
.create_writer(&mut b);

unwrap_all! {
w.write(XmlEvent::start_element("root"));
w.write(XmlEvent::characters("[[a]]>b"));
w.write(XmlEvent::end_element())
}
}

{
use xml::reader::{EventReader, XmlEvent};
let mut r = EventReader::new(&b[..]);
assert!(matches!(r.next().unwrap(), XmlEvent::StartDocument{..}));
assert!(matches!(r.next().unwrap(), XmlEvent::StartElement{..}));
assert_eq!(r.next().unwrap(), XmlEvent::Characters("[[a]]>b".into()));
assert!(matches!(r.next().unwrap(), XmlEvent::EndElement{..}));
assert!(matches!(r.next().unwrap(), XmlEvent::EndDocument));
}
}

0 comments on commit d8216c9

Please sign in to comment.