diff --git a/crates/flow/src/from_doc.rs b/crates/flow/src/from_doc.rs index b7204f1bf..d85953a0a 100644 --- a/crates/flow/src/from_doc.rs +++ b/crates/flow/src/from_doc.rs @@ -1101,7 +1101,10 @@ fn flow_table(doc: &Document, t: &Table, depth: usize, tracked: FlowOpts) -> FTa let ncols = t.rows.iter().map(|r| r.first_grid() + r.cells.iter().map(|c| c.span()).sum::() + r.grid_after()).max().unwrap_or(0).max(t.grid.len()); for (ri, r) in t.rows.iter().enumerate() { - let header = r.props.header || (ri == 0 && t.props.look.header_row && t.rows.len() > 1); + // The look's header row is an option of the table's style (`w:tblLook/@w:firstRow`): with + // no table style there is nothing to apply, so the first row stays an ordinary row, as in + // Word. Only Repeat as header row makes it one then. + let header = r.props.header || (ri == 0 && style.is_some() && t.props.look.header_row && t.rows.len() > 1); let mut cells = Vec::new(); for (ci, c) in r.cells.iter().enumerate() { let styled = if bake { styled_cell(t, style.as_ref(), (ri, ci), ncols) } else { None }; diff --git a/crates/flow/src/lib.rs b/crates/flow/src/lib.rs index 774b5ffd1..7dc83c99d 100644 --- a/crates/flow/src/lib.rs +++ b/crates/flow/src/lib.rs @@ -80,6 +80,32 @@ mod tests { assert!(kept); } + /// The default look's header row is an option of the table style: a table with no style (or a + /// style the document doesn't have) has an ordinary first row, so RTF doesn't bold it or + /// repeat it on each page, and HTML doesn't make it `th`. Repeat as header row still does. + #[test] + fn look_header_row_needs_a_table_style() { + use craft_wordmodel::{Block, Document, table::Table}; + let first_row_header = |style: Option<&str>, repeat: bool| { + let mut t = Table::new(2, 2, 200.0); + t.props.style = style.map(str::to_string); + assert!(t.props.look.header_row, "the default look has a header row"); + if let Some(r) = t.rows.first_mut() { + r.props.header = repeat; + } + let mut doc = Document::new(); + doc.body = vec![std::sync::Arc::new(Block::Table(t))]; + let flow = from_doc(&doc); + let Some(FBlock::Table(ft)) = flow.blocks.iter().find(|b| matches!(b, FBlock::Table(_))) else { panic!("no table in {:?}", flow.blocks) }; + assert!(ft.rows.get(1).is_some_and(|r| r.iter().all(|c| !c.header)), "the second row is never a header"); + ft.rows.first().is_some_and(|r| r.iter().all(|c| c.header)) + }; + assert!(first_row_header(Some("TableGrid"), false), "a table style applies the look's header row"); + assert!(!first_row_header(None, false), "no style, no header row"); + assert!(!first_row_header(Some("NoSuchStyle"), false), "a missing style applies nothing"); + assert!(first_row_header(None, true), "Repeat as header row"); + } + #[test] fn percent_decode_requires_hex_digits() { assert_eq!(percent_decode("%+A"), b"%+A".to_vec());