Files
craft-libs/crates/docx/tests/it/table_style_indent.rs
T
Kevin Joseph 041bafa259 craft-docx: keep a table style's w:tblInd on read and save (#2196)
* craft-docx: keep a table style's w:tblInd on read and save

A table style's own indent was dropped on read, so it was gone on save.
pandoc's Table style sets it to 0, and Word for Mac places a table whose
style chain sets no indent about 5.4 pt further right than one set to 0.

craft-wordmodel gains TableStyleParts::indent, merged through basedOn.
The reader takes a dxa indent (nil is 0; pct and auto set none) and the
writer emits it in schema order, 0 included.

Part of storytold/wordcraft#5767.

* craft-docx: a table style's w:tblInd without w:w is 0

CT_TblWidth's w:w defaults to 0, so the style resets a based-on style's
indent instead of inheriting it. The test helper now fails when a saved
style is missing rather than matching an empty string.

* craft-docx: the tblInd override test checks the base sets an indent

Without it the test would pass if the reader dropped the base style's
indent, since there would be nothing to override.
2026-11-18 15:35:42 -05:00

94 lines
6.0 KiB
Rust

//! The indent of a table style (`w:tblStyle/w:tblPr/w:tblInd`, ECMA-376 §17.4.50). pandoc's
//! "Table" style sets it to 0; dropped on save, Word for Mac moved every table of a manuscript
//! about 5.4 pt to the right (it places a table whose style chain sets no indent at the margin,
//! but one set to 0 with its first cell's text at the margin).
use std::io::{Read, Write};
fn docx(body: &str, styles: &str) -> Vec<u8> {
let root = r#"<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>"#;
let doc_rels = r#"<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#;
let doc = format!(
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>{body}</w:body></w:document>"#
);
let styles = format!(
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">{styles}</w:styles>"#
);
let parts = [
("_rels/.rels", root.to_string()),
("word/document.xml", doc),
("word/_rels/document.xml.rels", doc_rels.to_string()),
("word/styles.xml", styles),
];
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
for (name, xml) in &parts {
zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap();
zw.write_all(xml.as_bytes()).unwrap();
}
zw.finish().unwrap().into_inner()
}
fn style_xml(bytes: &[u8], id: &str) -> String {
let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap();
let mut s = String::new();
z.by_name("word/styles.xml").unwrap().read_to_string(&mut s).unwrap();
let key = format!(r#"w:styleId="{id}""#);
let style = s.split("<w:style ").find(|x| x.contains(&key)).unwrap_or_else(|| panic!("style {id} missing from the saved styles.xml"));
style.split("</w:style>").next().unwrap_or(style).to_string()
}
const BODY: &str = r#"<w:tbl><w:tblPr><w:tblStyle w:val="Indented"/><w:tblW w:w="0" w:type="auto"/></w:tblPr><w:tblGrid><w:gridCol w:w="2000"/></w:tblGrid><w:tr><w:tc><w:p><w:r><w:t>Cell</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p/>"#;
#[test]
fn a_table_style_indent_of_zero_survives_a_save() {
let styles = r#"<w:style w:type="table" w:default="1" w:styleId="Table"><w:name w:val="Table"/><w:tblPr><w:tblInd w:w="0" w:type="dxa"/><w:tblCellMar><w:left w:w="108" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style><w:style w:type="table" w:styleId="Indented"><w:name w:val="Indented"/><w:basedOn w:val="Table"/><w:tblPr><w:tblInd w:w="720" w:type="dxa"/></w:tblPr></w:style><w:style w:type="table" w:styleId="Plain"><w:name w:val="Plain"/><w:tblPr><w:tblCellMar><w:left w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style>"#;
let d = craft_docx::read(&docx(BODY, styles)).unwrap();
let parts = |id: &str| d.styles.get(id).and_then(|s| s.table.clone()).unwrap_or_default();
assert_eq!(parts("Table").indent, Some(0.0));
assert_eq!(parts("Indented").indent, Some(36.0));
assert_eq!(parts("Plain").indent, None, "no w:tblInd stays unset");
let out = craft_docx::write(&d).unwrap();
let table = style_xml(&out, "Table");
assert!(table.contains(r#"<w:tblInd w:w="0" w:type="dxa"/>"#), "{table}");
let indented = style_xml(&out, "Indented");
assert!(indented.contains(r#"<w:tblInd w:w="720" w:type="dxa"/>"#), "{indented}");
let plain = style_xml(&out, "Plain");
assert!(!plain.contains("w:tblInd"), "{plain}");
// ECMA-376 order in w:tblPr: w:tblInd before w:tblCellMar.
assert!(table.find("w:tblInd") < table.find("w:tblCellMar"), "{table}");
}
#[test]
fn a_table_style_indent_that_is_no_length_sets_none() {
// `pct` and `auto` are no lengths for an indent; `nil` is none.
let style = |ty: &str| {
format!(
r#"<w:style w:type="table" w:styleId="Indented"><w:name w:val="Indented"/><w:tblPr><w:tblInd w:w="720" w:type="{ty}"/></w:tblPr></w:style>"#
)
};
for (ty, want) in [("pct", None), ("auto", None), ("nil", Some(0.0)), ("dxa", Some(36.0))] {
let d = craft_docx::read(&docx(BODY, &style(ty))).unwrap();
let got = d.styles.get("Indented").and_then(|s| s.table.clone()).unwrap_or_default().indent;
assert_eq!(got, want, "w:type={ty}");
}
}
#[test]
fn a_table_style_indent_without_a_width_is_zero_and_overrides_its_base() {
// `CT_TblWidth`'s `w:w` defaults to 0, so a based-on style's indent is reset, not inherited.
let styles = r#"<w:style w:type="table" w:styleId="Base"><w:name w:val="Base"/><w:tblPr><w:tblInd w:w="720" w:type="dxa"/></w:tblPr></w:style><w:style w:type="table" w:styleId="Indented"><w:name w:val="Indented"/><w:basedOn w:val="Base"/><w:tblPr><w:tblInd w:type="dxa"/></w:tblPr></w:style>"#;
let d = craft_docx::read(&docx(BODY, styles)).unwrap();
let own_of = |d: &craft_wordmodel::Document, id: &str| d.styles.get(id).and_then(|s| s.table.clone()).unwrap_or_default().indent;
let own = |d: &craft_wordmodel::Document| own_of(d, "Indented");
assert_eq!(own_of(&d, "Base"), Some(36.0), "the base sets an indent to override");
assert_eq!(d.styles.table_style("Base").map(|s| s.parts.indent), Some(Some(36.0)));
assert_eq!(own(&d), Some(0.0));
assert_eq!(d.styles.table_style("Indented").map(|s| s.parts.indent), Some(Some(0.0)), "the base's 36 pt is overridden");
let out = craft_docx::write(&d).unwrap();
let indented = style_xml(&out, "Indented");
assert!(indented.contains(r#"<w:tblInd w:w="0" w:type="dxa"/>"#), "{indented}");
assert_eq!(own(&craft_docx::read(&out).unwrap()), Some(0.0));
}