Skip to main content

jubarte_dom/
namespaces.rs

1// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
2// SPDX-FileCopyrightText: Microsoft Corporation
3// SPDX-FileCopyrightText: 2025-2026 John Scrudato IV
4//
5// SPDX-License-Identifier: Apache-2.0 AND LicenseRef-Docxodus-MIT
6
7//! WordprocessingML and related namespace / `XName` constants ported from
8//! `PtOpenXmlUtil.ts` (the subset the comparer references) — M1.7.
9//!
10//! Each namespace is exposed as a unit struct with three associated
11//! items:
12//!
13//! - `Ns::ns()` returns the `XNamespace` for the URI;
14//! - `Ns::name(local)` returns an `XName` for `local` in that namespace;
15//! - `Ns::URI` is the namespace URI string (a `const` for switch arms).
16//!
17//! The hottest element and attribute names — those hit on most documents
18//! during hash-cloning, finalize, and the LCS walks — are predefined
19//! on `W` and `PT` as `OnceLock`-cached methods (e.g. `W::p`,
20//! `PT::unid`). Anything else is reachable via
21//! `Ns::name("local")`, which still benefits from the thread-local
22//! intern pool.
23//!
24//! Used by every code path that touches WML/DML/MC names: the comparer,
25//! edit pipeline, accept / reject, inspect, audit, markdown projection,
26//! `revision_processor`. CLI: these constants back every comparison the
27//! `jubarte` subcommands perform.
28
29use crate::xmllinq::{XName, XNamespace};
30
31macro_rules! ns_struct {
32    ($(#[$meta:meta])* $name:ident, $uri:literal) => {
33        $(#[$meta])*
34        #[doc = ""]
35        #[doc = concat!("Marker for the namespace `", $uri, "`. ")]
36        #[doc = "All three associated items are generated:"]
37        #[doc = " [`Self::ns`] for the [`XNamespace`], [`Self::name`] for an"]
38        #[doc = " [`XName`] in it, and [`Self::URI`] as a `&'static str`."]
39        pub struct $name;
40        impl $name {
41            /// The namespace, as an [`XNamespace`] rooted at this URI.
42            pub fn ns() -> XNamespace {
43                XNamespace::get($uri)
44            }
45            /// An [`XName`] for `local` in this namespace.
46            pub fn name(local: &str) -> XName {
47                XName::get(local, $uri)
48            }
49            /// The namespace URI as a `&'static str`. Use it in `match` arms
50            /// and other places that need a string constant.
51            pub const URI: &'static str = $uri;
52        }
53    };
54}
55
56ns_struct!(
57    /// WordprocessingML (`w:`) — the main namespace for Word body, paragraph,
58    /// run, table, and property elements and attributes. The hottest element
59    /// and attribute constants are predefined on this struct ([`W::p`],
60    /// [`W::r`], [`W::sect_pr`], …) as `OnceLock`-cached methods.
61    W,
62    "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
63);
64ns_struct!(
65    /// Office relationships namespace (`r:`) — `r:id` references between
66    /// parts (`word/_rels/document.xml.rels`).
67    R,
68    "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
69);
70ns_struct!(
71    /// Office Math (`m:`) — equations embedded in a paragraph.
72    M,
73    "http://schemas.openxmlformats.org/officeDocument/2006/math"
74);
75ns_struct!(
76    /// Markup compatibility (`mc:`) — `mc:AlternateContent`, `mc:Choice`,
77    /// `mc:Fallback`, `mc:Ignorable`, and the prefix-list attributes
78    /// (`Ignorable`, `PreserveAttributes`, `PreserveElements`,
79    /// `ProcessContent`, `MustUnderstand`, and the unqualified `Requires`
80    /// on `mc:Choice`) that the serializer must rewrite when prefixes are
81    /// rebound.
82    MC,
83    "http://schemas.openxmlformats.org/markup-compatibility/2006"
84);
85ns_struct!(
86    /// Microsoft `office:o` (VML / Office compatibility markup).
87    O,
88    "urn:schemas-microsoft-com:office:office"
89);
90ns_struct!(
91    /// VML (`v:`) — legacy vector markup still used by text boxes, shapes,
92    /// and some legacy drawings.
93    VML,
94    "urn:schemas-microsoft-com:vml"
95);
96ns_struct!(
97    /// DrawingML main (`a:`) — the shared shape / drawing element vocabulary.
98    A,
99    "http://schemas.openxmlformats.org/drawingml/2006/main"
100);
101ns_struct!(
102    /// DrawingML chart (`c:`) — chart parts referenced from `w:drawing` /
103    /// `wp:inline` / `wp:anchor`.
104    C,
105    "http://schemas.openxmlformats.org/drawingml/2006/chart"
106);
107ns_struct!(
108    /// Microsoft Word 2010 wordml (`w14:`) — paragraph / run extensions
109    /// (e.g. `w14:paraId`, `w14:textId`) introduced in Word 2010.
110    W14,
111    "http://schemas.microsoft.com/office/word/2010/wordml"
112);
113ns_struct!(
114    /// Microsoft Word 2012 wordml (`w15:`) — the Word 2013 extensions
115    /// (`w15:commentEx`, `w15:person`, `w15:dataBinding`, …).
116    W15,
117    "http://schemas.microsoft.com/office/word/2012/wordml"
118);
119ns_struct!(
120    /// DrawingML wordprocessingDrawing (`wp:`) — drawing anchors and inlines
121    /// inside a run; the parent namespace for inline and anchor wrapper
122    /// elements.
123    WP,
124    "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
125);
126// PowerTools correlation namespace (`PtOpenXml.pt`). NOTE: the actual URI is
127// `http://powertools.codeplex.com/2011` (verified in PtOpenXmlUtil.ts:3846) —
128// NOT the value the implementation plan claimed. Stamped on every
129// comparison unit and read back to keep the LCS / move detection
130// deterministic; stripped from the output before the package is written.
131ns_struct!(
132    /// PowerTools correlation namespace — `pt:Unid`, `pt:SHA1Hash`,
133    /// `pt:CorrelatedSHA1Hash`, `pt:StructureSHA1Hash`, `pt:Status`,
134    /// `pt:UniqueId`, `pt:RunIds`, and the Word-mode stamps `pt:DefaultLine`,
135    /// `pt:HasDefaultTableStyle`. Attributes stamped on every comparison
136    /// unit and read back to keep the LCS / move detection deterministic;
137    /// stripped from the output before the package is written.
138    PT,
139    "http://powertools.codeplex.com/2011"
140);
141// PowerTools "Insert" namespace (`PtOpenXml.ptOpenXml`).
142ns_struct!(
143    /// PowerTools `ptOpenXml` "Insert" namespace — used by the original
144    /// `DocumentBuilder` to carry `dataBinding` / build traces.
145    PTInsert,
146    "http://powertools.codeplex.com/documentbuilder/2011/insert"
147);
148ns_struct!(
149    /// Microsoft `office:word` (`w10:`) — Word's VML extensions (`w10:wrap`,
150    /// `w10:anchorlock`), distinct from `w14` / `w15`.
151    W10,
152    "urn:schemas-microsoft-com:office:word"
153);
154ns_struct!(
155    /// Microsoft drawing 2010 (`a14:`) — DrawingML shape extensions
156    /// (line / fill / effect extras).
157    A14,
158    "http://schemas.microsoft.com/office/drawing/2010/main"
159);
160ns_struct!(
161    /// Microsoft Word 2010 wordprocessingDrawing (`wp14:`) — drawing
162    /// extensions co-namespaced with `wp:` for the 2010 wordprocessing
163    /// surface.
164    WP14,
165    "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
166);
167ns_struct!(
168    /// DrawingML diagram (`dgm:`) — SmartArt diagrams.
169    DGM,
170    "http://schemas.openxmlformats.org/drawingml/2006/diagram"
171);
172ns_struct!(
173    /// Microsoft `wne:` namespace — Word-specific extension attributes
174    /// (notably used by `w:noProof`, `w:dataBinding`, and notification
175    /// helpers).
176    WNE,
177    "http://schemas.microsoft.com/office/word/2006/wordml"
178);
179
180// ── hottest WML names (extend as needed) ──────────────────────────────────────
181// NAME-01: cache the hottest XNames in process-wide OnceLocks so every compare
182// does not re-enter the interning table for the same syntactic names.
183macro_rules! cached_xname {
184    ($ns_uri:expr, $method:ident, $local:literal) => {
185        #[doc = concat!("Cached [`XName`] for the local name `", $local, "`.")]
186        #[doc = " Stored in a process-wide `OnceLock` so every compare avoids"]
187        #[doc = " re-entering the interning table for the same syntactic name"]
188        #[doc = " (NAME-01)."]
189        pub fn $method() -> XName {
190            static N: std::sync::OnceLock<XName> = std::sync::OnceLock::new();
191            N.get_or_init(|| XName::get($local, $ns_uri)).clone()
192        }
193    };
194}
195
196impl W {
197    cached_xname!(W::URI, document, "document");
198    cached_xname!(W::URI, background, "background");
199    cached_xname!(W::URI, body, "body");
200    cached_xname!(W::URI, p, "p");
201    cached_xname!(W::URI, r, "r");
202    cached_xname!(W::URI, t, "t");
203    // The markers a paragraph may hold and still bring nothing to a join.
204    cached_xname!(W::URI, proof_err, "proofErr");
205    cached_xname!(W::URI, comment_range_start, "commentRangeStart");
206    cached_xname!(W::URI, comment_range_end, "commentRangeEnd");
207    cached_xname!(W::URI, comment_reference, "commentReference");
208    cached_xname!(W::URI, comment, "comment");
209    cached_xname!(W::URI, content_part, "contentPart");
210    cached_xname!(W::URI, perm_start, "permStart");
211    cached_xname!(W::URI, perm_end, "permEnd");
212    cached_xname!(W::URI, p_pr, "pPr");
213    cached_xname!(W::URI, r_pr, "rPr");
214    cached_xname!(W::URI, footnote, "footnote");
215    cached_xname!(W::URI, endnote, "endnote");
216    cached_xname!(W::URI, id, "id");
217    cached_xname!(W::URI, ins, "ins");
218    cached_xname!(W::URI, del, "del");
219    cached_xname!(W::URI, author, "author");
220    cached_xname!(W::URI, date, "date");
221    cached_xname!(W::URI, val, "val");
222    cached_xname!(W::URI, abstract_num, "abstractNum");
223    cached_xname!(W::URI, abstract_num_id, "abstractNumId");
224    // NAME-01b: table / field / bookmark locals hit on every hash-clone walk.
225    cached_xname!(W::URI, tbl, "tbl");
226    cached_xname!(W::URI, tr, "tr");
227    cached_xname!(W::URI, tc, "tc");
228    cached_xname!(W::URI, tc_pr, "tcPr");
229    cached_xname!(W::URI, tbl_pr, "tblPr");
230    cached_xname!(W::URI, tbl_pr_ex, "tblPrEx");
231    cached_xname!(W::URI, tbl_pr_ex_change, "tblPrExChange");
232    cached_xname!(W::URI, tr_pr, "trPr");
233    cached_xname!(W::URI, grid_span, "gridSpan");
234    // The schema-hygiene pass: a table grid's columns and their `w:type`,
235    // shading and its `w:fill` (#1719).
236    cached_xname!(W::URI, grid_col, "gridCol");
237    cached_xname!(W::URI, type_, "type");
238    cached_xname!(W::URI, shd, "shd");
239    cached_xname!(W::URI, fill, "fill");
240    cached_xname!(W::URI, bookmark_start, "bookmarkStart");
241    cached_xname!(W::URI, bookmark_end, "bookmarkEnd");
242    cached_xname!(W::URI, del_text, "delText");
243    cached_xname!(W::URI, sect_pr, "sectPr");
244    cached_xname!(W::URI, drawing, "drawing");
245    cached_xname!(W::URI, object, "object");
246    cached_xname!(W::URI, pict, "pict");
247    cached_xname!(W::URI, txbx_content, "txbxContent");
248    cached_xname!(W::URI, move_from, "moveFrom");
249    cached_xname!(W::URI, move_to, "moveTo");
250    cached_xname!(W::URI, move_from_range_start, "moveFromRangeStart");
251    cached_xname!(W::URI, move_to_range_start, "moveToRangeStart");
252    // NAME-01c: accept / RP pipeline locals (hit every has_rev document).
253    cached_xname!(W::URI, move_from_range_end, "moveFromRangeEnd");
254    cached_xname!(W::URI, move_to_range_end, "moveToRangeEnd");
255    cached_xname!(W::URI, custom_xml, "customXml");
256    cached_xname!(W::URI, sdt, "sdt");
257    cached_xname!(W::URI, sdt_content, "sdtContent");
258    // NAME-01d: hottest remaining finalize/lcs locals (sectPr/pPrChange/
259    // spacing/… were still interning per call — thread-local pool hash each).
260    cached_xname!(W::URI, spacing_el, "spacing");
261    cached_xname!(W::URI, p_style, "pStyle");
262    cached_xname!(W::URI, r_style, "rStyle");
263    cached_xname!(W::URI, jc_el, "jc");
264    cached_xname!(W::URI, footnote_ref, "footnoteRef");
265    cached_xname!(W::URI, endnote_ref, "endnoteRef");
266
267    cached_xname!(W::URI, sdt_pr, "sdtPr");
268    cached_xname!(W::URI, fld_char, "fldChar");
269    cached_xname!(W::URI, instr_text, "instrText");
270    cached_xname!(W::URI, del_instr_text, "delInstrText");
271    cached_xname!(W::URI, num_pr, "numPr");
272    cached_xname!(W::URI, cell_del, "cellDel");
273    cached_xname!(W::URI, cell_ins, "cellIns");
274    cached_xname!(W::URI, cell_merge, "cellMerge");
275    cached_xname!(W::URI, hyperlink, "hyperlink");
276    cached_xname!(W::URI, smart_tag, "smartTag");
277    // Direction overrides: a refusal names them as the run's wrapper.
278    cached_xname!(W::URI, dir, "dir");
279    cached_xname!(W::URI, bdo, "bdo");
280    cached_xname!(W::URI, r_pr_change, "rPrChange");
281    cached_xname!(W::URI, p_pr_change, "pPrChange");
282    cached_xname!(W::URI, numbering_change, "numberingChange");
283    cached_xname!(W::URI, v_merge, "vMerge");
284    cached_xname!(W::URI, hdr, "hdr");
285    cached_xname!(W::URI, ftr, "ftr");
286    cached_xname!(W::URI, fld_simple, "fldSimple");
287    cached_xname!(W::URI, fld_char_type, "fldCharType");
288    cached_xname!(W::URI, instr, "instr");
289    // A section's story references and its distinct first page, read per
290    // section by the page-footer move (#1833).
291    cached_xname!(W::URI, header_reference, "headerReference");
292    cached_xname!(W::URI, footer_reference, "footerReference");
293    cached_xname!(W::URI, title_pg, "titlePg");
294    // Table grid, row and cell properties the comparer and the revision
295    // processor read per table, row or cell.
296    cached_xname!(W::URI, tbl_grid, "tblGrid");
297    cached_xname!(W::URI, tbl_ind, "tblInd");
298    cached_xname!(W::URI, tbl_w, "tblW");
299    cached_xname!(W::URI, tc_w, "tcW");
300    cached_xname!(W::URI, tc_pr_change, "tcPrChange");
301    cached_xname!(W::URI, grid_before, "gridBefore");
302    cached_xname!(W::URI, grid_after, "gridAfter");
303    cached_xname!(W::URI, sdt_end_pr, "sdtEndPr");
304    cached_xname!(W::URI, style_id, "styleId");
305    // The marks Word saves a table with, read per table by the convert fit.
306    cached_xname!(W::URI, rsid_r, "rsidR");
307    cached_xname!(W::URI, rsid_tr, "rsidTr");
308    // A table grid column's width, read per column (`grid_col` is above).
309    cached_xname!(W::URI, w, "w");
310}
311
312impl R {
313    cached_xname!(R::URI, id, "id");
314}
315
316// The drawing and object names the docPr and shape id passes read per
317// drawing or shape.
318impl WP {
319    cached_xname!(WP::URI, doc_pr, "docPr");
320    cached_xname!(WP::URI, inline, "inline");
321}
322
323impl VML {
324    cached_xname!(VML::URI, shape, "shape");
325    cached_xname!(VML::URI, shapetype, "shapetype");
326    cached_xname!(VML::URI, group, "group");
327}
328
329impl O {
330    cached_xname!(O::URI, ole_object, "OLEObject");
331}
332
333impl W14 {
334    cached_xname!(W14::URI, para_id, "paraId");
335}
336
337// ── PowerTools correlation attribute names ────────────────────────────────────
338impl PT {
339    cached_xname!(PT::URI, unid, "Unid");
340    cached_xname!(PT::URI, sha1_hash, "SHA1Hash");
341    cached_xname!(PT::URI, correlated_sha1_hash, "CorrelatedSHA1Hash");
342    cached_xname!(PT::URI, structure_sha1_hash, "StructureSHA1Hash");
343    cached_xname!(PT::URI, status, "Status");
344    // NAME-01c: accept strip / annotate attrs.
345    cached_xname!(PT::URI, unique_id, "UniqueId");
346    cached_xname!(PT::URI, run_ids, "RunIds");
347    // Word-mode stamp on the original root: the line pitch the output's
348    // default paragraph resolves to (read by the demo-spacing strip).
349    cached_xname!(PT::URI, default_line, "DefaultLine");
350    // Word-mode stamp on each source root: "1" when that document's styles
351    // define a default table style (read by the table-margin synthesis).
352    cached_xname!(PT::URI, has_default_table_style, "HasDefaultTableStyle");
353}
354
355impl MC {
356    cached_xname!(MC::URI, alternate_content, "AlternateContent");
357    cached_xname!(MC::URI, choice, "Choice");
358    cached_xname!(MC::URI, fallback, "Fallback");
359    cached_xname!(MC::URI, ignorable, "Ignorable");
360    cached_xname!(MC::URI, preserve_elements, "PreserveElements");
361    cached_xname!(MC::URI, preserve_attributes, "PreserveAttributes");
362}
363
364// ── OMML names the comparer's final-mark pass reads per paragraph ─────────────
365impl M {
366    cached_xname!(M::URI, r, "r");
367    cached_xname!(M::URI, r_pr, "rPr");
368    cached_xname!(M::URI, o_math, "oMath");
369}