Files
craft-libs/crates/docx/tests/it/wordml2003_write.rs
Dylan M. Taylor e3e31693c7 Add craft-docx: WordCraft's DOCX reader and writer on craft-wordmodel, craft-opc and craft-xml (#1015)
* Add craft-wordmodel: WordCraft's document model without its editing code

WordCraft's crates/doc (wordcraft-doc, at 07f97737) moved as it is, so the DOCX,
RTF, ODT and other text-format crates can read into and write from one model.
Module paths, type names and serde attributes are unchanged: an app that
re-exports the crate keeps its native file format.

Left in the app: range editing (edit.rs), IME text and equation editing. Not
model, and already in craft-libs: code pages (craft-rtf), the ZIP entry-count
check (craft-opc), the compound-file guard (craft-cfb). The paragraph walks
moved to walk.rs and the unit helpers of wordcraft-geom to geom.rs.

* craft-wordmodel: math_tree, the arguments of an equation node and a walk over its runs

The DOCX reader and writer walk an equation's runs. The walk and the child
accessors it uses were in WordCraft's equation editor (math_edit.rs), which
stays in the app; they are structure, not editing.

* craft-wordmodel: keep the document operations (edit.rs) with the model

The DOCX and RTF readers use them: alternative-format chunks are merged into
a document with adopt_blocks, and a reader formats ranges with format_range
and inserts objects. They operate on the document, not on a view, so they
belong beside it. The tests that drove them come back with them; the
paragraph walks stay in walk.rs and edit re-exports them.

base64 0.23, as WordCraft and craft-offcrypto use.

* craft-docx: WordCraft's crates/docx as it is at wordcraft 07f97737

The source, unchanged, so the next commit shows only what the move changes.
This commit does not build on its own (it still names wordcraft-doc and
wordcraft-geom, and is not a workspace member yet).

* craft-docx: on craft-wordmodel, craft-opc, craft-xml and craft-offcrypto

- The package (zip reading, part names, relationships, content types,
  zip writing) is craft-opc's; the module keeps WordCraft's bookkeeping
  (passthrough keys, the embedded-part manifest) and its own Rel/Rels shape.
- Parts are parsed with craft-xml's canonical names (the Strict drawing
  names and mc:Choice/@Requires through its hooks), then given the tree
  shape the reader was written against.
- Encryption and Restrict Editing hashes are craft-offcrypto's; this crate
  maps its errors and the w:documentProtection attributes.
- wordcraft-doc is craft-wordmodel; wordcraft-geom's helpers are its geom.

Tests that covered what now lives in craft-opc and craft-offcrypto are left to
those crates; the DIFAT regression is craft-cfb's. Test-only zip use follows
zip 9's fallible names. The attribute-cap test times the read only.
2026-11-11 20:31:40 -05:00

281 lines
12 KiB
Rust

//! Saving as Word 2003 XML (#1081): documents built in code, written as WordprocessingML 2003
//! and read back with the 2003 reader.
use std::sync::Arc;
use craft_docx::{XmlKind, read_xml, sniff_xml, write_wordml2003};
use craft_wordmodel::numbering::ListKind;
use craft_wordmodel::para::NoteKind;
use craft_wordmodel::props::{Align, CharProps, NumRef, Rgb, TextColor};
use craft_wordmodel::table::{Cell, Table};
use craft_wordmodel::{Block, Comment, Document, InlineObject, Paragraph, PartKind, Revision, RevisionKind, para_block};
fn tiny_png() -> Vec<u8> {
let img = image::RgbaImage::from_fn(3, 2, |x, y| image::Rgba([x as u8 * 80, y as u8 * 120, 200, 255]));
let mut out = std::io::Cursor::new(Vec::new());
img.write_to(&mut out, image::ImageFormat::Png).unwrap();
out.into_inner()
}
fn paras(doc: &Document) -> Vec<&Paragraph> {
doc.body.iter().filter_map(|b| b.as_para()).collect()
}
fn part_text(doc: &Document, id: Option<u32>) -> String {
id.and_then(|i| doc.parts.get(&i))
.map(|p| p.blocks.iter().filter_map(|b| b.as_para()).map(|p| p.plain_text()).collect::<Vec<_>>().join("\n"))
.unwrap_or_default()
}
fn sample() -> Document {
let mut d = Document::new();
d.core.title = "Garden <notes> & more".into();
d.core.creator = "Ada".into();
let key = d.add_media(tiny_png(), "png");
let num = Arc::make_mut(&mut d.numbering).add_list(ListKind::Numbered);
let head = d.add_part(PartKind::Header, vec![para_block(Paragraph::with_text("Head", CharProps::default()))]);
let foot = d.add_part(PartKind::Footnote, vec![para_block(Paragraph::with_text("Sown in May.", CharProps::default()))]);
let com = d.add_part(PartKind::Comment, vec![para_block(Paragraph::with_text("Too short?", CharProps::default()))]);
d.comments.insert(3, Comment { author: "Bo".into(), part: com, ..Default::default() });
d.revisions.push(Revision { kind: RevisionKind::Insert, author: "Ada".into(), date: String::new(), move_name: None });
d.last_section.headers.default = Some(head);
let mut p = Paragraph::new();
for (t, c) in [
("Plain ", CharProps::default()),
(
"loud",
CharProps { bold: Some(true), italic: Some(true), size: Some(14.0), color: Some(TextColor::Rgb(Rgb(255, 0, 0))), ..Default::default() },
),
(" link", CharProps { link: Some("https://example.org/beans?a=1&b=2".into()), ..Default::default() }),
] {
let off = p.len();
p.insert_text(off, t, &c).unwrap();
}
p.props.align = Some(Align::Center);
p.props.indent_left = Some(36.0);
p.props.space_after = Some(12.0);
p.insert_object(0, InlineObject::CommentStart { id: 3 }, &CharProps::default()).unwrap();
let end = p.len();
p.insert_object(end, InlineObject::CommentEnd { id: 3 }, &CharProps::default()).unwrap();
let at = p.text.find("loud").unwrap();
p.insert_object(at, InlineObject::NoteRef { kind: NoteKind::Footnote, id: foot, custom: String::new() }, &CharProps::default()).unwrap();
let mut blocks = vec![para_block(p)];
let mut li = Paragraph::with_text("listed", CharProps { ins: Some(0), ..Default::default() });
li.props.numbering = Some(NumRef { num, level: 0 });
blocks.push(para_block(li));
let mut img = Paragraph::new();
img.insert_object(
0,
InlineObject::Image {
media: key,
w: 30.0,
h: 20.0,
alt: "dot".into(),
float: Default::default(),
crop: [0.0; 4],
fx: Default::default(),
ole: None,
},
&CharProps::default(),
)
.unwrap();
blocks.push(para_block(img));
let mut t = Table::new(2, 2, 300.0);
t.rows[0].cells[0] = Cell::with_text("A1");
t.rows[0].cells[1] = Cell::with_text("B1");
t.rows[1].cells[0].props.shading = Some(Rgb(0, 128, 0));
blocks.push(Arc::new(Block::Table(t)));
blocks.push(para_block(Paragraph::new()));
d.body = blocks;
d
}
#[test]
fn round_trips_through_the_2003_reader() {
let d = sample();
let xml = write_wordml2003(&d).expect("write");
let text = std::str::from_utf8(&xml).expect("utf-8");
assert!(text.contains("<w:wordDocument") && text.contains("http://schemas.microsoft.com/office/word/2003/wordml"));
assert!(text.contains("<w:binData") && text.contains("<w:hlink w:dest=\"https://example.org/beans?a=1&amp;b=2\""));
assert!(text.contains("<w:footnote>") && text.contains("<wx:sect>") && text.contains("<aml:annotation"));
assert!(!text.contains("w:szCs") && !text.contains("<w:drawing"));
if std::env::var_os("WML2003_DUMP").is_some() {
println!("{text}");
}
assert_eq!(sniff_xml(&xml), XmlKind::WordMl2003);
let back = read_xml(&xml).expect("read");
assert_eq!(back.core.title, "Garden <notes> & more");
assert_eq!(back.core.creator, "Ada");
let ps = paras(&back);
assert_eq!(ps[0].plain_text(), "Plain loud link");
assert_eq!(ps[0].props.align, Some(Align::Center));
assert_eq!(ps[0].props.indent_left, Some(36.0));
assert_eq!(ps[0].props.space_after, Some(12.0));
let c = ps[0].props_of_char(ps[0].text.find("loud").unwrap());
assert_eq!((c.bold, c.italic, c.size), (Some(true), Some(true), Some(14.0)));
assert!(c.color.is_some());
assert_eq!(ps[0].props_at(ps[0].text.find("link").unwrap()).link.as_deref(), Some("https://example.org/beans?a=1&b=2"));
let note = ps[0].objects.iter().find_map(|o| match o {
InlineObject::NoteRef { id, .. } => Some(*id),
_ => None,
});
assert_eq!(part_text(&back, note), "Sown in May.");
let comment = back.comments.values().next().expect("comment");
assert_eq!((comment.author.as_str(), part_text(&back, Some(comment.part)).as_str()), ("Bo", "Too short?"));
let listed = ps[1];
assert!(listed.props.numbering.is_some(), "list paragraph keeps its list");
let ins = listed.props_of_char(0).ins.and_then(|i| back.revisions.get(i as usize));
assert_eq!(ins.map(|r| r.author.as_str()), Some("Ada"));
let pic = ps.iter().flat_map(|p| p.objects.iter()).find(|o| matches!(o, InlineObject::Image { .. }));
assert!(matches!(pic, Some(InlineObject::Image { w, h, alt, .. }) if *w == 30.0 && *h == 20.0 && alt == "dot"));
assert_eq!(back.media.len(), 1);
let table = back.body.iter().find_map(|b| match b.as_ref() {
Block::Table(t) => Some(t),
_ => None,
});
let table = table.expect("table");
assert_eq!(table.rows.len(), 2);
assert_eq!(table.rows[0].cells[1].blocks.first().and_then(|b| b.as_para()).map(|p| p.plain_text()).as_deref(), Some("B1"));
assert_eq!(table.rows[1].cells[0].props.shading, Some(Rgb(0, 128, 0)));
assert_eq!(part_text(&back, back.last_section.headers.default), "Head");
}
/// Features 2003 XML can't carry drop out without an error, and the text stays.
#[test]
fn control_characters_and_unsupported_content_do_not_break_the_file() {
let mut d = Document::new();
let mut p = Paragraph::with_text("a\u{1}b\u{FFFE}c <&> \"q\"", CharProps { size: Some(f32::NAN), ..Default::default() });
p.insert_object(
0,
InlineObject::Opaque { format: "docx".into(), xml: "<w:r><w:t>raw</w:t></w:r>".into(), text: "raw".into() },
&CharProps::default(),
)
.unwrap();
d.body = vec![para_block(p)];
let xml = write_wordml2003(&d).expect("write");
assert!(read_xml(&xml).is_ok());
let text = String::from_utf8(xml).expect("utf-8");
assert!(!text.contains('\u{1}') && !text.contains('\u{FFFE}'));
assert!(!text.contains("<w:drawing") && !text.contains("<mc:"));
}
#[test]
fn big_and_deep_documents_write() {
let mut d = Document::new();
d.body = vec![para_block(Paragraph::with_text(&"word ".repeat(400_000), CharProps::default()))];
let xml = write_wordml2003(&d).expect("write");
assert!(read_xml(&xml).is_ok());
// Tables nested deeper than the writers go: no panic, whatever comes out.
let mut inner = Cell::with_text("deep");
for _ in 0..60 {
let mut t = Table::new(1, 1, 100.0);
t.rows[0].cells[0] = inner;
inner = Cell::empty();
inner.blocks = vec![Arc::new(Block::Table(t)), para_block(Paragraph::new())];
}
let mut d = Document::new();
d.body = inner.blocks;
if let Ok(xml) = write_wordml2003(&d) {
let _ = read_xml(&xml);
}
}
/// Picture brightness and contrast (#1711) survive a Word 2003 XML save as VML `gain` and
/// `blacklevel`; transparency has no place on `v:imagedata` and is dropped.
#[test]
fn picture_brightness_and_contrast_round_trip() {
use craft_wordmodel::picfx::PicFx;
let cases = [PicFx::new(70_000, -70_000, 0), PicFx::new(-30_000, 50_000, 40_000), PicFx::default()];
let mut d = Document::new();
let key = d.add_media(tiny_png(), "png");
d.body = cases
.iter()
.map(|fx| {
let mut p = Paragraph::new();
let pic = InlineObject::Image {
media: key.clone(),
w: 30.0,
h: 20.0,
alt: "dot".into(),
float: Default::default(),
crop: [0.0; 4],
fx: *fx,
ole: None,
};
p.insert_object(0, pic, &CharProps::default()).unwrap();
para_block(p)
})
.collect();
let xml = write_wordml2003(&d).expect("write");
let text = String::from_utf8_lossy(&xml);
assert!(text.contains(r#"gain="19661f""#) && text.contains(r#"blacklevel="22938f""#), "Word's Washout values");
let back = read_xml(&xml).expect("read");
let got: Vec<PicFx> = paras(&back)
.iter()
.flat_map(|p| p.objects.iter())
.filter_map(|o| if let InlineObject::Image { fx, .. } = o { Some(*fx) } else { None })
.collect();
assert_eq!(got.len(), cases.len());
for (g, want) in got.iter().zip(cases) {
let near = |a: i32, b: i32| (a - b).abs() <= 2;
assert!(near(g.bright, want.bright) && near(g.contrast, want.contrast) && g.transparency == 0, "{g:?} vs {want:?}");
}
}
#[test]
fn shared_header_and_repeated_note_are_not_inlined_per_reference() {
// #3652: one ~10 KB header shared by 1000 sections and one ~10 KB footnote referenced 500
// times must not be copied into the output once per section or reference.
let mut d = Document::new();
let big = format!("HDRMARK {}", "h".repeat(10_000));
let head = d.add_part(PartKind::Header, vec![para_block(Paragraph::with_text(&big, CharProps::default()))]);
let note = format!("NOTEMARK {}", "n".repeat(10_000));
let foot = d.add_part(PartKind::Footnote, vec![para_block(Paragraph::with_text(&note, CharProps::default()))]);
d.last_section.headers.default = Some(head);
let mut blocks = Vec::new();
for i in 0..1000 {
let mut p = Paragraph::with_text("x", CharProps::default());
if i < 500 {
p.insert_object(1, InlineObject::NoteRef { kind: NoteKind::Footnote, id: foot, custom: String::new() }, &CharProps::default()).unwrap();
}
p.section = Some(Box::new(d.last_section.clone()));
blocks.push(para_block(p));
}
d.body = blocks;
let xml = write_wordml2003(&d).expect("write");
assert!(read_xml(&xml).is_ok());
let text = String::from_utf8(xml).expect("utf-8");
let hdr = text.matches("HDRMARK").count();
let notes = text.matches("NOTEMARK").count();
assert!(text.len() < 2_000_000 && hdr <= 3 && notes <= 3, "output {} bytes, header x{hdr}, note x{notes}", text.len());
}
#[test]
fn headers_past_the_inlined_byte_budget_fail_the_export() {
// #3652: two ~1 MB headers alternating over 80 sections must each be written where they
// change; past the byte budget the export fails rather than growing or dropping a header.
let mut d = Document::new();
let a = d.add_part(PartKind::Header, vec![para_block(Paragraph::with_text(&"a".repeat(1_000_000), CharProps::default()))]);
let b = d.add_part(PartKind::Header, vec![para_block(Paragraph::with_text(&"b".repeat(1_000_000), CharProps::default()))]);
let mut blocks = Vec::new();
for i in 0..80 {
let mut p = Paragraph::with_text("x", CharProps::default());
let mut s = d.last_section.clone();
s.headers.default = Some(if i % 2 == 0 { a } else { b });
p.section = Some(Box::new(s));
blocks.push(para_block(p));
}
d.body = blocks;
let out = write_wordml2003(&d);
assert!(matches!(out, Err(craft_docx::DocxError::Limit(_))), "expected a limit error, got {:?}", out.map(|x| x.len()));
}