mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-19 08:55:26 +00:00
This commit is contained in:
1 parent
406befeaa2
commit
29d862fabc
3 files changed
+486
-26
No files matched your search
@@ -34,6 +34,7 @@ struct DiffCtx<'a> {
|
||||
opts: &'a CompareOptions,
|
||||
budget: &'a mut u64,
|
||||
char_fallbacks: &'a mut usize,
|
||||
plain_revised: bool,
|
||||
}
|
||||
|
||||
/// Changed paragraphs longer than this (in characters) are compared word by word even at
|
||||
@@ -48,10 +49,12 @@ const MAX_CHAR_DIFF: usize = 2000;
|
||||
/// [`MAX_CHAR_DIFF`] characters, it falls back to word-level diff for that paragraph, surfaces the
|
||||
/// fallback count in `"characterFallback"`, and includes `"level"` in `"notHonoured"`.
|
||||
pub(super) fn compare(s: &mut Session, v: &Value) -> CmdResult {
|
||||
let revised = if let Some(t) = p::str(v, "text") {
|
||||
Document::from_text(t)
|
||||
// A revised side given as plain text carries no real runs or properties; one opened from a
|
||||
// path does. The diff takes the revised formatting only in the latter case.
|
||||
let (revised, plain_revised) = if let Some(t) = p::str(v, "text") {
|
||||
(Document::from_text(t), true)
|
||||
} else if let Some(path) = p::str(v, "path") {
|
||||
crate::io::open_path(std::path::Path::new(path)).map_err(CmdError::Failed)?
|
||||
(crate::io::open_path(std::path::Path::new(path)).map_err(CmdError::Failed)?, false)
|
||||
} else {
|
||||
return Err(CmdError::Params("`path` or `text` of the revised document is required".into()));
|
||||
};
|
||||
@@ -97,7 +100,7 @@ pub(super) fn compare(s: &mut Session, v: &Value) -> CmdResult {
|
||||
let mut budget = MAX_COMPARE_CELLS;
|
||||
let mut char_fallbacks = 0usize;
|
||||
let ops = lcs_ops(&old.iter().map(key).collect::<Vec<_>>(), &new.iter().map(key).collect::<Vec<_>>(), &mut budget);
|
||||
let mut ctx = DiffCtx { ins, del, opts: &opts, budget: &mut budget, char_fallbacks: &mut char_fallbacks };
|
||||
let mut ctx = DiffCtx { ins, del, opts: &opts, budget: &mut budget, char_fallbacks: &mut char_fallbacks, plain_revised };
|
||||
let mut out: Vec<Arc<Block>> = Vec::new();
|
||||
let (mut i, mut j) = (0usize, 0usize);
|
||||
let mut changes = 0usize;
|
||||
@@ -393,9 +396,11 @@ fn flatten_region(out: &mut Vec<Op>, region: &[(Op, usize, Kind)]) {
|
||||
|
||||
/// A token of a changed paragraph: a word (or character), or an inline object without text
|
||||
/// of its own (a text box anchor, a picture, a bookmark…), which is compared and kept whole.
|
||||
/// Each token also carries the byte range it came from in its source paragraph, so the output
|
||||
/// segment can take the formatting of the run it came from instead of the paragraph's first run.
|
||||
enum Tok<'a> {
|
||||
Text(String),
|
||||
Obj(&'a InlineObject),
|
||||
Text(String, std::ops::Range<usize>),
|
||||
Obj(&'a InlineObject, std::ops::Range<usize>),
|
||||
}
|
||||
|
||||
/// What a comparison sees of a token: the text as the options say, an object with the text of
|
||||
@@ -421,52 +426,131 @@ fn same_objects(doc: &Document, a: &Paragraph, b: &Paragraph) -> bool {
|
||||
}
|
||||
|
||||
/// Words with the spaces after them ("The ", "cat "), or single characters, and the objects
|
||||
/// between them. Objects with text (fields, equations…) count as their text.
|
||||
/// between them. Objects with text (fields, equations…) count as their text. Each token records
|
||||
/// the byte range it came from in `p`, so its output segment can take that source run's
|
||||
/// formatting. An object's flattened text (or a broken marker's placeholder) maps to the marker's
|
||||
/// own range, which is the nearest formatting the object has.
|
||||
fn tokens(p: &Paragraph, characters: bool) -> Vec<Tok<'_>> {
|
||||
fn push(v: &mut Vec<Tok<'_>>, cur: &mut String, c: char, characters: bool) {
|
||||
fn push(v: &mut Vec<Tok<'_>>, cur: &mut String, start: usize, end: usize, c: char, characters: bool) {
|
||||
cur.push(c);
|
||||
if characters || c == ' ' {
|
||||
v.push(Tok::Text(std::mem::take(cur)));
|
||||
v.push(Tok::Text(std::mem::take(cur), start..end));
|
||||
}
|
||||
}
|
||||
let mut v = Vec::new();
|
||||
let mut cur = String::new();
|
||||
let mut cur_start = 0usize;
|
||||
let mut objects = p.objects.iter();
|
||||
for c in p.text.chars() {
|
||||
for (i, c) in p.text.char_indices() {
|
||||
let marker = i..i + OBJ.len_utf8();
|
||||
if c != OBJ {
|
||||
push(&mut v, &mut cur, c, characters);
|
||||
if cur.is_empty() {
|
||||
cur_start = i;
|
||||
}
|
||||
push(&mut v, &mut cur, cur_start, i + c.len_utf8(), c, characters);
|
||||
continue;
|
||||
}
|
||||
match objects.next() {
|
||||
Some(o) if o.plain_text().is_empty() => {
|
||||
if !cur.is_empty() {
|
||||
v.push(Tok::Text(std::mem::take(&mut cur)));
|
||||
v.push(Tok::Text(std::mem::take(&mut cur), cur_start..i));
|
||||
}
|
||||
v.push(Tok::Obj(o, marker));
|
||||
}
|
||||
Some(o) => {
|
||||
for c in o.plain_text().chars() {
|
||||
if cur.is_empty() {
|
||||
cur_start = i;
|
||||
}
|
||||
push(&mut v, &mut cur, cur_start, marker.end, c, characters);
|
||||
}
|
||||
v.push(Tok::Obj(o));
|
||||
}
|
||||
Some(o) => o.plain_text().chars().for_each(|c| push(&mut v, &mut cur, c, characters)),
|
||||
None => {
|
||||
// A marker without its object (a broken paragraph): kept as a visible
|
||||
// placeholder, so the loss shows in the result instead of vanishing.
|
||||
log::warn!("compare: an object marker without its object");
|
||||
push(&mut v, &mut cur, char::REPLACEMENT_CHARACTER, characters);
|
||||
if cur.is_empty() {
|
||||
cur_start = i;
|
||||
}
|
||||
push(&mut v, &mut cur, cur_start, marker.end, char::REPLACEMENT_CHARACTER, characters);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !cur.is_empty() {
|
||||
v.push(Tok::Text(cur));
|
||||
v.push(Tok::Text(cur, cur_start..p.text.len()));
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// One side's run formatting for `word_diff`: the paragraph's run ranges (from
|
||||
/// [`Paragraph::run_ranges`]), walked with a forward cursor. `word_diff` consumes each side's
|
||||
/// tokens in increasing offset order, so the cursor makes a token's lookup amortised O(1)
|
||||
/// instead of rescanning the runs from the start for every token; an out-of-order offset falls
|
||||
/// back to a binary search, so the lookup stays correct for any offset.
|
||||
struct SideProps<'a> {
|
||||
ranges: Vec<(std::ops::Range<usize>, &'a CharProps)>,
|
||||
mark: &'a CharProps,
|
||||
cursor: usize,
|
||||
}
|
||||
|
||||
impl<'a> SideProps<'a> {
|
||||
fn new(p: &'a Paragraph) -> Self {
|
||||
SideProps { ranges: p.run_ranges().collect(), mark: &p.mark, cursor: 0 }
|
||||
}
|
||||
|
||||
/// The props of the run holding byte `off`: that run's props, or the paragraph mark when
|
||||
/// `off` is past the last run — the same answer `Paragraph::props_of_char` gives.
|
||||
fn run_props(&mut self, off: usize) -> &'a CharProps {
|
||||
while self.cursor < self.ranges.len() && self.ranges[self.cursor].0.end <= off {
|
||||
self.cursor += 1;
|
||||
}
|
||||
if let Some((range, props)) = self.ranges.get(self.cursor)
|
||||
&& range.contains(&off)
|
||||
{
|
||||
return props;
|
||||
}
|
||||
let idx = self.ranges.partition_point(|(r, _)| r.start <= off);
|
||||
match idx.checked_sub(1).and_then(|i| self.ranges.get(i)) {
|
||||
Some((range, props)) if range.contains(&off) => props,
|
||||
_ => self.mark,
|
||||
}
|
||||
}
|
||||
|
||||
/// The formatting of the source run a token came from: the run holding the token's first
|
||||
/// byte, with the revision metadata stripped. `ins`, `del` and `fmt_change` are indices into
|
||||
/// the *source* document's own revision table, and the rebuilt paragraph lives in the result
|
||||
/// document, whose table is a different one, so a source mark can never be carried across;
|
||||
/// `word_diff` stamps the compare's own `ins`/`del` on the segments it marks. The consequence
|
||||
/// is plain: a pre-existing pending revision inside a changed paragraph (say another author's
|
||||
/// deletion) is superseded by the compare result rather than preserved. The hyperlink is
|
||||
/// kept, because Reject restores it on a deleted run (at token granularity: the link is taken
|
||||
/// from the token's first byte, so a token that starts unlinked can lose a mid-token link).
|
||||
fn props_of(&mut self, t: &Tok<'_>) -> CharProps {
|
||||
let off = match t {
|
||||
Tok::Text(_, range) | Tok::Obj(_, range) => range.start,
|
||||
};
|
||||
let c = self.run_props(off);
|
||||
CharProps { ins: None, del: None, fmt_change: None, ..c.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
/// The props of an output token: those of the source run it came from, or — never expected, since
|
||||
/// [`tokens`] and [`lcs_ops`] consume both sides in step — the given fallback. A miss is loud.
|
||||
fn output_props(side: &mut SideProps<'_>, t: Option<&Tok<'_>>, fallback: &CharProps) -> CharProps {
|
||||
if t.is_none() {
|
||||
log::warn!("compare: a token range lookup missed; `tokens` and `lcs_ops` are out of step");
|
||||
}
|
||||
t.map(|t| side.props_of(t)).unwrap_or_else(|| fallback.clone())
|
||||
}
|
||||
|
||||
/// Add a token at the end of `p` with `props`.
|
||||
fn put(p: &mut Paragraph, t: Option<&Tok<'_>>, props: &CharProps) {
|
||||
let at = p.len();
|
||||
match t {
|
||||
Some(Tok::Text(t)) => {
|
||||
Some(Tok::Text(t, _)) => {
|
||||
let _ = p.insert_text(at, t, props);
|
||||
}
|
||||
Some(Tok::Obj(o)) => {
|
||||
Some(Tok::Obj(o, _)) => {
|
||||
let _ = p.insert_object(at, (*o).clone(), props);
|
||||
}
|
||||
None => {}
|
||||
@@ -500,10 +584,12 @@ fn word_diff(doc: &Document, a: &Paragraph, b: &Paragraph, ctx: &mut DiffCtx<'_>
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let plain_revised = ctx.plain_revised;
|
||||
let (ta, tb) = (tokens(a, chars), tokens(b, chars));
|
||||
let (mut a_props, mut b_props) = (SideProps::new(a), SideProps::new(b));
|
||||
let key = |t: &Tok<'_>| match t {
|
||||
Tok::Text(t) => Key::Text(ctx.opts.key(t)),
|
||||
Tok::Obj(o) => obj_key(doc, o),
|
||||
Tok::Text(t, _) => Key::Text(ctx.opts.key(t)),
|
||||
Tok::Obj(o, _) => obj_key(doc, o),
|
||||
};
|
||||
let ops = lcs_ops(&ta.iter().map(key).collect::<Vec<_>>(), &tb.iter().map(key).collect::<Vec<_>>(), ctx.budget);
|
||||
// Only at character granularity: a matched whole word almost never happens by coincidence,
|
||||
@@ -511,23 +597,46 @@ fn word_diff(doc: &Document, a: &Paragraph, b: &Paragraph, ctx: &mut DiffCtx<'_>
|
||||
// a short interior match need folding back into its surrounding edit.
|
||||
let ops = if chars { collapse_noise_matches(&ops) } else { ops };
|
||||
let mut p = Paragraph::new();
|
||||
p.props = b.props.clone();
|
||||
let base = a.runs.first().map(|r| r.props.clone()).unwrap_or_default();
|
||||
// The revised paragraph's properties are used with its revision bookkeeping stripped, like runs
|
||||
// (`SideProps::props_of`) and the mark, because `fmt_change`/`num_change` are indices into the revised
|
||||
// document's own revision table and are meaningless in the result document. A plain-text
|
||||
// revised side, which carries no paragraph properties of its own, falls back to the original's,
|
||||
// stripped the same way, so pre-existing revision state never leaks into the rebuilt paragraph.
|
||||
p.props = if plain_revised && b.props.is_empty() { a.props.formatting() } else { b.props.formatting() };
|
||||
// The paragraph mark (the formatting typing into the paragraph's end would get) is the revised
|
||||
// paragraph's when it comes from a document, and the original's when the revised side is plain
|
||||
// text and has no mark of its own. Its own revision bookkeeping (`ins`, `del`, `fmt_change`)
|
||||
// is cleared because those indices belong to another document's revision table.
|
||||
p.mark = if plain_revised {
|
||||
CharProps { ins: None, del: None, fmt_change: None, ..a.mark.clone() }
|
||||
} else {
|
||||
CharProps { ins: None, del: None, fmt_change: None, ..b.mark.clone() }
|
||||
};
|
||||
let fallback = a.runs.first().map(|r| CharProps { ins: None, del: None, fmt_change: None, ..r.props.clone() }).unwrap_or_default();
|
||||
let fallback_b = b.runs.first().map(|r| CharProps { ins: None, del: None, fmt_change: None, ..r.props.clone() }).unwrap_or_default();
|
||||
let (mut i, mut j) = (0, 0);
|
||||
for op in ops {
|
||||
match op {
|
||||
Op::Same => {
|
||||
// Equal as compared (perhaps up to case or spacing): the revised text.
|
||||
put(&mut p, tb.get(j), &base);
|
||||
// Equal as compared (perhaps up to case or spacing): the revised text, but the
|
||||
// formatting of the run it stands for. For a document revised side that is the
|
||||
// revised run, as issue #3749 prescribes ("Copy unchanged tokens from the revised
|
||||
// paragraph with their own properties"); a plain-text revised side has no runs to
|
||||
// speak of, so the original run's formatting is kept instead.
|
||||
let props =
|
||||
if plain_revised { output_props(&mut a_props, ta.get(i), &fallback) } else { output_props(&mut b_props, tb.get(j), &fallback_b) };
|
||||
put(&mut p, tb.get(j), &props);
|
||||
i += 1;
|
||||
j += 1;
|
||||
}
|
||||
Op::Del => {
|
||||
put(&mut p, ta.get(i), &CharProps { del: Some(ctx.del), ..base.clone() });
|
||||
let props = output_props(&mut a_props, ta.get(i), &fallback);
|
||||
put(&mut p, ta.get(i), &CharProps { del: Some(ctx.del), ..props });
|
||||
i += 1;
|
||||
}
|
||||
Op::Ins => {
|
||||
put(&mut p, tb.get(j), &CharProps { ins: Some(ctx.ins), ..base.clone() });
|
||||
let props = tb.get(j).map(|t| b_props.props_of(t)).unwrap_or_else(|| fallback.clone());
|
||||
put(&mut p, tb.get(j), &CharProps { ins: Some(ctx.ins), ..props });
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
mod compare_stage0;
|
||||
|
||||
#[test]
|
||||
fn compare_marks_changes() {
|
||||
let mut s = Session::new(Document::from_text("The cat sat.\nKeep this.\nOld line."));
|
||||
|
||||
@@ -0,0 +1,349 @@
|
||||
//! Regression tests for #3749 Stage 0: formatting-preserving compare.
|
||||
//!
|
||||
//! `word_diff` carries source byte ranges through tokenization, so every output segment takes its
|
||||
//! `CharProps` from the run it came from instead of stamping the original paragraph's first run on
|
||||
//! everything. The revised side decides which run each segment follows: when the revised side is a
|
||||
//! real document, Same/Ins take the revised run and Del takes the original run; when it is plain
|
||||
//! text (with no runs of its own), Same/Del take the original run and Ins takes the (default)
|
||||
//! revised run. The paragraph mark and `ParaProps` follow the revised side for a document, and fall
|
||||
//! back to the original's for plain text.
|
||||
//!
|
||||
//! The ins/del segmentation for text-only paragraphs is unchanged: the goldens below were captured
|
||||
//! from the pre-fix binary (`review.compare` over a plain original and a plain revised text, word
|
||||
//! and character level) and must keep matching.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A run signature: its text, and whether it is marked inserted / deleted.
|
||||
fn run_sig(p: &Paragraph) -> Vec<(String, bool, bool)> {
|
||||
p.run_ranges().map(|(r, c)| (p.text[r].to_string(), c.ins.is_some(), c.del.is_some())).collect()
|
||||
}
|
||||
|
||||
/// The first run whose text contains `needle`.
|
||||
fn run_with<'a>(p: &'a Paragraph, needle: &str) -> (&'a str, &'a CharProps) {
|
||||
p.run_ranges()
|
||||
.find(|(r, _)| p.text[r.clone()].contains(needle))
|
||||
.map(|(r, c)| (&p.text[r], c))
|
||||
.unwrap_or_else(|| panic!("no run contains {needle:?} in {:?}", p.text))
|
||||
}
|
||||
|
||||
// --- mixed character formatting -----------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn mixed_formatting_is_preserved_per_range() {
|
||||
let mut s = Session::new(Document::from_text("alpha beta gamma"));
|
||||
s.run("select.text", &json!({"text": "alpha"})).unwrap();
|
||||
s.run("format.bold", &json!({})).unwrap();
|
||||
s.run("select.text", &json!({"text": "gamma"})).unwrap();
|
||||
s.run("format.italic", &json!({})).unwrap();
|
||||
s.run("review.compare", &json!({"text": "alpha delta gamma"})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
// The deleted text stays in the tracked paragraph; the visible result is what Accept shows.
|
||||
assert_eq!(p.plain_text(), "alpha beta delta gamma");
|
||||
assert_eq!(p.final_text(), "alpha delta gamma");
|
||||
let (_, alpha) = run_with(p, "alpha");
|
||||
assert_eq!(alpha.bold, Some(true), "unchanged `alpha` keeps the original's bold");
|
||||
assert_ne!(alpha.italic, Some(true));
|
||||
let (_, gamma) = run_with(p, "gamma");
|
||||
assert_eq!(gamma.italic, Some(true), "unchanged `gamma` keeps the original's italic");
|
||||
assert_ne!(gamma.bold, Some(true));
|
||||
let (_, delta) = run_with(p, "delta");
|
||||
assert!(delta.ins.is_some(), "inserted `delta` is marked inserted");
|
||||
assert_ne!(delta.bold, Some(true), "inserted text does not inherit the original's bold");
|
||||
assert_ne!(delta.italic, Some(true), "inserted text does not inherit the original's italic");
|
||||
let (_, beta) = run_with(p, "beta");
|
||||
assert!(beta.del.is_some(), "removed `beta` is marked deleted");
|
||||
assert_ne!(beta.bold, Some(true));
|
||||
}
|
||||
|
||||
// --- byte-range props lookup --------------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn change_point_on_a_multibyte_char_takes_its_own_run() {
|
||||
// The change point is the second character, `ä` (2 bytes). The byte-range lookup must land on
|
||||
// `ä`'s own run; a character count used as a byte offset would land inside the preceding `é`
|
||||
// and stamp its bold on the deleted `ä`.
|
||||
let mut s = Session::new(Document::from_text("éä"));
|
||||
s.run("select.text", &json!({"text": "é"})).unwrap();
|
||||
s.run("format.bold", &json!({})).unwrap();
|
||||
s.run("select.text", &json!({"text": "ä"})).unwrap();
|
||||
s.run("format.italic", &json!({})).unwrap();
|
||||
s.run("review.compare", &json!({"text": "éö", "level": "character"})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p.plain_text(), "éäö", "deleted `ä` and inserted `ö`");
|
||||
let (_, ae) = run_with(p, "ä");
|
||||
assert!(ae.del.is_some(), "the changed multi-byte char is deleted");
|
||||
assert_eq!(ae.italic, Some(true), "it takes its own run");
|
||||
assert_ne!(ae.bold, Some(true), "and not the preceding multi-byte run's props");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_run_at_a_token_start_does_not_shadow_its_props() {
|
||||
let mut s = Session::new(Document::from_text("alpha beta gamma"));
|
||||
s.run("select.text", &json!({"text": "beta"})).unwrap();
|
||||
s.run("format.italic", &json!({})).unwrap();
|
||||
{
|
||||
let mut p = s.doc.body[0].as_para().unwrap().clone();
|
||||
let at = p.text.find("beta").unwrap();
|
||||
let mut idx = 0usize;
|
||||
let mut start = 0usize;
|
||||
for (i, r) in p.runs.iter().enumerate() {
|
||||
if start == at {
|
||||
idx = i;
|
||||
break;
|
||||
}
|
||||
start += r.len;
|
||||
}
|
||||
// An empty run with different props exactly at "beta"'s start.
|
||||
p.runs.insert(idx, wordcraft_doc::para::Run { len: 0, props: CharProps { bold: Some(true), ..CharProps::default() } });
|
||||
s.doc.body[0] = std::sync::Arc::new(Block::Para(p));
|
||||
}
|
||||
s.run("review.compare", &json!({"text": "alpha delta gamma"})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let (_, beta) = run_with(p, "beta");
|
||||
assert!(beta.del.is_some());
|
||||
assert_eq!(beta.italic, Some(true), "the token takes the run after the empty one");
|
||||
assert_ne!(beta.bold, Some(true), "an empty run at the token's start must not shadow the next run's props");
|
||||
}
|
||||
|
||||
// --- hyperlinks ---------------------------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn middle_run_hyperlink_survives_and_reject_restores_it() {
|
||||
let mut s = Session::new(Document::from_text("click here now"));
|
||||
s.run("select.text", &json!({"text": "here"})).unwrap();
|
||||
s.run("insert.link", &json!({"url": "https://example.com"})).unwrap();
|
||||
s.run("review.compare", &json!({"text": "click there now"})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let (_, here) = run_with(p, "here");
|
||||
assert!(here.del.is_some(), "the removed word is deleted");
|
||||
assert_eq!(here.link.as_deref(), Some("https://example.com"), "the deleted run keeps its hyperlink");
|
||||
let (_, there) = run_with(p, "there");
|
||||
assert!(there.ins.is_some());
|
||||
assert_eq!(there.link, None, "inserted text takes the revised run's formatting (no link)");
|
||||
s.run("review.rejectAll", &json!({})).unwrap();
|
||||
assert_eq!(s.doc.plain_text(StoryRef::Body), "click here now");
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let (_, here) = run_with(p, "here");
|
||||
assert_eq!(here.link.as_deref(), Some("https://example.com"), "Reject restores the hyperlink at token level");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_run_hyperlink_does_not_leak_onto_inserted_text() {
|
||||
let mut s = Session::new(Document::from_text("here click now"));
|
||||
s.run("select.text", &json!({"text": "here"})).unwrap();
|
||||
s.run("insert.link", &json!({"url": "https://example.com"})).unwrap();
|
||||
s.run("review.compare", &json!({"text": "here there click now"})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let (_, here) = run_with(p, "here");
|
||||
assert_eq!(here.link.as_deref(), Some("https://example.com"), "the unchanged first run keeps its link");
|
||||
let (_, there) = run_with(p, "there");
|
||||
assert!(there.ins.is_some());
|
||||
assert_eq!(there.link, None, "the first run's link must not leak onto inserted text");
|
||||
}
|
||||
|
||||
// --- reject restores formatting -----------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn deleted_segment_keeps_formatting_so_reject_restores_it() {
|
||||
let mut s = Session::new(Document::from_text("alpha beta gamma"));
|
||||
s.run("select.text", &json!({"text": "alpha"})).unwrap();
|
||||
s.run("format.bold", &json!({})).unwrap();
|
||||
s.run("select.text", &json!({"text": "beta"})).unwrap();
|
||||
s.run("format.italic", &json!({})).unwrap();
|
||||
s.run("review.compare", &json!({"text": "alpha delta gamma"})).unwrap();
|
||||
s.run("review.rejectAll", &json!({})).unwrap();
|
||||
assert_eq!(s.doc.plain_text(StoryRef::Body), "alpha beta gamma");
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let (_, alpha) = run_with(p, "alpha");
|
||||
assert_eq!(alpha.bold, Some(true));
|
||||
let (_, beta) = run_with(p, "beta");
|
||||
assert_eq!(beta.italic, Some(true), "the deleted token kept its italic, so Reject restores it at token level");
|
||||
}
|
||||
|
||||
// --- text-only segmentation identity (goldens from the pre-fix binary) --------------------------
|
||||
|
||||
#[test]
|
||||
fn text_only_segmentation_matches_pre_fix_golden() {
|
||||
// (original, revised, level, expected (text, ins, del) runs)
|
||||
let cases: &[(&str, &str, &str, &[(&str, bool, bool)])] = &[
|
||||
("The dog sat.", "The cat sat.", "word", &[("The ", false, false), ("dog ", false, true), ("cat ", true, false), ("sat.", false, false)]),
|
||||
("a cat ran", "a dog ran", "word", &[("a ", false, false), ("cat ", false, true), ("dog ", true, false), ("ran", false, false)]),
|
||||
("colour", "color", "word", &[("colour", false, true), ("color", true, false)]),
|
||||
(
|
||||
"zawarta w dniu 01.09.2026 pomiędzy:",
|
||||
"zawarta w dniu 19.10.2026 pomiędzy:",
|
||||
"word",
|
||||
&[("zawarta w dniu ", false, false), ("01.09.2026 ", false, true), ("19.10.2026 ", true, false), ("pomiędzy:", false, false)],
|
||||
),
|
||||
("The dog sat.", "The cat sat.", "character", &[("The ", false, false), ("dog", false, true), ("cat", true, false), (" sat.", false, false)]),
|
||||
("a cat ran", "a dog ran", "character", &[("a ", false, false), ("cat", false, true), ("dog", true, false), (" ran", false, false)]),
|
||||
("colour", "color", "character", &[("colo", false, false), ("u", false, true), ("r", false, false)]),
|
||||
(
|
||||
"zawarta w dniu 01.09.2026 pomiędzy:",
|
||||
"zawarta w dniu 19.10.2026 pomiędzy:",
|
||||
"character",
|
||||
&[("zawarta w dniu ", false, false), ("01.09", false, true), ("19.10", true, false), (".2026 pomiędzy:", false, false)],
|
||||
),
|
||||
];
|
||||
for (orig, rev, level, want) in cases {
|
||||
let mut s = Session::new(Document::from_text(orig));
|
||||
s.run("review.compare", &json!({"text": rev, "level": level})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let want: Vec<(String, bool, bool)> = want.iter().map(|(t, i, d)| ((*t).to_string(), *i, *d)).collect();
|
||||
assert_eq!(run_sig(p), want, "{orig:?} -> {rev:?} [{level}]");
|
||||
}
|
||||
}
|
||||
|
||||
// --- paragraph mark and paragraph properties ----------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn paragraph_mark_is_preserved() {
|
||||
let mut s = Session::new(Document::from_text("alpha beta gamma"));
|
||||
{
|
||||
let mut p = s.doc.body[0].as_para().unwrap().clone();
|
||||
p.mark.bold = Some(true);
|
||||
s.doc.body[0] = std::sync::Arc::new(Block::Para(p));
|
||||
}
|
||||
s.run("review.compare", &json!({"text": "alpha delta gamma"})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p.mark.bold, Some(true), "the rebuilt paragraph keeps the original paragraph mark");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_props_kept_when_revised_is_plain() {
|
||||
let mut s = Session::new(Document::from_text("alpha beta gamma"));
|
||||
s.run("para.style", &json!({"style": "Heading1"})).unwrap();
|
||||
s.run("para.alignCenter", &json!({})).unwrap();
|
||||
s.run("review.compare", &json!({"text": "alpha delta gamma"})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p.props.style.as_deref(), Some("Heading1"), "the original style survives a plain revised side");
|
||||
assert!(p.props.align.is_some(), "the original alignment survives a plain revised side");
|
||||
}
|
||||
|
||||
// --- object pass-through (not flattened away by the rebuild) -------------------------------------
|
||||
|
||||
/// A session whose single paragraph is `alpha beta gamma` with a picture after "beta ", already
|
||||
/// compared against `alpha delta gamma`: the picture's anchor sits in a deleted run.
|
||||
fn compared_with_picture() -> Session {
|
||||
let mut s = Session::new(Document::from_text("alpha beta gamma"));
|
||||
s.run("caret.set", &json!({"pos": Pos::body(0, "alpha beta ".len())})).unwrap();
|
||||
let png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
|
||||
s.run("insert.picture", &json!({"data": png})).unwrap();
|
||||
assert_eq!(s.doc.body[0].as_para().unwrap().objects.len(), 1, "the picture was inserted");
|
||||
s.run("review.compare", &json!({"text": "alpha delta gamma"})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn picture_anchor_survives_a_changed_paragraph() {
|
||||
let s = compared_with_picture();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p.objects.len(), 1, "the changed paragraph's picture anchor survives the rebuild");
|
||||
let off = p.object_offsets().first().copied().expect("the picture anchor is in the text");
|
||||
assert!(p.props_of_char(off).del.is_some(), "the picture anchor sits in a deleted run");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepting_removes_the_deleted_picture_and_rejecting_restores_it() {
|
||||
let mut accepted = compared_with_picture();
|
||||
accepted.run("review.acceptAll", &json!({})).unwrap();
|
||||
assert_eq!(accepted.doc.body[0].as_para().unwrap().objects.len(), 0, "accepting the deletion removes the picture");
|
||||
|
||||
let mut rejected = compared_with_picture();
|
||||
rejected.run("review.rejectAll", &json!({})).unwrap();
|
||||
assert_eq!(rejected.doc.body[0].as_para().unwrap().objects.len(), 1, "rejecting the deletion restores the picture");
|
||||
}
|
||||
|
||||
// --- document revised side: unchanged tokens follow the revised formatting (#3749) --------------
|
||||
|
||||
#[test]
|
||||
fn unchanged_word_takes_the_revised_formatting_document_path() {
|
||||
let rev = Session::new(Document::from_text("Read the manual now."));
|
||||
let path = std::env::temp_dir().join(format!("wc-compare-s0-rev-bold-{}.docx", std::process::id()));
|
||||
crate::io::save_path(&path, &rev.doc).unwrap();
|
||||
|
||||
let mut s = Session::new(Document::from_text("Read the manual today."));
|
||||
s.run("select.text", &json!({"text": "manual"})).unwrap();
|
||||
s.run("format.bold", &json!({})).unwrap();
|
||||
s.run("review.compare", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let (_, manual) = run_with(p, "manual");
|
||||
assert_ne!(manual.bold, Some(true), "the unchanged word takes the revised side's formatting, not the original's bold");
|
||||
let (_, today) = run_with(p, "today.");
|
||||
assert!(today.del.is_some(), "the removed word is deleted");
|
||||
let (_, now) = run_with(p, "now.");
|
||||
assert!(now.ins.is_some(), "the added word is inserted");
|
||||
|
||||
s.run("review.acceptAll", &json!({})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p.plain_text(), "Read the manual now.");
|
||||
let (_, manual) = run_with(p, "manual");
|
||||
assert_ne!(manual.bold, Some(true), "accepting leaves the revised (unbolded) formatting");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_style_change_is_not_masked_document_path() {
|
||||
let rev = Session::new(Document::from_text("Introduction to X"));
|
||||
let path = std::env::temp_dir().join(format!("wc-compare-s0-style-{}.docx", std::process::id()));
|
||||
crate::io::save_path(&path, &rev.doc).unwrap();
|
||||
|
||||
let mut s = Session::new(Document::from_text("Introduction"));
|
||||
s.run("para.style", &json!({"style": "Heading1"})).unwrap();
|
||||
s.run("review.compare", &json!({"path": path.to_string_lossy(), "level": "character"})).unwrap();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
assert_ne!(p.props.style.as_deref(), Some("Heading1"), "the revised paragraph's empty properties win, so the style change isn't masked");
|
||||
assert!(p.run_ranges().any(|(_, c)| c.ins.is_some()), "the text was diffed, not wholesale replaced");
|
||||
let (_, intro) = run_with(p, "Introduction");
|
||||
assert!(intro.ins.is_none() && intro.del.is_none(), "the unchanged text survives unmarked");
|
||||
let (_, added) = run_with(p, " to X");
|
||||
assert!(added.ins.is_some(), "only the added text is insertion-marked");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unchanged_word_gains_the_revised_formatting_document_path() {
|
||||
let mut rev = Session::new(Document::from_text("Read the manual now."));
|
||||
rev.run("select.text", &json!({"text": "manual"})).unwrap();
|
||||
rev.run("format.bold", &json!({})).unwrap();
|
||||
let path = std::env::temp_dir().join(format!("wc-compare-s0-rev-gains-bold-{}.docx", std::process::id()));
|
||||
crate::io::save_path(&path, &rev.doc).unwrap();
|
||||
|
||||
let mut s = Session::new(Document::from_text("Read the manual today."));
|
||||
s.run("review.compare", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let (_, manual) = run_with(p, "manual");
|
||||
assert_eq!(manual.bold, Some(true), "the unchanged word takes the revised side's bold");
|
||||
let (_, today) = run_with(p, "today.");
|
||||
assert!(today.del.is_some(), "the removed word is deleted");
|
||||
let (_, now) = run_with(p, "now.");
|
||||
assert!(now.ins.is_some(), "the added word is inserted");
|
||||
|
||||
s.run("review.acceptAll", &json!({})).unwrap();
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p.plain_text(), "Read the manual now.");
|
||||
let (_, manual) = run_with(p, "manual");
|
||||
assert_eq!(manual.bold, Some(true), "accepting leaves the revised (bolded) formatting");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removed_hyperlink_on_an_unchanged_word_follows_the_revised_side_document_path() {
|
||||
let rev = Session::new(Document::from_text("Read the manual now."));
|
||||
let path = std::env::temp_dir().join(format!("wc-compare-s0-link-{}.docx", std::process::id()));
|
||||
crate::io::save_path(&path, &rev.doc).unwrap();
|
||||
|
||||
let mut s = Session::new(Document::from_text("Read the manual today."));
|
||||
s.run("select.text", &json!({"text": "manual"})).unwrap();
|
||||
s.run("insert.link", &json!({"url": "https://example.com"})).unwrap();
|
||||
s.run("review.compare", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
|
||||
let p = s.doc.body[0].as_para().unwrap();
|
||||
let (_, manual) = run_with(p, "manual");
|
||||
assert_eq!(manual.link, None, "the unchanged word takes the revised side's run, which dropped the link");
|
||||
}
|
||||
Reference in new issue
Block a user