From ec4247be6a96e736cafe7211a4f705910cfbc64b Mon Sep 17 00:00:00 2001 From: alehel Date: Wed, 25 Nov 2026 01:54:25 +0100 Subject: [PATCH] fix(odf): a sublist's header written from the model stays in its parent item Co-Authored-By: Claude Opus 5.5 --- crates/odf/src/text/tests_more/lists.rs | 51 +++++++++++++ crates/odf/src/text/writer.rs | 97 +++++++++++-------------- 2 files changed, 94 insertions(+), 54 deletions(-) diff --git a/crates/odf/src/text/tests_more/lists.rs b/crates/odf/src/text/tests_more/lists.rs index b9bb69148f..115914ea02 100644 --- a/crates/odf/src/text/tests_more/lists.rs +++ b/crates/odf/src/text/tests_more/lists.rs @@ -428,3 +428,54 @@ fn zero_indent_multi_paragraph_header_survives_model_export() { assert!(p.props.numbering.is_none()); } } + +/// A sublist's header, written from the model, stays with the sublist's items in the item the +/// sublist is in: alone or of several paragraphs, below another header, and before the item goes +/// on after the sublist. +#[test] +fn a_sublists_header_written_from_the_model_stays_in_its_parent_item() { + let styles = format!( + r#""# + ); + // The lists of `xml`, without attributes. + let lists = |xml: &str| -> String { + let (from, to) = (xml.find("").unwrap()); + let mut out = String::new(); + for part in xml.get(from..to).unwrap().split('<').skip(1) { + let (tag, text) = part.split_once('>').unwrap(); + out.push_str(&format!("<{}>{text}", tag.split(' ').next().unwrap())); + } + out + "" + }; + let head = "Sub header"; + let sub = "Sub"; + for inner in [ + format!("{head}{sub}Sub two"), + format!( + "MoreSub headerSecond line{sub}" + ), + format!("{head}Parent again"), + "Sub headerDeeperDeepest".to_string(), + ] { + let list = |parent: &str, attrs: &str| { + format!( + "{parent}{inner}Next" + ) + }; + let content = format!( + "{}", + list("Parent", r#" text:style-name="L""#) + ); + let mut d = import(&package(&content, Some(&styles))).unwrap(); + // Edited, so the list is written from the model. + let i = d.body.iter().position(|b| b.as_para().is_some_and(|p| p.text == "Parent")).unwrap(); + d.insert_text(&craft_wordmodel::Pos::body(i, 0), "Edited ", &Default::default()).unwrap(); + let out = export(&d).unwrap(); + assert_eq!(lists(&crate::text::tests::content_of(&out)), list("Edited Parent", "")); + let rows = |d: &Document| { + let paras = d.body.iter().filter_map(|b| b.as_para()); + paras.map(|p| (p.text.clone(), p.props.numbering.map(|n| n.level), p.props.list_item)).collect::>() + }; + assert_eq!(rows(&import(&out).unwrap()), rows(&d)); + } +} diff --git a/crates/odf/src/text/writer.rs b/crates/odf/src/text/writer.rs index 84d0374477..bc268d786c 100644 --- a/crates/odf/src/text/writer.rs +++ b/crates/odf/src/text/writer.rs @@ -107,16 +107,13 @@ fn list_run(blocks: &[FBlock], start: usize, wraps: bool) -> (usize, [ListOut; 9 let mut kinds = [first; 9]; let mut seen = [false; 9]; let mut i = start; - // The level of the last item: an item's further paragraph goes in an item still open. - let mut last = usize::from(p.item.unwrap_or(0).min(8)); while let Some(FBlock::Para(q)) = blocks.get(i) { let Some(li) = q.list else { // A section it opens or closes ends the run, as an item's does. let wrap = q.wrap.as_deref().filter(|_| wraps); - match model::continues_list(blocks, i).map(usize::from) { - Some(lv) if lv <= last && (i == start || !wrap.is_some_and(|w| !w.0.is_empty())) => { - // The items below it are closed. - last = lv; + match model::continues_list(blocks, i) { + // (One below the last item is the header of a sublist in it.) + Some(_) if i == start || !wrap.is_some_and(|w| !w.0.is_empty()) => { i += 1; if wrap.is_some_and(|w| w.1 > 0) { break; @@ -127,7 +124,6 @@ fn list_run(blocks: &[FBlock], start: usize, wraps: bool) -> (usize, [ListOut; 9 } }; let l = li.level.min(8) as usize; - last = l; let list_id = *id.get_or_insert(li.list); if li.list != list_id || (list_id == 0 && l == 0 && seen[0] && kinds[0].ordered != li.ordered) { break; @@ -150,21 +146,36 @@ fn list_run(blocks: &[FBlock], start: usize, wraps: bool) -> (usize, [ListOut; 9 (i.max(start + 1), kinds, id.unwrap_or(0)) } -/// Close nested items and lists down to the requested open depth. -fn close_lists(out: &mut String, stack: &mut Vec, depth: usize) { - while stack.len() > depth { - if stack.pop() == Some(true) { - out.push_str(""); - } - out.push_str(""); - } +/// What is open in a list being written. +#[derive(Clone, Copy, PartialEq)] +enum ListPart { + None, + Item, + Header, } -/// Open or close the single leading header without splitting its consecutive paragraphs. -fn list_header(out: &mut String, is_open: &mut bool, open: bool) { - if *is_open != open { - out.push_str(if open { "" } else { "" }); - *is_open = open; +/// Close the open part of the innermost list, then open `part`. +fn list_part(out: &mut String, stack: &mut [ListPart], part: ListPart) { + let Some(top) = stack.last_mut() else { return }; + out.push_str(match top { + ListPart::None => "", + ListPart::Item => "", + ListPart::Header => "", + }); + out.push_str(match part { + ListPart::None => "", + ListPart::Item => "", + ListPart::Header => "", + }); + *top = part; +} + +/// Close nested items and lists down to the requested open depth. +fn close_lists(out: &mut String, stack: &mut Vec, depth: usize) { + while stack.len() > depth { + list_part(out, stack, ListPart::None); + stack.pop(); + out.push_str(""); } } @@ -976,30 +987,15 @@ impl Writer { self.list_styles.push(kinds); let lname = format!("{}L{}", self.prefix, self.list_styles.len()); let id_of_list = id; - // Open lists: per level, whether a list-item is open. - let mut stack: Vec = Vec::new(); - let mut header_open = false; + // Open lists: per level, what is open in it. + let mut stack: Vec = Vec::new(); for (qi, q) in blocks.get(start..i).unwrap_or(&[]).iter().enumerate() { let FBlock::Para(q) = q else { continue }; - if q.list.is_none() && stack.get(usize::from(q.item.unwrap_or(0).min(8))).copied() == Some(true) { - // An item's further paragraph (`list_run` took it only below an open - // item at its level): in that item, its sublists ended first. - let lv = q.item.map_or(0, |l| l.min(8) as usize); - close_lists(out, &mut stack, lv + 1); - self.para_in(q, chain(start + qi), out); - continue; - } - if q.list.is_some() { - list_header(out, &mut header_open, false); - } let lv = usize::from(q.list.map(|l| l.level).or(q.item).unwrap_or(0).min(8)); close_lists(out, &mut stack, lv + 1); while stack.len() < lv + 1 { - if let Some(top) = stack.last_mut() - && !*top - { - out.push_str(""); - *top = true; + if stack.last() == Some(&ListPart::None) { + list_part(out, &mut stack, ListPart::Item); } if stack.is_empty() { let id = format!("{}lst{}", self.prefix, self.list_styles.len()); @@ -1026,25 +1022,18 @@ impl Writer { } else { out.push_str(""); } - stack.push(false); + stack.push(ListPart::None); } - // Leading unnumbered paragraphs belong to one list header, not to - // the document body. Keep that membership when original markup is gone. - if q.list.is_none() { - list_header(out, &mut header_open, true); - self.para_in(q, chain(start + qi), out); - continue; + // An unnumbered paragraph goes in the item or header open at its level (its + // sublists ended first); a list's leading ones are its header's. Keep that + // membership when original markup is gone. + if q.list.is_some() { + list_part(out, &mut stack, ListPart::Item); + } else if stack.last() == Some(&ListPart::None) { + list_part(out, &mut stack, ListPart::Header); } - if let Some(top) = stack.last_mut() { - if *top { - out.push_str(""); - } - *top = true; - } - out.push_str(""); self.para_in(q, chain(start + qi), out); } - list_header(out, &mut header_open, false); close_lists(out, &mut stack, 0); self.close_wrap(closes.as_ref(), 0, out); out.push('\n');