diff --git a/crates/engine/src/cmd/tools/compare.rs b/crates/engine/src/cmd/tools/compare.rs index 29c5b8d062..86bcea156a 100644 --- a/crates/engine/src/cmd/tools/compare.rs +++ b/crates/engine/src/cmd/tools/compare.rs @@ -443,7 +443,11 @@ fn diff_blocks(doc: &mut Document, old: &[Arc], new: &[Arc], ctx: (Some(Block::Para(pa)), Some(Block::Para(pb))) => out.push(para_block(word_diff(doc, pa, pb, ctx))), _ => { if let Some(x) = a { - out.push(Arc::new(mark_block(x, &|c| c.del = Some(ctx.del)))); + out.push(Arc::new(mark_block(x, &|c| { + // An existing deletion is another author's: keep their revision + // stamp instead of re-attributing the paragraph to this compare. + c.del.get_or_insert(ctx.del); + }))); } if let Some(x) = b { out.push(Arc::new(mark_block(x, &|c| c.ins = Some(ctx.ins)))); @@ -1613,8 +1617,17 @@ fn block_text(b: &Block, depth: usize) -> String { } } -/// Returns a copy of `b` with all text runs and paragraph marks across all nested table cells -/// updated by `f` iteratively, without recursion or depth truncation. +/// A copy of the block with every run (in tables, every cell's) changed by `f`, and a paragraph's +/// mark along with them, so a whole inserted or deleted paragraph carries the revision even when +/// the paragraph is empty (`format` returns early for a length-0 range). Walks nested tables +/// iteratively, without recursion or depth truncation. +/// +/// Known limitation: resolving a revision needs a following paragraph, so a paragraph inserted at +/// the very end (rejected) or deleted from the very end (accepted) still leaves one empty +/// paragraph. That is a separate follow-up, not handled here. A section-carrying inserted +/// paragraph is not such a case: `resolve_para` removes the section break first (#5734, issue +/// #5667), so rejecting it takes the paragraph out whole and the following paragraph keeps its +/// own properties. fn mark_block(b: &Block, f: &dyn Fn(&mut CharProps)) -> Block { let mut out = b.clone(); let mut todo = vec![&mut out]; diff --git a/crates/engine/src/cmd/tools/tests.rs b/crates/engine/src/cmd/tools/tests.rs index 28d85caa55..d05745f705 100644 --- a/crates/engine/src/cmd/tools/tests.rs +++ b/crates/engine/src/cmd/tools/tests.rs @@ -11,6 +11,196 @@ fn compare_marks_changes() { assert_eq!(s.doc.plain_text(StoryRef::Body), "The dog sat.\nKeep this.\nNew line here."); } +/// Top-level body blocks that are empty paragraphs. +fn empty_paras(s: &Session) -> usize { + s.doc.body.iter().filter(|b| matches!(b.as_para(), Some(p) if p.is_empty())).count() +} + +#[test] +fn compare_marks_a_whole_paragraph_and_its_mark() { + // A whole inserted paragraph carries the insertion on its mark, not just on its runs. + let mut s = Session::new(Document::from_text("A\nB")); + s.run("review.compare", &json!({"text": "A\nNEW\nB"})).unwrap(); + assert_eq!(s.doc.body.len(), 3); + let mid = s.doc.body[1].as_para().unwrap(); + assert_eq!(mid.plain_text(), "NEW"); + assert!(mid.mark.ins.is_some(), "the inserted paragraph's mark is not marked inserted"); + s.run("review.rejectAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nB"); + assert_eq!(s.doc.body.len(), 2); + assert_eq!(empty_paras(&s), 0, "rejecting an inserted paragraph left an empty paragraph behind"); + let mut s = Session::new(Document::from_text("A\nB")); + s.run("review.compare", &json!({"text": "A\nNEW\nB"})).unwrap(); + s.run("review.acceptAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nNEW\nB"); + assert_eq!(s.doc.body.len(), 3); + assert_eq!(empty_paras(&s), 0, "accepting an inserted paragraph left an empty paragraph behind"); + + // A whole deleted paragraph carries the deletion on its mark. + let mut s = Session::new(Document::from_text("A\nOLD\nB")); + s.run("review.compare", &json!({"text": "A\nB"})).unwrap(); + assert_eq!(s.doc.body.len(), 3); + let mid = s.doc.body[1].as_para().unwrap(); + assert_eq!(mid.plain_text(), "OLD"); + assert!(mid.mark.del.is_some(), "the deleted paragraph's mark is not marked deleted"); + s.run("review.acceptAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nB"); + assert_eq!(s.doc.body.len(), 2); + assert_eq!(empty_paras(&s), 0, "accepting a deleted paragraph left an empty paragraph behind"); + let mut s = Session::new(Document::from_text("A\nOLD\nB")); + s.run("review.compare", &json!({"text": "A\nB"})).unwrap(); + s.run("review.rejectAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nOLD\nB"); + assert_eq!(s.doc.body.len(), 3); + assert_eq!(empty_paras(&s), 0, "rejecting a deleted paragraph left an empty paragraph behind"); +} + +#[test] +fn compare_marks_an_empty_paragraphs_mark() { + // An empty inserted paragraph is stamped on its mark, though `format` skips its empty range. + let mut s = Session::new(Document::from_text("A\nB")); + s.run("review.compare", &json!({"text": "A\n\nB"})).unwrap(); + assert_eq!(s.doc.body.len(), 3); + let empty = s.doc.body[1].as_para().unwrap(); + assert!(empty.is_empty()); + assert!(empty.mark.ins.is_some(), "the empty inserted paragraph's mark is not marked inserted"); + s.run("review.rejectAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nB"); + assert_eq!(s.doc.body.len(), 2); + assert_eq!(empty_paras(&s), 0, "rejecting an empty inserted paragraph left it behind"); + + // An empty deleted paragraph is stamped on its mark too. + let mut s = Session::new(Document::from_text("A\n\nB")); + s.run("review.compare", &json!({"text": "A\nB"})).unwrap(); + assert_eq!(s.doc.body.len(), 3); + let empty = s.doc.body[1].as_para().unwrap(); + assert!(empty.is_empty()); + assert!(empty.mark.del.is_some(), "the empty deleted paragraph's mark is not marked deleted"); + s.run("review.acceptAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nB"); + assert_eq!(s.doc.body.len(), 2); + assert_eq!(empty_paras(&s), 0, "accepting an empty deleted paragraph left it behind"); +} + +#[test] +fn compare_keeps_another_authors_deletion_stamp() { + // A paragraph the original already carries as a tracked deletion by another author: Compare + // must keep that author's revision on its runs and on its paragraph mark, not re-attribute + // the block to its own deletion revision. + let mut doc = Document::from_text("A\nOLD\nB"); + doc.revisions.push(wordcraft_doc::Revision::new(RevisionKind::Delete, "Alice", "")); + let p = doc.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(1)).unwrap(); + for r in &mut p.runs { + r.props.del = Some(0); + } + p.mark.del = Some(0); + let mut s = Session::new(doc); + s.run("review.compare", &json!({"text": "A\nB", "author": "Bob"})).unwrap(); + assert_eq!(s.doc.body.len(), 3); + let mid = s.doc.body[1].as_para().unwrap(); + assert_eq!(mid.plain_text(), "OLD"); + assert!(mid.runs.iter().all(|r| r.props.del == Some(0)), "the runs' deletion was re-stamped: {:?}", mid.runs); + assert_eq!(mid.mark.del, Some(0), "the paragraph mark's deletion was re-stamped"); + assert_eq!(s.doc.revisions.first().map(|r| (r.kind, r.author.as_str())), Some((RevisionKind::Delete, "Alice"))); +} + +#[test] +fn compare_pins_the_terminal_inserted_paragraph_limitation() { + // Characterizes the limitation documented on `mark_block` (compare.rs): resolving a revision + // needs a following paragraph, so a paragraph inserted at the very end is rejected but leaves + // one empty paragraph behind. Flip this test when the follow-up fix lands. + let mut s = Session::new(Document::from_text("A\nB")); + s.run("review.compare", &json!({"text": "A\nB\nC"})).unwrap(); + assert_eq!(s.doc.body.len(), 3); + s.run("review.rejectAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nB\n", "the rejected terminal paragraph still leaves a line"); + assert_eq!(s.doc.body.len(), 3); + assert_eq!(empty_paras(&s), 1, "the rejected terminal paragraph is left as one empty paragraph"); +} + +#[test] +fn compare_pins_the_terminal_deleted_paragraph_limitation() { + // The mirror of `compare_pins_the_terminal_inserted_paragraph_limitation`: a paragraph deleted + // from the very end is accepted but still leaves one empty paragraph. Characterizes the + // limitation documented on `mark_block` (compare.rs); flip when the follow-up fix lands. + let mut s = Session::new(Document::from_text("A\nB\nC")); + s.run("review.compare", &json!({"text": "A\nB"})).unwrap(); + assert_eq!(s.doc.body.len(), 3); + s.run("review.acceptAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nB\n", "the accepted terminal deletion still leaves a line"); + assert_eq!(s.doc.body.len(), 3); + assert_eq!(empty_paras(&s), 1, "the accepted terminal deletion is left as one empty paragraph"); +} + +#[test] +fn compare_pins_rejecting_an_inserted_section_keeps_the_next_paragraphs_style() { + // Pins the fixed behaviour for rejecting an inserted paragraph that carries a section + // break. This was a known limitation recorded on `mark_block` (compare.rs): `resolve`'s + // `remove_empty_para` refused paragraphs carrying a section, so once the inserted + // paragraph's text was rejected the empty paragraph was joined into the next one, which + // took its properties ("Body" came back Normal instead of its original Heading 1). Fixed + // on main by #5734 (issue #5667): `resolve_para` now removes the section break first, so + // the empty paragraph goes whole and the following paragraph keeps its own properties. + // After Reject All the original is restored exactly. + let mut original = Document::from_text("Intro\nBody"); + original.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(1)).unwrap().props.style = Some("Heading1".into()); + let mut s = Session::new(original); + + let mut rev = Document::from_text("Intro\nAppendix\nBody"); + { + let p = rev.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(1)).unwrap(); + p.props.style = Some("Normal".into()); + p.section = Some(Box::new(wordcraft_doc::section::SectionProps::default())); + } + rev.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(2)).unwrap().props.style = Some("Heading1".into()); + let path = std::env::temp_dir().join(format!("wc-compare-section-{}.docx", std::process::id())); + crate::io::save_path(&path, &rev).unwrap(); + s.run("review.compare", &json!({"path": path.to_string_lossy()})).unwrap(); + let _ = std::fs::remove_file(&path); + s.run("review.rejectAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "Intro\nBody"); + assert_eq!(s.doc.body.len(), 2); + let body = s.doc.body[1].as_para().unwrap(); + assert_eq!(body.plain_text(), "Body"); + assert_eq!(body.props.style.as_deref(), Some("Heading1"), "the following paragraph keeps its own properties (fixed on main by #5734)"); +} + +#[test] +fn compare_pins_accepting_a_deleted_section_keeps_the_next_paragraphs_style() { + // The Accept-side mirror of `compare_pins_rejecting_an_inserted_section_keeps_the_next_paragraphs_style`: + // compare deletes a paragraph that carries a section break whole (original `A`, `OLD` + // with a section break, `B`; revised `A`, `B`). Accept All goes through the same #5734 + // branch in `resolve_para` as the reject case: the section break is removed first, so + // the emptied paragraph goes whole instead of being joined into the next one. After + // Accept All the revised text stands exactly and `B` keeps its own properties. + let mut original = Document::from_text("A\nOLD\nB"); + { + let p = original.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(1)).unwrap(); + p.props.style = Some("Normal".into()); + p.section = Some(Box::new(wordcraft_doc::section::SectionProps::default())); + } + original.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(2)).unwrap().props.style = Some("Heading1".into()); + let mut s = Session::new(original); + + let mut rev = Document::from_text("A\nB"); + rev.para_mut(StoryRef::Body, &wordcraft_doc::Path::top(1)).unwrap().props.style = Some("Heading1".into()); + let path = std::env::temp_dir().join(format!("wc-compare-section-accept-{}.docx", std::process::id())); + crate::io::save_path(&path, &rev).unwrap(); + s.run("review.compare", &json!({"path": path.to_string_lossy()})).unwrap(); + let _ = std::fs::remove_file(&path); + assert_eq!(s.doc.body.len(), 3); + let mid = s.doc.body[1].as_para().unwrap(); + assert_eq!(mid.plain_text(), "OLD"); + assert!(mid.mark.del.is_some(), "the deleted sectioned paragraph's mark is not marked deleted"); + s.run("review.acceptAll", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A\nB"); + assert_eq!(s.doc.body.len(), 2); + assert_eq!(empty_paras(&s), 0, "accepting the deleted sectioned paragraph left it behind"); + let body = s.doc.body[1].as_para().unwrap(); + assert_eq!(body.plain_text(), "B"); + assert_eq!(body.props.style.as_deref(), Some("Heading1"), "the following paragraph keeps its own properties (fixed on main by #5734)"); +} + #[test] fn compare_options_ignore_case_and_white_space() { let original = "The Cat sat.\nTwo spaces here."; diff --git a/docs/gaps.md b/docs/gaps.md index 4c71a6a01b..3f62deb856 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -236,6 +236,7 @@ Page Setup's margin, paper-size and document-grid number boxes scroll long text | 2026-10-26 | trivial | Dialog sections improve readability across 13 dialogs; conditional scrolling keeps tall dialogs usable. Ranked gaps and estimates unchanged (#4035, #4038) | | 2026-11-12 | trivial | PDF exports and print jobs mark tracked changes in the Track Changes Options colours and marks, and deletions marked Hidden take no space; print jobs without Print Markup lay out the final text. The print preview pages as the job prints, and the preview and downloaded PNGs draw headers at full strength (#5232, #6267). MCP/control-screenshot rendering (#6266), STYLEREF of hidden deletions (#6263) and the preview cache's numeral key (#6265) remain; no ranked gap changed. | | 2026-11-12 | trivial | Clipboard: a copied picture is published to the system clipboard as PNG, with HTML where the host writes it, and WordCraft's own paste keeps the original fragment; a copy of only shapes stays in WordCraft (#6205). Native external-app paste remains untested. | +| 2026-11-16 | trivial | Compare: a whole-paragraph change at the very end of the body leaves one empty paragraph when resolved — rejecting a terminal inserted paragraph, or accepting a terminal deleted one — because revision resolution needs a following paragraph (#3750; pinned by tests in #5505). No ranked gap changed. | | 2026-11-08 | trivial | #10 narrowed: a turned inline picture with no recorded effect extent keeps its line to its frame (exchanged near a quarter turn in Word 2013+ layout), cut at the line; gap stays open | | 2026-11-08 | trivial | #4 narrowed: a cell's last Normal paragraph drops its contextual space after, the end-of-cell mark counting as a Normal paragraph (#5450) | | 2026-11-07 | trivial | Page Setup also bounds the unfocused margin display after a long numeric paste; the accepted value is retained (#5650). |