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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
//
// SPDX-License-Identifier: Apache-2.0

//! `existing_revisions: "keep"`: the redline by direct emission.
//!
//! The comparer accepts both inputs' revisions before it compares, so it
//! cannot keep another party's tracked changes. Under `keep` the plan's
//! resolved operations are replayed on a fresh copy of the base and written
//! as tracked changes by the plan's author and date, which is what Word does
//! when you type on a received redline with Track Changes on: new `w:ins`
//! and `w:del` runs, inserted and deleted paragraph marks, and `w:pPrChange`
//! for paragraph formatting. An edit inside another author's insertion
//! nests the deletion there and splits off the new words and remainder.
//! The editor's own insertion is edited in place.

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use super::structural;
use super::visible;
use super::{
    EditError, EditPlan, EditResult, ExistingRevisions, OperationKind, Resolved, RevisionCounter,
    RevisionCounts, ScheduledEdit, Side, ThreadOp, Transaction, anchor_comment, anchor_span,
    apply_run_format, apply_text_edit, attach_segment, build_paragraph, comment_bounds,
    copy_mark_formatting, format_paragraph, insert_ppr_child, new_position, place_reply_markers,
    remove_comment_markers, runs_in_range, set_text, split_run_at, toc_paragraphs,
};
use crate::changes::{list_changes, list_logical_changes};
use crate::inspect::{Opened, Piece, project_paragraph};
use crate::namespaces::W;
use crate::xmllinq::{Dom, NodeId, XName};

/// The original identity of a pending insertion, retained across every split.
#[derive(Clone)]
struct InsertionOrigin {
    story: String,
    id: String,
    root: NodeId,
}

/// Revision attribution for the containers the emitter creates.
struct Stamp<'a> {
    author: &'a str,
    date: &'a str,
    ids: RevisionCounter,
    insertions: HashMap<NodeId, InsertionOrigin>,
}

impl Stamp<'_> {
    fn container(&mut self, dom: &mut Dom, name: XName) -> NodeId {
        let node = dom.new_element(name);
        let id = self.ids.allocate();
        dom.set_attribute_value(node, &W::id(), Some(&id.to_string()));
        dom.set_attribute_value(node, &W::author(), Some(self.author));
        dom.set_attribute_value(node, &W::date(), Some(self.date));
        node
    }

    fn insertion_origins(&self, dom: &Dom) -> BTreeMap<String, String> {
        self.insertions
            .iter()
            .filter_map(|(&node, origin)| {
                if !dom.ancestors(node, None).contains(&origin.root) {
                    return None;
                }
                let id = dom.attribute(node, &W::id())?;
                Some((
                    crate::ids::ChangeId::new(&origin.story, id).short(),
                    origin.id.clone(),
                ))
            })
            .collect()
    }
}

/// The operation kind of `resolved` when the emitter cannot write it as a
/// tracked change: such a plan goes through the comparer, or is refused
/// under `keep`, never left out of the redline.
fn untracked_kind(tx: &Transaction<'_>, operation: usize) -> Option<&'static str> {
    let kind = super::kind_name(&tx.plan.operations[operation].kind);
    (!super::operation_supports_keep(kind)).then_some(kind)
}

/// Whether the emitter writes every operation of `tx` as tracked changes.
pub(super) fn emits_all(tx: &Transaction<'_>) -> bool {
    tx.resolved
        .iter()
        .all(|(op, _)| untracked_kind(tx, *op).is_none())
}

/// The tracked formatting change (`w:rPrChange`) of the first run holding
/// `[start, end)` of `paragraph` that has one.
fn formatting_tracked(dom: &Dom, paragraph: NodeId, start: usize, end: usize) -> Option<NodeId> {
    project_paragraph(dom, paragraph)
        .segments
        .iter()
        .filter(|s| s.start < end && start < s.end)
        .find_map(|s| {
            let (Piece::Text { run, .. } | Piece::Glyph { run, .. }) = &s.piece;
            dom.element(*run, &W::r_pr())
                .and_then(|rpr| dom.element(rpr, &W::r_pr_change()))
        })
}

/// Refusals that only `keep` needs, checked on the untouched working source.
/// Each revision conflict retains its actual carriers for caller-specific advice.
pub(super) fn check(tx: &Transaction<'_>) -> Result<(), EditError> {
    if tx.plan.existing_revisions != ExistingRevisions::Keep {
        return Ok(());
    }
    tx.revision_refusal.take();
    let dom = &tx.opened.dom;
    let emptied = tx.emptied_paragraphs();
    let refuse = |op: usize, message: &str| {
        let mut error = tx.conflict(op, message);
        error.code = "UNSUPPORTED_STRUCTURE".into();
        error.outcomes[op].code = Some(error.code.clone());
        tx.attach_revision_refusal(error)
    };
    for (op, resolved) in &tx.resolved {
        if let Some(kind) = untracked_kind(tx, *op) {
            return Err(refuse(
                *op,
                &format!(
                    "under keep, {kind} cannot be written as a tracked change yet; apply it in a plan without existing_revisions keep"
                ),
            ));
        }
        // What is refused, then the revision in the way and each route
        // around it (#1108).
        let named = |lead: &str, revision: NodeId| tx.revision_hint(revision, lead);
        let message = match resolved {
            Resolved::DeleteParagraph { para } => {
                let node = tx.paragraph_nodes[*para];
                let replacement = tx.final_replacement(*para);
                if let Some(moved) = moved_in(dom, node) {
                    // A mark stored in a formatting change is that change's
                    // history. Name the outermost one: a history recorded
                    // inside another is not listed on its own, so only the
                    // outer change is what the listing offers.
                    let mut recorded = None;
                    let mut current = dom.parent(moved);
                    while let Some(ancestor) = current {
                        if dom.name(ancestor).is_some_and(|name| {
                            name.namespace_name() == W::URI
                                && crate::changes::FORMATTING.contains(&name.local_name())
                        }) {
                            recorded = Some(ancestor);
                        }
                        current = dom.parent(ancestor);
                    }
                    Some(named(
                        "under keep, a paragraph that holds a move cannot be deleted yet: the move is",
                        recorded.unwrap_or(moved),
                    ))
                } else if let Some(foreign) = untrackable_content(dom, node) {
                    let name = dom.name(foreign).expect("an element");
                    Some(format!(
                        "under keep, a paragraph holding content other than runs ({} in {}) cannot be deleted as a tracked change yet; apply it in a plan without existing_revisions keep",
                        name.local_name(),
                        name.namespace_name()
                    ))
                } else if let Some(change) =
                    replacement.and_then(|op| record_replaced(tx, node, op))
                {
                    Some(named(
                        "under keep, a final paragraph whose formatting change is still tracked cannot be replaced by a paragraph formatted otherwise: that change is",
                        change,
                    ))
                } else if replacement.is_none()
                    && !emptied.contains(para)
                    && next_block(dom, node).is_none()
                    // A final paragraph's carrier keeps its own mark
                    // (`final_carrier`); this is the last paragraph of a
                    // content control or a custom XML element.
                    && tx.final_paragraph(node) != Some(node)
                {
                    previous_paragraph(dom, node)
                        .and_then(|p| mark_revision(dom, p, &DELETED))
                        .map(|mark| {
                            named(
                                "under keep, the last paragraph of a content control or a custom XML element cannot be deleted when the paragraph before it already has a deleted mark: that mark is",
                                mark,
                            )
                        })
                } else {
                    None
                }
            }
            // A table placed last leaves the final mark where it is: the
            // paragraph after it takes the inserted mark
            // (#1680).
            Resolved::InsertParagraph { .. }
                if appended_final(tx, resolved).is_some_and(|para| {
                    last_appended(tx, para) == Some(*op)
                        && !tx.resolved.iter().any(|(_, r)| {
                            matches!(r, Resolved::DeleteParagraph { para: p } if *p == para)
                        })
                }) =>
            {
                let para = appended_final(tx, resolved).expect("matched");
                let node = tx.paragraph_nodes[para];
                if let Some(mark) = mark_revision(dom, node, &TRACKED) {
                    Some(named(
                        "under keep, a paragraph cannot be added after a container's last paragraph whose mark is already tracked, since the inserted mark goes there: that mark is",
                        mark,
                    ))
                } else {
                    record_replaced(tx, node, *op).map(|change| {
                        named(
                            "under keep, a paragraph formatted otherwise cannot be added after a container's last paragraph whose formatting change is still tracked: that change is",
                            change,
                        )
                    })
                }
            }
            // A strike's mark is their insertion, which our deletion joins.
            Resolved::MergeParagraphs {
                para,
                strike: false,
                ..
            } => {
                mark_revision(dom, tx.paragraph_nodes[*para], &TRACKED).map(|mark| {
                    named(
                        "under keep, a paragraph whose mark is already tracked cannot be merged: that mark is",
                        mark,
                    )
                })
            }
            Resolved::FormatParagraph { para, .. } => {
                paragraph_format_change(dom, tx.paragraph_nodes[*para]).map(|change| {
                    named(
                        "under keep, a paragraph whose formatting change is still tracked cannot be formatted again: that change is",
                        change,
                    )
                })
            }
            Resolved::List { paras, .. } => paras
                .iter()
                .find_map(|&para| paragraph_format_change(dom, tx.paragraph_nodes[para]))
                .map(|change| {
                    named(
                        "under keep, a paragraph whose formatting change is still tracked cannot be numbered: that change is",
                        change,
                    )
                }),
            Resolved::FormatRun {
                para, start, end, ..
            } => formatting_tracked(dom, tx.paragraph_nodes[*para], *start, *end).map(|change| {
                named(
                    "under keep, text whose formatting change is still tracked cannot be formatted again: that change is",
                    change,
                )
            }),
            _ => None,
        };
        if let Some(message) = message {
            return Err(refuse(*op, &message));
        }
    }
    Ok(())
}

/// The result under `keep`: the clean copy as `finish` made it, the redline
/// by emission, and revision counts of the plan's own changes.
pub(super) fn result(tx: &Transaction<'_>, clean: Vec<u8>) -> Result<EditResult, EditError> {
    result_with_order(tx, clean, false)
}

/// The tracked primary's paragraph identity, for the v2 expectations
/// correspondence: `base` walks the replay's starting DOM (positionally the
/// transaction's own `paragraph_nodes`, both loaded from the same base
/// bytes), and `output` walks it again after the tracked emission — in the
/// checker's projection order, so output indices address the reopened
/// redline's paragraphs.
#[derive(Clone, Debug)]
pub(super) struct TrackedParagraphs {
    /// The replay's base paragraphs, one per transaction paragraph.
    pub(super) base: Vec<NodeId>,
    /// The emitted redline's paragraphs, in projection order.
    pub(super) output: Vec<NodeId>,
    /// Each `output` paragraph's final-view disposition, computed on the
    /// replay's own DOM before this call returns and that DOM is dropped.
    /// The correspondence reads the redline's marks here instead of
    /// reopening the emitted bytes (#1087); `None` when the caller asked
    /// for no final-view scan. These equal a reparse of the emitted
    /// redline because `finish` changes that DOM only through
    /// `apply_watermark`, which no v2 operation reaches; the regression
    /// net under `v2_apply`'s tests pins that equivalence.
    pub(super) output_states: Option<Vec<super::v2_apply::FinalView>>,
    /// Every surviving source insertion piece's emitted id -> source id.
    /// Split pieces share an origin even when a leading edit renumbers it.
    pub(super) insertion_origins: BTreeMap<String, String>,
}

/// As `result_v2`, also returning the tracked primary's paragraph walks
/// (see [`TrackedParagraphs`]).
pub(super) fn result_v2_with_paragraphs(
    tx: &Transaction<'_>,
    clean: Vec<u8>,
) -> Result<(EditResult, TrackedParagraphs), EditError> {
    let (redline, own, paragraphs) = redline(tx, true, true)?;
    let mut report = tx.report(true);
    report.paragraphs.to = crate::inspect::paragraphs(&clean)
        .map(|p| p.len())
        .unwrap_or(report.paragraphs.from);
    report.revisions = revision_counts(&redline, &tx.plan.author, &tx.date, &own);
    report.split_revisions = splits(&paragraphs.insertion_origins);
    report.clean_removed_comments = crate::comment_settlement::threads_the_clean_copy_drops(
        &clean,
        &redline,
        &report.removed_comments,
        &tx.deleted_paragraph_ids(),
    )?;
    Ok((
        EditResult {
            clean,
            redline,
            report,
        },
        paragraphs,
    ))
}

fn result_with_order(
    tx: &Transaction<'_>,
    clean: Vec<u8>,
    array_order: bool,
) -> Result<EditResult, EditError> {
    let (redline, own, paragraphs) = redline(tx, array_order, false)?;
    let mut report = tx.report(true);
    report.paragraphs.to = crate::inspect::paragraphs(&clean)
        .map(|p| p.len())
        .unwrap_or(report.paragraphs.from);
    report.revisions = revision_counts(&redline, &tx.plan.author, &tx.date, &own);
    report.split_revisions = splits(&paragraphs.insertion_origins);
    report.clean_removed_comments = crate::comment_settlement::threads_the_clean_copy_drops(
        &clean,
        &redline,
        &report.removed_comments,
        &tx.deleted_paragraph_ids(),
    )?;
    Ok(EditResult {
        clean,
        redline,
        report,
    })
}

/// Replay `tx`'s resolved operations on a fresh copy of its base as tracked
/// changes. `tx` has been applied, so its outcomes hold the comment ids.
/// Returns the redline, the ids its own revisions took (copies of source
/// records excluded) and the replay's paragraph walks
/// ([`TrackedParagraphs`]); the walk is taken after the emission and
/// before `finish`. `finish` changes the DOM only through
/// `apply_watermark`, a step no v2 operation reaches, so the walk still
/// addresses the emitted redline's paragraphs; the regression net under
/// `v2_apply`'s tests pins that. When `final_views` is
/// set, each output paragraph's final-view disposition is scanned off the
/// replay's DOM here, while that DOM still lives — the one place the
/// emitted redline's marks can be read without reopening its bytes.
fn redline(
    tx: &Transaction<'_>,
    array_order: bool,
    final_views: bool,
) -> Result<(Vec<u8>, BTreeSet<u64>, TrackedParagraphs), EditError> {
    let plan = EditPlan {
        source_sha256: None,
        date: Some(tx.date.clone()),
        initials: Some(tx.initials.clone()),
        resolve_revisions: None,
        existing_revisions: ExistingRevisions::Keep,
        ..tx.plan.clone()
    };
    let mut t = Transaction::start(&tx.base, &plan)?;
    t.resolved = tx.resolved.clone();
    // A watermark is header content, not a change: written as is.
    t.watermark = tx.watermark.clone();
    // Settings are not revisions either.
    t.settings = tx.settings.clone();
    // The comments of commented paragraph deletions are written here, on
    // the deleted text, instead of into a commented base for the comparer.
    let mut comments = tx.comments.clone();
    for &(id, op) in &tx.deletion_comments {
        if let Some(text) = tx.deleted_text_comment(op) {
            comments.push((id, text.to_string()));
        }
    }
    comments.sort_by_key(|&(id, _)| id);
    let comment_ids: BTreeMap<usize, u32> = tx
        .outcomes
        .iter()
        .enumerate()
        .filter_map(|(op, outcome)| outcome.comment_id.map(|id| (op, id)))
        .collect();
    let mut insertions = HashMap::new();
    for story in &t.stories {
        for node in t.opened.dom.descendants(story.root, Some(&W::ins())) {
            if let Some(id) = t.opened.dom.attribute(node, &W::id()) {
                insertions.insert(
                    node,
                    InsertionOrigin {
                        story: story.id.clone(),
                        id: crate::ids::ChangeId::new(&story.id, id).short(),
                        root: story.root,
                    },
                );
            }
        }
    }
    // The replay starts from the settled base: the source's ids, settled
    // away or not, stay taken too.
    let mut used = used_ids(&t.opened, &comments);
    used.extend(&tx.used_revision_ids);
    let mut stamp = Stamp {
        author: &tx.plan.author,
        date: &tx.date,
        ids: RevisionCounter::new(used),
        insertions,
    };
    let tables = emit(&mut t, &comment_ids, &mut stamp, array_order)?;
    thread_markers(&mut t, &comment_ids);
    // 7. A paragraph after each new table, and between it and a table
    // before it: inserted paragraphs here.
    for table in tables {
        separate_tracked(&mut t.opened.dom, table, &mut stamp);
    }
    t.comments = comments;
    t.reply_parents = tx.reply_parents.clone();
    let base = t.paragraph_nodes.clone();
    let output = t.paragraph_walk();
    let output_states = final_views.then(|| {
        output
            .iter()
            .map(|&node| super::v2_apply::paragraph_final_state(&t.opened.dom, node))
            .collect()
    });
    let insertion_origins = stamp.insertion_origins(&t.opened.dom);
    let (redline, _) = t.finish()?;
    Ok((
        redline,
        stamp.ids.own().clone(),
        TrackedParagraphs {
            base,
            output,
            output_states,
            insertion_origins,
        },
    ))
}

/// The source insertions the emission split, from each emitted piece's
/// origin: a source id with more than one piece, or whose one piece took a
/// new id.
fn splits(origins: &BTreeMap<String, String>) -> Vec<super::RevisionSplit> {
    let mut pieces: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
    for (emitted, source) in origins {
        pieces.entry(source).or_default().push(emitted);
    }
    // `r9` before `r10`: the number, then the text for an id without one.
    let order = |id: &str| {
        (
            crate::ids::change_id(id).and_then(|c| c.wid.parse::<u64>().ok()),
            id.to_string(),
        )
    };
    let mut out: Vec<super::RevisionSplit> = pieces
        .into_iter()
        .filter(|(source, to)| to.len() > 1 || to.first() != Some(source))
        .map(|(source, mut to)| {
            to.sort_by_key(|id| order(id));
            super::RevisionSplit {
                from: source.to_string(),
                to: to.into_iter().map(str::to_string).collect(),
            }
        })
        .collect();
    out.sort_by_key(|split| order(&split.from));
    out
}

/// Counts of the redline's logical changes holding a mark stamped with
/// `author` and `date` whose `w:id` is one of `own_ids`, the ids the edit's
/// own records took. A source change carrying the very same stamp, or a
/// copy of one a split made, has another id, so it does not count.
fn revision_counts(
    redline: &[u8],
    author: &str,
    date: &str,
    own_ids: &BTreeSet<u64>,
) -> RevisionCounts {
    let changes = list_changes(redline).unwrap_or_default();
    let own: HashSet<&str> = changes
        .iter()
        .filter(|c| {
            c.author.as_deref() == Some(author)
                && c.date.as_deref() == Some(date)
                && crate::ids::change_id(&c.id)
                    .and_then(|id| id.wid.parse::<u64>().ok())
                    .is_some_and(|id| own_ids.contains(&id))
        })
        .map(|c| c.id.as_str())
        .collect();
    let logical = list_logical_changes(redline).unwrap_or_default();
    RevisionCounts::from_changes(
        logical
            .iter()
            .filter(|c| c.element_ids().any(|id| own.contains(id))),
    )
}

/// Every `w:id` in the revision-bearing parts and among the plan's
/// comments, so a new revision id collides with none of them.
pub(super) fn used_ids(opened: &Opened, comments: &[(u32, String)]) -> HashSet<u64> {
    let mut used: HashSet<u64> = comments.iter().map(|&(id, _)| u64::from(id)).collect();
    for (part, _) in crate::revision_processor::revision_bearing_parts(&opened.pkg) {
        let Some(xml) = opened.pkg.part_string(&part) else {
            continue;
        };
        let mut dom = Dom::new();
        let document = dom.parse_xdocument(&xml);
        let Some(root) = dom.root(document) else {
            continue;
        };
        for node in dom.descendants(root, None) {
            if let Some(id) = dom
                .attribute(node, &W::id())
                .and_then(|v| v.parse::<u64>().ok())
            {
                used.insert(id);
            }
        }
    }
    used
}

/// The steps of `Transaction::apply`, in its order, as tracked changes.
/// Returns the new tables, which get their separating paragraphs last.
fn emit(
    t: &mut Transaction<'_>,
    comment_ids: &BTreeMap<usize, u32>,
    stamp: &mut Stamp<'_>,
    array_order: bool,
) -> Result<Vec<NodeId>, EditError> {
    // Read off the source's structure, before any step changes it: the
    // container-final paragraphs whose mark stays (Word never deletes it).
    let emptied = t.emptied_paragraphs();
    let finals: BTreeSet<usize> = t
        .resolved
        .iter()
        .filter_map(|(_, r)| match r {
            Resolved::DeleteParagraph { para }
                if t.final_paragraph(t.paragraph_nodes[*para])
                    == Some(t.paragraph_nodes[*para]) =>
            {
                Some(*para)
            }
            _ => None,
        })
        .collect();
    // The operation whose new paragraph takes each final mark that stays,
    // and the paragraph that carries each other final mark away.
    let replacements: BTreeMap<usize, usize> = finals
        .iter()
        .filter_map(|&para| t.final_replacement(para).map(|op| (para, op)))
        .collect();
    let carriers: BTreeMap<usize, NodeId> = finals
        .iter()
        .filter_map(|&para| t.final_carrier(para).map(|carrier| (para, carrier)))
        .collect();
    // The container-final paragraphs that stay, with blocks placed after
    // them (#1669).
    let appended: BTreeSet<usize> = t
        .resolved
        .iter()
        .filter_map(|(_, r)| appended_final(t, r))
        .filter(|para| !finals.contains(para))
        .collect();
    // 0. Run formatting, on source offsets (no text edit overlaps it), each
    // run's old properties in `w:rPrChange`.
    for (_, r) in &t.resolved {
        let Resolved::FormatRun {
            para,
            start,
            end,
            format,
        } = r
        else {
            continue;
        };
        let node = t.paragraph_nodes[*para];
        track_run_format(&mut t.opened.dom, node, (*start, *end), format, stamp);
    }
    // 1. Text edits and their comments, paragraph by paragraph.
    let mut by_para: BTreeMap<usize, Vec<ScheduledEdit>> = BTreeMap::new();
    let mut comment_ranges: BTreeMap<usize, Vec<(usize, usize, usize)>> = BTreeMap::new();
    let mut strike_comments: BTreeMap<usize, Vec<(usize, usize, usize)>> = BTreeMap::new();
    for (i, r) in &t.resolved {
        match r {
            Resolved::StrikeComment { para, start, end } => strike_comments
                .entry(*para)
                .or_default()
                .push((*start, *end, *i)),
            Resolved::Text {
                para,
                start,
                end,
                replacement,
                attach_before,
                comment,
                format,
            } => by_para.entry(*para).or_default().push(ScheduledEdit {
                start: *start,
                end: *end,
                op: *i,
                replacement: replacement.clone(),
                attach_before: *attach_before,
                comment: comment.clone(),
                format: format.clone(),
            }),
            Resolved::CommentRange {
                para, start, end, ..
            } => comment_ranges
                .entry(*para)
                .or_default()
                .push((*start, *end, *i)),
            _ => {}
        }
    }
    let touched: BTreeSet<usize> = by_para
        .keys()
        .chain(comment_ranges.keys())
        .copied()
        .collect();
    for para in touched {
        let node = t.paragraph_nodes[para];
        let mut edits = by_para.remove(&para).unwrap_or_default();
        let empty_text = project_paragraph(&t.opened.dom, node).text.is_empty();
        super::sort_edits(&mut edits, empty_text);
        let substitutions = super::boundary_substitutions(t, &edits, para)?;
        let dom = &mut t.opened.dom;
        let mut empty = empty_text.then(super::EmptyPlacement::default);
        // In reverse, so earlier source offsets stay valid: a deletion
        // leaves the projection and an insertion joins it, exactly as the
        // plain edit changes the clean copy.
        // Emit a simultaneous V2 insertion group once, using its agreed
        // original attachment side. Otherwise the first replayed insertion
        // becomes a non-direct segment and can hide the source insertion
        // point from the next replay (notably at paragraph start). Keep the
        // original edits below for each comment's separate resulting range.
        let emission = if array_order {
            coalesce_insertions(&substitutions)
        } else {
            substitutions
        };
        // A strike's comment, on the words before they are struck: their
        // text leaves the projection with the deletion.
        for (start, end, i) in strike_comments.remove(&para).unwrap_or_default() {
            if let Some(&id) = comment_ids.get(&i) {
                anchor_comment(dom, node, start, end, id, &mut stamp.ids);
            }
        }
        for edit in emission.iter().rev() {
            if matches!(
                t.plan.operations[edit.op].kind,
                OperationKind::Redact { .. }
            ) {
                // A redaction is no change: its blocks replace the text
                // outright, as in the clean copy.
                let projection = project_paragraph(dom, node);
                apply_text_edit(
                    dom,
                    &projection,
                    edit.start,
                    edit.end,
                    &edit.replacement,
                    edit.attach_before,
                );
                super::clean_new_words(
                    dom,
                    node,
                    edit.start,
                    edit.start + edit.replacement.len(),
                    &mut stamp.ids,
                );
            } else {
                let pieces = if matches!(
                    t.plan.operations[edit.op].kind,
                    OperationKind::Replace { whole: false, .. }
                ) {
                    insertion_replacement(dom, node, edit)
                } else {
                    vec![edit.clone()]
                };
                for piece in pieces.iter().rev() {
                    emit_text(dom, node, piece, stamp, empty.as_mut());
                }
            }
        }
        let mut pending: Vec<(usize, usize, usize)> = Vec::new();
        for edit in &edits {
            if edit.comment.is_some() {
                let s = new_position(&edits, edit.start, true, Some(edit.op));
                pending.push((s, s + edit.replacement.len(), edit.op));
            }
        }
        for (start, end, i) in comment_ranges.remove(&para).unwrap_or_default() {
            let (s, e) = comment_bounds(&edits, start, end, i);
            pending.push((s, e, i));
        }
        pending.sort_by_key(|&(s, _, i)| (s, i));
        for (start, end, i) in pending {
            if let Some(&id) = comment_ids.get(&i) {
                anchor_comment(dom, node, start, end, id, &mut stamp.ids);
            }
        }
    }
    // 1b. Comments over several paragraphs, in their edited text.
    for (i, r) in &t.resolved {
        if let Resolved::CommentSpan { para, last, .. } = r
            && let Some(&id) = comment_ids.get(i)
        {
            let (first, last) = (t.paragraph_nodes[*para], t.paragraph_nodes[*last]);
            anchor_span(&mut t.opened.dom, first, last, id, &mut stamp.ids);
        }
    }
    // 1c. Restores: their deletion stays, and our insertion of a copy of
    // its runs follows it. An operation's comment runs from its first copy
    // to its last.
    let mut spans: BTreeMap<usize, (NodeId, NodeId)> = BTreeMap::new();
    for (i, r) in &t.resolved {
        let Resolved::Restore { para, wid, .. } = r else {
            continue;
        };
        let node = t.paragraph_nodes[*para];
        let dom = &mut t.opened.dom;
        let Some(del) = visible::revision_in(dom, node, "del", wid) else {
            continue;
        };
        let ours = stamp.container(dom, W::ins());
        for copy in visible::copy_runs(dom, del) {
            dom.add(ours, copy);
        }
        let runs = dom.elements(ours, None);
        if let (Some(&first), Some(&last)) = (runs.first(), runs.last()) {
            spans.entry(*i).or_insert((first, last)).1 = last;
        }
        place_beside(dom, del, ours, stamp);
    }
    for (i, (first, last)) in spans {
        if let Some(&id) = comment_ids.get(&i) {
            visible::comment_around(&mut t.opened.dom, first, last, id);
        }
    }
    // 2. Paragraph and table insertions: every run, row and paragraph mark
    // inserted.
    let mut last_after: BTreeMap<usize, NodeId> = BTreeMap::new();
    // Each insertion's last new block.
    let mut inserted: BTreeMap<usize, NodeId> = BTreeMap::new();
    let mut tables: Vec<NodeId> = Vec::new();
    for (i, r) in &t.resolved {
        let (anchor, side, news, commented) = match r {
            Resolved::InsertParagraph {
                anchor,
                side,
                runs,
                like,
                style,
                comment,
                toc,
                keep_mark,
            } => {
                let like_node = t.paragraph_nodes[*like];
                let dom = &mut t.opened.dom;
                let news = match toc {
                    Some(toc) => toc_paragraphs(dom, toc),
                    None => vec![build_paragraph(dom, like_node, runs, style.as_deref())],
                };
                if *keep_mark {
                    copy_mark_formatting(dom, like_node, news[0]);
                }
                for &new in &news {
                    insert_paragraph_content(dom, new, stamp);
                }
                (*anchor, *side, news, comment.is_some())
            }
            Resolved::InsertTable {
                anchor,
                side,
                rows,
                header_row,
                widths,
                style,
                ..
            } => {
                let anchor_node = t.paragraph_nodes[*anchor];
                let dom = &mut t.opened.dom;
                let new =
                    structural::build_table(dom, anchor_node, rows, *header_row, widths, style);
                insert_table_content(dom, new, stamp);
                tables.push(new);
                (*anchor, *side, vec![new], false)
            }
            _ => continue,
        };
        let anchor_node = t.paragraph_nodes[anchor];
        let dom = &mut t.opened.dom;
        for &new in &news {
            match side {
                Side::After => {
                    let prev = last_after.get(&anchor).copied().unwrap_or(anchor_node);
                    dom.add_after_self(prev, new);
                    last_after.insert(anchor, new);
                }
                Side::Before => {
                    dom.add_before_self(anchor_node, new);
                }
            }
        }
        let new = news[news.len() - 1];
        inserted.insert(*i, new);
        if commented && let Some(&id) = comment_ids.get(i) {
            let projection = project_paragraph(dom, new);
            anchor_comment(dom, new, 0, projection.text.len(), id, &mut stamp.ids);
        }
    }
    // 2b. The last new paragraph after a container's last paragraph takes
    // the final mark, unrevised, and the inserted mark is the one before
    // it, as Word's Compare writes an append: a revision on a table cell's
    // final mark hangs Word's Accept All and Reject All (#1669). In the
    // body, whose final mark Word resolves, a mark that closes a section
    // stays: the record cannot hold the break (`CT_PPrBase` has no
    // `w:sectPr`), so Reject All would drop it (#1680).
    for para in appended {
        let Some(&last) = last_after.get(&para) else {
            continue;
        };
        let dom = &mut t.opened.dom;
        let old = t.paragraph_nodes[para];
        let closes_section = dom
            .parent(old)
            .is_some_and(|up| dom.name_is(up, &W::body()))
            && dom
                .element(old, &W::p_pr())
                .is_some_and(|ppr| dom.element(ppr, &W::sect_pr()).is_some());
        if dom.name_is(last, &W::p()) && !closes_section {
            rotate_final_mark(dom, old, last, stamp);
        }
    }
    // 3. Paragraph formatting, with the old properties in `w:pPrChange`.
    for (_, r) in &t.resolved {
        let Resolved::FormatParagraph {
            para,
            style,
            alignment,
            spacing,
        } = r
        else {
            continue;
        };
        let node = t.paragraph_nodes[*para];
        track_properties(&mut t.opened.dom, node, stamp, |dom| {
            format_paragraph(dom, node, style.as_deref(), *alignment, *spacing);
        });
    }
    // 3b. Lists, numbered as `Transaction::apply` numbers them, each
    // paragraph's old properties in `w:pPrChange`.
    let (mut next_abstract, mut next_num) = structural::next_numbering_ids(&t.opened);
    for (_, r) in &t.resolved {
        let Resolved::List {
            paras,
            kind,
            level,
            join,
            style,
            add_style,
        } = r
        else {
            continue;
        };
        let num_id = match join {
            Some(num_id) => num_id.clone(),
            None => {
                let (abstracts, nums) = &mut t.new_numbering;
                abstracts.push(crate::markdown::xml::abstract_num(
                    next_abstract,
                    kind.format(),
                ));
                nums.push(crate::markdown::xml::num(next_num, next_abstract, None));
                next_abstract += 1;
                next_num += 1;
                (next_num - 1).to_string()
            }
        };
        for &para in paras {
            let node = t.paragraph_nodes[para];
            let mut styled = false;
            track_properties(&mut t.opened.dom, node, stamp, |dom| {
                styled = structural::number_paragraph(dom, node, *level, &num_id, style);
            });
            if styled && *add_style {
                t.needed_styles.insert(style.clone());
            }
        }
    }
    // 4. Merges: the head's mark deleted, the separator inserted.
    let mut merges: Vec<(usize, String)> = t
        .resolved
        .iter()
        .filter_map(|(_, r)| match r {
            Resolved::MergeParagraphs {
                para, separator, ..
            } => Some((*para, separator.clone())),
            _ => None,
        })
        .collect();
    merges.sort_by_key(|&(para, _)| para);
    for (para, separator) in merges {
        let head = t.paragraph_nodes[para];
        let dom = &mut t.opened.dom;
        if !separator.is_empty() {
            let run = dom.new_element(W::r());
            let last_rpr = dom
                .elements(head, Some(&W::r()))
                .last()
                .and_then(|&r| dom.element(r, &W::r_pr()));
            if let Some(rpr) = last_rpr {
                let rpr = clean_rpr(dom, rpr);
                dom.add(run, rpr);
            }
            let text = dom.new_element(W::t());
            set_text(dom, text, &separator);
            dom.add(run, text);
            let ins = stamp.container(dom, W::ins());
            dom.add(ins, run);
            dom.add(head, ins);
        }
        mark(dom, head, W::del(), stamp);
    }
    // 5. Paragraph deletions: every run deleted, and a deleted mark.
    for (i, r) in &t.resolved {
        let Resolved::DeleteParagraph { para } = r else {
            continue;
        };
        let node = t.paragraph_nodes[*para];
        let dom = &mut t.opened.dom;
        if let Some(&id) = comment_ids.get(i) {
            let projection = project_paragraph(dom, node);
            anchor_comment(dom, node, 0, projection.text.len(), id, &mut stamp.ids);
        }
        delete_runs(dom, node, stamp);
        // A container's final mark stays, as Word keeps it: emptied, or
        // holding the new paragraphs' last (#1668).
        if emptied.contains(para) {
            continue;
        }
        if let Some(&last) = replacements.get(para).and_then(|op| inserted.get(op)) {
            keep_final_mark(dom, node, last, stamp);
            continue;
        }
        // A paragraph before another or before a table takes a deleted
        // mark of its own, as Word's Compare writes it. The mark of the
        // last paragraph of a container cannot go: its content joins the
        // paragraph before it, whose mark is deleted.
        let carrier = if let Some(next) = next_block(dom, node) {
            // Between a table the plan inserts and another table, the
            // paragraph stays, emptied: it is the paragraph the clean copy
            // puts between them (#1670, round 8).
            let before = super::previous_block(dom, node).filter(|&b| dom.name_is(b, &W::tbl()));
            let separates = dom.name_is(next, &W::tbl())
                && before.is_some_and(|b| tables.contains(&b) || tables.contains(&next));
            (!separates).then_some(node)
        } else if let Some(&carrier) = carriers.get(para) {
            Some(carrier)
        } else {
            let mut cursor = previous_paragraph(dom, node);
            while let Some(p) = cursor.filter(|&p| mark_has(dom, p, &DELETED)) {
                cursor = previous_paragraph(dom, p);
            }
            cursor
        };
        if let Some(carrier) = carrier {
            // The mark goes with this paragraph, so a nonempty bookmark ends
            // inside the deletion, as Word writes it. An empty bookmark, and
            // a cell whose mark stays, keep the end outside (#1676).
            if carrier == node {
                enclose_nonempty_bookmark_ends(dom, node);
            }
            mark(dom, carrier, W::del(), stamp);
        }
    }
    Ok(tables)
}

fn coalesce_insertions(edits: &[ScheduledEdit]) -> Vec<ScheduledEdit> {
    let mut emission: Vec<ScheduledEdit> = Vec::new();
    for edit in edits {
        if let Some(previous) = emission.last_mut()
            && previous.start == previous.end
            && edit.start == edit.end
            && previous.start == edit.start
            && previous.attach_before == edit.attach_before
            && previous.format == edit.format
        {
            previous.replacement.push_str(&edit.replacement);
        } else {
            emission.push(edit.clone());
        }
    }
    emission
}

/// `last`, the new paragraph that replaces the container-final `old`, takes
/// `old`'s place and mark, as Word writes text typed over a final
/// paragraph: `old` holds `last`'s inserted content before its own deleted
/// content, and `last`'s properties, the old ones in a `w:pPrChange` when
/// they differ; its mark takes `last`'s formatting, the old in a
/// `w:rPrChange`. A change record already there stays: `check` refuses,
/// under keep, the plans that would have to replace it. The other new
/// paragraphs keep their inserted marks before it.
fn keep_final_mark(dom: &mut Dom, old: NodeId, last: NodeId, stamp: &mut Stamp<'_>) {
    let content: Vec<NodeId> = dom
        .nodes(last)
        .into_iter()
        .filter(|&c| !dom.name_is(c, &W::p_pr()))
        .collect();
    let first = dom
        .nodes(old)
        .into_iter()
        .find(|&c| !dom.name_is(c, &W::p_pr()));
    for c in content {
        dom.remove(c);
        match first {
            Some(first) => dom.add_before_self(first, c),
            None => dom.add(old, c),
        }
    }
    let wanted = bare_properties(dom, last);
    if serialized(dom, &wanted) != serialized(dom, &bare_properties(dom, old)) {
        track_properties(dom, old, stamp, |dom| {
            let ppr = paragraph_properties(dom, old);
            for child in dom.elements(ppr, None) {
                if !dom.name_is(child, &W::r_pr())
                    && !dom.name_is(child, &W::sect_pr())
                    && !dom.name_is(child, &W::p_pr_change())
                {
                    dom.remove(child);
                }
            }
            for &child in &wanted {
                dom.remove(child);
                insert_ppr_child(dom, ppr, child);
            }
        });
    }
    let wanted = mark_formatting(dom, last);
    let had = mark_formatting(dom, old);
    if serialized(dom, &wanted) != serialized(dom, &had) {
        let rpr = mark_properties(dom, old);
        // A record already there holds the mark's original formatting.
        let change = dom.element(rpr, &W::r_pr_change());
        let before = dom.new_element(W::r_pr());
        for &child in &had {
            dom.remove(child);
            dom.add(before, child);
        }
        for child in wanted {
            dom.remove(child);
            match change {
                Some(change) => dom.add_before_self(change, child),
                None => dom.add(rpr, child),
            }
        }
        if change.is_none() {
            let record = stamp.container(dom, W::r_pr_change());
            dom.add(record, before);
            dom.add(rpr, record);
        }
    }
    // `old` takes `last`'s place when `last` follows it; placed before
    // paragraphs the plan deletes, `last` leaves `old` where it is, after
    // them (#1670, round 8).
    if precedes(dom, old, last) {
        dom.remove(old);
        dom.add_after_self(last, old);
    }
    dom.remove(last);
}

/// The container-final paragraph that `r` places a block after, if any.
fn appended_final(tx: &Transaction<'_>, r: &Resolved) -> Option<usize> {
    let (Resolved::InsertParagraph {
        anchor,
        side: Side::After,
        ..
    }
    | Resolved::InsertTable {
        anchor,
        side: Side::After,
        ..
    }) = r
    else {
        return None;
    };
    let node = tx.paragraph_nodes[*anchor];
    (tx.final_paragraph(node) == Some(node)).then_some(*anchor)
}

/// The operation whose block lands last after the container-final
/// `para`: blocks placed after one paragraph follow each other in plan
/// order.
fn last_appended(tx: &Transaction<'_>, para: usize) -> Option<usize> {
    tx.resolved
        .iter()
        .filter(|(_, r)| appended_final(tx, r) == Some(para))
        .map(|(op, _)| *op)
        .last()
}

/// `last`, the new paragraph placed last after the container-final `old`,
/// takes the final mark: its inserted mark goes, and `old`'s mark is the
/// inserted one. Rejected, `old`'s words join `last`'s mark, so that mark
/// records `old`'s properties and mark formatting where they differ from
/// its own; a change record `old` already holds moves to it (`check`
/// refuses, under keep, the plans whose new paragraph that record would
/// misdescribe).
fn rotate_final_mark(dom: &mut Dom, old: NodeId, last: NodeId, stamp: &mut Stamp<'_>) {
    if let Some(inserted) = mark_revision(dom, last, &["ins"]) {
        dom.remove(inserted);
    }
    let had = bare_properties(dom, old);
    if let Some(change) = paragraph_format_change(dom, old) {
        dom.remove(change);
        let ppr = paragraph_properties(dom, last);
        insert_ppr_child(dom, ppr, change);
    } else if serialized(dom, &bare_properties(dom, last)) != serialized(dom, &had) {
        let before = dom.new_element(W::p_pr());
        for child in had {
            let copy = dom.clone_subtree(child);
            dom.add(before, copy);
        }
        let record = stamp.container(dom, W::p_pr_change());
        dom.add(record, before);
        let ppr = paragraph_properties(dom, last);
        insert_ppr_child(dom, ppr, record);
    }
    let had = mark_formatting(dom, old);
    let ppr = paragraph_properties(dom, old);
    let change = dom
        .element(ppr, &W::r_pr())
        .and_then(|rpr| dom.element(rpr, &W::r_pr_change()));
    if let Some(change) = change {
        dom.remove(change);
        let rpr = mark_properties(dom, last);
        dom.add(rpr, change);
    } else if serialized(dom, &mark_formatting(dom, last)) != serialized(dom, &had) {
        let before = dom.new_element(W::r_pr());
        for child in had {
            let copy = dom.clone_subtree(child);
            dom.add(before, copy);
        }
        let record = stamp.container(dom, W::r_pr_change());
        dom.add(record, before);
        let rpr = mark_properties(dom, last);
        dom.add(rpr, record);
    }
    mark(dom, old, W::ins(), stamp);
}

/// Whether `a` comes before `b` in document order.
fn precedes(dom: &Dom, a: NodeId, b: NodeId) -> bool {
    let chain = dom.ancestors_and_self(a, None);
    dom.ancestors_and_self(b, None)
        .into_iter()
        .any(|up| dom.nodes_before_self(up).iter().any(|n| chain.contains(n)))
}

/// `paragraph`'s properties without the mark's own (`w:rPr`), its section
/// or a change record; none at all reads as an empty `w:pPr`.
fn bare_properties(dom: &Dom, paragraph: NodeId) -> Vec<NodeId> {
    dom.element(paragraph, &W::p_pr())
        .map(|ppr| dom.elements(ppr, None))
        .unwrap_or_default()
        .into_iter()
        .filter(|&child| {
            !dom.name_is(child, &W::r_pr())
                && !dom.name_is(child, &W::sect_pr())
                && !dom.name_is(child, &W::p_pr_change())
        })
        .collect()
}

/// The formatting of `paragraph`'s mark (`w:pPr/w:rPr`), without its
/// revisions or a change record.
fn mark_formatting(dom: &Dom, paragraph: NodeId) -> Vec<NodeId> {
    dom.element(paragraph, &W::p_pr())
        .and_then(|ppr| dom.element(ppr, &W::r_pr()))
        .map(|rpr| dom.elements(rpr, None))
        .unwrap_or_default()
        .into_iter()
        .filter(|&child| !is_one_of(dom, child, &MARK_RECORDS))
        .collect()
}

/// What a paragraph mark's `w:rPr` holds besides its formatting.
const MARK_RECORDS: [&str; 5] = ["ins", "del", "moveFrom", "moveTo", "rPrChange"];

/// `children` as XML, to compare property sets.
fn serialized(dom: &Dom, children: &[NodeId]) -> Vec<String> {
    children.iter().map(|&c| dom.serialize_element(c)).collect()
}

/// Wrap a new paragraph's content in one `w:ins` and mark it inserted.
fn insert_paragraph_content(dom: &mut Dom, paragraph: NodeId, stamp: &mut Stamp<'_>) {
    let content: Vec<NodeId> = dom
        .elements(paragraph, None)
        .into_iter()
        .filter(|&c| !dom.name_is(c, &W::p_pr()))
        .collect();
    if let Some(&first) = content.first() {
        let ins = stamp.container(dom, W::ins());
        dom.add_before_self(first, ins);
        for c in content {
            dom.remove(c);
            dom.add(ins, c);
        }
    }
    mark(dom, paragraph, W::ins(), stamp);
}

/// Mark every row of a new table inserted (`w:trPr/w:ins`, after the row's
/// other properties) and every cell paragraph's runs and mark inserted.
fn insert_table_content(dom: &mut Dom, table: NodeId, stamp: &mut Stamp<'_>) {
    for row in dom.elements(table, Some(&W::tr())) {
        let properties = match dom.element(row, &W::tr_pr()) {
            Some(properties) => properties,
            None => {
                let properties = dom.new_element(W::tr_pr());
                match dom.element(row, &W::name("tblPrEx")) {
                    Some(exceptions) => dom.add_after_self(exceptions, properties),
                    None => dom.add_first(row, properties),
                }
                properties
            }
        };
        let ins = stamp.container(dom, W::ins());
        dom.add(properties, ins);
        for paragraph in dom.descendants(row, Some(&W::p())) {
            insert_paragraph_content(dom, paragraph, stamp);
        }
    }
}

/// `structural::separate`, with each paragraph it adds marked inserted.
fn separate_tracked(dom: &mut Dom, table: NodeId, stamp: &mut Stamp<'_>) {
    let parent = dom.parent(table);
    let before: Vec<NodeId> = parent
        .map(|p| dom.elements(p, Some(&W::p())))
        .unwrap_or_default();
    structural::separate(dom, table);
    if let Some(parent) = parent {
        for paragraph in dom.elements(parent, Some(&W::p())) {
            if !before.contains(&paragraph) {
                mark(dom, paragraph, W::ins(), stamp);
            }
        }
    }
}

/// Run `change` on `paragraph`'s properties and track it: a `w:pPrChange`
/// holding the old `w:pPr`, unless the plan already tracked a change there
/// (that one holds the original).
fn track_properties(
    dom: &mut Dom,
    paragraph: NodeId,
    stamp: &mut Stamp<'_>,
    change: impl FnOnce(&mut Dom),
) {
    let tracked = dom
        .element(paragraph, &W::p_pr())
        .and_then(|ppr| dom.element(ppr, &W::p_pr_change()))
        .is_some();
    let old = (!tracked).then(|| {
        let old = match dom.element(paragraph, &W::p_pr()) {
            Some(ppr) => dom.clone_subtree(ppr),
            None => dom.new_element(W::p_pr()),
        };
        for child in dom.elements(old, None) {
            if dom.name_is(child, &W::r_pr())
                || dom.name_is(child, &W::sect_pr())
                || dom.name_is(child, &W::p_pr_change())
            {
                dom.remove(child);
            }
        }
        old
    });
    change(dom);
    if let Some(old) = old {
        let ppr = paragraph_properties(dom, paragraph);
        let record = stamp.container(dom, W::p_pr_change());
        dom.add(record, old);
        insert_ppr_child(dom, ppr, record);
    }
}

/// Format `range` of `paragraph` as `Transaction::apply` does, each changed
/// run's old properties in a `w:rPrChange`, the last child of its `w:rPr`.
fn track_run_format(
    dom: &mut Dom,
    paragraph: NodeId,
    (start, end): (usize, usize),
    format: &super::RunFormat,
    stamp: &mut Stamp<'_>,
) {
    for run in runs_in_range(dom, paragraph, start, end, &mut stamp.ids) {
        let rpr = match dom.element(run, &W::r_pr()) {
            Some(rpr) => rpr,
            None => {
                let rpr = dom.new_element(W::r_pr());
                dom.add_first(run, rpr);
                rpr
            }
        };
        // `check` refuses formatting over a source record, so a record here
        // is an earlier operation's: it already holds the run's old
        // properties, and a second one would copy it, id and all (#1656).
        if dom.element(rpr, &W::r_pr_change()).is_some() {
            apply_run_format(dom, rpr, format);
            continue;
        }
        let old = dom.clone_subtree(rpr);
        apply_run_format(dom, rpr, format);
        if dom.serialize_element(old) == dom.serialize_element(rpr) {
            if dom.elements(rpr, None).is_empty() {
                dom.remove(rpr);
            }
            continue;
        }
        let record = stamp.container(dom, W::r_pr_change());
        dom.add(record, old);
        dom.add(rpr, record);
    }
}

/// Reply markers beside their comment's, and deleted comments' markers
/// removed, as `Transaction::apply` does last.
fn thread_markers(t: &mut Transaction<'_>, comment_ids: &BTreeMap<usize, u32>) {
    let roots: Vec<NodeId> = t.stories.iter().map(|s| s.root).collect();
    for (i, r) in &t.resolved {
        let Resolved::Thread { op, .. } = r else {
            continue;
        };
        match op {
            ThreadOp::Reply { parent, .. } => {
                if let Some(&id) = comment_ids.get(i) {
                    place_reply_markers(&mut t.opened.dom, &roots, *parent, id);
                }
            }
            ThreadOp::Delete { id } => {
                let gone: Vec<String> = t
                    .family
                    .as_ref()
                    .map(|family| family.with_replies(*id))
                    .unwrap_or_default()
                    .iter()
                    .map(u32::to_string)
                    .collect();
                remove_comment_markers(&mut t.opened.dom, &roots, &gone);
            }
            ThreadOp::Resolve { .. } | ThreadOp::Edit { .. } => {}
        }
    }
}

/// Reuse the word diff for default replacements inside an insertion. For
/// example, `month.` -> `month, in cash.` only inserts `, in cash` and
/// preserves both the word and period. A `whole` replacement bypasses this.
fn insertion_replacement(dom: &Dom, paragraph: NodeId, edit: &ScheduledEdit) -> Vec<ScheduledEdit> {
    // A formatted replacement formats every word of it, the ones it keeps
    // too, as the clean copy does: one deletion, one insertion.
    if edit.format.is_some() {
        return vec![edit.clone()];
    }
    let Some(pieces) = insertion_pieces(dom, paragraph, edit.start, edit.end, &edit.replacement)
    else {
        return vec![edit.clone()];
    };
    pieces
        .into_iter()
        .map(|(start, end, replacement)| ScheduledEdit {
            start,
            end,
            replacement,
            ..edit.clone()
        })
        .collect()
}

/// The word-level edits, in projection offsets, that make `start..end`
/// read as `replacement` when the range lies in one insertion; `None` when
/// it reaches outside one, or the word diff cannot write it exactly.
pub(super) fn insertion_pieces(
    dom: &Dom,
    paragraph: NodeId,
    start: usize,
    end: usize,
    replacement: &str,
) -> Option<Vec<(usize, usize, String)>> {
    let projection = project_paragraph(dom, paragraph);
    let touched: Vec<_> = projection
        .segments
        .iter()
        .filter(|s| s.start < end && start < s.end)
        .collect();
    let ins = touched
        .first()
        .and_then(|s| super::editable_insertion(dom, &s.piece));
    if ins.is_none()
        || !touched
            .iter()
            .all(|s| super::editable_insertion(dom, &s.piece) == ins)
    {
        return None;
    }
    let old = &projection.text[start..end];
    let ranges = super::rewrite::rewrite_ranges(old, replacement);
    let mut rewritten = old.to_string();
    for (from, to, words) in ranges.iter().rev() {
        rewritten.replace_range(*from..*to, words);
    }
    // The rewrite diff normalizes whitespace. An exact replacement still
    // has to write every requested byte, including whitespace-only edits.
    if rewritten != replacement {
        return None;
    }
    Some(
        ranges
            .into_iter()
            .map(|(from, to, words)| (start + from, start + to, words))
            .collect(),
    )
}

/// Emit one text edit: the range's runs in `w:del`, then the replacement in
/// a `w:ins` run formatted like the first deleted run, or like the run the
/// insertion attaches to. `empty` places the text of a paragraph that had
/// none.
fn emit_text(
    dom: &mut Dom,
    paragraph: NodeId,
    edit: &ScheduledEdit,
    stamp: &mut Stamp<'_>,
    empty: Option<&mut super::EmptyPlacement>,
) {
    if let Some(placement) = empty {
        // Typed into an empty paragraph: a run formatted as the mark, in
        // our insertion, at its edge. Placed in reverse order, no text is
        // before it yet, so its words start at 0.
        if edit.replacement.is_empty() {
            return;
        }
        let ins = stamp.container(dom, W::ins());
        placement.place(dom, paragraph, ins, edit.attach_before);
        let run = super::mark_run(dom, paragraph);
        dom.add(ins, run);
        super::write_mark_run_text(dom, run, &edit.replacement);
        if let Some(format) = &edit.format {
            super::format_range(
                dom,
                paragraph,
                0,
                edit.replacement.len(),
                format,
                &mut stamp.ids,
            );
        }
        return;
    }
    let projection = project_paragraph(dom, paragraph);
    let touched: Vec<_> = if edit.start == edit.end {
        attach_segment(&projection, edit.start, edit.attach_before)
            .into_iter()
            .collect()
    } else {
        projection
            .segments
            .iter()
            .filter(|s| s.start < edit.end && edit.start < s.end)
            .collect()
    };
    // Word treats typing in one's own insertion as a correction to that
    // insertion, preserving its original id and timestamp.
    if let Some(ins) = touched
        .first()
        .and_then(|s| super::editable_insertion(dom, &s.piece))
        && dom.attribute(ins, &W::author()) == Some(stamp.author)
        && touched
            .iter()
            .all(|s| super::editable_insertion(dom, &s.piece) == Some(ins))
    {
        apply_text_edit(
            dom,
            &projection,
            edit.start,
            edit.end,
            &edit.replacement,
            edit.attach_before,
        );
        super::clean_new_words(
            dom,
            paragraph,
            edit.start,
            edit.start + edit.replacement.len(),
            &mut stamp.ids,
        );
        if let Some(format) = &edit.format {
            super::format_range(
                dom,
                paragraph,
                edit.start,
                edit.start + edit.replacement.len(),
                format,
                &mut stamp.ids,
            );
        }
        remove_empty_insertion(dom, ins);
        return;
    }
    let (start, end) = (edit.start, edit.end);
    let mut replacement = edit.replacement.as_str();
    // This edit's own deletions, in document order.
    let mut ours = Vec::new();
    // `None` places the insertion right after `anchor`, `Some(true)` before.
    let (template, anchor, before) = if start < end {
        let runs = isolate_range(dom, paragraph, (start, end), &mut stamp.ids);
        let mut groups: Vec<Vec<NodeId>> = Vec::new();
        for &run in &runs {
            match groups.last_mut() {
                Some(group) if dom.next_element(*group.last().expect("nonempty")) == Some(run) => {
                    group.push(run);
                }
                _ => groups.push(vec![run]),
            }
        }
        let mut last_del = None;
        for (index, group) in groups.into_iter().enumerate() {
            if let Some(ins) = dom.parent(group[0]).filter(|&n| dom.name_is(n, &W::ins()))
                && dom.attribute(ins, &W::author()) == Some(stamp.author)
            {
                let projection = project_paragraph(dom, paragraph);
                let pieces: Vec<_> = projection
                    .segments
                    .iter()
                    .filter(|s| group.contains(&super::run_of(&s.piece)))
                    .collect();
                if let (Some(first), Some(last)) = (pieces.first(), pieces.last()) {
                    let text = if index == 0 { replacement } else { "" };
                    apply_text_edit(dom, &projection, first.start, last.end, text, true);
                    super::clean_new_words(
                        dom,
                        paragraph,
                        first.start,
                        first.start + text.len(),
                        &mut stamp.ids,
                    );
                    if index == 0 {
                        if let Some(format) = &edit.format {
                            super::format_range(
                                dom,
                                paragraph,
                                first.start,
                                first.start + text.len(),
                                format,
                                &mut stamp.ids,
                            );
                        }
                        replacement = "";
                    }
                }
                remove_empty_insertion(dom, ins);
                continue;
            }
            let del = stamp.container(dom, W::del());
            dom.add_before_self(group[0], del);
            for run in group {
                dom.remove(run);
                dom.add(del, run);
                rename_deleted_text(dom, run);
            }
            ours.push(del);
            last_del = Some(del);
        }
        (runs.first().copied(), last_del, false)
    } else {
        let projection = project_paragraph(dom, paragraph);
        let Some(seg) = attach_segment(&projection, start, edit.attach_before).cloned() else {
            return;
        };
        split_run_at(dom, &seg, start, &mut stamp.ids);
        let projection = project_paragraph(dom, paragraph);
        let text_piece = |s: &crate::inspect::Segment| {
            if !s.direct && super::editable_insertion(dom, &s.piece).is_none() {
                return None;
            }
            Some(match s.piece {
                Piece::Text { t, run } => (t, run),
                Piece::Glyph { element, run } => (element, run),
            })
        };
        let left = projection
            .segments
            .iter()
            .rev()
            .find(|s| s.end == start && s.start < s.end)
            .and_then(text_piece);
        let right = projection
            .segments
            .iter()
            .find(|s| s.start == start && s.start < s.end)
            .and_then(text_piece);
        let (chosen, before) = match (left, right) {
            (Some(l), _) if edit.attach_before => (l, false),
            (_, Some(r)) => (r, true),
            (Some(l), None) => (l, false),
            (None, None) => return,
        };
        let (t, run) = chosen;
        // An empty `w:t` covers no character. Peeling it off the piece the
        // insertion attaches to moves Alice's id onto that empty run and
        // mints a fresh id for the tab. `audit_tracked` then reports the
        // original id as `FOREIGN_AUTHOR` (#1663).
        if split_around_piece(dom, run, t) {
            isolate(dom, run, t, &mut stamp.ids);
        }
        let anchor = insertion_boundary_anchor(dom, run, before);
        (Some(run), Some(anchor), before)
    };
    // A deletion stays inside the source insertion, which stays whole, as
    // Word leaves it: only the editor's words split it. Range endpoints stay
    // beside their words; the remaining children follow the editor's words.
    let split = anchor
        .filter(|_| !replacement.is_empty())
        .and_then(|anchor| {
            let ins = dom.parent(anchor).filter(|&n| dom.name_is(n, &W::ins()))?;
            let children = dom.elements(ins, None);
            let at = children.iter().position(|&n| n == anchor)? + usize::from(!before);
            let at = insertion_split_boundary(dom, &children, at, before);
            if at == 0 {
                // There is no first piece to keep. The complete source
                // insertion becomes the newly numbered trailing piece.
                return Some((ins, true, Some(ins)));
            }
            let tail = if at < children.len() {
                let tail = dom.clone_subtree(ins);
                for child in dom.nodes(tail) {
                    dom.remove(child);
                }
                for &child in &children[at..] {
                    dom.remove(child);
                    dom.add(tail, child);
                }
                Some(tail)
            } else {
                None
            };
            Some((ins, false, tail))
        });
    let (anchor, before) = split.map_or((anchor, before), |(ins, before, _)| (Some(ins), before));
    // New words meet no range they were not typed in: past the openers
    // before them, past the closers after them (#1156). A replacement takes
    // the place of words its ranges cover, and stays beside its deletion,
    // past the end of a comment its deletion holds whole: Accept removes
    // that comment, and the new words must not be in it.
    let anchor = anchor.map(|anchor| {
        if start == end {
            outside_ranges(dom, anchor, before)
        } else if before || replacement.is_empty() {
            anchor
        } else {
            let (last, references) = past_deleted_comments(dom, anchor, &ours);
            // Their references go with the deleted text, as deleting the
            // whole commented text writes them.
            let mut last = last;
            for run in references {
                let del = stamp.container(dom, W::del());
                dom.add_before_self(run, del);
                dom.remove(run);
                dom.add(del, run);
                if last == run {
                    last = del;
                }
            }
            last
        }
    });
    let mut last = anchor;
    if !replacement.is_empty() {
        let run = dom.new_element(W::r());
        let rpr = template
            .and_then(|r| dom.element(r, &W::r_pr()))
            .map(|rpr| clean_rpr(dom, rpr));
        let rpr = match (rpr, &edit.format) {
            (Some(rpr), _) => Some(rpr),
            (None, Some(_)) => Some(dom.new_element(W::r_pr())),
            (None, None) => None,
        };
        if let Some(rpr) = rpr {
            if let Some(format) = &edit.format {
                apply_run_format(dom, rpr, format);
            }
            if dom.elements(rpr, None).is_empty() {
                dom.remove(rpr);
            } else {
                dom.add(run, rpr);
            }
        }
        let text = dom.new_element(W::t());
        set_text(dom, text, replacement);
        dom.add(run, text);
        let ins = stamp.container(dom, W::ins());
        dom.add(ins, run);
        match anchor {
            Some(anchor) if before => dom.add_before_self(anchor, ins),
            Some(anchor) => dom.add_after_self(anchor, ins),
            None => dom.add(paragraph, ins),
        }
        last = Some(ins);
    }
    if let Some((source, _, Some(tail))) = split {
        if let Some(origin) = stamp.insertions.get(&source).cloned() {
            stamp.insertions.insert(tail, origin);
        }
        // Allocate after the deletion and editor insertion, using the same
        // package-wide allocator. The source attribution stays on the tail.
        let id = stamp.ids.allocate();
        dom.set_attribute_value(tail, &W::id(), Some(&id.to_string()));
        dom.remove(tail);
        dom.add_after_self(last.expect("a split has a first piece"), tail);
    }
}

/// The sibling an insertion at `run`'s edge goes beside: past the range
/// markers that open before the run (`before`) or close after it, so those
/// ranges keep their source endpoints. `run` itself when none sit there.
pub(super) fn insertion_boundary_anchor(dom: &Dom, run: NodeId, before: bool) -> NodeId {
    let Some(parent) = dom.parent(run) else {
        return run;
    };
    let children = dom.elements(parent, None);
    let Some(index) = children.iter().position(|&n| n == run) else {
        return run;
    };
    let at = insertion_split_boundary(dom, &children, index + usize::from(!before), before);
    children[if before { at } else { at - 1 }]
}

/// The range openers an insertion can hold: they travel with the words to
/// their right.
pub(super) const RANGE_STARTS: [&str; 5] = [
    "bookmarkStart",
    "commentRangeStart",
    "permStart",
    "moveFromRangeStart",
    "moveToRangeStart",
];
/// The range closers that travel with the words to their left; a comment's
/// end also takes its reference, so it is read on its own.
pub(super) const RANGE_ENDS: [&str; 4] = [
    "bookmarkEnd",
    "permEnd",
    "moveFromRangeEnd",
    "moveToRangeEnd",
];

/// A comment range's closer: it travels with the words to its left, and
/// takes its reference along.
pub(super) const COMMENT_RANGE_END: &str = "commentRangeEnd";

/// Whether `node` is a `w:` element named one of `locals`.
fn is_one_of(dom: &Dom, node: NodeId, locals: &[&str]) -> bool {
    dom.name(node)
        .is_some_and(|n| n.namespace_name() == W::URI && locals.contains(&n.local_name()))
}

/// The sibling of `anchor` that new words go `before` (or after) so they
/// join no range they meet: the first of the openers right before
/// `anchor`, or the last of the closers right after it, a comment's end
/// with its reference. `anchor` itself when no marker is in the way. Body-
/// level markers are no siblings of a run and stay where they are.
pub(super) fn outside_ranges(dom: &Dom, anchor: NodeId, before: bool) -> NodeId {
    let Some(parent) = dom.parent(anchor) else {
        return anchor;
    };
    let children = dom.elements(parent, None);
    let Some(index) = children.iter().position(|&n| n == anchor) else {
        return anchor;
    };
    let at = insertion_split_boundary(dom, &children, index + usize::from(!before), before);
    if before {
        children[at]
    } else {
        children[at - 1]
    }
}

/// Opening markers travel with the words to their right, closing markers
/// with the words to their left. A comment reference stays with its end.
fn insertion_split_boundary(dom: &Dom, children: &[NodeId], mut at: usize, before: bool) -> usize {
    if before {
        while at > 0 && is_one_of(dom, children[at - 1], &RANGE_STARTS) {
            at -= 1;
        }
        return at;
    }
    let mut closed_comments = BTreeSet::new();
    while let Some(&child) = children.get(at) {
        if dom.name_is(child, &W::name(COMMENT_RANGE_END)) {
            if let Some(id) = dom.attribute(child, &W::id()) {
                closed_comments.insert(id);
            }
        } else if !is_one_of(dom, child, &RANGE_ENDS) {
            if !dom.name_is(child, &W::r()) {
                break;
            }
            let references = dom.elements(child, Some(&W::comment_reference()));
            if references.is_empty()
                || references.iter().any(|&reference| {
                    dom.attribute(reference, &W::id())
                        .is_none_or(|id| !closed_comments.contains(id))
                })
                || dom.elements(child, None).iter().any(|&element| {
                    !dom.name_is(element, &W::r_pr())
                        && !dom.name_is(element, &W::comment_reference())
                })
            {
                break;
            }
        }
        at += 1;
    }
    at
}

/// The last sibling after deletion `del` that closes a comment opened
/// inside the deletions `ours` of one edit, after its first deleted word:
/// its end and the run with its reference (`del` itself when none follows),
/// and those reference runs. A comment opened on kept text, or on the
/// edit's first word (the replaced text is the commented text), keeps the
/// new words, in Word's order.
fn past_deleted_comments(dom: &Dom, del: NodeId, ours: &[NodeId]) -> (NodeId, Vec<NodeId>) {
    let Some(parent) = dom.parent(del) else {
        return (del, Vec::new());
    };
    let children = dom.elements(parent, None);
    let Some(index) = children.iter().position(|&n| n == del) else {
        return (del, Vec::new());
    };
    let opened_in_deletion = |id: &str| {
        let mut back = children[..index].iter().rev();
        let found = back.by_ref().find_map(|&c| {
            if dom.name_is(c, &W::comment_range_start()) && dom.attribute(c, &W::id()) == Some(id) {
                Some(true)
            } else if ours.contains(&c) || is_one_of(dom, c, &RANGE_STARTS) {
                None
            } else {
                Some(false)
            }
        }) == Some(true);
        found
            && back
                .find(|&&c| !is_one_of(dom, c, &RANGE_STARTS))
                .is_some_and(|c| ours.contains(c))
    };
    let mut closed = BTreeSet::new();
    let mut last = del;
    let mut references_passed = Vec::new();
    for &child in &children[index + 1..] {
        let is_end = dom.name_is(child, &W::name(COMMENT_RANGE_END));
        let passes = if is_end {
            dom.attribute(child, &W::id())
                .filter(|id| opened_in_deletion(id))
                .is_some_and(|id| closed.insert(id))
        } else {
            let references = dom.elements(child, Some(&W::comment_reference()));
            dom.name_is(child, &W::r())
                && !references.is_empty()
                && references.iter().all(|&r| {
                    dom.attribute(r, &W::id())
                        .is_some_and(|id| closed.contains(id))
                })
                && dom
                    .elements(child, None)
                    .iter()
                    .all(|&e| dom.name_is(e, &W::r_pr()) || dom.name_is(e, &W::comment_reference()))
        };
        if !passes {
            break;
        }
        if !is_end {
            references_passed.push(child);
        }
        last = child;
    }
    (last, references_passed)
}

/// Our insertion `ours` right after deletion `del`. A deletion inside an
/// insertion splits it there, as typing does in Word: ours follows the
/// piece that holds the deletion, and the rest follows ours under a new id
/// with the source attribution.
fn place_beside(dom: &mut Dom, del: NodeId, ours: NodeId, stamp: &mut Stamp<'_>) {
    let Some(host) = dom.parent(del).filter(|&p| dom.name_is(p, &W::ins())) else {
        dom.add_after_self(del, ours);
        return;
    };
    let children = dom.elements(host, None);
    let at = children
        .iter()
        .position(|&n| n == del)
        .map_or(children.len(), |i| i + 1);
    let at = insertion_split_boundary(dom, &children, at, false);
    dom.add_after_self(host, ours);
    if at < children.len() {
        let tail = dom.clone_subtree(host);
        for child in dom.nodes(tail) {
            dom.remove(child);
        }
        for &child in &children[at..] {
            dom.remove(child);
            dom.add(tail, child);
        }
        if let Some(origin) = stamp.insertions.get(&host).cloned() {
            stamp.insertions.insert(tail, origin);
        }
        let id = stamp.ids.allocate();
        dom.set_attribute_value(tail, &W::id(), Some(&id.to_string()));
        dom.add_after_self(ours, tail);
    }
}

/// Empty run shells must not keep an otherwise deleted own insertion alive.
pub(super) fn remove_empty_insertion(dom: &mut Dom, ins: NodeId) {
    for run in dom.elements(ins, Some(&W::r())) {
        if dom.elements(run, None).iter().all(|&c| {
            dom.name_is(c, &W::r_pr()) || (dom.name_is(c, &W::t()) && dom.value(c).is_empty())
        }) {
            dom.remove(run);
        }
    }
    if dom.elements(ins, None).is_empty() {
        dom.remove(ins);
    }
}

/// Split runs so each text or glyph of `[start, end)` sits alone in its
/// run (beside the run's `w:rPr`); return the runs in document order.
fn isolate_range(
    dom: &mut Dom,
    paragraph: NodeId,
    (start, end): (usize, usize),
    ids: &mut RevisionCounter,
) -> Vec<NodeId> {
    for at in [start, end] {
        let projection = project_paragraph(dom, paragraph);
        if let Some(seg) = projection
            .segments
            .iter()
            .find(|s| s.start < at && at < s.end)
            .cloned()
        {
            split_run_at(dom, &seg, at, ids);
        }
    }
    loop {
        let projection = project_paragraph(dom, paragraph);
        let pieces: Vec<(NodeId, NodeId)> = projection
            .segments
            .iter()
            .filter(|s| s.start >= start && s.end <= end && s.start < s.end)
            .map(|s| match s.piece {
                Piece::Text { t, run } => (t, run),
                Piece::Glyph { element, run } => (element, run),
            })
            .collect();
        match pieces.iter().find(|&&(t, run)| !alone(dom, run, t)) {
            Some(&(t, run)) => isolate(dom, run, t, ids),
            None => {
                let mut runs: Vec<NodeId> = Vec::new();
                for (_, run) in pieces {
                    if !runs.contains(&run) {
                        runs.push(run);
                    }
                }
                return runs;
            }
        }
    }
}

/// Whether `t` has a sibling the insertion must leave in another run.
/// An empty `w:t` stays with `t`: it is not a character the insertion splits.
fn split_around_piece(dom: &Dom, run: NodeId, t: NodeId) -> bool {
    child_elements(dom, run).any(|child| {
        if child == t || dom.name_is(child, &W::r_pr()) {
            return false;
        }
        if dom.name_is(child, &W::t()) {
            return !dom.value(child).is_empty();
        }
        true
    })
}

/// Whether `t` is the only child of `run` besides its `w:rPr`.
fn alone(dom: &Dom, run: NodeId, t: NodeId) -> bool {
    child_elements(dom, run).all(|c| c == t || dom.name_is(c, &W::r_pr()))
}

/// The element children of `run`, without collecting them.
fn child_elements(dom: &Dom, run: NodeId) -> impl Iterator<Item = NodeId> + '_ {
    (0..dom.child_count(run))
        .map(move |i| dom.child_at(run, i))
        .filter(|&child| dom.is_element(child))
}

/// Move the children of `run` before `t` into a copy of the run placed
/// before it, and those after `t` into a copy placed after it; each copy
/// keeps the run's `w:rPr`. The first fragment keeps the ids of the run's
/// formatting history; the others take fresh ids from `ids` (#1656).
fn isolate(dom: &mut Dom, run: NodeId, t: NodeId, ids: &mut RevisionCounter) {
    let children = dom.elements(run, None);
    let Some(index) = children.iter().position(|&c| c == t) else {
        return;
    };
    let content = |dom: &Dom, c: NodeId| !dom.name_is(c, &W::r_pr());
    for (keep_before, place_before) in [(true, true), (false, false)] {
        // `keep_before`: the copy keeps the children before `t`.
        let side: Vec<NodeId> = children
            .iter()
            .enumerate()
            .filter(|&(i, &c)| (if keep_before { i < index } else { i > index }) && content(dom, c))
            .map(|(_, &c)| c)
            .collect();
        if side.is_empty() {
            continue;
        }
        let copy = dom.clone_subtree(run);
        for (i, c) in dom.elements(copy, None).into_iter().enumerate() {
            let kept = if keep_before { i < index } else { i > index };
            if !kept && content(dom, c) {
                dom.remove(c);
            }
        }
        if place_before {
            dom.add_before_self(run, copy);
            // The copy is now the first fragment: the original's record
            // becomes the copy.
            super::refresh_run_history(dom, run, ids);
        } else {
            super::refresh_run_history(dom, copy, ids);
            dom.add_after_self(run, copy);
        }
    }
    // Both copies need the original child indexes. Removing the left
    // side before cloning the right shifts those indexes and loses text.
    for child in children {
        if child != t && content(dom, child) {
            dom.remove(child);
        }
    }
}

/// `w:t` to `w:delText` and `w:instrText` to `w:delInstrText` in a run that
/// now sits inside a `w:del`.
fn rename_deleted_text(dom: &mut Dom, run: NodeId) {
    for child in dom.elements(run, None) {
        if dom.name_is(child, &W::t()) {
            dom.set_name(child, W::del_text());
        } else if dom.name_is(child, &W::instr_text()) {
            dom.set_name(child, W::del_instr_text());
        }
    }
}

/// Every run of `container`, at any depth of hyperlinks, content controls,
/// smart tags and simple fields, inside a `w:del`; consecutive runs share one.
/// Under `keep` (#1136), another author's insertion keeps its words with
/// the deletion nested in it, the plan author's own pending insertion goes,
/// and a deletion stays as it is.
fn delete_runs(dom: &mut Dom, container: NodeId, stamp: &mut Stamp<'_>) {
    let mut group: Vec<NodeId> = Vec::new();
    for child in dom.elements(container, None) {
        if dom.name_is(child, &W::r()) {
            group.push(child);
            continue;
        }
        wrap_deleted(dom, std::mem::take(&mut group), stamp);
        let Some(name) = dom.name(child).filter(|n| n.namespace_name() == W::URI) else {
            continue;
        };
        match name.local_name() {
            "hyperlink" | "smartTag" | "customXml" | "dir" | "bdo" | "fldSimple" => {
                delete_runs(dom, child, stamp);
            }
            "sdt" => {
                if let Some(content) = dom.element(child, &W::sdt_content()) {
                    delete_runs(dom, content, stamp);
                }
            }
            "ins" if dom.attribute(child, &W::author()) == Some(stamp.author) => {
                remove_own_insertion(dom, child, stamp);
            }
            "ins" => delete_runs(dom, child, stamp),
            _ => {}
        }
    }
    wrap_deleted(dom, group, stamp);
}

/// What a run may hold and still go outright: text Word types, nothing a
/// field or a note needs.
const TYPED: [&str; 9] = [
    "rPr",
    "t",
    "tab",
    "br",
    "cr",
    "sym",
    "noBreakHyphen",
    "softHyphen",
    "lastRenderedPageBreak",
];

/// The plan author's own pending insertion, deleted: its typed runs go
/// outright, a run holding a field character or a reference is deleted
/// inside it so no field or note loses a part, and the markers it held
/// (bookmarks, comment ranges) stay where they were.
fn remove_own_insertion(dom: &mut Dom, ins: NodeId, stamp: &mut Stamp<'_>) {
    for run in dom.elements(ins, Some(&W::r())) {
        if dom
            .elements(run, None)
            .into_iter()
            .all(|c| is_one_of(dom, c, &TYPED))
        {
            dom.remove(run);
        }
    }
    delete_runs(dom, ins, stamp);
    let left = dom.elements(ins, None);
    if left.iter().all(|&c| {
        is_one_of(dom, c, &RANGE_STARTS)
            || is_one_of(dom, c, &RANGE_ENDS)
            || is_one_of(dom, c, &[COMMENT_RANGE_END])
    }) {
        for marker in left {
            dom.remove(marker);
            dom.add_before_self(ins, marker);
        }
        dom.remove(ins);
    }
}

fn wrap_deleted(dom: &mut Dom, group: Vec<NodeId>, stamp: &mut Stamp<'_>) {
    let Some(&first) = group.first() else {
        return;
    };
    let del = stamp.container(dom, W::del());
    dom.add_before_self(first, del);
    for run in group {
        dom.remove(run);
        dom.add(del, run);
        rename_deleted_text(dom, run);
    }
}

/// A nonempty bookmark's end follows its deleted words into the content
/// `w:del`. An empty bookmark has no deleted words between its endpoints
/// and stays outside.
fn enclose_nonempty_bookmark_ends(dom: &mut Dom, paragraph: NodeId) {
    let children = dom.elements(paragraph, None);
    let mut moves = Vec::new();
    for (index, &child) in children.iter().enumerate() {
        if !dom.name_is(child, &W::bookmark_end()) {
            continue;
        }
        let Some(id) = dom.attribute(child, &W::id()) else {
            continue;
        };
        let Some(start_at) = children[..index].iter().rposition(|&start| {
            dom.name_is(start, &W::bookmark_start()) && dom.attribute(start, &W::id()) == Some(id)
        }) else {
            continue;
        };
        let Some(&del) = children[start_at + 1..index]
            .iter()
            .rev()
            .find(|&&node| dom.name_is(node, &W::del()) && deletion_has_text(dom, node))
        else {
            continue;
        };
        moves.push((child, del));
    }
    for (end, del) in moves {
        dom.remove(end);
        dom.add(del, end);
    }
}

fn deletion_has_text(dom: &Dom, del: NodeId) -> bool {
    dom.descendants(del, Some(&W::del_text()))
        .iter()
        .any(|&text| !dom.value(text).is_empty())
}

/// Revision elements of a paragraph mark that delete it.
const DELETED: [&str; 2] = ["del", "moveFrom"];
/// Every revision element a paragraph mark can carry.
pub(super) const TRACKED: [&str; 4] = ["ins", "del", "moveFrom", "moveTo"];

/// Whether `paragraph`'s mark (`w:pPr/w:rPr`) carries one of `locals`.
fn mark_has(dom: &Dom, paragraph: NodeId, locals: &[&str]) -> bool {
    mark_revision(dom, paragraph, locals).is_some()
}

/// The revision among `locals` that `paragraph`'s mark (`w:pPr/w:rPr`)
/// carries.
fn mark_revision(dom: &Dom, paragraph: NodeId, locals: &[&str]) -> Option<NodeId> {
    let rpr = dom
        .element(paragraph, &W::p_pr())
        .and_then(|ppr| dom.element(ppr, &W::r_pr()))?;
    dom.elements(rpr, None).into_iter().find(|&c| {
        dom.name(c)
            .is_some_and(|n| n.namespace_name() == W::URI && locals.contains(&n.local_name()))
    })
}

/// `paragraph`'s tracked properties change (`w:pPr/w:pPrChange`).
fn paragraph_format_change(dom: &Dom, paragraph: NodeId) -> Option<NodeId> {
    dom.element(paragraph, &W::p_pr())
        .and_then(|ppr| dom.element(ppr, &W::p_pr_change()))
}

/// The change record of the container-final `paragraph` that operation
/// `op`'s new paragraph would have to replace when it takes the final mark
/// (`keep_final_mark`): its `w:pPrChange` when the new properties differ,
/// or its mark's `w:rPrChange` when the mark's formatting goes (a new
/// paragraph's mark carries none, as `build_paragraph` writes it).
fn record_replaced(tx: &Transaction<'_>, paragraph: NodeId, op: usize) -> Option<NodeId> {
    let dom = &tx.opened.dom;
    let Some((
        _,
        Resolved::InsertParagraph {
            like, style, toc, ..
        },
    )) = tx.resolved.iter().find(|(i, _)| *i == op)
    else {
        return None;
    };
    let like = tx.paragraph_nodes[*like];
    let style_of = |p: NodeId| {
        dom.element(p, &W::p_pr())
            .and_then(|ppr| dom.element(ppr, &W::p_style()))
            .and_then(|s| dom.attribute(s, &W::val()))
    };
    let unstyled = |p: NodeId| -> Vec<String> {
        let children: Vec<NodeId> = bare_properties(dom, p)
            .into_iter()
            .filter(|&c| !dom.name_is(c, &W::p_style()))
            .collect();
        serialized(dom, &children)
    };
    // A table of contents ends on its field paragraph, which has no
    // properties (`toc_paragraphs`).
    let same = match toc {
        Some(_) => style_of(paragraph).is_none() && unstyled(paragraph).is_empty(),
        None => {
            style.as_deref().or(style_of(like)) == style_of(paragraph)
                && unstyled(like) == unstyled(paragraph)
        }
    };
    let properties = paragraph_format_change(dom, paragraph).filter(|_| !same);
    let mark = dom
        .element(paragraph, &W::p_pr())
        .and_then(|ppr| dom.element(ppr, &W::r_pr()))
        .and_then(|rpr| dom.element(rpr, &W::r_pr_change()))
        .filter(|_| !mark_formatting(dom, paragraph).is_empty());
    properties.or(mark)
}

/// The first element `paragraph` holds, at the levels `delete_runs` walks,
/// that the replay cannot delete as a tracked change: one that is no
/// WordprocessingML (an equation, `m:oMath`), or a subdocument (`w:subDoc`,
/// #1670, round 8).
fn untrackable_content(dom: &Dom, container: NodeId) -> Option<NodeId> {
    dom.elements(container, None).into_iter().find_map(|child| {
        let name = dom.name(child)?;
        if name.namespace_name() != W::URI {
            return Some(child);
        }
        match name.local_name() {
            "subDoc" => Some(child),
            "hyperlink" | "smartTag" | "customXml" | "dir" | "bdo" | "fldSimple" | "ins" => {
                untrackable_content(dom, child)
            }
            "sdt" => dom
                .element(child, &W::sdt_content())
                .and_then(|content| untrackable_content(dom, content)),
            _ => None,
        }
    })
}

/// The first move (`w:moveFrom`, `w:moveTo`) `node` holds, in its runs or
/// its mark.
fn moved_in(dom: &Dom, node: NodeId) -> Option<NodeId> {
    dom.descendants(node, None)
        .into_iter()
        .find(|&n| is_one_of(dom, n, &["moveFrom", "moveTo"]))
}

/// The block right after `node` among its siblings, past the range markers
/// and proofing marks between: a paragraph, a table, a block content
/// control or a custom XML element. Word's Compare deletes the mark of a
/// paragraph deleted before any of them (#1670, round 8).
fn next_block(dom: &Dom, node: NodeId) -> Option<NodeId> {
    dom.nodes_after_self(node).into_iter().find(|&n| {
        dom.name(n).is_some_and(|name| {
            name.namespace_name() == W::URI
                && matches!(name.local_name(), "p" | "tbl" | "sdt" | "customXml")
        })
    })
}

/// The paragraph right before `node` among its siblings.
fn previous_paragraph(dom: &Dom, node: NodeId) -> Option<NodeId> {
    dom.nodes_before_self(node)
        .into_iter()
        .rev()
        .find(|&n| dom.is_element(n))
        .filter(|&n| dom.name_is(n, &W::p()))
}

/// `paragraph`'s mark properties (`w:pPr/w:rPr`), created when missing.
fn mark_properties(dom: &mut Dom, paragraph: NodeId) -> NodeId {
    let ppr = paragraph_properties(dom, paragraph);
    match dom.element(ppr, &W::r_pr()) {
        Some(rpr) => rpr,
        None => {
            let rpr = dom.new_element(W::r_pr());
            insert_ppr_child(dom, ppr, rpr);
            rpr
        }
    }
}

/// `paragraph`'s `w:pPr`, created when missing.
fn paragraph_properties(dom: &mut Dom, paragraph: NodeId) -> NodeId {
    match dom.element(paragraph, &W::p_pr()) {
        Some(ppr) => ppr,
        None => {
            let ppr = dom.new_element(W::p_pr());
            dom.add_first(paragraph, ppr);
            ppr
        }
    }
}

/// Mark `paragraph`'s mark inserted or deleted (`w:pPr/w:rPr/w:ins|w:del`,
/// first in `w:rPr` as the schema orders them, a deletion after an
/// insertion). A mark already deleted stays as it is.
fn mark(dom: &mut Dom, paragraph: NodeId, name: XName, stamp: &mut Stamp<'_>) {
    let deleting = name == W::del();
    if deleting && mark_has(dom, paragraph, &DELETED) {
        return;
    }
    let rpr = mark_properties(dom, paragraph);
    let node = stamp.container(dom, name);
    match dom.element(rpr, &W::ins()) {
        Some(ins) if deleting => dom.add_after_self(ins, node),
        _ => dom.add_first(rpr, node),
    }
}

/// A copy of a run's `w:rPr` for new text: without another revision's
/// format change or mark revisions, whose ids must stay unique.
pub(super) fn clean_rpr(dom: &mut Dom, rpr: NodeId) -> NodeId {
    let copy = dom.clone_subtree(rpr);
    strip_records(dom, copy);
    copy
}

/// Remove the format change and mark revisions from `rpr`.
pub(super) fn strip_records(dom: &mut Dom, rpr: NodeId) {
    for child in dom.elements(rpr, None) {
        if dom.name_is(child, &W::r_pr_change())
            || dom.name_is(child, &W::ins())
            || dom.name_is(child, &W::del())
            || dom.name_is(child, &W::move_from())
            || dom.name_is(child, &W::move_to())
        {
            dom.remove(child);
        }
    }
}

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

    /// A counter whose next id is `next`.
    pub(super) fn counter_at(next: u64) -> RevisionCounter {
        match next.checked_sub(1) {
            Some(below) => RevisionCounter::new(HashSet::from([below])),
            None => RevisionCounter::default(),
        }
    }

    fn paragraph(dom: &mut Dom, xml: &str) -> NodeId {
        let document = dom.parse_xdocument(&format!(r#"<w:p xmlns:w="{}">{xml}</w:p>"#, W::URI));
        dom.root(document).expect("root")
    }

    #[test]
    fn isolate_moves_neighbours_into_copies() {
        let mut dom = Dom::new();
        let p = paragraph(
            &mut dom,
            "<w:r><w:rPr><w:b/></w:rPr><w:tab/><w:t>a</w:t><w:br/></w:r>",
        );
        let run = dom.elements(p, Some(&W::r()))[0];
        let t = dom.element(run, &W::t()).expect("t");
        isolate(&mut dom, run, t, &mut RevisionCounter::default());
        let runs = dom.elements(p, Some(&W::r()));
        assert_eq!(runs.len(), 3);
        assert!(alone(&dom, runs[1], t));
        for r in runs {
            assert!(dom.element(r, &W::r_pr()).is_some(), "each copy keeps rPr");
        }
        // Already alone: nothing changes.
        let before = dom.elements(p, None).len();
        isolate(&mut dom, run, t, &mut RevisionCounter::default());
        assert_eq!(dom.elements(p, None).len(), before);
    }

    #[test]
    fn a_deleted_mark_goes_after_an_inserted_one_and_is_not_doubled() {
        let mut dom = Dom::new();
        let p = paragraph(&mut dom, "<w:r><w:t>x</w:t></w:r>");
        let mut stamp = Stamp {
            author: "Me",
            date: "2026-10-02T00:00:00Z",
            ids: counter_at(5),
            insertions: HashMap::new(),
        };
        mark(&mut dom, p, W::ins(), &mut stamp);
        mark(&mut dom, p, W::del(), &mut stamp);
        mark(&mut dom, p, W::del(), &mut stamp);
        let rpr = dom
            .element(dom.element(p, &W::p_pr()).expect("pPr"), &W::r_pr())
            .expect("rPr");
        let names: Vec<_> = dom
            .elements(rpr, None)
            .into_iter()
            .map(|c| dom.name(c).expect("name").local_name().to_string())
            .collect();
        assert_eq!(names, ["ins", "del"]);
        assert_eq!(stamp.ids.own(), &BTreeSet::from([5, 6]));
        assert!(mark_has(&dom, p, &DELETED));
        assert!(moved_in(&dom, dom.elements(p, Some(&W::r()))[0]).is_none());
    }

    #[test]
    fn delete_runs_groups_siblings_and_recurses_into_hyperlinks() {
        let mut dom = Dom::new();
        let p = paragraph(
            &mut dom,
            r#"<w:r><w:t>a</w:t></w:r><w:r><w:t>b</w:t></w:r><w:bookmarkStart w:id="0" w:name="x"/><w:hyperlink><w:r><w:instrText>c</w:instrText></w:r></w:hyperlink><w:sdt><w:sdtContent><w:r><w:t>d</w:t></w:r></w:sdtContent></w:sdt>"#,
        );
        let mut stamp = Stamp {
            author: "Me",
            date: "d",
            ids: counter_at(0),
            insertions: HashMap::new(),
        };
        delete_runs(&mut dom, p, &mut stamp);
        assert_eq!(
            dom.elements(p, Some(&W::del())).len(),
            1,
            "a and b share one"
        );
        assert_eq!(dom.descendants(p, Some(&W::del())).len(), 3);
        assert_eq!(dom.descendants(p, Some(&W::del_text())).len(), 3);
        assert_eq!(dom.descendants(p, Some(&W::del_instr_text())).len(), 1);
        assert!(dom.descendants(p, Some(&W::t())).is_empty());
    }
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod nested_run_deletion_source_boundary_tests {
    use super::tests::counter_at;
    use super::*;
    fn semantic(dom: &Dom, node: NodeId) -> String {
        if !dom.is_element(node) {
            return dom.text_value(node).unwrap_or_default().into();
        }
        let name = dom.name(node).unwrap();
        let mut attrs = dom
            .attributes_iter(node)
            .filter(|(n, _)| {
                n.namespace_name() != "http://www.w3.org/2000/xmlns/" && n.local_name() != "xmlns"
            })
            .collect::<Vec<_>>();
        if attrs.is_empty()
            && dom.nodes(node).is_empty()
            && (name == W::r_pr() || name == W::p_pr())
        {
            return String::new();
        }
        attrs.sort_by_key(|(n, v)| {
            (
                n.namespace_name().to_string(),
                n.local_name().to_string(),
                *v,
            )
        });
        format!(
            "{:?}{attrs:?}[{}]",
            name,
            dom.nodes(node)
                .into_iter()
                .map(|n| semantic(dom, n))
                .filter(|s| !s.is_empty())
                .collect::<Vec<_>>()
                .join("|")
        )
    }
    #[test]
    fn deleting_supported_nested_run_containers_preserves_complete_rejected_sources() {
        let runs = "<w:r><w:rPr><w:b/><w:color w:val='123456'/></w:rPr><w:t>owned text</w:t><w:tab/><w:br w:type='page'/></w:r><w:r><w:rPr><w:i/></w:rPr><w:t>second run</w:t></w:r>";
        let field = "<w:r><w:fldChar w:fldCharType='begin'/></w:r><w:r><w:rPr><w:color w:val='234567'/></w:rPr><w:instrText xml:space='preserve'> REF Clause </w:instrText></w:r><w:r><w:fldChar w:fldCharType='separate'/></w:r><w:r><w:t>cached clause</w:t></w:r><w:r><w:fldChar w:fldCharType='end'/></w:r>";
        for content in [
            runs.to_owned(),
            format!("<w:hyperlink w:anchor='Clause'>{runs}</w:hyperlink>"),
            format!("<w:smartTag w:uri='urn:source' w:element='clause'>{runs}</w:smartTag>"),
            format!("<w:customXml w:uri='urn:source' w:element='clause'>{runs}</w:customXml>"),
            format!("<w:dir w:val='rtl'>{runs}</w:dir>"),
            format!("<w:bdo w:val='rtl'>{runs}</w:bdo>"),
            format!("<w:fldSimple w:instr=' DATE '>{runs}</w:fldSimple>"),
            format!(
                "<w:sdt><w:sdtPr><w:alias w:val='Clause'/><w:tag w:val='owner'/><w:id w:val='31'/><w:richText/></w:sdtPr><w:sdtContent>{runs}</w:sdtContent></w:sdt>"
            ),
            "<w:sdt><w:sdtPr><w:tag w:val='empty-owner'/><w:richText/></w:sdtPr></w:sdt>"
                .to_owned(),
            field.to_owned(),
            format!(
                "<w:hyperlink w:anchor='Clause'><w:sdt><w:sdtPr><w:tag w:val='nested-owner'/><w:id w:val='32'/><w:richText/></w:sdtPr><w:sdtContent>{runs}</w:sdtContent></w:sdt></w:hyperlink>"
            ),
        ] {
            let mut dom = Dom::new();
            let document = dom.parse_xdocument(&format!("<w:document xmlns:w='{}'><w:body><w:p><w:pPr><w:spacing w:after='120'/></w:pPr><w:bookmarkStart w:id='40' w:name='Clause'/>{content}<w:bookmarkEnd w:id='40'/></w:p><w:p><w:r><w:t>independent tail</w:t></w:r></w:p><w:sectPr><w:pgSz w:w='12240' w:h='15840'/></w:sectPr></w:body></w:document>",W::URI));
            let root = dom.root(document).unwrap();
            let expected = semantic(&dom, root);
            let paragraph = dom.descendants(root, Some(&W::p()))[0];
            let source_runs = dom.descendants(paragraph, Some(&W::r()));
            let mut stamp = Stamp {
                author: "New owner",
                date: "2001-02-03T04:05:06Z",
                ids: counter_at(100),
                insertions: HashMap::new(),
            };
            delete_runs(&mut dom, paragraph, &mut stamp);
            mark(&mut dom, paragraph, W::del(), &mut stamp);
            for run in source_runs {
                let owner = dom.parent(run).unwrap();
                assert!(dom.name_is(owner, &W::del()), "run owner: {content}");
                assert_eq!(dom.attribute(owner, &W::author()), Some("New owner"));
                assert_eq!(
                    dom.attribute(owner, &W::date()),
                    Some("2001-02-03T04:05:06Z")
                );
                assert!(dom.descendants(run, Some(&W::t())).is_empty());
                assert!(dom.descendants(run, Some(&W::instr_text())).is_empty());
            }
            let result = crate::revision_processor::reject_revisions_document(&mut dom, root);
            assert_eq!(
                semantic(&dom, result),
                expected,
                "nested container complete source recovery: {content}"
            );
        }
    }
}
#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod tracked_source_property_boundary_tests {
    use super::tests::counter_at;
    use super::*;

    fn paragraph(dom: &mut Dom, content: &str) -> NodeId {
        let doc = dom.parse_xdocument(&format!(r#"<w:p xmlns:w="{}">{content}</w:p>"#, W::URI));
        dom.root(doc).unwrap()
    }

    #[test]
    fn property_history_keeps_original_layout_and_leaves_live_marks_and_sections_on_the_source() {
        for shell in [
            "",
            "<w:pPr/>",
            "<w:pPr><w:pStyle w:val=\"BodyText\"/><w:spacing w:after=\"240\"/><w:rPr><w:b/><w:ins w:id=\"7\" w:author=\"Old\" w:date=\"2026-01-01T00:00:00Z\"/></w:rPr><w:sectPr><w:pgSz w:w=\"12240\" w:h=\"15840\"/></w:sectPr></w:pPr>",
        ] {
            let mut dom = Dom::new();
            let p = paragraph(
                &mut dom,
                &format!("{shell}<w:r><w:rPr><w:i/></w:rPr><w:t>owned</w:t></w:r>"),
            );
            let run = dom.element(p, &W::r()).unwrap();
            let frozen_run = dom.serialize_element(run);
            let original_ppr = dom.element(p, &W::p_pr());
            let mark = original_ppr
                .and_then(|node| dom.element(node, &W::r_pr()))
                .map(|node| dom.serialize_element(node));
            let section = original_ppr
                .and_then(|node| dom.element(node, &W::sect_pr()))
                .map(|node| dom.serialize_element(node));
            let mut stamp = Stamp {
                author: "Ada",
                date: "2026-10-09T12:34:00Z",
                ids: counter_at(50),
                insertions: HashMap::new(),
            };
            track_properties(&mut dom, p, &mut stamp, |dom| {
                let ppr = paragraph_properties(dom, p);
                let jc = dom.new_element(W::jc_el());
                dom.set_attribute_value(jc, &W::val(), Some("right"));
                insert_ppr_child(dom, ppr, jc);
            });
            let ppr = dom.element(p, &W::p_pr()).unwrap();
            let record = dom.element(ppr, &W::p_pr_change()).unwrap();
            assert_eq!(dom.attribute(record, &W::id()), Some("50"));
            assert_eq!(dom.attribute(record, &W::author()), Some("Ada"));
            assert_eq!(
                dom.attribute(record, &W::date()),
                Some("2026-10-09T12:34:00Z")
            );
            let old = dom.element(record, &W::p_pr()).unwrap();
            assert!(dom.element(old, &W::r_pr()).is_none());
            assert!(dom.element(old, &W::sect_pr()).is_none());
            assert!(dom.element(old, &W::jc_el()).is_none());
            if shell.contains("BodyText") {
                assert_eq!(
                    dom.attribute(dom.element(old, &W::p_style()).unwrap(), &W::val()),
                    Some("BodyText")
                );
                assert_eq!(
                    dom.attribute(
                        dom.element(old, &W::spacing_el()).unwrap(),
                        &W::name("after")
                    ),
                    Some("240")
                );
                assert_eq!(dom.elements(old, None).len(), 2);
            } else {
                assert!(dom.elements(old, None).is_empty());
            }
            assert_eq!(
                dom.element(ppr, &W::r_pr())
                    .map(|node| dom.serialize_element(node)),
                mark
            );
            assert_eq!(
                dom.element(ppr, &W::sect_pr())
                    .map(|node| dom.serialize_element(node)),
                section
            );
            assert_eq!(dom.serialize_element(run), frozen_run);
            let frozen_record = dom.serialize_element(record);
            track_properties(&mut dom, p, &mut stamp, |dom| {
                let ppr = paragraph_properties(dom, p);
                let jc = dom.element(ppr, &W::jc_el()).unwrap();
                dom.set_attribute_value(jc, &W::val(), Some("center"));
            });
            assert_eq!(dom.serialize_element(record), frozen_record);
            assert_eq!(stamp.ids.own(), &BTreeSet::from([50]));
            assert_eq!(dom.elements(ppr, Some(&W::p_pr_change())).len(), 1);
            assert_eq!(dom.serialize_element(run), frozen_run);
        }
    }

    #[test]
    fn inserted_run_format_clones_drop_only_foreign_change_records() {
        let mut dom = Dom::new();
        let p = paragraph(
            &mut dom,
            r#"<w:r><w:rPr><w:b/><w:i/><w:color w:val="123456"/><w:sz w:val="24"/><w:rPrChange w:id="3" w:author="Old" w:date="2026-01-01T00:00:00Z"><w:rPr><w:u w:val="single"/></w:rPr></w:rPrChange></w:rPr><w:t>owned</w:t></w:r>"#,
        );
        let rpr = dom
            .element(dom.element(p, &W::r()).unwrap(), &W::r_pr())
            .unwrap();
        let frozen = dom.serialize_element(p);
        let copy = clean_rpr(&mut dom, rpr);
        let expected_p = paragraph(
            &mut dom,
            r#"<w:r><w:rPr><w:b/><w:i/><w:color w:val="123456"/><w:sz w:val="24"/></w:rPr></w:r>"#,
        );
        let expected = dom
            .element(dom.element(expected_p, &W::r()).unwrap(), &W::r_pr())
            .unwrap();
        assert_eq!(dom.serialize_element(copy), dom.serialize_element(expected));
        assert_eq!(dom.serialize_element(p), frozen);
        let second = clean_rpr(&mut dom, copy);
        assert_eq!(
            dom.serialize_element(second),
            dom.serialize_element(expected)
        );
    }
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod public_adjacent_run_replacement_owner_tests {
    use super::*;
    use crate::changes::ChangeKind;
    use crate::edit::deeper_boundary_fixture::docx;

    fn tree(dom: &Dom, node: NodeId) -> String {
        if !dom.is_element(node) {
            return dom.text_value(node).unwrap_or_default().into();
        }
        let mut attrs = dom
            .attributes_iter(node)
            .filter(|(n, _)| {
                n.namespace_name() != "http://www.w3.org/2000/xmlns/" && n.local_name() != "xmlns"
            })
            .collect::<Vec<_>>();
        attrs.sort_by_key(|(n, v)| (n.namespace_name().to_owned(), n.local_name().to_owned(), *v));
        format!(
            "{:?}{attrs:?}[{}]",
            dom.name(node),
            dom.nodes(node)
                .into_iter()
                .map(|n| tree(dom, n))
                .collect::<Vec<_>>()
                .join("|")
        )
    }
    fn source_events(bytes: &[u8]) -> Vec<String> {
        let pkg = crate::opc::PartFs::open(bytes).unwrap();
        let mut dom = Dom::new();
        let doc = dom.parse_xdocument(&pkg.part_string("/word/document.xml").unwrap());
        let root = dom.root(doc).unwrap();
        let body = dom.element(root, &W::body()).unwrap();
        let mut out = Vec::new();
        for child in dom.elements(body, None) {
            if !dom.name_is(child, &W::p()) {
                out.push(tree(&dom, child));
                continue;
            }
            out.push("paragraph".into());
            for node in dom.elements(child, None) {
                if !dom.name_is(node, &W::r()) {
                    out.push(tree(&dom, node));
                    continue;
                }
                let props = dom
                    .element(node, &W::r_pr())
                    .map(|n| tree(&dom, n))
                    .unwrap_or_default();
                for content in dom.elements(node, None) {
                    if dom.name_is(content, &W::r_pr()) {
                        continue;
                    }
                    if dom.name_is(content, &W::t()) {
                        out.extend(dom.value(content).chars().map(|ch| format!("{ch}:{props}")));
                    } else {
                        out.push(format!("{}:{props}", tree(&dom, content)));
                    }
                }
            }
            out.push("end paragraph".into());
        }
        out
    }
    #[test]
    fn public_keep_replacement_across_adjacent_unicode_runs_restores_every_source_owner() {
        let ppr = "<w:pPr><w:spacing w:before='120' w:after='80'/><w:ind w:left='180'/></w:pPr>";
        let first = "<w:rPr><w:b/><w:color w:val='123456'/><w:sz w:val='22'/></w:rPr>";
        for second in [
            first,
            "<w:rPr><w:i/><w:color w:val='654321'/><w:sz w:val='28'/></w:rPr>",
        ] {
            for split_text in [false, true] {
                let lhs = if split_text {
                    "<w:t>αfirst</w:t><w:t>part</w:t>"
                } else {
                    "<w:t>αfirstpart</w:t>"
                };
                let body = format!(
                    "<w:p>{ppr}<w:r>{first}{lhs}</w:r><w:r>{second}<w:t>secondΖ</w:t></w:r><w:r><w:rPr><w:u w:val='single'/></w:rPr><w:tab/><w:t>tail</w:t><w:br/></w:r></w:p>"
                );
                let source = docx(&body);
                let frozen = source.clone();
                let plan = r#"{"schema_version":1,"author":"Source owner","date":"2001-02-03T04:05:06Z","existing_revisions":"keep","operations":[{"kind":"replace","paragraph":"p0","find":"firstpartsecond","replacement":"owned replacement"}]}"#;
                let out = crate::edit::apply_plan_json(&source, plan).unwrap();
                let expected = docx(&format!(
                    "<w:p>{ppr}<w:r>{first}<w:t>αowned replacement</w:t></w:r><w:r>{second}<w:t>Ζ</w:t></w:r><w:r><w:rPr><w:u w:val='single'/></w:rPr><w:tab/><w:t>tail</w:t><w:br/></w:r></w:p>"
                ));
                let accepted = crate::changes::accept_changes(
                    &out.redline,
                    &crate::changes::ChangeFilter::default(),
                )
                .unwrap();
                let rejected = crate::changes::reject_changes(
                    &out.redline,
                    &crate::changes::ChangeFilter::default(),
                )
                .unwrap();
                assert_eq!(
                    source_events(&rejected),
                    source_events(&source),
                    "all rejected glyphs/properties/controls/section owners"
                );
                assert_eq!(
                    source_events(&accepted),
                    source_events(&expected),
                    "replacement inherits first deleted source format only"
                );
                assert_eq!(source_events(&out.clean), source_events(&expected));
                assert_eq!(source, frozen);
                let changes = crate::changes::list_changes(&out.redline).unwrap();
                assert_eq!(
                    changes
                        .iter()
                        .map(|c| (c.kind, c.text.as_str(), c.author.as_deref()))
                        .collect::<Vec<_>>(),
                    [
                        (
                            ChangeKind::Deletion,
                            "firstpartsecond",
                            Some("Source owner")
                        ),
                        (
                            ChangeKind::Insertion,
                            "owned replacement",
                            Some("Source owner")
                        )
                    ]
                );
            }
        }
    }
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod writer_branch_tests {
    use super::super::deeper_boundary_fixture::{docx, part_string};
    use super::super::{EditPlan, apply_plan};

    fn redline(body: &str, ops: serde_json::Value) -> String {
        let mut value = serde_json::json!({
            "schema_version": 1,
            "author": "Ed",
            "date": "2026-02-02T00:00:00Z",
        });
        value["operations"] = ops;
        let plan = EditPlan::from_json(&value.to_string()).unwrap();
        let result = apply_plan(&docx(body), &plan).unwrap_or_else(|e| panic!("{e:?}"));
        part_string(&result.redline, "word/document.xml").unwrap()
    }

    #[test]
    fn formatted_words_typed_into_an_empty_paragraph_are_an_insertion() {
        let xml = redline(
            r#"<w:p><w:pPr><w:rPr><w:i/></w:rPr></w:pPr></w:p>"#,
            serde_json::json!([{"kind": "insert", "paragraph": "p0", "position": "end", "text": "New", "format": {"bold": true}}]),
        );
        let ins = &xml[xml.find("<w:ins ").unwrap()..xml.find("</w:ins>").unwrap()];
        assert!(ins.contains("New") && ins.contains("<w:b />"), "{ins}");
    }

    #[test]
    fn a_merge_separator_takes_the_head_runs_formatting() {
        let xml = redline(
            r#"<w:p><w:r><w:rPr><w:b/></w:rPr><w:t>Head</w:t></w:r></w:p><w:p><w:r><w:t>Tail</w:t></w:r></w:p>"#,
            serde_json::json!([{"kind": "merge_paragraphs", "paragraph": "p0", "separator": " - "}]),
        );
        let ins = &xml[xml.find("<w:ins ").unwrap()..];
        let ins = &ins[..ins.find("</w:ins>").unwrap()];
        assert!(ins.contains(" - ") && ins.contains("<w:b />"), "{ins}");
        assert!(xml.contains("<w:del "), "the head's mark is deleted: {xml}");
    }

    #[test]
    fn a_formatted_replacement_carries_its_format_on_the_new_words() {
        let xml = redline(
            r#"<w:p><w:r><w:t>Alpha beta</w:t></w:r></w:p>"#,
            serde_json::json!([{"kind": "replace", "paragraph": "p0", "find": "beta", "replacement": "gamma", "format": {"italic": true}}]),
        );
        let ins = &xml[xml.find("<w:ins ").unwrap()..xml.find("</w:ins>").unwrap()];
        assert!(ins.contains("gamma") && ins.contains("<w:i />"), "{ins}");
    }
}