Add basic support for CDATA

This commit is contained in:
Dirkjan Ochtman 2023-10-25 16:00:17 +02:00
parent a6774baef4
commit 3372ccb981
2 changed files with 28 additions and 0 deletions

View File

@ -339,6 +339,9 @@ impl<'xml> Iterator for Context<'xml> {
Ok(Token::Text { text }) => { Ok(Token::Text { text }) => {
return Some(decode(text.as_str()).map(Node::Text)); return Some(decode(text.as_str()).map(Node::Text));
} }
Ok(Token::Cdata { text, .. }) => {
return Some(Ok(Node::Text(Cow::Borrowed(text.as_str()))));
}
Ok(Token::Declaration { .. }) => match self.stack.is_empty() { Ok(Token::Declaration { .. }) => match self.stack.is_empty() {
false => return Some(Err(Error::UnexpectedToken(format!("{token:?}")))), false => return Some(Err(Error::UnexpectedToken(format!("{token:?}")))),
true => {} true => {}

View File

@ -55,3 +55,28 @@ fn special_entities() {
"<StructSpecialEntities xmlns=\"URI\"><string>&amp;&quot;&lt;&gt;&apos;aa</string><cow>&amp;&quot;&lt;&gt;&apos;cc</cow></StructSpecialEntities>", "<StructSpecialEntities xmlns=\"URI\"><string>&amp;&quot;&lt;&gt;&apos;aa</string><cow>&amp;&quot;&lt;&gt;&apos;cc</cow></StructSpecialEntities>",
); );
} }
#[derive(Debug, PartialEq, Eq, FromXml, ToXml)]
struct SimpleCData<'a> {
#[xml(borrow)]
foo: Cow<'a, str>,
}
#[test]
fn simple_cdata() {
assert_eq!(
from_str::<SimpleCData>("<SimpleCData><foo><![CDATA[<fo&amp;o>]]></foo></SimpleCData>")
.unwrap(),
SimpleCData {
foo: Cow::Borrowed("<fo&amp;o>")
}
);
assert_eq!(
to_string(&SimpleCData {
foo: Cow::Borrowed("<foo>")
})
.unwrap(),
"<SimpleCData><foo>&lt;foo&gt;</foo></SimpleCData>",
);
}