jubarte-edit 0.20.0

The edit product of the jubarte engine: edit plans as tracked changes, text diff views, capabilities and staged publication
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
//
// SPDX-License-Identifier: Apache-2.0

//! The smallest text edits that make a paragraph read as new text, for the
//! `rewrite` operation, and that make a `replace`'s matched text read as its
//! replacement.

use std::io::{Cursor, Write};
use std::ops::Range;

use similar::{Algorithm, DiffTag, capture_diff_slices};
use zip::ZipWriter;
use zip::write::SimpleFileOptions;

use super::anchor::reads_as;
use crate::comparer::WmlComparerSettings;
use crate::namespaces::W;
use crate::util::word_tokens;
use crate::xmllinq::Dom;

/// A character that holds a place in the projection but is no run text: a
/// symbol (`w:sym`, U+FFFC). It never takes part in an edit.
fn kept(c: char) -> bool {
    c == '\u{FFFC}'
}

/// A comparison unit of the old text: its key and its bytes.
struct Unit {
    key: String,
    start: usize,
    end: usize,
}

/// The old text in units, each keyed as it reads (a curly quote as the
/// straight one, a non-breaking space as a space, a zero-width character as
/// nothing), so a look-alike never differs: words and marks as they are,
/// any run of whitespace as one space when `collapse` (a tab or a line
/// break equals a space), symbols and text that reads as nothing left out.
fn units(text: &str, collapse: bool) -> Vec<Unit> {
    let mut out: Vec<Unit> = Vec::new();
    let mut at = 0;
    for token in word_tokens(text) {
        let (start, end) = (at, at + token.len());
        at = end;
        let key: String = token.chars().filter_map(reads_as).collect();
        if token.chars().all(kept) || key.is_empty() {
            continue;
        }
        if collapse && token.chars().all(char::is_whitespace) {
            // Whitespace on both sides of a left-out symbol is one unit.
            if let Some(last) = out.last_mut()
                && last.key == " "
            {
                last.end = end;
                continue;
            }
            out.push(Unit {
                key: " ".to_string(),
                start,
                end,
            });
            continue;
        }
        out.push(Unit { key, start, end });
    }
    out
}

/// The tabs and line breaks of some whitespace, in order.
fn breaks(whitespace: &str) -> impl Iterator<Item = char> + '_ {
    whitespace.chars().filter(|c| matches!(c, '\t' | '\n'))
}

/// Byte ranges of `old` and their replacements that make it read as `new`,
/// in order and without overlaps. A tab or a line break of `old` may be
/// written as a space in `new` and stays; a `\t` or `\n` that `new` writes
/// where `old` has none is written. Symbols (U+FFFC) may be left out of
/// `new`: they stay where they are, and no edit covers one. Words an edit
/// replaces take the tabs and breaks between them along. Text of `old`
/// that reads as `new`'s (a curly quote, a non-breaking space) stays as it
/// is. Changes that only whitespace separates are one range: every deleted
/// word, then every inserted word, never one substitution per word (#1923).
/// A kept word still starts a new range.
pub(super) fn rewrite_ranges(old: &str, new: &str) -> Vec<(usize, usize, String)> {
    ranges(old, new, true)
}

/// [`rewrite_ranges`] for a `replace`: whitespace compares as written, so
/// a double space or a tab the anchor names is replaced as asked, while
/// a look-alike of the replacement's character still stays.
pub(super) fn replace_ranges(old: &str, new: &str) -> Vec<(usize, usize, String)> {
    ranges(old, new, false)
}

fn ranges(old: &str, new: &str, collapse: bool) -> Vec<(usize, usize, String)> {
    let olds = units(old, collapse);
    let news = units(new, collapse);
    let old_keys: Vec<&str> = olds.iter().map(|u| u.key.as_str()).collect();
    let new_keys: Vec<&str> = news.iter().map(|u| u.key.as_str()).collect();
    // Each op with the equal gaps a join took in: their units stay `old`'s.
    type Gaps = Vec<(Range<usize>, Range<usize>)>;
    let mut ops: Vec<(DiffTag, Range<usize>, Range<usize>, Gaps)> = Vec::new();
    for op in capture_diff_slices(Algorithm::Myers, &old_keys, &new_keys) {
        let (tag, old_range, new_range) = op.as_tag_tuple();
        // A hunk is every deleted word and then every inserted word. The
        // space tokens between them would otherwise become one substitution
        // per deleted word, paired by position (#1923). A kept word still
        // splits the hunks. `replace` joined this way already; a `rewrite`
        // joins the same way.
        if tag != DiffTag::Equal {
            let len = ops.len();
            let joins = match ops.as_slice() {
                [.., (a, ..), (DiffTag::Equal, gap, ..)] if *a != DiffTag::Equal => {
                    olds[gap.clone()].iter().all(|u| u.key.trim().is_empty())
                }
                [.., (a, ..)] => *a != DiffTag::Equal,
                [] => false,
            };
            if joins {
                let first = if ops[len - 1].0 == DiffTag::Equal {
                    len - 2
                } else {
                    len - 1
                };
                let mut drained = ops.drain(first..);
                let (_, old_start, new_start, mut gaps) = drained.next().expect("ops to join");
                gaps.extend(drained.map(|(_, o, n, _)| (o, n)));
                ops.push((
                    DiffTag::Replace,
                    old_start.start..old_range.end,
                    new_start.start..new_range.end,
                    gaps,
                ));
                continue;
            }
        }
        ops.push((tag, old_range, new_range, Vec::new()));
    }
    let mut edits = Vec::new();
    for (tag, old_range, new_range, gaps) in ops {
        if tag == DiffTag::Equal {
            for (o, n) in olds[old_range].iter().zip(&news[new_range]) {
                let wanted = &new[n.start..n.end];
                if collapse
                    && o.key == " "
                    && breaks(wanted).next().is_some()
                    && !breaks(wanted).eq(breaks(&old[o.start..o.end]))
                {
                    edits.extend(split_around_kept(old, o.start, o.end, wanted));
                }
            }
            continue;
        }
        // A gap a join took in reads as `new`'s and keeps `old`'s text.
        let replacement: String = new_range
            .clone()
            .map(|i| {
                gaps.iter().find(|(_, n)| n.contains(&i)).map_or(
                    &new[news[i].start..news[i].end],
                    |(o, n)| {
                        let u = &olds[o.start + (i - n.start)];
                        &old[u.start..u.end]
                    },
                )
            })
            .collect();
        let (start, end) = match (olds.get(old_range.start), old_range.is_empty()) {
            (Some(first), false) => (first.start, olds[old_range.end - 1].end),
            _ => {
                // An insertion: after the unit before it, or at the start.
                let at = old_range
                    .start
                    .checked_sub(1)
                    .and_then(|i| olds.get(i))
                    .map_or(0, |u| u.end);
                (at, at)
            }
        };
        edits.extend(split_around_kept(old, start, end, &replacement));
    }
    edits
}

/// `old` after `edits`, applied from the end so earlier offsets stay put.
pub(super) fn apply_ranges(old: &str, edits: &[(usize, usize, String)]) -> String {
    let mut out = old.to_string();
    for (start, end, text) in edits.iter().rev() {
        out.replace_range(*start..*end, text);
    }
    out
}

/// The comparer's hunks of `old` against `new`, in `old`'s byte offsets.
///
/// A keep-path rewrite emits these so its marks are the ones the clean path
/// writes. The word diff joins a whitespace-separated hunk, but its longest
/// common subsequence still anchors on a different word than the comparer
/// (`that` against `provided`, #1923). `None` when the compare does not tile
/// `old` or does not read back as `new`: the caller keeps the word diff.
pub(super) fn comparer_ranges(old: &str, new: &str) -> Option<Vec<(usize, usize, String)>> {
    let settings = WmlComparerSettings {
        preserve_untouched: true,
        ..WmlComparerSettings::default()
    };
    let compared = crate::document_comparer::compare_documents_for_edit(
        &plain_docx(old)?,
        &plain_docx(new)?,
        &settings,
    )
    .ok()?;
    let compared = super::replacement_order::typing_order(&compared).unwrap_or(compared);
    let xml = crate::opc::PartFs::open(&compared)
        .ok()?
        .part_string("word/document.xml")?;
    let mut dom = Dom::new();
    let doc = dom.parse_xdocument(&xml);
    let root = dom.root(doc)?;
    let paras = dom.descendants(root, Some(&W::p()));
    if paras.len() != 1 {
        return None;
    }
    let mut segs: Vec<(char, String)> = Vec::new();
    for node in dom.descendants(paras[0], None) {
        let kind = if dom.name_is(node, &W::del_text()) {
            '-'
        } else if dom.name_is(node, &W::t()) {
            let inserted = dom
                .ancestors(node, None)
                .into_iter()
                .take_while(|&a| a != paras[0])
                .any(|a| dom.name_is(a, &W::ins()));
            if inserted { '+' } else { '=' }
        } else {
            continue;
        };
        let text = dom.value(node);
        match segs.last_mut() {
            Some((k, s)) if *k == kind => s.push_str(&text),
            _ => segs.push((kind, text)),
        }
    }
    let edits = ranges_from_marks(old, &segs)?;
    (apply_ranges(old, &edits) == new).then_some(edits)
}

/// Kept text must be the next bytes of `old`. A deletion consumes `old`;
/// an insertion that follows it is that deletion's replacement. An
/// insertion with nothing deleted lands at the cursor.
fn ranges_from_marks(old: &str, segs: &[(char, String)]) -> Option<Vec<(usize, usize, String)>> {
    let mut cursor = 0;
    let mut i = 0;
    let mut edits = Vec::new();
    while i < segs.len() {
        let (kind, text) = &segs[i];
        match *kind {
            '=' => {
                let end = cursor + text.len();
                if old.get(cursor..end) != Some(text.as_str()) {
                    return None;
                }
                cursor = end;
                i += 1;
            }
            '-' => {
                let start = cursor;
                let end = cursor + text.len();
                if old.get(start..end) != Some(text.as_str()) {
                    return None;
                }
                cursor = end;
                i += 1;
                let mut replacement = String::new();
                while i < segs.len() && segs[i].0 == '+' {
                    replacement.push_str(&segs[i].1);
                    i += 1;
                }
                edits.push((start, end, replacement));
            }
            '+' => {
                let mut replacement = String::new();
                while i < segs.len() && segs[i].0 == '+' {
                    replacement.push_str(&segs[i].1);
                    i += 1;
                }
                if !replacement.is_empty() {
                    edits.push((cursor, cursor, replacement));
                }
            }
            _ => return None,
        }
    }
    (cursor == old.len()).then_some(edits)
}

/// One paragraph, the smallest package the comparer will read.
fn plain_docx(text: &str) -> Option<Vec<u8>> {
    let body = format!(
        "<w:p><w:r><w:t xml:space=\"preserve\">{}</w:t></w:r></w:p>",
        xml_text(text)?
    );
    let document = format!(
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\
<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" \
xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">\
<w:body>{body}<w:sectPr><w:pgSz w:w=\"12240\" w:h=\"15840\"/>\
<w:pgMar w:top=\"1440\" w:right=\"1440\" w:bottom=\"1440\" w:left=\"1440\" \
w:header=\"720\" w:footer=\"720\" w:gutter=\"0\"/></w:sectPr></w:body></w:document>"
    );
    let types = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\
<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\
<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>\
<Default Extension=\"xml\" ContentType=\"application/xml\"/>\
<Override PartName=\"/word/document.xml\" \
ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml\"/>\
</Types>";
    let rels = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\
<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 mut zip = ZipWriter::new(Cursor::new(Vec::new()));
    let opts = SimpleFileOptions::default();
    for (name, data) in [
        ("[Content_Types].xml", types),
        ("_rels/.rels", rels),
        ("word/document.xml", document.as_str()),
        (
            "word/_rels/document.xml.rels",
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\
<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"/>",
        ),
    ] {
        zip.start_file(name, opts).ok()?;
        zip.write_all(data.as_bytes()).ok()?;
    }
    Some(zip.finish().ok()?.into_inner())
}

/// Escape `text` for a `w:t`. A control character other than tab or a
/// line break cannot sit in XML text; the caller falls back.
fn xml_text(text: &str) -> Option<String> {
    let mut out = String::with_capacity(text.len());
    for c in text.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            c if c.is_control() && c != '\t' && c != '\n' && c != '\r' => return None,
            c => out.push(c),
        }
    }
    Some(out)
}

/// `[start, end)` of `old` with `replacement`, split so that no piece covers
/// a symbol. A replacement holding as many symbols as the range puts the
/// text between them in the matching gaps; any other goes, without its
/// symbols, to the first piece, and the other pieces are deleted.
fn split_around_kept(
    old: &str,
    start: usize,
    end: usize,
    replacement: &str,
) -> Vec<(usize, usize, String)> {
    // The gaps between the kept chars of the range, empty ones too.
    let mut gaps: Vec<(usize, usize)> = Vec::new();
    let mut gap_start = start;
    for (offset, c) in old[start..end].char_indices() {
        if kept(c) {
            let at = start + offset;
            gaps.push((gap_start, at));
            gap_start = at + c.len_utf8();
        }
    }
    gaps.push((gap_start, end));
    // The symbols themselves are never text.
    let parts: Vec<&str> = replacement.split(kept).collect();
    if parts.len() == gaps.len() {
        return gaps
            .into_iter()
            .zip(parts)
            .filter(|&((s, e), text)| old[s..e] != *text)
            .map(|((s, e), text)| (s, e, text.to_string()))
            .collect();
    }
    let replacement: String = parts.concat();
    let mut pieces: Vec<(usize, usize)> = gaps.into_iter().filter(|(s, e)| s < e).collect();
    if pieces.is_empty() {
        pieces.push((end, end));
    }
    let mut replacement = Some(replacement);
    pieces
        .into_iter()
        .filter_map(|(s, e)| {
            let text = replacement.take().unwrap_or_default();
            (s < e || !text.is_empty()).then_some((s, e, text))
        })
        .collect()
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod tests {
    use super::*;

    fn apply(old: &str, edits: &[(usize, usize, String)]) -> String {
        let mut out = old.to_string();
        for (start, end, text) in edits.iter().rev() {
            out.replace_range(*start..*end, text);
        }
        out
    }

    #[test]
    fn one_word_is_one_edit() {
        let old = "The fee is ten dollars.";
        let edits = rewrite_ranges(old, "The fee is twelve dollars.");
        assert_eq!(edits, [(11, 14, "twelve".to_string())]);
        assert_eq!(apply(old, &edits), "The fee is twelve dollars.");
    }

    #[test]
    fn a_decomposed_word_is_replaced_whole_with_its_accent() {
        let old = "Le cafe\u{301} est chaud";
        let edits = rewrite_ranges(old, "Le the\u{301} est chaud");
        // One edit over "cafe" + U+0301 (4 + 2 bytes), not over "cafe" with
        // the accent left behind as a stray mark.
        assert_eq!(edits, [(3, 9, "the\u{301}".to_string())]);
        assert_eq!(apply(old, &edits), "Le the\u{301} est chaud");
        assert!(rewrite_ranges(old, old).is_empty());
    }

    #[test]
    fn insertions_at_the_start_and_end() {
        assert_eq!(rewrite_ranges("b", "a b"), [(0, 0, "a ".to_string())]);
        assert_eq!(rewrite_ranges("a b", "a b c"), [(3, 3, " c".to_string())]);
        assert_eq!(rewrite_ranges("", "x"), [(0, 0, "x".to_string())]);
    }

    #[test]
    fn unchanged_text_and_whitespace_of_another_kind_need_nothing() {
        assert!(rewrite_ranges("same text", "same text").is_empty());
        assert!(rewrite_ranges("1.\tDefinitions", "1. Definitions").is_empty());
        assert!(rewrite_ranges("a  b", "a b").is_empty());
        assert!(rewrite_ranges("line\nbreak", "line break").is_empty());
    }

    #[test]
    fn tabs_breaks_and_symbols_stay() {
        let old = "1.\tDefinitions apply";
        let edits = rewrite_ranges(old, "1. Definitions now apply");
        assert_eq!(apply(old, &edits), "1.\tDefinitions now apply");

        let old = "x \u{FFFC} y";
        assert!(rewrite_ranges(old, "x y").is_empty());
        let edits = rewrite_ranges(old, "x z");
        assert_eq!(apply(old, &edits), "x \u{FFFC} z");

        // Words deleted take the tab between them along; a symbol stays.
        let old = "a\tb c";
        assert_eq!(apply(old, &rewrite_ranges(old, "c")), "c");
        let old = "a \u{FFFC} b c";
        assert_eq!(apply(old, &rewrite_ranges(old, "c")), "\u{FFFC}c");
    }

    #[test]
    fn new_text_writes_its_tabs_and_breaks() {
        let edits = rewrite_ranges("a", "a\tb\nc");
        assert_eq!(apply("a", &edits), "a\tb\nc");
        // Whitespace the old text has as spaces becomes the tab or the
        // break asked for; a tab asked for where one is needs nothing.
        assert_eq!(rewrite_ranges("a b", "a\tb"), [(1, 2, "\t".to_string())]);
        assert_eq!(rewrite_ranges("a\tb", "a\nb"), [(1, 2, "\n".to_string())]);
        assert_eq!(
            rewrite_ranges("a \tb", "a\t\tb"),
            [(1, 3, "\t\t".to_string())]
        );
        assert!(rewrite_ranges("a\tb", "a\tb").is_empty());
        assert!(rewrite_ranges("a \t b", "a\tb").is_empty());
    }

    #[test]
    fn look_alikes_read_as_the_plain_character_and_stay() {
        let old = "The \u{201C}Seller\u{201D} shall\u{A0}deliver";
        let edits = rewrite_ranges(old, "The \"Seller\" must deliver");
        assert_eq!(
            apply(old, &edits),
            "The \u{201C}Seller\u{201D} must\u{A0}deliver"
        );
        assert_eq!(edits.len(), 1, "{edits:?}");
        // A zero-width space reads as nothing: it is no word of its own.
        assert!(rewrite_ranges("a\u{200B} b", "a b").is_empty());
    }

    #[test]
    fn replace_ranges_compare_whitespace_as_written() {
        assert_eq!(replace_ranges("a  b", "a b"), [(1, 3, " ".to_string())]);
        assert_eq!(replace_ranges("a\tb", "a b"), [(1, 2, " ".to_string())]);
        assert!(replace_ranges("a\u{A0}b", "a b").is_empty());
        let old = "price is ten dollars";
        assert_eq!(
            replace_ranges(old, "price is twelve dollars"),
            [(9, 12, "twelve".to_string())]
        );
    }

    /// Changes a whitespace gap joins into one keep the gap as the
    /// document writes it: a non-breaking space the replacement types as a
    /// plain one stays (#1384 review).
    #[test]
    fn a_joined_replacement_keeps_its_gaps_look_alike() {
        for (old, new, want) in [
            ("Section\u{A0}5", "Article 6", "Article\u{A0}6"),
            ("ten\u{A0}dollars", "twelve euros", "twelve\u{A0}euros"),
            (
                "a ten\u{A0}dollars b",
                "a twelve euros b",
                "a twelve\u{A0}euros b",
            ),
        ] {
            let edits = replace_ranges(old, new);
            assert_eq!(apply(old, &edits), want, "{edits:?}");
            assert_eq!(edits.len(), 1, "{edits:?}");
        }
    }

    /// A rewrite's hunk is one edit, as a `replace` already joins one.
    /// Pairing the deleted words with the inserted words by index is the
    /// keep-path mark the plan rewrite used to write (#1923).
    #[test]
    fn rewrite_ranges_join_a_hunk_only_whitespace_separates() {
        assert_eq!(
            rewrite_ranges("alpha bravo charlie", "one two three four five"),
            [(0, 19, "one two three four five".to_string())]
        );
        assert_eq!(
            rewrite_ranges("alpha bravo charlie", "one two three"),
            [(0, 19, "one two three".to_string())]
        );
        let old = "alpha bravo charlie delta echo";
        assert_eq!(
            rewrite_ranges(old, "one two"),
            [(0, old.len(), "one two".to_string())]
        );
        // A kept word between two changes still splits them.
        assert_eq!(
            rewrite_ranges("pay ten big dollars", "pay twelve big euros"),
            [(4, 7, "twelve".to_string()), (12, 19, "euros".to_string())]
        );
    }

    #[test]
    fn replace_ranges_join_changes_only_whitespace_separates() {
        // Every word changes: one edit, as Word Compare shows it, not two
        // pairs around a kept space.
        assert_eq!(
            replace_ranges("ten dollars", "twelve euros"),
            [(0, 11, "twelve euros".to_string())]
        );
        let old = "The cap is";
        let edits = replace_ranges(old, "It is");
        assert_eq!(apply(old, &edits), "It is");
        assert_eq!(edits.len(), 1, "{edits:?}");
        // A kept word between two changes still splits them.
        assert_eq!(
            replace_ranges("pay ten big dollars", "pay twelve big euros"),
            [(4, 7, "twelve".to_string()), (12, 19, "euros".to_string())]
        );
    }

    #[test]
    fn everything_can_go() {
        let old = "gone words";
        let edits = rewrite_ranges(old, "");
        assert_eq!(apply(old, &edits), "");
    }
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod marks_branch_tests {
    use super::*;

    fn segs(list: &[(char, &str)]) -> Vec<(char, String)> {
        list.iter().map(|&(k, s)| (k, s.to_string())).collect()
    }

    #[test]
    fn marks_rebuild_edits_only_when_they_read_back_as_old() {
        let old = "the red fox";
        let edits = ranges_from_marks(
            old,
            &segs(&[
                ('=', "the "),
                ('-', "red"),
                ('+', "big"),
                ('+', " brown"),
                ('=', " fox"),
            ]),
        )
        .unwrap();
        assert_eq!(edits, [(4, 7, "big brown".to_string())]);
        // A pure insertion lands at the cursor; an empty one is no edit.
        let edits = ranges_from_marks(
            old,
            &segs(&[('+', ""), ('=', "the "), ('+', "sly "), ('=', "red fox")]),
        )
        .unwrap();
        assert_eq!(edits, [(4, 4, "sly ".to_string())]);
        // Kept or deleted text that is not old's next bytes, an unknown
        // mark, or marks that stop short of old's end: no edits.
        assert_eq!(ranges_from_marks(old, &segs(&[('=', "a red fox")])), None);
        assert_eq!(ranges_from_marks(old, &segs(&[('-', "a red fox")])), None);
        assert_eq!(ranges_from_marks(old, &segs(&[('?', "the red fox")])), None);
        assert_eq!(ranges_from_marks(old, &segs(&[('=', "the ")])), None);
    }

    #[test]
    fn xml_text_escapes_markup_and_refuses_controls() {
        assert_eq!(
            xml_text("a<b>&c\td\ne\r").as_deref(),
            Some("a&lt;b&gt;&amp;c\td\ne\r")
        );
        assert_eq!(xml_text("bell\u{7}"), None);
        assert_eq!(plain_docx("bell\u{7}"), None);
        assert_eq!(comparer_ranges("bell\u{7}", "bell"), None);
    }

    #[test]
    fn the_comparer_marks_whole_words_and_reads_back_as_new() {
        let old = "The quick brown fox jumps.";
        let new = "The quick red fox leaps.";
        let edits = comparer_ranges(old, new).unwrap();
        assert_eq!(apply_ranges(old, &edits), new);
        assert!(edits.iter().all(|(s, e, _)| s <= e), "{edits:?}");
        // An insertion alone.
        let edits = comparer_ranges("one three", "one two three").unwrap();
        assert_eq!(apply_ranges("one three", &edits), "one two three");
    }

    #[test]
    fn replacements_split_around_kept_symbols() {
        let old = "a\u{FFFC}bc\u{FFFC}d";
        // As many symbols in the replacement: each gap takes its part, and
        // an unchanged gap is no edit.
        let pieces = split_around_kept(old, 0, old.len(), "x\u{FFFC}bc\u{FFFC}y");
        assert_eq!(
            pieces,
            [
                (0, 1, "x".to_string()),
                (old.len() - 1, old.len(), "y".to_string())
            ]
        );
        // Fewer symbols: the text goes to the first piece, the others go.
        let pieces = split_around_kept(old, 0, old.len(), "zz");
        assert_eq!(pieces.len(), 3);
        assert_eq!(pieces[0], (0, 1, "zz".to_string()));
        assert!(pieces[1..].iter().all(|(_, _, t)| t.is_empty()));
        // Only symbols in the range: the text lands after them, or nothing.
        let sym = "\u{FFFC}\u{FFFC}";
        let end = sym.len();
        assert_eq!(
            split_around_kept(sym, 0, end, "new"),
            [(end, end, "new".to_string())]
        );
        assert!(split_around_kept(sym, 0, end, "").is_empty());
    }

    #[test]
    fn deletions_join_across_spaces_into_one_hunk() {
        let old = "keep alpha beta gamma end";
        let new = "keep one two end";
        let edits = rewrite_ranges(old, new);
        assert_eq!(apply_ranges(old, &edits), new);
        assert_eq!(edits.len(), 1, "{edits:?}");
        // A kept word between two changes splits them.
        let old = "aa keep bb";
        let new = "xx keep yy";
        let edits = rewrite_ranges(old, new);
        assert_eq!(apply_ranges(old, &edits), new);
        assert_eq!(edits.len(), 2, "{edits:?}");
    }
}