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
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
//
// SPDX-License-Identifier: Apache-2.0

//! `jubarte validate --rules a11y,style,structure`: accessibility, style and
//! structure findings with the paragraph id an agent can act on.
//!
//! Every rule reads the admitted package through [`crate::inspect`]'s read
//! model, so a finding's `location` is the same paragraph id (`p12`,
//! `header1.p1`) that `jubarte read --json` prints and an edit plan targets.
//! When a finding has no paragraph (a document-wide setting), the location
//! is the part name.
//!
//! Two rules need a layout pass: `FONT_SUBSTITUTED` reads the renderer's font
//! resolutions, and `STALE_FIELD_CACHE` compares a cached `NUMPAGES` result
//! with the laid-out page count. The pass runs only when one of them is
//! selected and has something to read, and it lays out in
//! `jubarte_render::convert::RevisionStyle::Word` because the cached page count it is checked
//! against is Word's. [`AuditReport::layout`](crate::audit::AuditReport::layout) says whether it ran.
//! [`audit_report_with`](crate::audit::audit_report_with) takes the pass from the caller, or none, for builds
//! that leave the renderer out.

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

use serde::Serialize;

use crate::inspect::{
    InspectError, Opened, body_paragraph_nodes, parse_part, project_paragraph,
    story_paragraph_nodes,
};
use crate::namespaces::{MC, W, WP};
use crate::xmllinq::{Dom, NodeId, XName};
#[cfg(feature = "render")]
use jubarte_render::convert::{PdfOptions, RenderReport, RevisionStyle, docx_render_report};

/// Every rule: `(code, rule set, severity)`, in report order.
pub const RULES: &[(&str, &str, &str)] = &[
    ("HEADING_SKIP", "a11y", "warning"),
    ("IMAGE_NO_DESCR", "a11y", "error"),
    ("TABLE_NO_HEADER_ROW", "a11y", "warning"),
    ("MISSING_LANG", "a11y", "warning"),
    ("LITERAL_BULLET", "style", "warning"),
    ("EMPTY_SPACER_PARAGRAPH", "style", "info"),
    ("DIRECT_FORMATTING_OVERRIDES_STYLE", "style", "info"),
    ("STALE_FIELD_CACHE", "structure", "warning"),
    ("FONT_SUBSTITUTED", "structure", "info"),
];

/// The rule sets [`RULES`] groups codes into: the last three of
/// [`crate::validate::RULE_SETS`].
#[doc(hidden)]
pub const RULE_SETS: &[&str] = crate::validate::RULE_SETS.split_at(2).1;

/// Characters typed as list markers instead of using Word numbering.
const LITERAL_MARKERS: &[char] = &['•', '◦', '▪', '-', '*', '·'];

/// One audit finding.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct AuditFinding {
    /// Rule code.
    pub code: String,
    /// `a11y`, `style` or `structure`.
    pub rule_set: String,
    /// `error`, `warning` or `info`.
    pub severity: String,
    /// A paragraph id (`p12`, `header1.p1`) or a part name.
    pub location: String,
    /// What was found, in one sentence.
    pub message: String,
}

/// Findings plus what ran to produce them.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct AuditReport {
    /// Findings, rule by rule in [`RULES`] order, each in document order.
    pub findings: Vec<AuditFinding>,
    /// The codes that ran.
    pub rules: Vec<String>,
    /// Whether a layout pass ran (`FONT_SUBSTITUTED`, or a numeric
    /// `NUMPAGES` cache for `STALE_FIELD_CACHE`).
    pub layout: bool,
}

/// Why an audit could not run.
#[derive(Debug)]
pub enum AuditError {
    /// The package could not be admitted or read.
    Inspect(InspectError),
    /// The layout pass failed.
    Layout(String),
    /// A requested rule is neither a rule code nor a rule set.
    UnknownRule(String),
}

impl fmt::Display for AuditError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Inspect(error) => write!(f, "{error}"),
            Self::Layout(error) => write!(f, "layout pass failed: {error}"),
            Self::UnknownRule(rule) => {
                let codes: Vec<&str> = RULES.iter().map(|rule| rule.0).collect();
                write!(
                    f,
                    "unknown audit rule {rule}; expected one of {} or {}",
                    RULE_SETS.join(", "),
                    codes.join(", ")
                )
            }
        }
    }
}

impl std::error::Error for AuditError {}

impl From<InspectError> for AuditError {
    fn from(error: InspectError) -> Self {
        Self::Inspect(error)
    }
}

/// Audit `docx` with `rules` (rule sets or codes; empty selects every rule).
///
/// # Errors
///
/// [`AuditError::UnknownRule`] when a rule is neither a known set nor a
/// known code; [`AuditError::Layout`] when the layout pass fails;
/// [`AuditError::Inspect`] when the package cannot be admitted or read.
#[cfg(feature = "render")]
pub fn audit(docx: &[u8], rules: &[&str]) -> Result<Vec<AuditFinding>, AuditError> {
    Ok(audit_report(docx, rules)?.findings)
}

#[cfg(feature = "render")]
/// [`audit`] plus the codes that ran and whether a layout pass ran.
///
/// # Errors
///
/// As [`audit`].
#[cfg(feature = "render")]
pub fn audit_report(docx: &[u8], rules: &[&str]) -> Result<AuditReport, AuditError> {
    audit_report_with(docx, rules, Some(&mut word_layout))
}

#[cfg(feature = "render")]
/// The native layout pass: the document laid out in
/// [`RevisionStyle::Word`], whose page count the `NUMPAGES` cache is
/// checked against.
///
/// # Errors
///
/// [`AuditError::Layout`] when the renderer fails.
pub fn word_layout(docx: &[u8]) -> Result<RenderReport, AuditError> {
    let options = PdfOptions {
        revisions: RevisionStyle::Word,
        ..PdfOptions::default()
    };
    docx_render_report(docx, options).map_err(|error| AuditError::Layout(error.to_string()))
}

/// True when `rule` is one of the audit's rule sets or codes.
#[doc(hidden)]
pub fn is_rule(rule: &str) -> bool {
    RULE_SETS.contains(&rule) || RULES.iter().any(|known| known.0 == rule)
}

#[cfg(feature = "render")]
/// A layout pass [`audit_report_with`] calls at most once.
pub type LayoutPass<'a> = &'a mut dyn FnMut(&[u8]) -> Result<RenderReport, AuditError>;

/// No value: without the `render` feature there is no layout pass, so the
/// only [`LayoutPass`] option [`audit_report_with`] takes is `None`.
#[cfg(not(feature = "render"))]
#[derive(Debug)]
pub enum NoLayout {}

/// Without the `render` feature a layout pass cannot exist: pass `None`.
#[cfg(not(feature = "render"))]
pub type LayoutPass<'a> = &'a NoLayout;

/// `audit_report` (with the `render` feature) with the layout pass supplied by the caller, so a build
/// without the renderer (the slim WASM build) can audit without linking it.
///
/// Without a pass (`None`), `FONT_SUBSTITUTED` is left out of the default
/// selection and asking for it is [`AuditError::Layout`];
/// `STALE_FIELD_CACHE` still reports empty TOC and `NUMPAGES` caches but
/// cannot compare a cached page count with a layout.
///
/// # Errors
///
/// [`AuditError::UnknownRule`] when a rule is neither a known set nor a
/// known code; [`AuditError::Layout`] when the supplied layout pass fails
/// or when `FONT_SUBSTITUTED` is requested without a layout pass;
/// [`AuditError::Inspect`] when the package cannot be admitted or read.
pub fn audit_report_with(
    docx: &[u8],
    rules: &[&str],
    layout: Option<LayoutPass<'_>>,
) -> Result<AuditReport, AuditError> {
    let selected = select(rules, layout.is_some())?;
    let opened = Opened::open(docx)?;
    let package = Package::read(opened)?;
    let mut findings = Vec::new();
    #[cfg(feature = "render")]
    let (mut layout, mut laid_out) = (layout, None);
    for &(code, _, _) in &selected {
        match code {
            "HEADING_SKIP" => package.heading_skip(&mut findings),
            "IMAGE_NO_DESCR" => package.image_no_descr(&mut findings),
            "TABLE_NO_HEADER_ROW" => package.table_no_header_row(&mut findings),
            "MISSING_LANG" => package.missing_lang(&mut findings),
            "LITERAL_BULLET" => package.literal_bullet(&mut findings),
            "EMPTY_SPACER_PARAGRAPH" => package.empty_spacers(&mut findings),
            "DIRECT_FORMATTING_OVERRIDES_STYLE" => package.direct_formatting(&mut findings),
            "STALE_FIELD_CACHE" => {
                let fields = package.fields();
                #[cfg(feature = "render")]
                let pages = {
                    let numeric = fields
                        .iter()
                        .any(|field| field.keyword == "NUMPAGES" && field.number().is_some());
                    match layout.as_deref_mut() {
                        Some(pass) if numeric => {
                            Some(lay_out(docx, pass, &mut laid_out)?.page_count)
                        }
                        _ => None,
                    }
                };
                #[cfg(not(feature = "render"))]
                let pages = None;
                stale_fields(&fields, pages, &mut findings);
            }
            #[cfg(not(feature = "render"))]
            _ => unreachable!("select keeps FONT_SUBSTITUTED only with a layout pass"),
            #[cfg(feature = "render")]
            _ => {
                let pass = layout
                    .as_deref_mut()
                    .expect("select keeps FONT_SUBSTITUTED only with a layout pass");
                let fonts = &lay_out(docx, pass, &mut laid_out)?.fonts;
                let mut seen = BTreeSet::new();
                for font in fonts.iter().filter(|font| font.substituted()) {
                    if seen.insert(font.requested.clone()) {
                        findings.push(finding(
                            code,
                            package.main.clone(),
                            format!(
                                "font \"{}\" is not installed here and is drawn with {} ({})",
                                font.requested,
                                font.physical,
                                font.step.as_str()
                            ),
                        ));
                    }
                }
            }
        }
    }
    Ok(AuditReport {
        findings,
        rules: selected.iter().map(|rule| rule.0.to_string()).collect(),
        #[cfg(feature = "render")]
        layout: laid_out.is_some(),
        #[cfg(not(feature = "render"))]
        layout: false,
    })
}

/// The rules `requested` names, in [`RULES`] order. Without a layout pass
/// the default leaves out `FONT_SUBSTITUTED`, and naming it is an error.
fn select(
    requested: &[&str],
    can_lay_out: bool,
) -> Result<Vec<(&'static str, &'static str, &'static str)>, AuditError> {
    let names = |rule: &(&str, &str, &str), item: &str| rule.0 == item || rule.1 == item;
    let mut wanted = BTreeSet::new();
    for item in requested.iter().map(|item| item.trim()) {
        if !RULES.iter().any(|rule| names(rule, item)) {
            return Err(AuditError::UnknownRule(item.to_string()));
        }
        wanted.extend(
            RULES
                .iter()
                .filter(|rule| names(rule, item))
                .map(|rule| rule.0),
        );
    }
    if requested.is_empty() {
        wanted.extend(RULES.iter().map(|rule| rule.0));
        if !can_lay_out {
            wanted.remove("FONT_SUBSTITUTED");
        }
    } else if !can_lay_out && wanted.contains("FONT_SUBSTITUTED") {
        return Err(AuditError::Layout(
            "FONT_SUBSTITUTED needs a layout pass, which this build does not have".to_string(),
        ));
    }
    Ok(RULES
        .iter()
        .filter(|rule| wanted.contains(rule.0))
        .copied()
        .collect())
}

#[cfg(feature = "render")]
/// The layout report, computed by `pass` on first use.
fn lay_out<'a>(
    docx: &[u8],
    pass: &mut dyn FnMut(&[u8]) -> Result<RenderReport, AuditError>,
    slot: &'a mut Option<RenderReport>,
) -> Result<&'a RenderReport, AuditError> {
    if slot.is_none() {
        *slot = Some(pass(docx)?);
    }
    Ok(slot.as_ref().expect("layout report was just stored"))
}

fn finding(code: &str, location: String, message: String) -> AuditFinding {
    let &(code, rule_set, severity) = RULES
        .iter()
        .find(|rule| rule.0 == code)
        .expect("finding codes come from RULES");
    AuditFinding {
        code: code.to_string(),
        rule_set: rule_set.to_string(),
        severity: severity.to_string(),
        location,
        message,
    }
}

fn on(value: Option<&str>) -> bool {
    !matches!(value, Some("0" | "false" | "off"))
}

/// One parsed part with its addressable paragraphs.
struct Story {
    /// `body`, or the story id (`header1`, `footnotes`, ...).
    id: String,
    /// Part name, the location when no paragraph applies.
    part: String,
    dom: Dom,
    root: NodeId,
    paragraphs: Vec<NodeId>,
}

impl Story {
    /// The id of the indexed paragraph holding `node` (the nearest one,
    /// so an image in a text box reports the paragraph that anchors it).
    fn location(&self, node: NodeId) -> String {
        std::iter::once(node)
            .chain(self.dom.ancestors(node, Some(&W::p())))
            .find_map(|p| self.paragraphs.iter().position(|&q| q == p))
            .map_or_else(|| self.part.clone(), |index| self.paragraph_id(index))
    }

    fn paragraph_id(&self, index: usize) -> String {
        crate::ids::paragraph_id(&self.id, index)
    }
}

/// A style's facts the rules read.
#[derive(Default)]
struct StyleFacts {
    name: Option<String>,
    based_on: Option<String>,
    numbering: Option<bool>,
    font: Option<String>,
    size: Option<String>,
    lang: bool,
}

/// The package, read once.
struct Package {
    main: String,
    stories: Vec<Story>,
    styles_part: Option<String>,
    styles: BTreeMap<String, StyleFacts>,
    default_paragraph_style: String,
    defaults_font: Option<String>,
    defaults_size: Option<String>,
    defaults_lang: bool,
    theme_font_lang: bool,
}

impl Package {
    fn read(opened: Opened) -> Result<Self, AuditError> {
        let story_parts = opened.story_parts();
        let styles_part = opened.related("styles").into_iter().next();
        let settings_part = opened.related("settings").into_iter().next();
        let Opened {
            pkg,
            main,
            dom,
            body,
            ..
        } = opened;
        let mut stories = vec![Story {
            id: "body".to_string(),
            part: main.clone(),
            paragraphs: body_paragraph_nodes(&dom, body),
            dom,
            root: body,
        }];
        for (id, _, part) in story_parts {
            let (dom, _, root) = parse_part(&pkg, &part)?;
            stories.push(Story {
                id,
                paragraphs: story_paragraph_nodes(&dom, root),
                part,
                dom,
                root,
            });
        }
        let mut package = Self {
            main,
            stories,
            styles_part,
            styles: BTreeMap::new(),
            default_paragraph_style: "Normal".to_string(),
            defaults_font: None,
            defaults_size: None,
            defaults_lang: false,
            theme_font_lang: false,
        };
        if let Some(part) = package.styles_part.clone() {
            let (dom, _, root) = parse_part(&pkg, &part)?;
            package.read_styles(&dom, root);
        }
        if let Some(part) = settings_part {
            let (dom, _, root) = parse_part(&pkg, &part)?;
            package.theme_font_lang = dom
                .element(root, &W::name("themeFontLang"))
                .is_some_and(|lang| has_lang(&dom, lang));
        }
        Ok(package)
    }

    fn read_styles(&mut self, dom: &Dom, root: NodeId) {
        if let Some(defaults) = dom.element(root, &W::name("docDefaults")) {
            let rpr = dom
                .element(defaults, &W::name("rPrDefault"))
                .and_then(|default| dom.element(default, &W::r_pr()));
            if let Some(rpr) = rpr {
                (self.defaults_font, self.defaults_size) = run_font_and_size(dom, rpr);
            }
            self.defaults_lang = dom
                .descendants(defaults, Some(&W::name("lang")))
                .into_iter()
                .any(|lang| has_lang(dom, lang));
        }
        let mut marked_default = None;
        let mut named_normal = None;
        for style in dom.elements(root, Some(&W::name("style"))) {
            let Some(id) = dom.attribute(style, &W::name("styleId")) else {
                continue;
            };
            let paragraph = dom.attribute(style, &W::type_()).unwrap_or("paragraph") == "paragraph";
            if !paragraph {
                continue;
            }
            if dom
                .attribute(style, &W::name("default"))
                .is_some_and(|value| matches!(value, "1" | "true" | "on"))
            {
                marked_default.get_or_insert_with(|| id.to_string());
            }
            let child_val = |name: &str| {
                dom.element(style, &W::name(name))
                    .and_then(|node| dom.attribute(node, &W::val()))
                    .map(str::to_string)
            };
            let mut facts = StyleFacts {
                name: child_val("name"),
                based_on: child_val("basedOn"),
                numbering: dom
                    .element(style, &W::p_pr())
                    .and_then(|ppr| dom.element(ppr, &W::num_pr()))
                    .map(|num_pr| numbering_on(dom, num_pr).unwrap_or(true)),
                ..StyleFacts::default()
            };
            if let Some(rpr) = dom.element(style, &W::r_pr()) {
                (facts.font, facts.size) = run_font_and_size(dom, rpr);
                facts.lang = dom
                    .elements(rpr, Some(&W::name("lang")))
                    .into_iter()
                    .any(|lang| has_lang(dom, lang));
            }
            if facts
                .name
                .as_deref()
                .is_some_and(|name| name.eq_ignore_ascii_case("normal"))
            {
                named_normal.get_or_insert_with(|| id.to_string());
            }
            self.styles.insert(id.to_string(), facts);
        }
        // The style marked default; failing that the one named Normal
        // (LibreOffice writes it as `style0` without `w:default`).
        if let Some(id) = marked_default.or(named_normal) {
            self.default_paragraph_style = id;
        }
    }

    /// Paragraph styles from `id` up its `basedOn` chain (cycles cut).
    fn style_chain(&self, id: &str) -> Vec<&StyleFacts> {
        let mut chain = Vec::new();
        let mut seen = BTreeSet::new();
        let mut next = Some(id.to_string());
        while let Some(id) = next {
            if !seen.insert(id.clone()) {
                break;
            }
            let Some(facts) = self.styles.get(&id) else {
                break;
            };
            chain.push(facts);
            next = facts.based_on.clone();
        }
        chain
    }

    fn paragraph_style<'a>(&self, dom: &'a Dom, p: NodeId) -> Option<&'a str> {
        dom.element(p, &W::p_pr())
            .and_then(|ppr| dom.element(ppr, &W::p_style()))
            .and_then(|style| dom.attribute(style, &W::val()))
    }

    /// The heading level of a paragraph style: its `w:name` is
    /// `heading N` (how Word names them in every UI language), else its id
    /// is `HeadingN`.
    fn heading_level(&self, style: &str) -> Option<u32> {
        let from_name = self
            .styles
            .get(style)
            .and_then(|facts| facts.name.as_deref())
            .and_then(|name| level_after(name, "heading "));
        from_name.or_else(|| level_after(style, "heading"))
    }

    /// Direct `w:numPr` decides (a `numId` of 0 removes numbering);
    /// otherwise the style chain's.
    fn numbered(&self, dom: &Dom, p: NodeId) -> bool {
        let direct = dom
            .element(p, &W::p_pr())
            .and_then(|ppr| dom.element(ppr, &W::num_pr()))
            .and_then(|num_pr| numbering_on(dom, num_pr));
        direct.unwrap_or_else(|| {
            self.paragraph_style(dom, p).is_some_and(|style| {
                self.style_chain(style)
                    .into_iter()
                    .find_map(|facts| facts.numbering)
                    .unwrap_or(false)
            })
        })
    }

    fn body(&self) -> &Story {
        &self.stories[0]
    }

    fn heading_skip(&self, findings: &mut Vec<AuditFinding>) {
        let body = self.body();
        let mut previous: Option<u32> = None;
        for (index, &p) in body.paragraphs.iter().enumerate() {
            let Some(level) = self
                .paragraph_style(&body.dom, p)
                .and_then(|style| self.heading_level(style))
            else {
                continue;
            };
            let message = match previous {
                None if level > 1 => {
                    Some(format!("the first heading is level {level}, not level 1"))
                }
                Some(before) if level > before + 1 => Some(format!(
                    "a level {level} heading follows a level {before} heading"
                )),
                _ => None,
            };
            if let Some(message) = message {
                findings.push(finding("HEADING_SKIP", body.paragraph_id(index), message));
            }
            previous = Some(level);
        }
    }

    fn image_no_descr(&self, findings: &mut Vec<AuditFinding>) {
        let descr = XName::get("descr", "");
        for story in &self.stories {
            let dom = &story.dom;
            for doc_pr in dom.descendants(story.root, Some(&WP::doc_pr())) {
                if under_fallback(dom, doc_pr) {
                    continue;
                }
                let described = dom
                    .attribute(doc_pr, &descr)
                    .is_some_and(|text| !text.trim().is_empty());
                if described || decorative(dom, doc_pr) {
                    continue;
                }
                let name = dom
                    .attribute(doc_pr, &XName::get("name", ""))
                    .unwrap_or("unnamed");
                findings.push(finding(
                    "IMAGE_NO_DESCR",
                    story.location(doc_pr),
                    format!(
                        "drawing \"{name}\" has no alternative text and is not marked decorative"
                    ),
                ));
            }
        }
    }

    fn table_no_header_row(&self, findings: &mut Vec<AuditFinding>) {
        for story in &self.stories {
            let dom = &story.dom;
            for table in dom.descendants(story.root, Some(&W::tbl())) {
                let rows = dom.elements(table, Some(&W::tr()));
                if rows.len() < 2 {
                    continue;
                }
                let header = dom
                    .element(rows[0], &W::tr_pr())
                    .and_then(|tr_pr| dom.element(tr_pr, &W::name("tblHeader")))
                    .is_some_and(|header| on(dom.attribute(header, &W::val())));
                if header {
                    continue;
                }
                let location = dom
                    .descendants(table, Some(&W::p()))
                    .first()
                    .map_or_else(|| story.part.clone(), |&p| story.location(p));
                findings.push(finding(
                    "TABLE_NO_HEADER_ROW",
                    location,
                    format!(
                        "a {}-row table does not mark its first row as a header row",
                        rows.len()
                    ),
                ));
            }
        }
    }

    fn missing_lang(&self, findings: &mut Vec<AuditFinding>) {
        let normal = self
            .style_chain(&self.default_paragraph_style)
            .into_iter()
            .any(|facts| facts.lang);
        if self.defaults_lang || normal || self.theme_font_lang {
            return;
        }
        findings.push(finding(
            "MISSING_LANG",
            self.styles_part
                .clone()
                .unwrap_or_else(|| self.main.clone()),
            "no document language is set (docDefaults, the default paragraph style and \
             themeFontLang carry no w:lang)"
                .to_string(),
        ));
    }

    fn literal_bullet(&self, findings: &mut Vec<AuditFinding>) {
        for story in &self.stories {
            for (index, &p) in story.paragraphs.iter().enumerate() {
                let text = project_paragraph(&story.dom, p).text;
                let mut chars = text.trim_start_matches([' ', '\u{a0}']).chars();
                let (Some(marker), Some(gap)) = (chars.next(), chars.next()) else {
                    continue;
                };
                if !LITERAL_MARKERS.contains(&marker) || !matches!(gap, ' ' | '\t' | '\u{a0}') {
                    continue;
                }
                if self.numbered(&story.dom, p) {
                    continue;
                }
                findings.push(finding(
                    "LITERAL_BULLET",
                    story.paragraph_id(index),
                    format!(
                        "the paragraph starts with a typed \"{marker}\" instead of list numbering"
                    ),
                ));
            }
        }
    }

    fn empty_spacers(&self, findings: &mut Vec<AuditFinding>) {
        for story in &self.stories {
            let dom = &story.dom;
            let breaks_section = |p: NodeId| {
                dom.element(p, &W::p_pr())
                    .is_some_and(|ppr| dom.element(ppr, &W::sect_pr()).is_some())
            };
            let empty = |p: NodeId| {
                if !dom.ancestors(p, Some(&W::tc())).is_empty() {
                    return false;
                }
                let projection = project_paragraph(dom, p);
                projection.text.trim().is_empty()
                    && !projection.page_break
                    && projection.limitations.is_empty()
            };
            let mut index = 0;
            while index < story.paragraphs.len() {
                let first = story.paragraphs[index];
                if !empty(first) {
                    index += 1;
                    continue;
                }
                let mut end = index + 1;
                while end < story.paragraphs.len()
                    && dom.parent(story.paragraphs[end]) == dom.parent(first)
                    && empty(story.paragraphs[end])
                    && !breaks_section(story.paragraphs[end - 1])
                {
                    end += 1;
                }
                let count = end - index;
                let last = story.paragraphs[end - 1];
                let before_break = breaks_section(last)
                    || story.paragraphs.get(end).is_some_and(|&next| {
                        dom.parent(next) == dom.parent(first) && breaks_section(next)
                    });
                if count >= 2 && !before_break {
                    findings.push(finding(
                        "EMPTY_SPACER_PARAGRAPH",
                        story.paragraph_id(index),
                        format!(
                            "{count} consecutive empty paragraphs ({} to {}) are used as vertical space",
                            crate::ids::short_paragraph(&story.paragraph_id(index)),
                            crate::ids::short_paragraph(&story.paragraph_id(end - 1))
                        ),
                    ));
                }
                index = end;
            }
        }
    }

    fn direct_formatting(&self, findings: &mut Vec<AuditFinding>) {
        let chain = self.style_chain(&self.default_paragraph_style);
        let font = chain
            .iter()
            .find_map(|facts| facts.font.clone())
            .or_else(|| self.defaults_font.clone());
        let size = chain
            .iter()
            .find_map(|facts| facts.size.clone())
            .or_else(|| self.defaults_size.clone());
        let body = self.body();
        let dom = &body.dom;
        let (mut runs, mut overrides) = (0usize, 0usize);
        for &p in &body.paragraphs {
            let default_style = self
                .paragraph_style(dom, p)
                .is_none_or(|style| style == self.default_paragraph_style);
            if !default_style {
                continue;
            }
            for run in dom.descendants(p, Some(&W::r())) {
                let own = dom.ancestors(run, Some(&W::p())).first() == Some(&p);
                if !own || dom.element(run, &W::t()).is_none() {
                    continue;
                }
                runs += 1;
                let Some(rpr) = dom.element(run, &W::r_pr()) else {
                    continue;
                };
                let (run_font, run_size) = run_font_and_size(dom, rpr);
                let font_differs = run_font.is_some() && run_font != font;
                let size_differs = run_size.is_some() && run_size != size;
                if font_differs || size_differs {
                    overrides += 1;
                }
            }
        }
        if runs > 0 && overrides * 10 > runs * 3 {
            findings.push(finding(
                "DIRECT_FORMATTING_OVERRIDES_STYLE",
                self.main.clone(),
                format!(
                    "{overrides} of {runs} runs in default-style paragraphs set a font or size \
                     that differs from the style"
                ),
            ));
        }
    }

    /// Every complex and simple field, story by story in document order.
    fn fields(&self) -> Vec<Field> {
        let mut out = Vec::new();
        for (story_index, story) in self.stories.iter().enumerate() {
            let dom = &story.dom;
            let mut open: Vec<Field> = Vec::new();
            for node in dom.descendants(story.root, None) {
                if under_fallback(dom, node) {
                    continue;
                }
                if dom.name_is(node, &W::fld_char()) {
                    match dom.attribute(node, &W::name("fldCharType")) {
                        Some("begin") => open.push(Field::new(story_index, node)),
                        Some("separate") => {
                            if let Some(field) = open.last_mut() {
                                field.separated = true;
                            }
                        }
                        Some("end") => {
                            if let Some(mut field) = open.pop() {
                                field.finish(self);
                                out.push(field);
                            }
                        }
                        _ => {}
                    }
                } else if dom.name_is(node, &W::instr_text()) {
                    if let Some(field) = open.last_mut().filter(|field| !field.separated) {
                        field.instruction.push_str(&dom.value_str(node));
                    }
                } else if dom.name_is(node, &W::t()) {
                    let text = dom.value_str(node);
                    for field in open.iter_mut().filter(|field| field.separated) {
                        field.result.push_str(&text);
                    }
                } else if dom.name_is(node, &W::fld_simple()) {
                    let mut field = Field::new(story_index, node);
                    field.instruction = dom
                        .attribute(node, &W::name("instr"))
                        .unwrap_or_default()
                        .to_string();
                    field.separated = true;
                    field.result = dom
                        .descendants(node, Some(&W::t()))
                        .into_iter()
                        .map(|t| dom.value(t))
                        .collect();
                    field.finish(self);
                    out.push(field);
                }
            }
            for mut field in open.into_iter().rev() {
                field.finish(self);
                out.push(field);
            }
        }
        out
    }
}

/// One field's instruction and cached result.
struct Field {
    story: usize,
    node: NodeId,
    instruction: String,
    separated: bool,
    result: String,
    /// Upper-cased first word of the instruction, set by `finish`.
    keyword: String,
    location: String,
}

impl Field {
    fn new(story: usize, node: NodeId) -> Self {
        Self {
            story,
            node,
            instruction: String::new(),
            separated: false,
            result: String::new(),
            keyword: String::new(),
            location: String::new(),
        }
    }

    fn finish(&mut self, package: &Package) {
        self.keyword = self
            .instruction
            .split_whitespace()
            .next()
            .unwrap_or("-")
            .to_ascii_uppercase();
        self.location = package.stories[self.story].location(self.node);
    }

    fn cached(&self) -> bool {
        self.separated && !self.result.trim().is_empty()
    }

    fn number(&self) -> Option<usize> {
        self.cached()
            .then(|| self.result.trim().parse().ok())
            .flatten()
    }
}

fn stale_fields(fields: &[Field], pages: Option<usize>, findings: &mut Vec<AuditFinding>) {
    for field in fields {
        let message = match field.keyword.as_str() {
            "TOC" if !field.cached() => {
                Some("the table of contents has no cached entries; update fields".to_string())
            }
            "NUMPAGES" if !field.cached() => {
                Some("the NUMPAGES field has no cached page count; update fields".to_string())
            }
            "NUMPAGES" => match (field.number(), pages) {
                (Some(cached), Some(pages)) if cached != pages => Some(format!(
                    "the NUMPAGES field caches {cached} pages but the document lays out to {pages}"
                )),
                _ => None,
            },
            _ => None,
        };
        if let Some(message) = message {
            findings.push(finding(
                "STALE_FIELD_CACHE",
                field.location.clone(),
                message,
            ));
        }
    }
}

/// `rFonts` (`ascii`, else `hAnsi`) and `sz` of an `rPr`.
fn run_font_and_size(dom: &Dom, rpr: NodeId) -> (Option<String>, Option<String>) {
    let font = dom.element(rpr, &W::name("rFonts")).and_then(|fonts| {
        dom.attribute(fonts, &W::name("ascii"))
            .or_else(|| dom.attribute(fonts, &W::name("hAnsi")))
            .map(str::to_string)
    });
    let size = dom
        .element(rpr, &W::name("sz"))
        .and_then(|sz| dom.attribute(sz, &W::val()))
        .map(str::to_string);
    (font, size)
}

/// A `w:lang`-shaped element naming any language.
fn has_lang(dom: &Dom, lang: NodeId) -> bool {
    ["val", "eastAsia", "bidi"].iter().any(|attribute| {
        dom.attribute(lang, &W::name(attribute))
            .is_some_and(|value| !value.trim().is_empty())
    })
}

/// `Some(false)` when a `numPr` names `numId` 0 (numbering removed),
/// `Some(true)` for any other `numId`, `None` when it names none.
fn numbering_on(dom: &Dom, num_pr: NodeId) -> Option<bool> {
    dom.element(num_pr, &W::name("numId"))
        .and_then(|num_id| dom.attribute(num_id, &W::val()))
        .map(|id| id.trim() != "0")
}

/// `N` from `{prefix}N` (case-insensitive), for heading levels 1-9.
fn level_after(text: &str, prefix: &str) -> Option<u32> {
    let head = text.get(..prefix.len())?;
    if !head.eq_ignore_ascii_case(prefix) {
        return None;
    }
    text[prefix.len()..]
        .trim()
        .parse()
        .ok()
        .filter(|level| (1..=9).contains(level))
}

/// The Office 2019 decorative flag (`adec:decorative val="1"` in the
/// `docPr`'s extension list). Matched by local name: OOXML defines no
/// `w:decorative`.
fn decorative(dom: &Dom, doc_pr: NodeId) -> bool {
    dom.descendants(doc_pr, None).into_iter().any(|node| {
        dom.name(node)
            .is_some_and(|name| name.local_name() == "decorative")
            && matches!(
                dom.attribute(node, &XName::get("val", "")),
                Some("1" | "true")
            )
    })
}

/// Inside an `mc:Fallback`, which repeats its `mc:Choice` for old readers.
fn under_fallback(dom: &Dom, node: NodeId) -> bool {
    !dom.ancestors(node, Some(&MC::name("Fallback"))).is_empty()
}

// Its one test reads the layout's page facts.
#[cfg(all(test, feature = "render"))]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod public_memory_spacer_ownership_tests {
    use super::*;
    use crate::opc::PartFs;

    fn package(body: &str) -> PartFs {
        let mut pkg = PartFs::open(include_bytes!(
            "../../../tests/fixtures/redline/original.docx"
        ))
        .unwrap();
        pkg.set_part("word/document.xml",format!("<w:document xmlns:w='{}'><w:body>{body}<w:sectPr><w:pgSz w:w='12240' w:h='15840'/></w:sectPr></w:body></w:document>",W::URI).into_bytes());
        pkg
    }
    fn snapshot(pkg: &PartFs) -> Vec<(String, Vec<u8>)> {
        let mut parts = pkg.parts();
        parts.sort();
        parts
            .into_iter()
            .map(|name| {
                let bytes = pkg.part_bytes(&name).unwrap().to_vec();
                (name, bytes)
            })
            .collect()
    }

    #[cfg(feature = "render")]
    #[test]
    fn spacer_audit_respects_authored_sections_page_breaks_and_table_cell_ownership() {
        let blank = "<w:p><w:pPr><w:spacing w:after='80'/></w:pPr></w:p>";
        let section =
            "<w:p><w:pPr><w:sectPr><w:pgSz w:w='12240' w:h='15840'/></w:sectPr></w:pPr></w:p>";
        let page = "<w:p><w:r><w:br w:type='page'/></w:r></w:p>";
        let table = format!(
            "<w:tbl><w:tblPr><w:tblW w:w='1800' w:type='dxa'/></w:tblPr><w:tblGrid><w:gridCol w:w='1800'/></w:tblGrid><w:tr><w:tc><w:tcPr><w:tcW w:w='1800' w:type='dxa'/></w:tcPr>{blank}{blank}</w:tc></w:tr></w:tbl>"
        );
        for (label, body, warns) in [
            ("authored vertical spacer", format!("{blank}{blank}"), true),
            ("single authored blank", blank.to_string(), false),
            (
                "second blank closes source section",
                format!("{blank}{section}"),
                false,
            ),
            (
                "section carrier precedes two independent blanks",
                format!("{section}{blank}{blank}"),
                true,
            ),
            (
                "page break splits blank run",
                format!("{blank}{page}{blank}"),
                false,
            ),
            ("cell blanks own their geometry", table.clone(), false),
            (
                "table separates body spacer owners",
                format!("{blank}{table}{blank}"),
                false,
            ),
        ] {
            let pkg = package(&body);
            let original = snapshot(&pkg);
            let bytes = pkg.to_zip().unwrap();
            let report = audit_report_with(&bytes, &["EMPTY_SPACER_PARAGRAPH"], None).unwrap();
            assert_eq!(report.rules, ["EMPTY_SPACER_PARAGRAPH"]);
            assert!(!report.layout);
            let expected = if warns {
                vec![AuditFinding {
                    code: "EMPTY_SPACER_PARAGRAPH".into(),
                    rule_set: "style".into(),
                    severity: "info".into(),
                    location: if label == "section carrier precedes two independent blanks" {
                        "p1".into()
                    } else {
                        "p0".into()
                    },
                    message: if label == "section carrier precedes two independent blanks" {
                        "2 consecutive empty paragraphs (p1 to p2) are used as vertical space"
                            .into()
                    } else {
                        "2 consecutive empty paragraphs (p0 to p1) are used as vertical space"
                            .into()
                    },
                }]
            } else {
                Vec::new()
            };
            assert_eq!(report.findings, expected, "{label}");
            assert_eq!(
                audit_report_with(&bytes, &["EMPTY_SPACER_PARAGRAPH"], None).unwrap(),
                report
            );
            assert_eq!(
                snapshot(&PartFs::open(&bytes).unwrap()),
                original,
                "{label}: audit is source read-only"
            );
        }
    }
}