Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Keep CDATA text in the DOCX/XLSX XML parser

NetterSilver177ac8674195
diff --git a/crates/textimport/src/docx.rs b/crates/textimport/src/docx.rs
index 783b010c6..4d90a6b7a 100644
--- a/crates/textimport/src/docx.rs
+++ b/crates/textimport/src/docx.rs
@@ -115,6 +115,12 @@ pub(crate) fn parse(xml: &str) -> Result<El, ImportError> {
p.kids.push(Node::Text(s));
}
}
+ Event::CData(t) => {
+ let s = t.decode().map(|s| s.to_string()).unwrap_or_default();
+ if let Some(p) = stack.last_mut() {
+ p.kids.push(Node::Text(s));
+ }
+ }
Event::GeneralRef(e) => {
let name = String::from_utf8_lossy(&e).to_string();
let s = match name.as_str() {
@@ -578,6 +584,12 @@ mod tests {
use super::*;
use std::io::Write;
+ #[test]
+ fn cdata_text() {
+ let el = parse("<a><t><![CDATA[x < y]]></t></a>").unwrap();
+ assert_eq!(el.child("t").unwrap().text(), "x < y");
+ }
+
/// Issue #712: nesting past the budget is an error from the parser, not a stack overflow later.
#[test]
fn nesting_past_the_budget_is_refused() {