jubarte-doc 0.20.0

The document operations of the jubarte engine above compare: changes, comments, validation, audit, scrub, append, fields and the debug views
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
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
//
// SPDX-License-Identifier: Apache-2.0

//! `jubarte debug FILE --check render`: what each story part should put on
//! the page, for diagnosing a low score against Word's PDF. Every story part
//! is listed (body, headers, footers, notes, comments; text boxes inside
//! them), not only `word/document.xml`, and a `(layout)` entry adds what
//! jubarte's own layout pass makes of the package: pages and the face each
//! requested font resolved to.

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

use super::{
    Package, TEXT_PARTS, attr, clip, decode_xml, local, percent_decode, rels_of, resolve_target,
    story_roles_all,
};
use crate::xmllinq::{Dom, NodeId};

/// Sample text kept per aggregated line.
const SAMPLE: usize = 40;
/// Longest first-row text a table line keeps.
const FIRST_ROW: usize = 60;
/// Deepest `basedOn` chain followed.
const CHAIN: usize = 16;

/// Part name → (lines, paragraph or page count), the `(layout)` entry last.
pub(super) fn render_parts(
    pkg: &Package,
    part: Option<&str>,
) -> BTreeMap<String, (Vec<String>, usize)> {
    let rels = main_rels(pkg);
    let styles = rels
        .iter()
        .find(|r| r.kind == "styles")
        .and_then(|r| parse(pkg, &r.part))
        .map(|(dom, root)| StyleBook::new(&dom, root))
        .unwrap_or_default();
    let roles = story_roles_all(pkg);
    let mut map = BTreeMap::new();
    for e in &pkg.entries {
        if part.is_some_and(|p| !e.name.contains(p)) || !e.name.ends_with(".xml") {
            continue;
        }
        let Some((dom, root)) = parse(pkg, &e.name) else {
            continue;
        };
        if !TEXT_PARTS.contains(&local(&dom, root).as_str()) {
            continue;
        }
        let mut lines: Vec<String> = roles
            .get(&e.name)
            .into_iter()
            .flatten()
            .map(|r| format!("  shown as {r}"))
            .collect();
        let paras = part_lines(&dom, root, &styles, &rels, &mut lines);
        map.insert(e.name.clone(), (lines, paras));
    }
    if part.is_none_or(|p| "(layout)".contains(p)) {
        map.insert("(layout)".to_string(), layout_lines(pkg, &rels));
    }
    map
}

/// A relationship of the main document part: its type's last segment
/// (`styles`, `theme`, `header`, …), id and target part.
struct Rel {
    kind: String,
    id: String,
    part: String,
}

/// The main document part's relationships, found through the package's own
/// `officeDocument` relationship (the part need not be `word/document.xml`).
fn main_rels(pkg: &Package) -> Vec<Rel> {
    let rels_in = |rels: &str| -> Vec<Rel> {
        let Some((dom, root)) = parse(pkg, rels) else {
            return Vec::new();
        };
        dom.elements(root, None)
            .into_iter()
            .filter(|&r| attr(&dom, r, "TargetMode") != "External")
            .map(|r| Rel {
                kind: attr(&dom, r, "Type")
                    .rsplit('/')
                    .next()
                    .unwrap_or_default()
                    .to_string(),
                id: attr(&dom, r, "Id"),
                part: resolve_target(rels, &percent_decode(&attr(&dom, r, "Target"))),
            })
            .collect()
    };
    let Some(main) = rels_in("_rels/.rels")
        .into_iter()
        .find(|r| r.kind == "officeDocument")
    else {
        return Vec::new();
    };
    rels_in(&rels_of(&main.part))
}

fn parse(pkg: &Package, name: &str) -> Option<(Dom, NodeId)> {
    let e = pkg.entries.iter().find(|e| e.name == name)?;
    let xml = decode_xml(&e.data)?;
    let mut dom = Dom::new();
    let doc = dom.parse_xdocument(&xml);
    let root = dom.root(doc)?;
    // Strict files write universal measures (`612pt`) where Transitional
    // writes integer twips: list them alike. A plain value stays as the
    // file spells it — the diagnostic reports, it does not repair.
    crate::comparer::finalize::normalize_unit_measures(&mut dom, root);
    Some((dom, root))
}

/// What a style contributes to the page, its `basedOn` parent and name.
#[derive(Default)]
struct StyleInfo {
    name: String,
    based: String,
    /// The style's own paragraph fill: `Some(None)` for a nil `shd`, which
    /// stops its base's.
    para_shd: Option<Option<String>>,
    /// The style's own run colour, highlight and shading: `None` when it
    /// sets none (its base's shows), `Some(None)` when it resets them
    /// (`auto`, `none`, a nil `shd`).
    color: Option<Option<String>>,
    highlight: Option<Option<String>>,
    run_shd: Option<Option<String>>,
    /// The style's own `w:vanish`: `Some(true)` hides, `Some(false)` turns
    /// a base's off.
    vanish: Option<bool>,
    /// The style's own `w:framePr` attributes, `name=value`.
    frame: Vec<(String, String)>,
    /// The face each slot of the style's own `w:rFonts` asks for.
    fonts: Vec<(&'static str, String)>,
    /// Table-wide and conditional shading: `whole` (`tblPr`), `cell`
    /// (`tcPr`), or the `tblStylePr` type; `None` for a nil `shd`, which
    /// resets the base's.
    table_shd: Vec<(String, Option<String>)>,
}

#[derive(Default)]
struct StyleBook {
    by_id: HashMap<String, StyleInfo>,
    /// What `docDefaults` paint: the layer under every style.
    defaults: StyleInfo,
    /// `docDefaults` run fonts, `name=value` sorted.
    default_fonts: Option<String>,
    /// The `w:default="1"` paragraph style: an unstyled paragraph's.
    default_para: String,
}

impl StyleBook {
    fn new(dom: &Dom, root: NodeId) -> Self {
        let mut book = StyleBook::default();
        for s in dom.elements(root, None) {
            match local(dom, s).as_str() {
                "docDefaults" => {
                    let rpr = descendant(dom, s, "rPrDefault").and_then(|d| child(dom, d, "rPr"));
                    book.defaults = StyleInfo {
                        name: "docDefaults".to_string(),
                        para_shd: descendant(dom, s, "pPrDefault")
                            .and_then(|d| child(dom, d, "pPr"))
                            .and_then(|p| child(dom, p, "shd"))
                            .map(|x| shd(dom, x)),
                        color: rpr.and_then(|r| color(dom, r)),
                        highlight: rpr.and_then(|r| highlight(dom, r)),
                        run_shd: rpr.and_then(|r| run_shd(dom, r)),
                        vanish: rpr
                            .and_then(|r| child(dom, r, "vanish").map(|_| on(dom, r, "vanish"))),
                        ..StyleInfo::default()
                    };
                    book.default_fonts = descendant(dom, s, "rFonts").map(|f| {
                        let mut items: Vec<String> = dom
                            .attributes_iter(f)
                            .map(|(k, v)| format!("{}={v}", k.local_name()))
                            .collect();
                        items.sort();
                        items.join(" ")
                    });
                }
                "style" => {
                    let val = |name: &str| child(dom, s, name).map(|c| attr(dom, c, "val"));
                    let rpr = child(dom, s, "rPr");
                    let mut info = StyleInfo {
                        name: val("name").unwrap_or_default(),
                        based: val("basedOn").unwrap_or_default(),
                        para_shd: child(dom, s, "pPr")
                            .and_then(|p| child(dom, p, "shd"))
                            .map(|x| shd(dom, x)),
                        color: rpr.and_then(|r| color(dom, r)),
                        highlight: rpr.and_then(|r| highlight(dom, r)),
                        run_shd: rpr.and_then(|r| run_shd(dom, r)),
                        vanish: rpr
                            .and_then(|r| child(dom, r, "vanish").map(|_| on(dom, r, "vanish"))),
                        frame: child(dom, s, "pPr")
                            .and_then(|p| child(dom, p, "framePr"))
                            .map(|f| attr_pairs(dom, f))
                            .unwrap_or_default(),
                        fonts: rpr
                            .and_then(|r| child(dom, r, "rFonts"))
                            .map(|f| font_slots(dom, f))
                            .unwrap_or_default(),
                        table_shd: Vec::new(),
                    };
                    let mut table = |label: String, holder: NodeId| {
                        for (pr, what) in [("tblPr", "whole"), ("tcPr", "cell")] {
                            if let Some(fill) = child(dom, holder, pr)
                                .and_then(|p| child(dom, p, "shd"))
                                .map(|x| shd(dom, x))
                            {
                                let key = if label.is_empty() {
                                    what.to_string()
                                } else {
                                    label.clone()
                                };
                                info.table_shd.push((key, fill));
                            }
                        }
                    };
                    table(String::new(), s);
                    for cond in kids(dom, s, "tblStylePr") {
                        table(attr(dom, cond, "type"), cond);
                    }
                    let id = attr(dom, s, "styleId");
                    if attr(dom, s, "type") == "paragraph"
                        && matches!(attr(dom, s, "default").as_str(), "1" | "true" | "on")
                    {
                        book.default_para.clone_from(&id);
                    }
                    book.by_id.insert(id, info);
                }
                _ => {}
            }
        }
        book
    }

    fn name(&self, id: &str) -> String {
        self.by_id
            .get(id)
            .map(|s| s.name.clone())
            .filter(|n| !n.is_empty())
            .unwrap_or_else(|| id.to_string())
    }

    /// `id`'s table shading down its `basedOn` chain: a base's conditions
    /// stay unless the derived style sets the same one; a nil `shd` resets
    /// it.
    fn table_shd(&self, id: &str) -> Vec<(String, String)> {
        let mut chain = Vec::new();
        let mut id = id.to_string();
        for _ in 0..CHAIN {
            let Some(s) = self.by_id.get(&id) else {
                break;
            };
            chain.push(s);
            if s.based.is_empty() {
                break;
            }
            id = s.based.clone();
        }
        let mut out: Vec<(String, Option<String>)> = Vec::new();
        for s in chain.into_iter().rev() {
            for (key, fill) in &s.table_shd {
                match out.iter_mut().find(|(k, _)| k == key) {
                    Some(slot) => slot.1.clone_from(fill),
                    None => out.push((key.clone(), fill.clone())),
                }
            }
        }
        out.into_iter()
            .filter_map(|(k, fill)| Some((k, fill?)))
            .collect()
    }

    /// The faces `id`'s `basedOn` chain asks for, slot by slot, each with
    /// the name of the style that sets it (a derived style's slot over its
    /// base's, as the converter overlays them).
    fn font_slots(&self, id: &str) -> Vec<(&'static str, String, String)> {
        let mut chain = Vec::new();
        let mut cur = id.to_string();
        for _ in 0..CHAIN {
            let Some(s) = self.by_id.get(&cur) else {
                break;
            };
            chain.push((s, self.name(&cur)));
            if s.based.is_empty() {
                break;
            }
            cur = s.based.clone();
        }
        let mut out: Vec<(&'static str, String, String)> = Vec::new();
        for (s, name) in chain.into_iter().rev() {
            for (slot, face) in &s.fonts {
                out.retain(|(k, _, _)| k != slot);
                out.push((slot, face.clone(), name.clone()));
            }
        }
        out
    }

    /// `id`'s frame attributes down its `basedOn` chain, a derived
    /// style's value over its base's.
    fn frame(&self, id: &str) -> Vec<(String, String)> {
        let mut chain = Vec::new();
        let mut id = id.to_string();
        for _ in 0..CHAIN {
            let Some(s) = self.by_id.get(&id) else {
                break;
            };
            chain.push(s);
            if s.based.is_empty() {
                break;
            }
            id = s.based.clone();
        }
        let mut out: Vec<(String, String)> = Vec::new();
        for s in chain.into_iter().rev() {
            merge_pairs(&mut out, &s.frame);
        }
        out
    }

    /// The paragraph style `id` names, or the default one.
    fn para_style(&self, id: String) -> String {
        if id.is_empty() {
            self.default_para.clone()
        } else {
            id
        }
    }

    /// The first value `f` finds walking `id`'s `basedOn` chain, with the
    /// name of the style that holds it.
    fn find<T>(&self, id: &str, f: impl Fn(&StyleInfo) -> Option<T>) -> Option<(T, String)> {
        let mut id = id.to_string();
        for _ in 0..CHAIN {
            let s = self.by_id.get(&id)?;
            if let Some(v) = f(s) {
                return Some((v, self.name(&id)));
            }
            if s.based.is_empty() {
                return None;
            }
            id = s.based.clone();
        }
        None
    }
}

fn kids(dom: &Dom, n: NodeId, name: &str) -> Vec<NodeId> {
    dom.elements(n, None)
        .into_iter()
        .filter(|&c| local(dom, c) == name)
        .collect()
}

fn child(dom: &Dom, n: NodeId, name: &str) -> Option<NodeId> {
    dom.elements(n, None)
        .into_iter()
        .find(|&c| local(dom, c) == name)
}

fn descendant(dom: &Dom, n: NodeId, name: &str) -> Option<NodeId> {
    dom.descendants(n, None)
        .into_iter()
        .find(|&c| local(dom, c) == name)
}

/// The nearest ancestor of `n` named `name`.
fn nearest(dom: &Dom, n: NodeId, name: &str) -> Option<NodeId> {
    dom.ancestors(n, None)
        .into_iter()
        .find(|&a| local(dom, a) == name)
}

/// A `w:shd`'s visible fill: `fill` (or `theme:…`), the pattern's colour
/// after `/` when the pattern is not clear. `None` for no fill at all.
fn shd(dom: &Dom, x: NodeId) -> Option<String> {
    let (val, fill, theme, color) = (
        attr(dom, x, "val"),
        attr(dom, x, "fill"),
        attr(dom, x, "themeFill"),
        attr(dom, x, "color"),
    );
    if val == "nil" {
        return None;
    }
    if val == "solid" {
        return Some(if color.is_empty() || color == "auto" {
            "solid:auto".to_string()
        } else {
            color
        });
    }
    let base = if !theme.is_empty() {
        format!("theme:{theme}")
    } else if fill.is_empty() || fill == "auto" {
        String::new()
    } else {
        fill
    };
    let pattern = (!matches!(val.as_str(), "" | "clear")).then(|| format!("/{val}:{color}"));
    match (base.is_empty(), pattern) {
        (true, None) => None,
        (_, p) => Some(format!("{base}{}", p.unwrap_or_default())),
    }
}

/// `rPr`'s text colour: the value, or `theme:…` for a theme colour;
/// `Some(None)` for `auto`, which resets an inherited one.
fn color(dom: &Dom, rpr: NodeId) -> Option<Option<String>> {
    let c = child(dom, rpr, "color")?;
    let theme = attr(dom, c, "themeColor");
    if !theme.is_empty() {
        // Tint and shade change the ink the converter paints.
        let mut out = format!("theme:{theme}");
        for (name, label) in [("themeTint", "tint"), ("themeShade", "shade")] {
            let v = attr(dom, c, name);
            if !v.is_empty() {
                out.push_str(&format!("/{label}:{v}"));
            }
        }
        return Some(Some(out));
    }
    let val = attr(dom, c, "val");
    Some((!val.is_empty() && val != "auto").then_some(val))
}

/// `rPr`'s highlight; `Some(None)` for `none`, a reset.
fn highlight(dom: &Dom, rpr: NodeId) -> Option<Option<String>> {
    let val = attr(dom, child(dom, rpr, "highlight")?, "val");
    Some((!val.is_empty() && val != "none").then_some(val))
}

/// `rPr`'s shading; `Some(None)` for one that paints nothing (nil), a reset.
fn run_shd(dom: &Dom, rpr: NodeId) -> Option<Option<String>> {
    child(dom, rpr, "shd").map(|x| shd(dom, x))
}

/// A toggle property that is on (`w:val` absent, `1`, `true` or `on`).
fn on(dom: &Dom, pr: NodeId, name: &str) -> bool {
    child(dom, pr, name)
        .is_some_and(|c| !matches!(attr(dom, c, "val").as_str(), "0" | "false" | "off"))
}

/// Text `n` paints in its own paragraph: `w:t` and `w:delText` whose
/// nearest paragraph is `para` (a text box's paragraphs are their own).
fn own_text(dom: &Dom, n: NodeId, para: NodeId) -> String {
    let mut s = String::new();
    for t in dom.descendants(n, None) {
        if matches!(local(dom, t).as_str(), "t" | "delText") && nearest(dom, t, "p") == Some(para) {
            for c in dom.nodes(t) {
                s.push_str(dom.text_value(c).unwrap_or_default());
            }
        }
    }
    s
}

/// Each fact's stretches and the first one's text. A stretch is a run of
/// runs in one paragraph that share the fact: Word splits runs where
/// nothing on the page changes (`monospace` and `.`), so a fact counts
/// once until a run without it comes between.
#[derive(Default)]
struct Tally {
    facts: BTreeMap<String, (usize, String)>,
    /// The paragraph of the run being read, and the facts the run before
    /// it in that paragraph had and the facts it has so far.
    para: Option<NodeId>,
    before: BTreeSet<String>,
    now: BTreeSet<String>,
}

impl Tally {
    /// A run of `para` starts: the facts of the run before it carry on.
    fn next_run(&mut self, para: NodeId) {
        if self.para == Some(para) {
            self.before = std::mem::take(&mut self.now);
        } else {
            self.para = Some(para);
            self.before.clear();
            self.now.clear();
        }
    }

    fn add(&mut self, key: String, sample: &str) {
        self.now.insert(key.clone());
        if self.before.contains(&key) {
            return;
        }
        let e = self.facts.entry(key).or_default();
        if e.0 == 0 {
            e.1 = clip(sample, SAMPLE);
        }
        e.0 += 1;
    }

    fn lines(self, out: &mut Vec<String>) {
        for (key, (n, sample)) in self.facts {
            if sample.is_empty() {
                out.push(format!("  {key} ×{n}"));
            } else {
                out.push(format!("  {key} ×{n} \"{sample}\""));
            }
        }
    }
}

/// One story part's lines: sections, tables, frames and drawings in
/// document order, then the tallies. Returns the paragraph count.
fn part_lines(
    dom: &Dom,
    root: NodeId,
    styles: &StyleBook,
    rels: &[Rel],
    out: &mut Vec<String>,
) -> usize {
    let mut tally = Tally::default();
    let mut paras = 0;
    let mut tables = 0;
    let mut sections = 0;
    // Field codes being collected, innermost last: (code, recorded).
    let mut fields: Vec<(String, bool)> = Vec::new();
    for n in dom.descendants(root, None) {
        // Word renders an mc:AlternateContent's Choice; its Fallback is a
        // copy of the same content.
        if nearest(dom, n, "Fallback").is_some() {
            continue;
        }
        match local(dom, n).as_str() {
            "p" => {
                paras += 1;
                paragraph(dom, n, styles, &mut tally, out);
            }
            "r" => run(dom, n, styles, &mut tally),
            "tbl" => {
                tables += 1;
                out.push(table(dom, n, tables, styles));
            }
            // A `w:sectPrChange`'s old `w:sectPr` is history.
            "sectPr" if nearest(dom, n, "sectPrChange").is_none() => {
                sections += 1;
                out.push(section(dom, n, sections, rels));
            }
            "anchor" => out.push(anchor(dom, n)),
            "inline" => tally.add("inline".to_string(), ""),
            "pict" | "object" => tally.add(format!("vml {}", local(dom, n)), ""),
            "AlternateContent" => tally.add("AlternateContent".to_string(), ""),
            "fldSimple" => {
                let code = attr(dom, n, "instr");
                if let Some(w) = code.split_whitespace().next() {
                    tally.add(format!("field {}", w.to_uppercase()), "");
                }
            }
            "fldChar" => match attr(dom, n, "fldCharType").as_str() {
                "begin" => fields.push((String::new(), false)),
                kind => {
                    if let Some(top) = fields.last_mut()
                        && !top.1
                    {
                        top.1 = true;
                        if let Some(w) = top.0.split_whitespace().next() {
                            tally.add(format!("field {}", w.to_uppercase()), "");
                        }
                    }
                    if kind == "end" {
                        fields.pop();
                    }
                }
            },
            "instrText" | "delInstrText" => {
                if let Some(top) = fields.last_mut() {
                    for c in dom.nodes(n) {
                        top.0.push_str(dom.text_value(c).unwrap_or_default());
                    }
                }
            }
            _ => {}
        }
    }
    tally.lines(out);
    paras
}

/// A paragraph's style, shading, frame and the order of its revisions.
fn paragraph(dom: &Dom, p: NodeId, styles: &StyleBook, tally: &mut Tally, out: &mut Vec<String>) {
    let text = own_text(dom, p, p);
    let ppr = child(dom, p, "pPr");
    let named = ppr
        .and_then(|x| child(dom, x, "pStyle"))
        .map(|s| attr(dom, s, "val"))
        .unwrap_or_default();
    if !named.is_empty() {
        tally.add(format!("pstyle \"{}\"", styles.name(&named)), "");
    }
    let pstyle = styles.para_style(named);
    match ppr.and_then(|x| child(dom, x, "shd")).map(|x| shd(dom, x)) {
        Some(Some(fill)) => tally.add(format!("para-shd {fill}"), &text),
        Some(None) => {}
        None => match styles.find(&pstyle, |s| s.para_shd.clone()) {
            Some((Some(fill), name)) => tally.add(format!("para-shd {fill} via \"{name}\""), &text),
            Some((None, _)) => {}
            None => {
                if let Some(Some(fill)) = &styles.defaults.para_shd {
                    tally.add(format!("para-shd {fill} via \"docDefaults\""), &text);
                }
            }
        },
    }
    // The style chain's frame under the paragraph's own attributes, as
    // the converter merges them.
    let mut frame = styles.frame(&pstyle);
    if let Some(own) = ppr.and_then(|x| child(dom, x, "framePr")) {
        merge_pairs(&mut frame, &attr_pairs(dom, own));
    }
    if !frame.is_empty() {
        let mut items: Vec<String> = frame.iter().map(|(k, v)| format!("{k}={v}")).collect();
        items.sort();
        out.push(format!(
            "  frame({}) \"{}\"",
            items.join(","),
            clip(&text, SAMPLE)
        ));
    }
    // Revision containers side by side, a plain run between them breaking
    // the pair.
    let mut order: Vec<char> = Vec::new();
    revision_order(dom, p, &mut order);
    for &[a, b] in order.array_windows() {
        match (a, b) {
            ('D', 'I') => tally.add("revisions del→ins".to_string(), &text),
            ('I', 'D') => tally.add("revisions ins→del".to_string(), &text),
            _ => {}
        }
    }
}

/// The revision containers under `n` in document order, looking through
/// hyperlinks, smart tags, content controls and customXml (their runs are
/// the paragraph's own).
fn revision_order(dom: &Dom, n: NodeId, order: &mut Vec<char>) {
    for c in dom.elements(n, None) {
        let k = match local(dom, c).as_str() {
            "del" | "moveFrom" => 'D',
            "ins" | "moveTo" => 'I',
            "fldSimple" => '-',
            "r" if paints(dom, c) => '-',
            "hyperlink" | "smartTag" | "sdt" | "sdtContent" | "customXml" => {
                revision_order(dom, c, order);
                continue;
            }
            _ => continue,
        };
        if order.last() != Some(&k) {
            order.push(k);
        }
    }
}

/// A run that puts something on the page: text, a tab or break, a symbol,
/// a field character or a drawing (anything but its properties, a
/// rendered-page-break hint, an empty `w:t` or a comment reference).
fn paints(dom: &Dom, r: NodeId) -> bool {
    dom.elements(r, None)
        .into_iter()
        .any(|c| match local(dom, c).as_str() {
            "rPr" | "lastRenderedPageBreak" | "commentReference" => false,
            "t" | "delText" => dom
                .nodes(c)
                .into_iter()
                .any(|t| !dom.text_value(t).unwrap_or_default().is_empty()),
            _ => true,
        })
}

/// A run that paints something a highlight can colour: text, a symbol,
/// a tab (plain or positioned) or a hyphen glyph (a non-breaking or soft
/// one). A break paints no glyph (a page break ends the page, a line
/// break wraps), so a highlight on it never reaches the page.
fn paints_glyphs(dom: &Dom, r: NodeId) -> bool {
    dom.elements(r, None)
        .into_iter()
        .any(|c| match local(dom, c).as_str() {
            "t" | "delText" => dom
                .nodes(c)
                .into_iter()
                .any(|t| !dom.text_value(t).unwrap_or_default().is_empty()),
            "tab" | "ptab" | "sym" | "noBreakHyphen" | "softHyphen" => true,
            _ => false,
        })
}

/// The run's leading `rPr` blocks by local name: a Strict package writes
/// them in the Strict namespace, which a qualified lookup misses.
fn leading_rprs(dom: &Dom, r: NodeId) -> Vec<NodeId> {
    let mut out = Vec::new();
    for i in 0..dom.child_count(r) {
        let child = dom.child_at(r, i);
        if local(dom, child) == "rPr" {
            out.push(child);
        } else if dom.text_value(child).is_none() {
            break;
        }
    }
    out
}

/// A run's colour, highlight, shading, hidden state and requested font,
/// direct or from its character or paragraph style, over `docDefaults`.
/// The leading `w:rPr` blocks apply in order, each its character style and
/// then its own properties, as the converter folds them (an `rPr` after the
/// run's content is ignored).
fn run(dom: &Dom, r: NodeId, styles: &StyleBook, tally: &mut Tally) {
    let Some(para) = nearest(dom, r, "p") else {
        return;
    };
    tally.next_run(para);
    let text = own_text(dom, r, para);
    let rprs = leading_rprs(dom, r);
    let style_of = |pr: Option<NodeId>, name: &str| {
        pr.and_then(|x| child(dom, x, name))
            .map(|s| attr(dom, s, "val"))
            .unwrap_or_default()
    };
    let pstyle = styles.para_style(style_of(child(dom, para, "pPr"), "pStyle"));
    // The value that reaches the page and the style it comes through
    // (`None` for direct formatting).
    type Layer = Option<(String, Option<String>)>;
    let fold = |get: &dyn Fn(NodeId) -> Option<Option<String>>,
                from_style: &dyn Fn(&StyleInfo) -> Option<Option<String>>|
     -> Layer {
        let mut cur: Layer = match styles.find(&pstyle, from_style) {
            Some((v, name)) => v.map(|v| (v, Some(name))),
            None => from_style(&styles.defaults)
                .flatten()
                .map(|v| (v, Some(styles.defaults.name.clone()))),
        };
        for &rpr in &rprs {
            let rstyle = style_of(Some(rpr), "rStyle");
            if let Some((v, name)) = styles.find(&rstyle, from_style) {
                cur = v.map(|v| (v, Some(name)));
            }
            if let Some(v) = get(rpr) {
                cur = v.map(|v| (v, None));
            }
        }
        cur
    };
    let mut add = |label: &str, layer: Layer| match layer {
        Some((v, None)) => tally.add(format!("{label} {v}"), &text),
        Some((v, Some(name))) => tally.add(format!("{label} {v} via \"{name}\""), &text),
        None => {}
    };
    add("color", fold(&|x| color(dom, x), &|s| s.color.clone()));
    // A highlight paints behind glyphs, so only a run that paints some
    // (text, a symbol, a tab) can show one on the page.
    if paints_glyphs(dom, r) {
        add(
            "highlight",
            fold(&|x| highlight(dom, x), &|s| s.highlight.clone()),
        );
    }
    add(
        "run-shd",
        fold(&|x| run_shd(dom, x), &|s| s.run_shd.clone()),
    );
    // Hidden the same way, a style's `vanish` standing in for a value.
    let as_value = |v: bool| v.then(|| "vanish".to_string());
    if let Some((_, via)) = fold(
        &|x| child(dom, x, "vanish").map(|_| as_value(on(dom, x, "vanish"))),
        &|s| s.vanish.map(as_value),
    ) {
        match via {
            None => tally.add("vanish".to_string(), &text),
            Some(name) => tally.add(format!("vanish via \"{name}\""), &text),
        }
    }
    // Fonts slot by slot: the paragraph style's chain, then each block's
    // character style and its own `w:rFonts`.
    let mut slots: Vec<(&'static str, String, Option<String>)> = styles
        .font_slots(&pstyle)
        .into_iter()
        .map(|(k, f, n)| (k, f, Some(n)))
        .collect();
    let overlay = |slots: &mut Vec<(&'static str, String, Option<String>)>,
                   new: Vec<(&'static str, String, Option<String>)>| {
        for (k, f, n) in new {
            slots.retain(|(s, _, _)| *s != k);
            slots.push((k, f, n));
        }
    };
    for &rpr in &rprs {
        let rstyle = style_of(Some(rpr), "rStyle");
        if !rstyle.is_empty() {
            let from = styles
                .font_slots(&rstyle)
                .into_iter()
                .map(|(k, f, n)| (k, f, Some(n)))
                .collect();
            overlay(&mut slots, from);
        }
        if let Some(f) = child(dom, rpr, "rFonts") {
            let direct = font_slots(dom, f)
                .into_iter()
                .map(|(k, face)| (k, face, None))
                .collect();
            overlay(&mut slots, direct);
        }
    }
    let mut seen: Vec<(String, Option<String>)> = Vec::new();
    for (_, face, via) in slots {
        if seen.iter().any(|(f, v)| *f == face && *v == via) {
            continue;
        }
        match &via {
            None => tally.add(format!("rfonts \"{face}\""), ""),
            Some(name) => tally.add(format!("rfonts \"{face}\" via \"{name}\""), ""),
        }
        seen.push((face, via));
    }
}

/// Each slot's face in `w:rFonts` (a theme font where the slot names
/// none): a run can paint Latin, East Asian and complex-script text in
/// different faces.
fn font_slots(dom: &Dom, f: NodeId) -> Vec<(&'static str, String)> {
    let mut faces: Vec<(&'static str, String)> = Vec::new();
    for (slot, theme) in [
        ("ascii", "asciiTheme"),
        ("hAnsi", "hAnsiTheme"),
        ("eastAsia", "eastAsiaTheme"),
        ("cs", "cstheme"),
    ] {
        let (face, theme) = (attr(dom, f, slot), attr(dom, f, theme));
        let face = if !face.is_empty() {
            face
        } else if !theme.is_empty() {
            format!("theme:{theme}")
        } else {
            continue;
        };
        faces.push((slot, face));
    }
    faces
}

/// An element's attributes as `(local name, value)`.
fn attr_pairs(dom: &Dom, n: NodeId) -> Vec<(String, String)> {
    dom.attributes_iter(n)
        .map(|(k, v)| (k.local_name().to_string(), v.to_string()))
        .collect()
}

/// `over`'s values replace or join `base`'s.
fn merge_pairs(base: &mut Vec<(String, String)>, over: &[(String, String)]) {
    for (k, v) in over {
        match base.iter_mut().find(|(bk, _)| bk == k) {
            Some(slot) => slot.1.clone_from(v),
            None => base.push((k.clone(), v.clone())),
        }
    }
}

/// `table N RxC style=… float(…) shd=… cells-shd[…] style-shd[…] "first row"`.
fn table(dom: &Dom, t: NodeId, n: usize, styles: &StyleBook) -> String {
    let tblpr = child(dom, t, "tblPr");
    // Wrapper-held rows are the table's own, as the converter lays them
    // out (`customXml`, and `sdt`/`sdtContent` as Strict files spell them).
    let rows: Vec<NodeId> = crate::table_dom::wrapped_children(dom, t, "tr");
    // The columns the converter lays out: the grid, or the widest row
    // (its spans, grid before/after and, with a live `w:cellDel`, the
    // "Deleted Cells" column Word's All Markup appends).
    let grid = child(dom, t, "tblGrid").map_or(0, |g| kids(dom, g, "gridCol").len());
    let widest = rows
        .iter()
        .map(|&row| {
            let trpr = child(dom, row, "trPr");
            let skip = |name: &str| {
                trpr.and_then(|p| child(dom, p, name))
                    .and_then(|g| attr(dom, g, "val").parse::<usize>().ok())
                    .unwrap_or(0)
            };
            let cells = crate::table_dom::wrapped_children(dom, row, "tc");
            let spans: usize = cells
                .iter()
                .map(|&tc| {
                    child(dom, tc, "tcPr")
                        .and_then(|p| child(dom, p, "gridSpan"))
                        .and_then(|g| attr(dom, g, "val").parse::<usize>().ok())
                        .unwrap_or(1)
                        .max(1)
                })
                .sum();
            let stamp = cells
                .iter()
                .any(|&tc| crate::table_dom::cell_is_deleted(dom, tc));
            skip("gridBefore") + spans + skip("gridAfter") + usize::from(stamp)
        })
        .max()
        .unwrap_or(0);
    let cols = grid.max(widest);
    let mut s = format!("  table {n} {}x{cols}", rows.len());
    if nearest(dom, t, "tbl").is_some() {
        s.push_str(" nested");
    }
    if nearest(dom, t, "txbxContent").is_some() {
        s.push_str(" in-textbox");
    }
    let style = tblpr
        .and_then(|x| child(dom, x, "tblStyle"))
        .map(|x| attr(dom, x, "val"))
        .unwrap_or_default();
    if !style.is_empty() {
        s.push_str(&format!(" style=\"{}\"", styles.name(&style)));
    }
    if let Some(float) = tblpr.and_then(|x| child(dom, x, "tblpPr")) {
        let mut items: Vec<String> = dom
            .attributes_iter(float)
            .map(|(k, v)| format!("{}={v}", k.local_name()))
            .collect();
        items.sort();
        s.push_str(&format!(" float({})", items.join(",")));
    }
    if let Some(fill) = tblpr
        .and_then(|x| child(dom, x, "shd"))
        .and_then(|x| shd(dom, x))
    {
        s.push_str(&format!(" shd={fill}"));
    }
    // The borders the table and its cells carry themselves, as a table
    // line prints them.
    let borders = |props: Option<NodeId>, all: &[&'static str]| -> String {
        use crate::borders::{self, Border};
        let found: Vec<_> = props
            .map(|x| dom.elements(x, None))
            .unwrap_or_default()
            .into_iter()
            .filter_map(|side| {
                let name = borders::side(&local(dom, side), all)?;
                let get = |key: &str| Some(attr(dom, side, key)).filter(|v| !v.is_empty());
                let border = Border::new(
                    &get("val")?,
                    get("sz").as_deref(),
                    get("color").as_deref(),
                    get("space").as_deref(),
                );
                Some((name, border))
            })
            .collect();
        borders::describe(&borders::ordered(&found, all), all)
    };
    let own = borders(
        tblpr.and_then(|x| child(dom, x, "tblBorders")),
        &crate::borders::TABLE_SIDES,
    );
    if !own.is_empty() {
        s.push_str(&format!(" borders[{own}]"));
    }
    let mut cell_borders: BTreeMap<String, usize> = BTreeMap::new();
    let mut cells: BTreeMap<String, usize> = BTreeMap::new();
    for tc in dom.descendants(t, None) {
        if local(dom, tc) != "tc" || nearest(dom, tc, "tbl") != Some(t) {
            continue;
        }
        if let Some(fill) = child(dom, tc, "tcPr")
            .and_then(|x| child(dom, x, "shd"))
            .and_then(|x| shd(dom, x))
        {
            *cells.entry(fill).or_default() += 1;
        }
        let own = borders(
            child(dom, tc, "tcPr").and_then(|x| child(dom, x, "tcBorders")),
            &crate::borders::CELL_SIDES,
        );
        if !own.is_empty() {
            *cell_borders.entry(own).or_default() += 1;
        }
    }
    if !cell_borders.is_empty() {
        let items: Vec<String> = cell_borders
            .iter()
            .map(|(b, k)| format!("{b}×{k}"))
            .collect();
        s.push_str(&format!(" cells-borders[{}]", items.join(",")));
    }
    if !cells.is_empty() {
        let items: Vec<String> = cells.iter().map(|(f, k)| format!("{f}×{k}")).collect();
        s.push_str(&format!(" cells-shd[{}]", items.join(",")));
    }
    // The conditions the table's look turns off paint nothing (the
    // converter's own reading of `w:tblLook`).
    let look = crate::table_dom::table_look(dom, t);
    let conds: Vec<(String, String)> = styles
        .table_shd(&style)
        .into_iter()
        .filter(|(k, _)| match k.as_str() {
            "firstRow" => look.first_row,
            "firstCol" => look.first_col,
            "band1Horz" | "band2Horz" => !look.no_h_band,
            _ => true,
        })
        .collect();
    if !conds.is_empty() {
        let items: Vec<String> = conds.iter().map(|(k, f)| format!("{k}={f}")).collect();
        s.push_str(&format!(" style-shd[{}]", items.join(",")));
    }
    if let Some(first) = rows.first() {
        let texts: Vec<String> = crate::table_dom::wrapped_children(dom, *first, "tc")
            .into_iter()
            .map(|tc| {
                crate::table_dom::wrapped_children(dom, tc, "p")
                    .into_iter()
                    .map(|p| own_text(dom, p, p))
                    .collect::<Vec<_>>()
                    .join(" ")
            })
            .collect();
        s.push_str(&format!(" \"{}\"", clip(&texts.join(" | "), FIRST_ROW)));
    }
    s
}

/// `section N page=WxH margins=t,r,b,l header=… footer=… cols=… TYPE-KIND=part`.
fn section(dom: &Dom, sect: NodeId, n: usize, rels: &[Rel]) -> String {
    let mut s = format!("  section {n}");
    if let Some(sz) = child(dom, sect, "pgSz") {
        s.push_str(&format!(
            " page={}x{}",
            attr(dom, sz, "w"),
            attr(dom, sz, "h")
        ));
        // The page's orientation, not the attribute's: `portrait` is the
        // default a page need not state, and a page wider than tall is
        // landscape (as the view reads it).
        let number = |k: &str| attr(dom, sz, k).parse::<u64>().unwrap_or(0);
        if attr(dom, sz, "orient") == "landscape" || number("w") > number("h") {
            s.push_str(" landscape");
        }
    }
    if let Some(m) = child(dom, sect, "pgMar") {
        let a = |k: &str| attr(dom, m, k);
        s.push_str(&format!(
            " margins={},{},{},{} header={} footer={}",
            a("top"),
            a("right"),
            a("bottom"),
            a("left"),
            a("header"),
            a("footer")
        ));
    }
    if let Some(c) = child(dom, sect, "cols") {
        // Unequal columns: each width and the gap after it.
        let custom: Vec<String> = kids(dom, c, "col")
            .into_iter()
            .map(|col| {
                let gap = attr(dom, col, "space");
                if gap.is_empty() {
                    attr(dom, col, "w")
                } else {
                    format!("{}+{gap}", attr(dom, col, "w"))
                }
            })
            .collect();
        let unequal = matches!(attr(dom, c, "equalWidth").as_str(), "0" | "false");
        let num = attr(dom, c, "num");
        if unequal && !custom.is_empty() {
            s.push_str(&format!(" cols={}[{}]", custom.len(), custom.join(",")));
        } else if !num.is_empty() && num != "1" {
            s.push_str(&format!(" cols={num}"));
        }
    }
    if let Some(t) = child(dom, sect, "type") {
        s.push_str(&format!(" type={}", attr(dom, t, "val")));
    }
    if on(dom, sect, "titlePg") {
        s.push_str(" titlePg");
    }
    for r in dom.elements(sect, None) {
        let kind = match local(dom, r).as_str() {
            "headerReference" => "header",
            "footerReference" => "footer",
            _ => continue,
        };
        let ty = attr(dom, r, "type");
        let ty = if ty.is_empty() {
            "default".to_string()
        } else {
            ty
        };
        let id = attr(dom, r, "id");
        let part = rels
            .iter()
            .find(|x| x.id == id)
            .map_or_else(|| format!("{id}?"), |x| x.part.clone());
        s.push_str(&format!(" {ty}-{kind}={part}"));
    }
    s
}

/// `anchor wrap H=from:offset V=from:offset CXxCY [behind] "name"`.
fn anchor(dom: &Dom, a: NodeId) -> String {
    let wrap = dom
        .elements(a, None)
        .into_iter()
        .map(|c| local(dom, c))
        .find(|l| l.starts_with("wrap"))
        .unwrap_or_default();
    let pos = |name: &str| {
        child(dom, a, name).map_or_else(String::new, |p| {
            let at = dom
                .elements(p, None)
                .into_iter()
                .map(|c| {
                    let v: String = dom
                        .nodes(c)
                        .into_iter()
                        .filter_map(|t| dom.text_value(t).map(str::to_string))
                        .collect();
                    if local(dom, c) == "align" {
                        v
                    } else {
                        v.trim().to_string()
                    }
                })
                .next()
                .unwrap_or_default();
            format!("{}:{at}", attr(dom, p, "relativeFrom"))
        })
    };
    let size = child(dom, a, "extent")
        .map(|e| format!("{}x{}", attr(dom, e, "cx"), attr(dom, e, "cy")))
        .unwrap_or_default();
    let behind = if matches!(attr(dom, a, "behindDoc").as_str(), "1" | "true") {
        " behind"
    } else {
        ""
    };
    let name = child(dom, a, "docPr")
        .map(|d| attr(dom, d, "name"))
        .unwrap_or_default();
    format!(
        "  anchor {wrap} H={} V={} {size}{behind} \"{name}\"",
        pos("positionH"),
        pos("positionV")
    )
}

/// `(layout)`: jubarte's page count and font resolution (Word revision
/// style, as the bench renders it), then the fonts the package asks for:
/// `docDefaults`, the theme and `fontTable.xml`.
fn layout_lines(pkg: &Package, rels: &[Rel]) -> (Vec<String>, usize) {
    let mut out = Vec::new();
    let options = jubarte_render::convert::PdfOptions {
        revisions: jubarte_render::convert::RevisionStyle::Word,
        ..Default::default()
    };
    let pages = match jubarte_render::convert::docx_render_report(&pkg.raw, options) {
        Ok(report) => {
            let mut fonts: Vec<String> = report
                .fonts
                .iter()
                .map(|f| {
                    let style = match (f.bold, f.italic) {
                        (false, false) => "regular",
                        (true, false) => "bold",
                        (false, true) => "italic",
                        (true, true) => "bold-italic",
                    };
                    format!(
                        "  font \"{}\" {style} → {} {}{}",
                        f.requested,
                        f.physical,
                        f.step,
                        if f.synthetic { " synthetic" } else { "" }
                    )
                })
                .collect();
            fonts.sort();
            out.push(format!("  pages {}", report.page_count));
            out.extend(fonts);
            report.page_count
        }
        Err(e) => {
            out.push(format!("  layout failed: {e}"));
            0
        }
    };
    if let Some(d) = rels
        .iter()
        .find(|r| r.kind == "styles")
        .and_then(|r| parse(pkg, &r.part))
        .and_then(|(dom, root)| StyleBook::new(&dom, root).default_fonts)
    {
        out.push(format!("  docDefaults rfonts {d}"));
    }
    if let Some((dom, root)) = rels
        .iter()
        .find(|r| r.kind == "theme")
        .and_then(|r| parse(pkg, &r.part))
    {
        for (slot, label) in [("majorFont", "major"), ("minorFont", "minor")] {
            let face = descendant(&dom, root, slot)
                .and_then(|f| child(&dom, f, "latin"))
                .map(|l| attr(&dom, l, "typeface"));
            if let Some(face) = face {
                out.push(format!("  theme {label} latin=\"{face}\""));
            }
        }
    }
    if let Some((dom, root)) = rels
        .iter()
        .find(|r| r.kind == "fontTable")
        .and_then(|r| parse(pkg, &r.part))
    {
        for f in kids(&dom, root, "font") {
            let mut s = format!("  fontTable \"{}\"", attr(&dom, f, "name"));
            if let Some(alt) = child(&dom, f, "altName") {
                s.push_str(&format!(" alt=\"{}\"", attr(&dom, alt, "val")));
            }
            let embedded: Vec<String> = dom
                .elements(f, None)
                .into_iter()
                .map(|c| local(&dom, c))
                .filter_map(|l| l.strip_prefix("embed").map(str::to_lowercase))
                .collect();
            if !embedded.is_empty() {
                s.push_str(&format!(" embedded({})", embedded.join(",")));
            }
            out.push(s);
        }
    }
    (out, pages)
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod story_diagnostic_contract_tests {
    use super::*;
    use std::io::{Cursor, Write};

    const W: &str = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
    const R: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
    const PR: &str = "http://schemas.openxmlformats.org/package/2006/relationships";

    fn dom(xml: &str) -> (Dom, NodeId) {
        let mut d = Dom::new();
        let doc = d.parse_xdocument(xml);
        let root = d.root(doc).unwrap();
        (d, root)
    }

    fn document(body: &str) -> String {
        format!(
            r#"<w:document xmlns:w="{W}" xmlns:r="{R}" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><w:body>{body}</w:body></w:document>"#
        )
    }

    // The dependency's own in-memory ZIP writer, also used by debug.rs tests.
    fn package(entries: &[(&str, &str)]) -> Package {
        let mut out = Cursor::new(Vec::new());
        {
            let mut zip = zip::ZipWriter::new(&mut out);
            zip.start_file(
                "[Content_Types].xml",
                zip::write::SimpleFileOptions::default(),
            )
            .unwrap();
            zip.write_all(br#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/header1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/></Types>"#).unwrap();
            for (name, text) in entries {
                zip.start_file(*name, zip::write::SimpleFileOptions::default())
                    .unwrap();
                zip.write_all(text.as_bytes()).unwrap();
            }
            zip.finish().unwrap();
        }
        Package::open(&out.into_inner()).unwrap()
    }

    fn report(body: &str, styles: &StyleBook, rels: &[Rel]) -> (Vec<String>, usize) {
        let (d, root) = dom(&document(body));
        let before = d.serialize_element(root);
        let mut lines = Vec::new();
        let count = part_lines(&d, root, styles, rels, &mut lines);
        assert_eq!(d.serialize_element(root), before);
        (lines, count)
    }

    #[test]
    fn nested_complex_fields_record_each_code_once_and_ignore_unowned_instructions() {
        let body = r#"<w:p><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> IF </w:instrText></w:r><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> pa</w:instrText></w:r><w:r><w:instrText>ge </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r><w:t>2</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r><w:r><w:instrText> = 2 "yes" "no" </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r><w:t>yes</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r><w:fldSimple w:instr=" DATE "><w:r><w:t>June</w:t></w:r></w:fldSimple></w:p>"#;
        assert_eq!(
            report(body, &StyleBook::default(), &[]),
            (
                vec![
                    "  field DATE ×1".into(),
                    "  field IF ×1".into(),
                    "  field PAGE ×1".into()
                ],
                1
            )
        );
        // Intentional malformed diagnostic probes: unmatched field ends,
        // empty instructions, and a run outside any paragraph paint no field.
        let malformed = r#"<w:r><w:rPr><w:color w:val="FF0000"/></w:rPr><w:t>orphan</w:t></w:r><w:p><w:r><w:instrText>STRAY</w:instrText><w:fldChar w:fldCharType="end"/></w:r><w:r><w:fldChar w:fldCharType="begin"/><w:instrText> </w:instrText><w:fldChar w:fldCharType="end"/></w:r><w:fldSimple w:instr=" "/></w:p>"#;
        assert_eq!(
            report(malformed, &StyleBook::default(), &[]),
            (Vec::new(), 1)
        );
    }

    #[test]
    fn shading_reports_literal_solid_theme_and_pattern_ink_with_explicit_resets() {
        for (attrs, expected) in [
            (
                r#"w:val="solid" w:color="A1B2C3" w:fill="FFFFFF""#,
                Some("A1B2C3"),
            ),
            (r#"w:val="solid" w:color="auto""#, Some("solid:auto")),
            (r#"w:val="solid""#, Some("solid:auto")),
            (
                r#"w:val="clear" w:themeFill="accent2" w:fill="112233""#,
                Some("theme:accent2"),
            ),
            (
                r#"w:val="pct20" w:fill="112233" w:color="445566""#,
                Some("112233/pct20:445566"),
            ),
            (
                r#"w:val="pct20" w:fill="auto" w:color="445566""#,
                Some("/pct20:445566"),
            ),
            (r#"w:val="clear" w:fill="auto""#, None),
            (r#"w:val="clear""#, None),
            (r#"w:val="nil" w:fill="112233""#, None),
        ] {
            let (d, root) = dom(&format!(r#"<w:shd xmlns:w="{W}" {attrs}/>"#));
            let before = d.serialize_element(root);
            assert_eq!(shd(&d, root).as_deref(), expected, "{attrs}");
            assert_eq!(d.serialize_element(root), before);
        }
        let (d, root) = dom(&format!(
            r#"<w:rPr xmlns:w="{W}"><w:color/><w:highlight/></w:rPr>"#
        ));
        assert_eq!(color(&d, root), Some(None));
        assert_eq!(highlight(&d, root), Some(None));
        let (d, root) = dom(&format!(
            r#"<w:rPr xmlns:w="{W}"><w:color w:themeColor="accent1" w:themeTint="80" w:themeShade="40"/></w:rPr>"#
        ));
        assert_eq!(
            color(&d, root),
            Some(Some("theme:accent1/tint:80/shade:40".into()))
        );
    }

    #[test]
    fn inherited_frame_overrides_and_repeated_run_tallies_keep_first_source_sample() {
        let (d, root) = dom(&format!(
            r#"<w:styles xmlns:w="{W}"><w:style w:type="paragraph" w:styleId="Base"><w:name w:val="Base face"/><w:pPr><w:framePr w:w="2000" w:x="100" w:hAnchor="page"/></w:pPr><w:rPr><w:color w:val="112233"/><w:highlight w:val="yellow"/><w:rFonts w:ascii="Example" w:hAnsi="Example"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Child"><w:name w:val="Child face"/><w:basedOn w:val="Base"/><w:pPr><w:framePr w:x="200"/></w:pPr></w:style></w:styles>"#
        ));
        let styles = StyleBook::new(&d, root);
        let body = r#"<w:p><w:pPr><w:pStyle w:val="Child"/><w:framePr w:x="300"/></w:pPr><w:r><w:t>first</w:t></w:r><w:r><w:t>second</w:t></w:r><w:r><w:rPr><w:color w:val="auto"/><w:highlight w:val="none"/></w:rPr><w:t>reset</w:t></w:r></w:p>"#;
        assert_eq!(
            report(body, &styles, &[]),
            (
                vec![
                    "  frame(hAnchor=page,w=2000,x=300) \"firstsecondreset\"".into(),
                    // `first` and `second` are one stretch; the face
                    // spans all three runs.
                    "  color 112233 via \"Base face\" ×1 \"first\"".into(),
                    "  highlight yellow via \"Base face\" ×1 \"first\"".into(),
                    "  pstyle \"Child face\" ×1".into(),
                    "  rfonts \"Example\" via \"Base face\" ×1".into(),
                ],
                1
            )
        );
    }

    #[test]
    fn a_stretch_split_into_runs_counts_once_and_a_gap_counts_twice() {
        // file_170's "monospace" and "." are two runs of one highlight; a
        // round trip that leaves the space between unhighlighted has two.
        let split = r#"<w:p><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:t xml:space="preserve">yellow, </w:t></w:r><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:t>mono</w:t></w:r><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:t>.</w:t></w:r></w:p>"#;
        let gap = r#"<w:p><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:t>yellow,</w:t></w:r><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:t>mono.</w:t></w:r></w:p>"#;
        assert_eq!(
            report(split, &StyleBook::default(), &[]).0,
            vec!["  highlight yellow ×1 \"yellow,\"".to_string()]
        );
        assert_eq!(
            report(gap, &StyleBook::default(), &[]).0,
            vec!["  highlight yellow ×2 \"yellow,\"".to_string()]
        );
        // Two paragraphs are two stretches.
        assert_eq!(
            report(&format!("{split}{split}"), &StyleBook::default(), &[]).0,
            vec!["  highlight yellow ×2 \"yellow,\"".to_string()]
        );
    }

    #[test]
    fn strict_rprs_tally_by_local_name() {
        // A Strict package writes its parts in the purl.oclc.org namespace
        // (Strict01): a run's leading `rPr` blocks are found by local name,
        // so their direct highlight still counts.
        let (d, root) = dom(
            r#"<w:document xmlns:w="http://purl.oclc.org/ooxml/wordprocessingml/main"><w:body><w:p><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:t>Video provides</w:t></w:r></w:p></w:body></w:document>"#,
        );
        let mut lines = Vec::new();
        part_lines(&d, root, &StyleBook::default(), &[], &mut lines);
        assert_eq!(
            lines,
            vec!["  highlight yellow ×1 \"Video provides\"".to_string()]
        );
    }

    #[test]
    fn a_highlight_on_a_run_that_paints_no_glyph_is_not_tallied() {
        // 791ca61933's only highlight sits on a deleted page-break run: a
        // break paints no glyph a highlight can colour, while a tab or
        // text does.
        let br = r#"<w:p><w:del w:id="1" w:author="A" w:date="2024-01-01T00:00:00Z"><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:br w:type="page"/></w:r></w:del><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:t>lit</w:t></w:r></w:p>"#;
        assert_eq!(
            report(br, &StyleBook::default(), &[]).0,
            vec!["  highlight yellow ×1 \"lit\"".to_string()]
        );
        let tab = r#"<w:p><w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:tab/></w:r></w:p>"#;
        assert_eq!(
            report(tab, &StyleBook::default(), &[]).0,
            vec!["  highlight yellow ×1".to_string()]
        );
    }

    #[test]
    fn a_highlight_on_a_hyphen_or_position_tab_is_tallied() {
        // Word draws the highlight band behind a non-breaking or soft
        // hyphen and behind a position tab as behind any other glyph, so
        // the comparator counts them: a faithful round trip reports no
        // difference and a lost one a real difference, instead of hiding
        // both behind a run that paints nothing.
        for (run, line) in [
            (
                r#"<w:r><w:rPr><w:highlight w:val="cyan"/></w:rPr><w:noBreakHyphen/></w:r>"#,
                "  highlight cyan ×1",
            ),
            (
                r#"<w:r><w:rPr><w:highlight w:val="green"/></w:rPr><w:softHyphen/></w:r>"#,
                "  highlight green ×1",
            ),
            (
                r#"<w:r><w:rPr><w:highlight w:val="yellow"/></w:rPr><w:ptab/></w:r>"#,
                "  highlight yellow ×1",
            ),
        ] {
            let body =
                format!(r#"<w:p><w:r><w:t>A </w:t></w:r>{run}<w:r><w:t> B</w:t></w:r></w:p>"#);
            assert_eq!(
                report(&body, &StyleBook::default(), &[]).0,
                vec![line.to_string()],
                "{run}"
            );
        }
    }

    #[test]
    fn a_table_reports_its_own_borders_and_its_cells() {
        // 5331b981b7's shape: a grid written with start/end, grey cells.
        let body = r#"<w:tbl><w:tblPr><w:tblBorders><w:top w:val="single" w:sz="2" w:color="000000"/><w:start w:val="single" w:sz="2" w:color="000000"/><w:bottom w:val="nil"/></w:tblBorders></w:tblPr><w:tblGrid><w:gridCol w:w="3000"/><w:gridCol w:w="3000"/></w:tblGrid><w:tr><w:tc><w:tcPr><w:tcBorders><w:top w:val="single" w:sz="8" w:color="CCCCCC"/></w:tcBorders></w:tcPr><w:p><w:r><w:t>A</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcBorders><w:top w:val="single" w:sz="8" w:color="CCCCCC"/></w:tcBorders></w:tcPr><w:p><w:r><w:t>B</w:t></w:r></w:p></w:tc></w:tr></w:tbl>"#;
        assert_eq!(
            report(body, &StyleBook::default(), &[]).0,
            vec![
                "  table 1 1x2 borders[top,left single 0.25pt 000000 + bottom none] cells-borders[top single 1pt CCCCCC×2] \"A | B\""
                    .to_string()
            ]
        );
    }

    #[test]
    fn nested_table_shading_and_textbox_frames_report_only_their_own_text() {
        let body = r#"<w:tbl><w:tblPr><w:tblpPr w:tblpX="40" w:horzAnchor="margin"/></w:tblPr><w:tblGrid><w:gridCol w:w="3000"/></w:tblGrid><w:tr><w:tc><w:tcPr><w:shd w:fill="112233"/></w:tcPr><w:p><w:r><w:t>Outer</w:t></w:r></w:p><w:tbl><w:tblGrid><w:gridCol w:w="1500"/></w:tblGrid><w:tr><w:tc><w:tcPr><w:shd w:fill="445566"/></w:tcPr><w:p><w:r><w:t>Inner</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p/></w:tc></w:tr></w:tbl><w:p><w:pPr><w:framePr w:w="900"/></w:pPr><w:r><w:t>Host</w:t><w:pict><v:shape><v:textbox><w:txbxContent><w:tbl><w:tblGrid><w:gridCol w:w="800"/></w:tblGrid><w:tr><w:tc><w:p><w:r><w:t>Box</w:t></w:r></w:p></w:tc></w:tr></w:tbl></w:txbxContent></v:textbox></v:shape></w:pict></w:r></w:p>"#;
        assert_eq!(
            report(body, &StyleBook::default(), &[]),
            (
                vec![
                    "  table 1 1x1 float(horzAnchor=margin,tblpX=40) cells-shd[112233×1] \"Outer\""
                        .into(),
                    "  table 2 1x1 nested cells-shd[445566×1] \"Inner\"".into(),
                    "  frame(w=900) \"Host\"".into(),
                    "  table 3 1x1 in-textbox \"Box\"".into(),
                    "  vml pict ×1".into(),
                ],
                5
            )
        );
        // Explicit malformed table probe, not a schema-valid authored table.
        assert_eq!(
            report("<w:tbl/>", &StyleBook::default(), &[]),
            (vec!["  table 1 0x0".into()], 0)
        );
    }

    #[test]
    fn section_columns_geometry_and_story_reference_diagnostics_are_literal() {
        let rels = [Rel {
            kind: "header".into(),
            id: "h".into(),
            part: "word/header1.xml".into(),
        }];
        let body = r#"<w:sectPr><w:headerReference w:type="first" r:id="h"/><w:footerReference w:type="default" r:id="missing"/><w:type w:val="continuous"/><w:pgSz w:w="15840" w:h="12240" w:orient="landscape"/><w:pgMar w:top="100" w:right="200" w:bottom="300" w:left="400" w:header="50" w:footer="60"/><w:cols w:equalWidth="0"><w:col w:w="2000" w:space="300"/><w:col w:w="4000"/></w:cols><w:titlePg/><w:sectPrChange w:id="1" w:author="A"><w:sectPr><w:pgSz w:w="1" w:h="2"/></w:sectPr></w:sectPrChange></w:sectPr>"#;
        assert_eq!(report(body, &StyleBook::default(), &rels), (vec!["  section 1 page=15840x12240 landscape margins=100,200,300,400 header=50 footer=60 cols=2[2000+300,4000] type=continuous titlePg first-header=word/header1.xml default-footer=missing?".into()], 0));
        for (attrs, children, expected) in [
            (r#"w:num="2""#, "", "  section 1 cols=2"),
            (r#"w:num="1""#, "", "  section 1"),
            ("", "", "  section 1"),
            (
                r#"w:equalWidth="false" w:num="3""#,
                "",
                "  section 1 cols=3",
            ), // malformed missing unequal column definitions
            (
                r#"w:equalWidth="1" w:num="2""#,
                r#"<w:col w:w="1000"/>"#,
                "  section 1 cols=2",
            ),
        ] {
            assert_eq!(
                report(
                    &format!("<w:sectPr><w:cols {attrs}>{children}</w:cols></w:sectPr>"),
                    &StyleBook::default(),
                    &[]
                ),
                (vec![expected.into()], 0)
            );
        }
        // Required type is deliberately absent to test the diagnostic fallback.
        assert_eq!(
            report(
                r#"<w:sectPr><w:headerReference r:id="h"/></w:sectPr>"#,
                &StyleBook::default(),
                &rels
            ),
            (
                vec!["  section 1 default-header=word/header1.xml".into()],
                0
            )
        );
    }

    #[test]
    fn section_geometry_prints_one_spelling_of_the_same_measure() {
        // A strict `w:sectPr` writes universal measures (`612pt`) where
        // the transitional form writes twips: the diagnostic lists the
        // twips, so a round trip compares one spelling while a changed
        // size still differs and a malformed value stays raw. Packages,
        // not `report`'s bare Dom: the normalisation lives in `parse`.
        let sect = |size: &str, mar: &str| {
            let main = document(&format!(
                r#"<w:p><w:r><w:t>x</w:t></w:r></w:p><w:sectPr><w:pgSz {size}/><w:pgMar {mar}/></w:sectPr>"#
            ));
            let root_rels = format!(
                r#"<Relationships xmlns="{PR}"><Relationship Id="main" Type="{R}/officeDocument" Target="word/document.xml"/></Relationships>"#
            );
            let pkg = package(&[("_rels/.rels", &root_rels), ("word/document.xml", &main)]);
            render_parts(&pkg, None)["word/document.xml"]
                .0
                .iter()
                .find(|l| l.starts_with("  section"))
                .cloned()
                .unwrap()
        };
        let strict = sect(
            r#"w:w="612pt" w:h="792pt""#,
            r#"w:top="72pt" w:right="1440" w:bottom="1in" w:left="36pt" w:header="0.5in" w:footer="720""#,
        );
        assert_eq!(
            strict,
            "  section 1 page=12240x15840 margins=1440,1440,1440,720 header=720 footer=720"
        );
        let transitional = sect(
            r#"w:w="12240" w:h="15840""#,
            r#"w:top="1440" w:right="1440" w:bottom="1440" w:left="720" w:header="720" w:footer="720""#,
        );
        assert_eq!(transitional, strict);
        // Wider than tall is landscape in either spelling, and a page a
        // twip apart is a different page.
        assert!(sect(r#"w:w="792pt" w:h="612pt""#, "").contains("page=15840x12240 landscape"));
        assert_ne!(
            sect(
                r#"w:w="12241" w:h="15840""#,
                r#"w:top="1440" w:right="1440" w:bottom="1440" w:left="720" w:header="720" w:footer="720""#
            ),
            strict
        );
        assert!(sect(r#"w:w="6..1" w:h="792pt""#, "").contains("page=6..1x15840"));
    }

    #[test]
    fn section_prints_a_plain_value_as_the_file_spells_it() {
        // A plain fractional dxa (gdocs-style) is the file's own literal,
        // not a unit spelling to canonicalize: the diagnostic reports what
        // the part carries (#531 review defect 3). Only unit-bearing
        // measures ("612pt") list as the twips they name.
        let sect = |mar: &str| {
            let main = document(&format!(
                r#"<w:p><w:r><w:t>x</w:t></w:r></w:p><w:sectPr><w:pgSz w:w="12240" w:h="15840"/><w:pgMar {mar}/></w:sectPr>"#
            ));
            let root_rels = format!(
                r#"<Relationships xmlns="{PR}"><Relationship Id="main" Type="{R}/officeDocument" Target="word/document.xml"/></Relationships>"#
            );
            let pkg = package(&[("_rels/.rels", &root_rels), ("word/document.xml", &main)]);
            render_parts(&pkg, None)["word/document.xml"]
                .0
                .iter()
                .find(|l| l.starts_with("  section"))
                .cloned()
                .unwrap()
        };
        assert_eq!(
            sect(
                r#"w:top="1440.4" w:right="1440" w:bottom="1in" w:left="1440" w:header="720" w:footer="720""#
            ),
            "  section 1 page=12240x15840 margins=1440.4,1440,1440,1440 header=720 footer=720"
        );
    }

    #[test]
    fn section_orientation_is_the_pages_not_the_attributes() {
        // `w:orient="portrait"` is the schema's default: a page that says
        // it and one that does not are the same page (Demo.docx's round
        // trip reported them as a lost and a new section).
        for (size, expected) in [
            (
                r#"w:w="11906" w:h="16838" w:orient="portrait""#,
                "  section 1 page=11906x16838",
            ),
            (r#"w:w="11906" w:h="16838""#, "  section 1 page=11906x16838"),
            (
                r#"w:w="16838" w:h="11906" w:orient="landscape""#,
                "  section 1 page=16838x11906 landscape",
            ),
            // Wider than tall is landscape, as the view reads it.
            (
                r#"w:w="16838" w:h="11906""#,
                "  section 1 page=16838x11906 landscape",
            ),
            (
                r#"w:w="11906" w:h="16838" w:orient="landscape""#,
                "  section 1 page=11906x16838 landscape",
            ),
        ] {
            assert_eq!(
                report(
                    &format!("<w:sectPr><w:pgSz {size}/></w:sectPr>"),
                    &StyleBook::default(),
                    &[]
                ),
                (vec![expected.into()], 0),
                "{size}"
            );
        }
    }

    #[test]
    fn anchor_alignment_and_offsets_preserve_authored_extent_name_and_layer() {
        for (behind, suffix) in [
            ("1", " behind"),
            ("true", " behind"),
            ("0", ""),
            ("false", ""),
        ] {
            let body = format!(
                r#"<w:p><w:r><w:drawing><wp:anchor behindDoc="{behind}" simplePos="0" relativeHeight="0" locked="0" layoutInCell="1" allowOverlap="1"><wp:simplePos x="0" y="0"/><wp:positionH relativeFrom="margin"><wp:align>center</wp:align></wp:positionH><wp:positionV relativeFrom="paragraph"><wp:posOffset> 123 </wp:posOffset></wp:positionV><wp:extent cx="914400" cy="457200"/><wp:wrapSquare wrapText="bothSides"/><wp:docPr id="1" name="Picture 1"/><wp:cNvGraphicFramePr/><a:graphic><a:graphicData uri="urn:diagnostic:graphic"/></a:graphic></wp:anchor></w:drawing></w:r></w:p>"#
            );
            assert_eq!(
                report(&body, &StyleBook::default(), &[]),
                (
                    vec![format!(
                        "  anchor wrapSquare H=margin:center V=paragraph:123 914400x457200{suffix} \"Picture 1\""
                    )],
                    1
                )
            );
        }
    }

    #[test]
    fn memory_package_filter_resolves_story_roles_and_excludes_nonstories_without_mutation() {
        let main = document(
            r#"<w:p><w:r><w:t>Body</w:t></w:r></w:p><w:sectPr><w:headerReference w:type="default" r:id="h"/></w:sectPr>"#,
        );
        let root_rels = format!(
            r#"<Relationships xmlns="{PR}"><Relationship Id="main" Type="{R}/officeDocument" Target="word/document.xml"/></Relationships>"#
        );
        let main_rels = format!(
            r#"<Relationships xmlns="{PR}"><Relationship Id="h" Type="{R}/header" Target="header1.xml"/><Relationship Id="external" Type="{R}/hyperlink" Target="https://example.invalid/" TargetMode="External"/></Relationships>"#
        );
        let header = format!(
            r#"<w:hdr xmlns:w="{W}"><w:p><w:r><w:rPr><w:color w:val="112233"/></w:rPr><w:t>Head</w:t></w:r></w:p></w:hdr>"#
        );
        let pkg = package(&[
            ("_rels/.rels", &root_rels),
            ("word/_rels/document.xml.rels", &main_rels),
            ("word/document.xml", &main),
            ("word/header1.xml", &header),
            ("word/styles.xml", "<styles/>"),
            ("word/empty.xml", ""),
            ("word/picture.bin", "binary"),
        ]);
        let before: Vec<_> = pkg.entries.iter().map(|e| (&e.name, &e.data)).collect();
        let raw = pkg.raw.clone();
        let result = render_parts(&pkg, Some("header"));
        assert_eq!(result.len(), 1);
        assert_eq!(
            result["word/header1.xml"],
            (
                vec![
                    "  shown as section 1 default header".into(),
                    "  color 112233 ×1 \"Head\"".into()
                ],
                1
            )
        );
        assert_eq!(
            render_parts(&pkg, Some("word/"))
                .keys()
                .cloned()
                .collect::<Vec<_>>(),
            vec!["word/document.xml", "word/header1.xml"]
        );
        assert_eq!(pkg.raw, raw);
        assert_eq!(
            pkg.entries
                .iter()
                .map(|e| (&e.name, &e.data))
                .collect::<Vec<_>>(),
            before
        );
    }

    #[test]
    fn malformed_package_relationship_diagnostics_do_not_invent_main_or_story_parts() {
        for relationships in [
            None,
            Some(""),
            Some(
                r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="other" Type="urn:other" Target="word/document.xml"/></Relationships>"#,
            ),
        ] {
            let main = document("<w:p/>");
            let mut entries = vec![("word/document.xml", main.as_str())];
            if let Some(xml) = relationships {
                entries.push(("_rels/.rels", xml));
            }
            let pkg = package(&entries);
            assert!(main_rels(&pkg).is_empty());
            assert_eq!(
                render_parts(&pkg, Some("document")),
                BTreeMap::from([("word/document.xml".into(), (Vec::new(), 1))])
            );
            assert!(parse(&pkg, "word/absent.xml").is_none());
        }
    }

    #[test]
    fn table_style_resets_and_look_flags_report_only_live_conditional_fills() {
        let (d, root) = dom(&format!(
            r#"<w:styles xmlns:w="{W}"><w:style w:type="table" w:styleId="Base"><w:name w:val="Base table"/><w:tblPr><w:shd w:fill="111111"/></w:tblPr><w:tblStylePr w:type="firstRow"><w:tcPr><w:shd w:fill="222222"/></w:tcPr></w:tblStylePr><w:tblStylePr w:type="firstCol"><w:tcPr><w:shd w:fill="333333"/></w:tcPr></w:tblStylePr><w:tblStylePr w:type="band1Horz"><w:tcPr><w:shd w:fill="444444"/></w:tcPr></w:tblStylePr></w:style><w:style w:type="table" w:styleId="Child"><w:name w:val="Child table"/><w:basedOn w:val="Base"/><w:tblPr><w:shd w:val="nil"/></w:tblPr></w:style></w:styles>"#
        ));
        let styles = StyleBook::new(&d, root);
        for (first_row, first_col, no_band, suffix) in [
            (
                "1",
                "1",
                "0",
                " style-shd[firstRow=222222,firstCol=333333,band1Horz=444444]",
            ),
            ("0", "1", "1", " style-shd[firstCol=333333]"),
            ("1", "0", "1", " style-shd[firstRow=222222]"),
            ("0", "0", "1", ""),
        ] {
            let body = format!(
                r#"<w:tbl><w:tblPr><w:tblStyle w:val="Child"/><w:tblLook w:firstRow="{first_row}" w:firstColumn="{first_col}" w:noHBand="{no_band}"/></w:tblPr><w:tblGrid><w:gridCol w:w="2400"/></w:tblGrid><w:tr><w:tc><w:p><w:r><w:t>Cell</w:t></w:r></w:p></w:tc></w:tr></w:tbl>"#
            );
            assert_eq!(
                report(&body, &styles, &[]),
                (
                    vec![format!(
                        "  table 1 1x1 style=\"Child table\"{suffix} \"Cell\""
                    )],
                    1
                )
            );
        }
    }

    #[test]
    fn revisions_cross_transparent_owners_but_live_field_payload_breaks_adjacency() {
        let deleted =
            r#"<w:del w:id="1" w:author="A"><w:r><w:delText>Old</w:delText></w:r></w:del>"#;
        let inserted = r#"<w:ins w:id="2" w:author="A"><w:r><w:t>New</w:t></w:r></w:ins>"#;
        let body = format!(
            r#"<w:p><w:hyperlink r:id="link">{deleted}</w:hyperlink><w:sdt><w:sdtPr><w:id w:val="42"/></w:sdtPr><w:sdtContent>{inserted}</w:sdtContent></w:sdt></w:p><w:p>{inserted}<w:r><w:rPr/><w:t/><w:commentReference w:id="0"/></w:r>{deleted}</w:p>"#
        );
        assert_eq!(
            report(&body, &StyleBook::default(), &[]),
            (
                vec![
                    "  revisions del→ins ×1 \"OldNew\"".into(),
                    "  revisions ins→del ×1 \"NewOld\"".into()
                ],
                2
            )
        );
        for separating_run in [
            "<w:r><w:t> </w:t></w:r>",
            "<w:r><w:tab/></w:r>",
            "<w:r><w:br/></w:r>",
        ] {
            assert_eq!(
                report(
                    &format!("<w:p>{deleted}{separating_run}{inserted}</w:p>"),
                    &StyleBook::default(),
                    &[]
                ),
                (Vec::new(), 1)
            );
        }
        let field = format!(
            r#"<w:p>{deleted}<w:fldSimple w:instr="PAGE"><w:r><w:t>1</w:t></w:r></w:fldSimple>{inserted}</w:p>"#
        );
        assert_eq!(
            report(&field, &StyleBook::default(), &[]),
            (vec!["  field PAGE ×1".into()], 1)
        );
    }
}