diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 820b85ee7..4b0ae856a 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -362,7 +362,7 @@ impl Writer<'_> { } fn write_shape(&mut self, w: &mut W, o: &InlineObject, props: &CharProps, rels: &mut PartRels, depth: usize) { - let InlineObject::Shape { kind, w: sw, h: sh, float, effects, freeform, extra, .. } = o else { return }; + let InlineObject::Shape { kind, w: sw, h: sh, float, effects, freeform, extra, story, .. } = o else { return }; self.rev_open(w, props); w.open("w:r", &[]); self.rpr(w, props); @@ -371,7 +371,11 @@ impl Writer<'_> { // VML fallback instead of refusing the file. A shape without one stays bare: guarded with // no fallback, that reader would open the file without the shape and drop it on its next // save, which is worse than refusing the file. - let fallback = freeform.is_none() && vml_tag(*kind).is_some(); + // VML currently has no chain IDs or mso-next-textbox links. Keep every member of a + // linked story bare, including its head: an unlinked fallback would silently lose + // overflow text in older readers. Unlinked boxes still get the compatibility fallback. + let linked = story.is_some_and(|id| self.txbx_links.contains_key(&id)); + let fallback = freeform.is_none() && vml_tag(*kind).is_some() && !linked; if fallback { w.open("mc:AlternateContent", &[]); w.open("mc:Choice", &[("Requires", "wps")]); diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs index e2d960cf9..9b209082b 100644 --- a/crates/docx/tests/it/wps_fallback.rs +++ b/crates/docx/tests/it/wps_fallback.rs @@ -156,3 +156,33 @@ fn a_shape_without_a_vml_form_is_not_guarded() { }); assert_eq!(kinds, vec![ShapeKind::Star]); } + +/// A partial fallback must not turn a linked story into independent boxes in older readers. +#[test] +fn linked_boxes_stay_bare_while_unlinked_boxes_keep_their_fallback() { + let mut d = Document::from_text("a\nb\nc\nd"); + let mut ids = Vec::new(); + for (i, text) in ["Flows on", "", "", "Independent"].into_iter().enumerate() { + let id = d.add_part(PartKind::TextBox, vec![para_block(craft_wordmodel::Paragraph::with_text(text, Default::default()))]); + ids.push(id); + d.insert_object(&craft_wordmodel::Pos::body(i, 1), shape(ShapeKind::TextBox, Some(id)), &Default::default()).unwrap(); + } + d.parts.get_mut(&ids[0]).unwrap().body.next = Some(ids[1]); + d.parts.get_mut(&ids[1]).unwrap().body.next = Some(ids[2]); + let bytes = craft_docx::write(&d).unwrap(); + let xml = document_xml(&bytes); + assert_eq!(xml.matches("").count(), 1, "{xml}"); + assert_eq!(xml.matches("").count(), 1, "{xml}"); + let fallback = xml.split("").nth(1).unwrap().split("").next().unwrap(); + assert!(fallback.contains(">Independent"), "{fallback}"); + assert!(!fallback.contains("Flows on"), "the chain head must not get an unlinked fallback"); + assert_eq!(xml.matches(">Flows on").count(), 1); + assert!(xml.contains(r#""#), "{xml}"); + assert!(xml.contains(r#""#), "{xml}"); + assert!(xml.contains(r#""#), "{xml}"); + let read = craft_docx::read(&bytes).unwrap(); + let chains = read.text_box_chains(); + assert_eq!(chains.len(), 1); + assert_eq!(chains[0].len(), 3); + assert_eq!(read.plain_text(craft_wordmodel::StoryRef::Part(chains[0][0])), "Flows on"); +}