Skip to content

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

Merge PR #1054: Keep text of inline content controls in DOCX import

aiongg3ea3cd97dd46
diff --cc crates/textimport/src/docx.rs
index bc2afc8ad,6d3d05d53..24843c64c
--- a/crates/textimport/src/docx.rs
+++ b/crates/textimport/src/docx.rs
@@@ -640,6 -626,38 +646,20 @@@ mod tests
assert_eq!(i.char_styles[0].chars.font_style.as_deref(), Some("Italic"));
}
+ /// A content control inside a paragraph (`w:sdt`) keeps its text, also around a hyperlink.
+ #[test]
+ fn inline_content_control_text_is_kept() {
+ let document = format!(
+ r#"<w:document {W}><w:body>
+ <w:p><w:r><w:t xml:space="preserve">Date: </w:t></w:r><w:sdt><w:sdtPr><w:alias w:val="d"/></w:sdtPr><w:sdtContent><w:r><w:t>1 May 2026</w:t></w:r></w:sdtContent></w:sdt><w:r><w:t>.</w:t></w:r></w:p>
+ <w:p><w:sdt><w:sdtContent><w:hyperlink><w:r><w:t>Link</w:t></w:r></w:hyperlink></w:sdtContent></w:sdt></w:p>
+ </w:body></w:document>"#
+ );
+ let st = import(&docx(&document, "", "")).unwrap().story;
+ st.check().unwrap();
+ assert_eq!(st.text, "Date: 1 May 2026.\nLink");
+ }
+
- /// A page break ends its paragraph: the text after it is a paragraph of its own (same style).
- #[test]
- fn page_break_ends_its_paragraph() {
- let styles = format!(r#"<w:styles {W}><w:style w:type="paragraph" w:styleId="H"><w:name w:val="Chapter"/></w:style></w:styles>"#);
- let document = format!(
- r#"<w:document {W}><w:body>
- <w:p><w:r><w:t>End of one</w:t></w:r><w:r><w:br w:type="page"/></w:r></w:p>
- <w:p><w:pPr><w:pStyle w:val="H"/></w:pPr><w:r><w:br w:type="page"/><w:t>Two</w:t><w:br w:type="column"/></w:r></w:p>
- </w:body></w:document>"#
- );
- let st = import(&docx(&document, &styles, "")).unwrap().story;
- st.check().unwrap();
- let (pb, cb) = (designcraft_doc::PAGE_BREAK, designcraft_doc::COLUMN_BREAK);
- assert_eq!(st.text, format!("End of one{pb}\n{pb}\nTwo{cb}\n"));
- let styles: Vec<&str> = st.paras.iter().map(|p| p.style.as_str()).collect();
- assert_eq!(styles, [BASIC_PARAGRAPH, "Chapter", "Chapter", "Chapter"]);
- }
-
#[test]
fn tracked_changes_import_as_accepted_including_moves() {
// Regression (#256): text in a tracked move destination (`w:moveTo`) vanished on import.