@docx4j/core-ts 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/NOTICE +4 -0
  2. package/README.md +92 -4
  3. package/dist/html/convert.mjs +2 -1
  4. package/dist/html/elements.mjs +2 -1
  5. package/dist/model/color/ColorMath.d.mts +29 -0
  6. package/dist/model/color/ColorMath.mjs +146 -0
  7. package/dist/model/color/ColorTables.d.mts +6 -0
  8. package/dist/model/color/ColorTables.mjs +83 -0
  9. package/dist/model/color/DrawingMLColors.d.mts +52 -0
  10. package/dist/model/color/DrawingMLColors.mjs +374 -0
  11. package/dist/model/color/ResolvedColor.d.mts +36 -0
  12. package/dist/model/color/ResolvedColor.mjs +65 -0
  13. package/dist/model/color/SpreadsheetColors.d.mts +36 -0
  14. package/dist/model/color/SpreadsheetColors.mjs +84 -0
  15. package/dist/model/color/StyleFill.d.mts +28 -0
  16. package/dist/model/color/StyleFill.mjs +21 -0
  17. package/dist/model/color/ThemeColors.d.mts +86 -0
  18. package/dist/model/color/ThemeColors.mjs +304 -0
  19. package/dist/model/color/WordColors.d.mts +71 -0
  20. package/dist/model/color/WordColors.mjs +121 -0
  21. package/dist/model/color/index.d.mts +6 -0
  22. package/dist/model/color/index.mjs +30 -0
  23. package/dist/model/content/Body.d.mts +145 -23
  24. package/dist/model/content/Body.mjs +507 -150
  25. package/dist/model/content/Comment.d.mts +23 -5
  26. package/dist/model/content/Comment.mjs +207 -26
  27. package/dist/model/content/ContentControl.d.mts +198 -19
  28. package/dist/model/content/ContentControl.mjs +672 -99
  29. package/dist/model/content/Font.d.mts +70 -17
  30. package/dist/model/content/Font.mjs +119 -36
  31. package/dist/model/content/InlinePicture.d.mts +11 -0
  32. package/dist/model/content/InlinePicture.mjs +21 -1
  33. package/dist/model/content/List.d.mts +6 -3
  34. package/dist/model/content/List.mjs +18 -8
  35. package/dist/model/content/Paragraph.d.mts +200 -14
  36. package/dist/model/content/Paragraph.mjs +898 -82
  37. package/dist/model/content/Range.d.mts +227 -12
  38. package/dist/model/content/Range.mjs +785 -59
  39. package/dist/model/content/ResolvedView.d.mts +104 -0
  40. package/dist/model/content/ResolvedView.mjs +596 -0
  41. package/dist/model/content/Table.d.mts +29 -3
  42. package/dist/model/content/Table.mjs +135 -49
  43. package/dist/model/content/TrackedChange.d.mts +174 -15
  44. package/dist/model/content/TrackedChange.mjs +734 -170
  45. package/dist/model/content/comments.d.mts +20 -6
  46. package/dist/model/content/comments.mjs +67 -57
  47. package/dist/model/content/generalException.d.mts +18 -0
  48. package/dist/model/content/generalException.mjs +24 -0
  49. package/dist/model/content/index.d.mts +10 -3
  50. package/dist/model/content/index.mjs +10 -2
  51. package/dist/model/content/mathText.d.mts +21 -0
  52. package/dist/model/content/mathText.mjs +267 -0
  53. package/dist/model/content/officeText.d.mts +27 -0
  54. package/dist/model/content/officeText.mjs +74 -0
  55. package/dist/model/content/ooxml.mjs +12 -1
  56. package/dist/model/content/outline.d.mts +5 -0
  57. package/dist/model/content/outline.mjs +13 -0
  58. package/dist/model/content/paraIds.d.mts +5 -0
  59. package/dist/model/content/paraIds.mjs +54 -0
  60. package/dist/model/content/placeholders.d.mts +110 -0
  61. package/dist/model/content/placeholders.mjs +253 -0
  62. package/dist/model/content/search.d.mts +74 -0
  63. package/dist/model/content/search.mjs +255 -9
  64. package/dist/model/content/spanEdits.d.mts +159 -0
  65. package/dist/model/content/spanEdits.mjs +728 -0
  66. package/dist/model/content/story.d.mts +47 -0
  67. package/dist/model/content/story.mjs +95 -0
  68. package/dist/model/content/tableColumns.d.mts +1 -1
  69. package/dist/model/content/tableColumns.mjs +108 -80
  70. package/dist/model/content/tableRevisions.d.mts +11 -3
  71. package/dist/model/content/tableRevisions.mjs +72 -42
  72. package/dist/model/content/tableRules.d.mts +30 -0
  73. package/dist/model/content/tableRules.mjs +102 -0
  74. package/dist/model/content/textBoxes.d.mts +18 -0
  75. package/dist/model/content/textBoxes.mjs +105 -0
  76. package/dist/model/content/trackedChangeFilter.d.mts +39 -0
  77. package/dist/model/content/trackedChangeFilter.mjs +81 -0
  78. package/dist/model/content/tracking.d.mts +63 -6
  79. package/dist/model/content/tracking.mjs +242 -22
  80. package/dist/model/content/tree.d.mts +172 -24
  81. package/dist/model/content/tree.mjs +328 -54
  82. package/dist/model/content/wordRange.d.mts +48 -0
  83. package/dist/model/content/wordRange.mjs +97 -0
  84. package/dist/model/customxml/CustomXmlPartCollection.d.mts +30 -1
  85. package/dist/model/customxml/CustomXmlPartCollection.mjs +81 -12
  86. package/dist/model/customxml/XmlMapping.d.mts +32 -8
  87. package/dist/model/customxml/XmlMapping.mjs +64 -10
  88. package/dist/model/customxml/bindings.d.mts +33 -4
  89. package/dist/model/customxml/bindings.mjs +171 -45
  90. package/dist/model/customxml/index.d.mts +1 -0
  91. package/dist/model/customxml/index.mjs +1 -0
  92. package/dist/model/customxml/kinds.d.mts +8 -3
  93. package/dist/model/customxml/kinds.mjs +26 -5
  94. package/dist/model/customxml/opendope.d.mts +4 -1
  95. package/dist/model/customxml/opendope.mjs +9 -6
  96. package/dist/model/customxml/picturePlaceholder.d.mts +17 -0
  97. package/dist/model/customxml/picturePlaceholder.mjs +32 -0
  98. package/dist/model/customxml/pictures.d.mts +4 -3
  99. package/dist/model/customxml/pictures.mjs +22 -8
  100. package/dist/model/customxml/storeItems.d.mts +57 -0
  101. package/dist/model/customxml/storeItems.mjs +122 -0
  102. package/dist/model/customxml/xhtml.mjs +23 -22
  103. package/dist/model/dateZone.d.mts +25 -0
  104. package/dist/model/dateZone.mjs +57 -0
  105. package/dist/model/index.d.mts +3 -0
  106. package/dist/model/index.mjs +5 -0
  107. package/dist/model/listnumbering/Emulator.d.mts +37 -5
  108. package/dist/model/listnumbering/Emulator.mjs +63 -9
  109. package/dist/model/properties/PropertyResolver.d.mts +74 -2
  110. package/dist/model/properties/PropertyResolver.mjs +133 -5
  111. package/dist/model/properties/catalogue.d.mts +19 -5
  112. package/dist/model/properties/catalogue.mjs +41 -15
  113. package/dist/model/properties/index.d.mts +1 -1
  114. package/dist/model/properties/log.d.mts +1 -1
  115. package/dist/model/properties/log.mjs +5 -5
  116. package/dist/model/properties/styleUtil.d.mts +3 -1
  117. package/dist/model/properties/styleUtil.mjs +20 -24
  118. package/dist/model/properties/table.mjs +4 -2
  119. package/dist/model/text/CaseMapping.d.mts +38 -0
  120. package/dist/model/text/CaseMapping.mjs +86 -0
  121. package/dist/model/text/Extractor.d.mts +349 -0
  122. package/dist/model/text/Extractor.mjs +2406 -0
  123. package/dist/model/text/LinkTargets.d.mts +45 -0
  124. package/dist/model/text/LinkTargets.mjs +170 -0
  125. package/dist/model/text/MarkdownExporter.d.mts +140 -0
  126. package/dist/model/text/MarkdownExporter.mjs +255 -0
  127. package/dist/model/text/PlainText.d.mts +29 -0
  128. package/dist/model/text/PlainText.mjs +542 -0
  129. package/dist/model/text/TextContext.d.mts +256 -0
  130. package/dist/model/text/TextContext.mjs +600 -0
  131. package/dist/model/text/TextOptions.d.mts +233 -0
  132. package/dist/model/text/TextOptions.mjs +419 -0
  133. package/dist/model/text/TextResult.d.mts +621 -0
  134. package/dist/model/text/TextResult.mjs +629 -0
  135. package/dist/model/text/Texts.d.mts +99 -0
  136. package/dist/model/text/Texts.mjs +187 -0
  137. package/dist/model/text/WmlToMarkdown.d.mts +120 -0
  138. package/dist/model/text/WmlToMarkdown.mjs +936 -0
  139. package/dist/model/text/caseMapping.generated.d.mts +7 -0
  140. package/dist/model/text/caseMapping.generated.mjs +214 -0
  141. package/dist/model/text/commonmark.d.mts +121 -0
  142. package/dist/model/text/commonmark.mjs +655 -0
  143. package/dist/model/text/index.d.mts +8 -0
  144. package/dist/model/text/index.mjs +9 -0
  145. package/dist/model/text/normalize.d.mts +8 -0
  146. package/dist/model/text/normalize.mjs +13 -0
  147. package/dist/model/text/outline.d.mts +4 -0
  148. package/dist/model/text/outline.mjs +71 -0
  149. package/dist/model/wordIntegers.d.mts +29 -0
  150. package/dist/model/wordIntegers.mjs +78 -0
  151. package/dist/office-js/document.mjs +15 -1
  152. package/dist/office-js/fragment.d.mts +7 -5
  153. package/dist/office-js/fragment.mjs +63 -14
  154. package/dist/office-js/proxy.d.mts +8 -0
  155. package/dist/office-js/proxy.mjs +29 -0
  156. package/dist/office-js/run.mjs +2 -0
  157. package/dist/office-js/supported.generated.mjs +16 -0
  158. package/dist/office-js/toApiScript.mjs +29 -12
  159. package/dist/opc/DirectoryPartStore.d.mts +6 -2
  160. package/dist/opc/DirectoryPartStore.mjs +11 -4
  161. package/dist/opc/FlatOpcPartStore.d.mts +4 -2
  162. package/dist/opc/FlatOpcPartStore.mjs +11 -8
  163. package/dist/opc/Load.d.mts +32 -0
  164. package/dist/opc/Load.mjs +115 -5
  165. package/dist/opc/PartStore.d.mts +34 -1
  166. package/dist/opc/PartStore.mjs +71 -8
  167. package/dist/opc/Save.d.mts +32 -0
  168. package/dist/opc/Save.mjs +110 -4
  169. package/dist/opc/ZipPartStore.d.mts +4 -2
  170. package/dist/opc/ZipPartStore.mjs +12 -7
  171. package/dist/opc/exceptions.d.mts +140 -0
  172. package/dist/opc/exceptions.mjs +108 -0
  173. package/dist/opc/index.d.mts +8 -5
  174. package/dist/opc/index.mjs +5 -3
  175. package/dist/opc/mce/preprocessor.d.mts +141 -5
  176. package/dist/opc/mce/preprocessor.mjs +119 -18
  177. package/dist/opc/mce/understood.d.mts +3 -11
  178. package/dist/opc/mce/understood.mjs +27 -23
  179. package/dist/opc/repair/breaks.d.mts +23 -0
  180. package/dist/opc/repair/breaks.mjs +288 -0
  181. package/dist/opc/repair/hosts.d.mts +12 -0
  182. package/dist/opc/repair/hosts.mjs +33 -0
  183. package/dist/opc/repair/mainRoot.d.mts +19 -0
  184. package/dist/opc/repair/mainRoot.mjs +168 -0
  185. package/dist/opc/repair/unknownElements.d.mts +8 -0
  186. package/dist/opc/repair/unknownElements.mjs +180 -0
  187. package/dist/opc/repair/wmlNames.generated.d.mts +2 -0
  188. package/dist/opc/repair/wmlNames.generated.mjs +80 -0
  189. package/dist/opc/skipped.d.mts +50 -0
  190. package/dist/opc/skipped.mjs +140 -0
  191. package/dist/packages/OpcPackage.d.mts +110 -4
  192. package/dist/packages/OpcPackage.mjs +123 -7
  193. package/dist/packages/WordprocessingMLPackage.d.mts +45 -8
  194. package/dist/packages/WordprocessingMLPackage.mjs +158 -21
  195. package/dist/packages/index.d.mts +1 -1
  196. package/dist/parts/DefaultXmlPart.mjs +2 -2
  197. package/dist/parts/Part.d.mts +14 -2
  198. package/dist/parts/RelationshipsPart.d.mts +15 -0
  199. package/dist/parts/RelationshipsPart.mjs +66 -1
  200. package/dist/parts/XmlPart.d.mts +63 -32
  201. package/dist/parts/XmlPart.mjs +240 -96
  202. package/dist/parts/wml/comments.mjs +30 -0
  203. package/dist/parts/wml/index.d.mts +12 -1
  204. package/dist/parts/wml/index.mjs +37 -1
  205. package/dist/xml/dom.d.mts +7 -0
  206. package/dist/xml/dom.mjs +18 -0
  207. package/package.json +2 -2
@@ -0,0 +1,600 @@
1
+ import * as f from '@docx4j/generated-objects-ts/factory/org_docx4j_wml';
2
+ import { PropertyResolver } from '../properties/PropertyResolver.mjs';
3
+ import { NumberingDefinitions } from '../listnumbering/definitions.mjs';
4
+ import { Emulator } from '../listnumbering/Emulator.mjs';
5
+ import { NumberingStates } from '../listnumbering/state.mjs';
6
+ import { Body } from '../content/Body.mjs';
7
+ import { ResolvedView } from '../content/ResolvedView.mjs';
8
+ import { parsePrefixMappings } from '../customxml/xpath.mjs';
9
+ import { CORE_PROPERTIES_STOREITEMID, EXTENDED_PROPERTIES_STOREITEMID, StoreItemResolver, isPropertiesStoreItem, normaliseStoreItemId } from '../customxml/storeItems.mjs';
10
+ import { parseXml, decodeXmlText } from '../../xml/dom.mjs';
11
+ import { walk } from '@docx4j/generated-objects-ts/builders/wml';
12
+ /** Where a package keeps its numbering part: what `part.absent` names where it has none (WD 0.76). */
13
+ const NUMBERING_PART_NAME = '/word/numbering.xml';
14
+ /** `readContents()`, undefined for a part that cannot be read. */
15
+ async function readQuietly(part) {
16
+ if (!part)
17
+ return undefined;
18
+ try {
19
+ return await part.readContents();
20
+ }
21
+ catch {
22
+ return undefined;
23
+ }
24
+ }
25
+ /**
26
+ * The trees one reading walks, each read **privately** where its part was not unmarshalled
27
+ * (`XmlPart.readContents`), so that reading a document costs no part its byte-for-byte round trip
28
+ * (REQ-204), and the live tree where it was. Every story is read, so that the views' plan covers them
29
+ * all ("the view is the package's"); the glossary only when it is included.
30
+ */
31
+ export class PackageTrees {
32
+ constructor(pkg) {
33
+ this.pkg = pkg;
34
+ /**
35
+ * Each comment's `w16cex:dateUtc`, by its last paragraph's `w14:paraId` (upper case): the comments ids part gives the
36
+ * paragraph's durable id, the comments extensible part the durable id's date (REQ-702's offset; docx4j's `commentDatesUtc`).
37
+ */
38
+ this.commentDatesUtc = new Map();
39
+ /** Each header and footer part related from the main document part, with its content. */
40
+ this.headersAndFooters = new Map();
41
+ /** Each story part that could not be read, with the reason: a reading taking it raises `content.not_read` (REQ-801). */
42
+ this.unreadable = new Map();
43
+ /** The parts this reading could not read that it may need, by role, each with its part's name (CR-012 section 28). */
44
+ this.notRead = new Map();
45
+ /** The custom XML stores and properties parts a binding could not be read against, by normalised store item id: their part names. */
46
+ this.unreadStores = new Map();
47
+ /** The core and application properties' DOMs, by their normalised store item ids. */
48
+ this.stores = new Map();
49
+ /** The custom XML parts' DOMs, in the package's order: what a binding resolves to (CR-020). */
50
+ this.customStores = [];
51
+ this.storeItems = new StoreItemResolver(() => this.customStores);
52
+ }
53
+ /** The trees of a package. */
54
+ static async read(pkg) {
55
+ const trees = new PackageTrees(pkg);
56
+ let main;
57
+ try {
58
+ main = pkg.getMainDocumentPart();
59
+ }
60
+ catch {
61
+ main = undefined;
62
+ }
63
+ if (main) {
64
+ trees.main = { part: main, document: await main.readContents() };
65
+ await trees.readStylesAndNumbering(pkg, main);
66
+ trees.settings = await trees.readOrNote(main.documentSettingsPart, 'settings');
67
+ const footnotes = await trees.readStory(main.footnotesPart);
68
+ if (footnotes && main.footnotesPart)
69
+ trees.footnotes = { part: main.footnotesPart, tree: footnotes };
70
+ const endnotes = await trees.readStory(main.endnotesPart);
71
+ if (endnotes && main.endnotesPart)
72
+ trees.endnotes = { part: main.endnotesPart, tree: endnotes };
73
+ const comments = await trees.readStory(main.commentsPart);
74
+ if (comments && main.commentsPart)
75
+ trees.comments = { part: main.commentsPart, tree: comments };
76
+ trees.commentsExtended = await readQuietly(main.commentsExtendedPart); // passed over silently: CR-012 section 28's D3 (a), not ruled
77
+ await trees.readCommentDates(main);
78
+ for (const part of [...main.headerParts, ...main.footerParts]) {
79
+ const content = await trees.readStory(part);
80
+ if (content)
81
+ trees.headersAndFooters.set(part, content);
82
+ }
83
+ }
84
+ trees.properties = await properties(pkg, (part, role) => trees.readOrNote(part, role));
85
+ await trees.readStores(pkg);
86
+ return trees;
87
+ }
88
+ /**
89
+ * The resolver and the numbering (CR-012 section 28, WD REQ-801). The package's own where they can be built; where a
90
+ * styles or numbering part cannot be read, a resolver of the reading's own without it, as for a package that lacks the
91
+ * part, the part kept in `notRead`. The package's resolver is never set from it, so the content API still refuses the
92
+ * part (CR-001 section 26), and a part put right is read again by the next reading. Where the styles cannot be read the
93
+ * numbering's definitions are read directly and counted by an emulator of the reading's own, so that nothing is kept on
94
+ * the numbering part. A numbering part absent or unread is Word's default definition (`numberingFor`, WD 0.76).
95
+ */
96
+ async readStylesAndNumbering(pkg, main) {
97
+ const stylesPart = main.styleDefinitionsPart;
98
+ const numberingPart = main.numberingDefinitionsPart;
99
+ if (stylesPart || numberingPart) {
100
+ let packages = true;
101
+ try {
102
+ this.numberingResolver = await pkg.getPropertyResolver();
103
+ }
104
+ catch {
105
+ packages = false;
106
+ try {
107
+ this.numberingResolver = await PropertyResolver.create(pkg, {
108
+ onUnreadable: (role) => {
109
+ const part = role === 'styles' ? stylesPart : numberingPart;
110
+ if (part)
111
+ this.notRead.set(role, part.partName.name);
112
+ },
113
+ });
114
+ }
115
+ catch {
116
+ this.numberingResolver = undefined; // hidden text and indents not resolved through styles
117
+ }
118
+ }
119
+ if (stylesPart && !this.notRead.has('styles'))
120
+ this.resolver = this.numberingResolver;
121
+ if (numberingPart && !this.notRead.has('numbering')) {
122
+ try {
123
+ if (packages) {
124
+ await pkg.getNumberingEmulator();
125
+ this.numbering = await numberingPart.getDefinitions();
126
+ this.emulator = numberingPart.getEmulator();
127
+ }
128
+ else {
129
+ const resolver = this.numberingResolver;
130
+ this.numbering = new NumberingDefinitions(await numberingPart.readContents(), (id) => resolver === null || resolver === void 0 ? void 0 : resolver.getStyle(id));
131
+ this.emulator = new Emulator(this.numbering, resolver);
132
+ }
133
+ }
134
+ catch {
135
+ this.numbering = undefined;
136
+ this.emulator = undefined;
137
+ this.notRead.set('numbering', numberingPart.partName.name);
138
+ }
139
+ }
140
+ }
141
+ if (this.emulator === undefined) {
142
+ this.numberingMissing = numberingPart
143
+ ? { kind: 'part.not_read', partName: numberingPart.partName.name }
144
+ : { kind: 'part.absent', partName: NUMBERING_PART_NAME };
145
+ }
146
+ }
147
+ /** `commentDatesUtc`, from the comments ids and extensible parts, read privately; neither read, as D3 (a) leaves them, nothing. */
148
+ async readCommentDates(main) {
149
+ var _a, _b;
150
+ const ids = await readQuietly(main.commentsIdsPart);
151
+ const extensible = await readQuietly(main.commentsExtensiblePart);
152
+ if (!ids || !extensible)
153
+ return;
154
+ const byDurableId = new Map();
155
+ for (const ce of (_a = extensible.commentExtensible) !== null && _a !== void 0 ? _a : []) {
156
+ if (ce.durableId && ce.dateUtc)
157
+ byDurableId.set(ce.durableId.toUpperCase(), ce.dateUtc);
158
+ }
159
+ for (const ci of (_b = ids.commentId) !== null && _b !== void 0 ? _b : []) {
160
+ if (!ci.paraId || !ci.durableId)
161
+ continue;
162
+ const utc = byDurableId.get(ci.durableId.toUpperCase());
163
+ if (utc)
164
+ this.commentDatesUtc.set(ci.paraId.toUpperCase(), utc);
165
+ }
166
+ }
167
+ /** `readContents()`, undefined for a part that cannot be read, which is then kept in `notRead` under its role. */
168
+ async readOrNote(part, role) {
169
+ if (!part)
170
+ return undefined;
171
+ try {
172
+ return await part.readContents();
173
+ }
174
+ catch {
175
+ this.notRead.set(role, part.partName.name);
176
+ return undefined;
177
+ }
178
+ }
179
+ /**
180
+ * The numbering a numbered paragraph is counted by. Where the numbering part is absent or cannot be read, Word's default
181
+ * definition (WD 0.76, REQ-510), made here once, at the first numbered paragraph, and never written to the package: one
182
+ * abstract definition of nine decimal levels, each `w:start` 1 and `w:lvlText` `%{level+1}.`, and one instance per
183
+ * `w:numId` other than 0 the document names, plain where a paragraph style's `w:numPr` names it, else with a
184
+ * `w:startOverride` of 1 at every level, so that the direct paragraphs restart at their first and then share the style's
185
+ * count.
186
+ */
187
+ numberingFor() {
188
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
189
+ if (this.emulator !== undefined || this.numberingMissing === undefined)
190
+ return this.emulator;
191
+ const resolver = this.numberingResolver;
192
+ const plain = new Set();
193
+ for (const style of (_a = resolver === null || resolver === void 0 ? void 0 : resolver.getStylesInOrder()) !== null && _a !== void 0 ? _a : []) {
194
+ const numId = Number(style.type === 'paragraph' ? (_d = (_c = (_b = style.pPr) === null || _b === void 0 ? void 0 : _b.numPr) === null || _c === void 0 ? void 0 : _c.numId) === null || _d === void 0 ? void 0 : _d.val : undefined);
195
+ if (Number.isInteger(numId) && numId !== 0)
196
+ plain.add(numId);
197
+ }
198
+ const direct = new Set();
199
+ const roots = [(_e = this.main) === null || _e === void 0 ? void 0 : _e.document, ...this.headersAndFooters.values(), (_f = this.footnotes) === null || _f === void 0 ? void 0 : _f.tree, (_g = this.endnotes) === null || _g === void 0 ? void 0 : _g.tree, (_h = this.comments) === null || _h === void 0 ? void 0 : _h.tree, (_j = this.glossary) === null || _j === void 0 ? void 0 : _j.tree];
200
+ for (const root of roots) {
201
+ if (root === undefined)
202
+ continue;
203
+ walk(root, (v) => {
204
+ var _a;
205
+ if (v.TYPE_NAME !== 'org_docx4j_wml.PPrBase.NumPr')
206
+ return;
207
+ const numId = Number((_a = v.numId) === null || _a === void 0 ? void 0 : _a.val);
208
+ if (Number.isInteger(numId) && numId !== 0 && !plain.has(numId))
209
+ direct.add(numId);
210
+ });
211
+ }
212
+ const levels = [0, 1, 2, 3, 4, 5, 6, 7, 8];
213
+ const num = (numId, restart) => f.createNumberingNum({
214
+ numId,
215
+ abstractNumId: f.createNumberingNumAbstractNumId({ val: 0 }),
216
+ lvlOverride: restart
217
+ ? levels.map((ilvl) => f.createNumberingNumLvlOverride({ ilvl, startOverride: f.createNumberingNumLvlOverrideStartOverride({ val: 1 }) }))
218
+ : [],
219
+ });
220
+ const tree = f.createNumbering({
221
+ abstractNum: [f.createNumberingAbstractNum({
222
+ abstractNumId: 0,
223
+ lvl: levels.map((ilvl) => f.createLvl({
224
+ ilvl, start: f.createLvlStart({ val: 1 }), numFmt: f.createNumFmt({ val: 'decimal' }), lvlText: f.createLvlLvlText({ val: `%${ilvl + 1}.` }),
225
+ })),
226
+ })],
227
+ num: [...[...plain].map((n) => num(n, false)), ...[...direct].map((n) => num(n, true))],
228
+ });
229
+ this.numbering = new NumberingDefinitions(tree, (id) => resolver === null || resolver === void 0 ? void 0 : resolver.getStyle(id));
230
+ this.emulator = new Emulator(this.numbering, resolver);
231
+ return this.emulator;
232
+ }
233
+ /** Trees with no package: content read as it stands (docx4j's planless `Texts.of(content)`). */
234
+ static none() {
235
+ return new PackageTrees(undefined);
236
+ }
237
+ /** The glossary document, read when `include: glossary` first asks for it. */
238
+ async readGlossary() {
239
+ var _a;
240
+ const part = (_a = this.main) === null || _a === void 0 ? void 0 : _a.part.glossaryDocumentPart;
241
+ if (this.glossary || !part)
242
+ return;
243
+ const tree = await this.readStory(part);
244
+ if (tree)
245
+ this.glossary = { part, tree };
246
+ }
247
+ /** A story part's content; undefined where it has none, and where it could not be read, then kept in `unreadable`. */
248
+ async readStory(part) {
249
+ if (!part)
250
+ return undefined;
251
+ try {
252
+ return await part.readContents();
253
+ }
254
+ catch (e) {
255
+ this.unreadable.set(part, e);
256
+ return undefined;
257
+ }
258
+ }
259
+ // --- bound values (REQ-509) ---------------------------------------------------------------------
260
+ async readStores(pkg) {
261
+ let context;
262
+ for (const [id, part] of pkg.customXmlDataStorageParts) {
263
+ let doc;
264
+ try {
265
+ doc = await part.parseDocument();
266
+ context !== null && context !== void 0 ? context : (context = doc);
267
+ }
268
+ catch {
269
+ // read as absent (CR-012 section 28, D3; WD 0.69): no candidate for a binding, which REQ-509's repair resolves to the
270
+ // store its XPath fits, else its surface; its id kept, for the part.not_read a binding naming it raises
271
+ this.unreadStores.set(normaliseStoreItemId(id), part.partName.name);
272
+ continue;
273
+ }
274
+ this.customStores.push({ part: doc, id, partName: part.partName.name, root: doc.documentElement, selects: (xpath, mappings) => this.selectIn(doc, xpath, mappings).length > 0 });
275
+ }
276
+ for (const [id, part] of [[CORE_PROPERTIES_STOREITEMID, pkg.docPropsCorePart], [EXTENDED_PROPERTIES_STOREITEMID, pkg.docPropsExtendedPart]]) {
277
+ if (!part)
278
+ continue;
279
+ try {
280
+ // the part's XML as it would be saved, parsed apart from it: the part keeps its round trip
281
+ const doc = parseXml(decodeXmlText(await part.getBytes()));
282
+ this.stores.set(normaliseStoreItemId(id), doc);
283
+ context !== null && context !== void 0 ? context : (context = doc);
284
+ }
285
+ catch {
286
+ this.stores.set(normaliseStoreItemId(id), undefined);
287
+ this.unreadStores.set(normaliseStoreItemId(id), part.partName.name);
288
+ }
289
+ }
290
+ if (context === undefined)
291
+ return;
292
+ try {
293
+ await pkg.xpathEngine.ready(context);
294
+ this.xpath = pkg.xpathEngine;
295
+ }
296
+ catch {
297
+ this.xpath = undefined; // no XPath here: every control's surface is read
298
+ }
299
+ }
300
+ /**
301
+ * A data binding's value, docx4j `BoundValues`: the binding's XPath, with its own prefix mappings,
302
+ * against the custom XML part its `w:storeItemID` names (or the core or application properties), or,
303
+ * where it names none, the part its XPath fits, as Word repairs it (CR-020; nothing is written back);
304
+ * the string value of the first node selected. Undefined where the binding does not resolve.
305
+ */
306
+ boundValue(binding) {
307
+ var _a, _b;
308
+ if (!binding.xpath || !binding.storeItemID || !this.xpath)
309
+ return undefined;
310
+ const doc = isPropertiesStoreItem(binding.storeItemID)
311
+ ? this.stores.get(normaliseStoreItemId(binding.storeItemID))
312
+ : (_a = this.storeItems.resolve(binding.storeItemID, binding.xpath, binding.prefixMappings)) === null || _a === void 0 ? void 0 : _a.part;
313
+ try {
314
+ const node = this.selectIn(doc, binding.xpath, binding.prefixMappings)[0];
315
+ if (!node)
316
+ return undefined;
317
+ return node.nodeType === 2 ? node.value : ((_b = node.textContent) !== null && _b !== void 0 ? _b : '');
318
+ }
319
+ catch {
320
+ return undefined;
321
+ }
322
+ }
323
+ /**
324
+ * The name of the part a binding names that could not be read: a custom XML store, or the core or application
325
+ * properties (CR-012 section 28, D3); undefined where it names a part that was read, or none.
326
+ */
327
+ unreadStoreOf(binding) {
328
+ return binding.storeItemID ? this.unreadStores.get(normaliseStoreItemId(binding.storeItemID)) : undefined;
329
+ }
330
+ /** The nodes an XPath selects in a store, with a binding's prefix mappings (the default namespace's left out). */
331
+ selectIn(doc, xpath, prefixMappings) {
332
+ if (!doc || !this.xpath)
333
+ return [];
334
+ const mappings = parsePrefixMappings(prefixMappings);
335
+ delete mappings[''];
336
+ return this.xpath.select(xpath, doc, mappings);
337
+ }
338
+ // --- the comments anchored in the document (REQ-504) -----------------------------------------------
339
+ /**
340
+ * The ids of the comments anchored in the document: a `w:commentReference` or a `w:commentRangeStart`
341
+ * in the main document, a header or footer, or a note, in any branch.
342
+ */
343
+ anchoredComments() {
344
+ var _a, _b, _c;
345
+ if (this.anchored)
346
+ return this.anchored;
347
+ const ids = new Set();
348
+ const roots = [(_a = this.main) === null || _a === void 0 ? void 0 : _a.document, ...this.headersAndFooters.values(), (_b = this.footnotes) === null || _b === void 0 ? void 0 : _b.tree, (_c = this.endnotes) === null || _c === void 0 ? void 0 : _c.tree];
349
+ for (const root of roots) {
350
+ if (root === undefined)
351
+ continue;
352
+ walk(root, (v) => {
353
+ const tn = v.TYPE_NAME;
354
+ if (tn === 'org_docx4j_wml.R.CommentReference' || tn === 'org_docx4j_wml.CommentRangeStart') {
355
+ const id = v.id;
356
+ if (id !== undefined && id !== null)
357
+ ids.add(String(id));
358
+ }
359
+ });
360
+ }
361
+ return (this.anchored = ids);
362
+ }
363
+ /** Every story's body, over these trees, for the views' plan: the main document, each note and comment, the headers and footers. */
364
+ stories() {
365
+ var _a, _b, _c, _d;
366
+ const pkg = this.pkg;
367
+ const out = [];
368
+ if ((_a = this.main) === null || _a === void 0 ? void 0 : _a.document.body)
369
+ out.push(new Body(this.main.part, this.main.document.body, 'body', pkg));
370
+ for (const [holder, kind] of [[this.footnotes, 'footnote'], [this.endnotes, 'endnote']]) {
371
+ if (!holder)
372
+ continue;
373
+ const notes = (_b = (kind === 'footnote' ? holder.tree.footnote : holder.tree.endnote)) !== null && _b !== void 0 ? _b : [];
374
+ for (const note of notes)
375
+ out.push(new Body(holder.part, note, `${kind}:${note.id}`, pkg));
376
+ }
377
+ for (const comment of (_d = (_c = this.comments) === null || _c === void 0 ? void 0 : _c.tree.comment) !== null && _d !== void 0 ? _d : []) {
378
+ out.push(new Body(this.comments.part, comment, `comment:${comment.id}`, pkg));
379
+ }
380
+ for (const [part, content] of this.headersAndFooters)
381
+ out.push(new Body(part, content, 'story', pkg));
382
+ return out;
383
+ }
384
+ /** The part whose tree holds an object, by its `PARENT`s, or undefined. */
385
+ partOf(object) {
386
+ let root = object;
387
+ for (let o = object; o; o = o.PARENT)
388
+ root = o;
389
+ if (this.main && (root === this.main.document || root === this.main.document.body))
390
+ return this.main.part;
391
+ for (const [part, content] of this.headersAndFooters)
392
+ if (root === content)
393
+ return part;
394
+ if (this.footnotes && root === this.footnotes.tree)
395
+ return this.footnotes.part;
396
+ if (this.endnotes && root === this.endnotes.tree)
397
+ return this.endnotes.part;
398
+ if (this.comments && root === this.comments.tree)
399
+ return this.comments.part;
400
+ if (this.glossary && root === this.glossary.tree)
401
+ return this.glossary.part;
402
+ return undefined;
403
+ }
404
+ }
405
+ /**
406
+ * What a story needs from the rest of the document (CR-012 section 4.4): the note references' order, for
407
+ * `[n]`; the numbering states per story, for list labels and `value`; the bookmarks, for `fields: update`;
408
+ * and, made once, each view's plan of what Accept All or Reject All would leave. A call given a context
409
+ * uses it and adds to it, so that the editor's calls over one part at a time, in the order the
410
+ * specification reads the stories (REQ-501), number the notes and count the lists as one reading of the
411
+ * whole package does; `TextResult.concat` assembles their results.
412
+ *
413
+ * A context is a snapshot of the package as it was read (`Texts.contextOf`): after the package changes,
414
+ * make another. It is for one reading, in one view: the calls under it take the same options, as the parts
415
+ * of one `Texts.of(pkg)` would.
416
+ */
417
+ export class TextContext {
418
+ constructor(trees) {
419
+ this.trees = trees;
420
+ /** Notes referenced, in reference order: id to number, 0 for a note with a custom mark. */
421
+ this.footnotes = new Map();
422
+ this.endnotes = new Map();
423
+ this.footnoteCount = 0;
424
+ this.endnoteCount = 0;
425
+ /** The numbering states, one per story (`Emulator`). */
426
+ this.numberingStates = new NumberingStates();
427
+ /** The `w:lastRenderedPageBreak`s of the main story so far (REQ-708). */
428
+ this.pageMarkers = 0;
429
+ /** The bookmarks' text, by lower-cased name (REQ-506's REF, CR-012 phase 5a). */
430
+ this.bookmarks = new Map();
431
+ /** Whether the main story has been read under this context, which numbers the notes. */
432
+ this.mainRead = false;
433
+ this.plans = new Map();
434
+ /** Per container, then per options, what the blocks begin inside (`Extractor.fieldsAtBlocks`), made once; shared with siblings. */
435
+ this.starts = new WeakMap();
436
+ }
437
+ /** What `make` gives for a container's blocks under options keyed `key`, made once in this context (CR-012 section 17.13). */
438
+ blockStarts(container, key, make) {
439
+ let byKey = this.starts.get(container);
440
+ if (!byKey)
441
+ this.starts.set(container, byKey = new Map());
442
+ if (!byKey.has(key))
443
+ byKey.set(key, make());
444
+ return byKey.get(key);
445
+ }
446
+ /** The package this context reads. */
447
+ get pkg() {
448
+ return this.trees.pkg;
449
+ }
450
+ /** A note's number, given at its first reference: in reference order, footnotes and endnotes apart, 0 for a custom mark (REQ-503). */
451
+ noteNumber(type, id, customMark) {
452
+ const numbers = type === 'footnote' ? this.footnotes : this.endnotes;
453
+ let n = numbers.get(id);
454
+ if (n === undefined) {
455
+ n = customMark ? 0 : type === 'footnote' ? ++this.footnoteCount : ++this.endnoteCount;
456
+ numbers.set(id, n);
457
+ }
458
+ return n;
459
+ }
460
+ /** The plan of Accept All (`accepted`) or Reject All, over every story, made once. */
461
+ plan(accepted) {
462
+ let plan = this.plans.get(accepted);
463
+ if (!plan) {
464
+ const stories = this.trees.stories();
465
+ plan = accepted ? ResolvedView.accepted(stories) : ResolvedView.original(stories);
466
+ this.plans.set(accepted, plan);
467
+ }
468
+ return plan;
469
+ }
470
+ /** A context over the same trees and plans, its own notes and states: for a reading whose blocks are not the result's. */
471
+ sibling() {
472
+ const c = new TextContext(this.trees);
473
+ for (const [k, v] of this.plans)
474
+ c.plans.set(k, v);
475
+ c.starts = this.starts;
476
+ return c;
477
+ }
478
+ /** Adopts the note numbers another context gave (`sibling`'s, for a notes part read before its story). */
479
+ adoptNotes(other) {
480
+ for (const [id, n] of other.footnotes)
481
+ if (!this.footnotes.has(id))
482
+ this.footnotes.set(id, n);
483
+ for (const [id, n] of other.endnotes)
484
+ if (!this.endnotes.has(id))
485
+ this.endnotes.set(id, n);
486
+ this.footnoteCount = Math.max(this.footnoteCount, other.footnoteCount);
487
+ this.endnoteCount = Math.max(this.endnoteCount, other.endnoteCount);
488
+ }
489
+ }
490
+ /** The properties under `core`, `app` and `custom`, each where the package has that part and it can be read. */
491
+ async function properties(pkg, read) {
492
+ const out = {};
493
+ const c = await read(pkg.docPropsCorePart, 'core');
494
+ if (c)
495
+ out.core = coreProperties(c);
496
+ const a = await read(pkg.docPropsExtendedPart, 'app');
497
+ if (a)
498
+ out.app = appProperties(a);
499
+ const x = await read(pkg.docPropsCustomPart, 'custom');
500
+ if (x)
501
+ out.custom = customProperties(x);
502
+ return out;
503
+ }
504
+ function put(m, name, value) {
505
+ if (value === undefined || value === null || value === '')
506
+ return;
507
+ m[name] = value;
508
+ }
509
+ function literal(o) {
510
+ const v = typeof o === 'object' && o !== null && 'value' in o && 'name' in o ? o.value : o;
511
+ const content = v === null || v === void 0 ? void 0 : v.content;
512
+ return content === undefined || content.length === 0 ? undefined : content.join('');
513
+ }
514
+ /** The core properties in the schema's order. */
515
+ function coreProperties(c) {
516
+ const m = {};
517
+ put(m, 'title', literal(c.title));
518
+ put(m, 'subject', literal(c.subject));
519
+ put(m, 'creator', literal(c.creator));
520
+ put(m, 'keywords', c.keywords);
521
+ put(m, 'description', literal(c.description));
522
+ put(m, 'category', c.category);
523
+ put(m, 'contentStatus', c.contentStatus);
524
+ put(m, 'language', literal(c.language));
525
+ put(m, 'identifier', literal(c.identifier));
526
+ put(m, 'version', c.version);
527
+ put(m, 'revision', c.revision);
528
+ put(m, 'lastModifiedBy', c.lastModifiedBy);
529
+ put(m, 'created', literal(c.created));
530
+ put(m, 'modified', literal(c.modified));
531
+ put(m, 'lastPrinted', calendarText(c.lastPrinted));
532
+ return m;
533
+ }
534
+ /** The application properties that describe the document; the statistics, which depend on layout, left out. */
535
+ function appProperties(p) {
536
+ const m = {};
537
+ put(m, 'Application', p.application);
538
+ put(m, 'AppVersion', p.appVersion);
539
+ put(m, 'Company', p.company);
540
+ put(m, 'Manager', p.manager);
541
+ put(m, 'Template', p.template);
542
+ put(m, 'HyperlinkBase', p.hyperlinkBase);
543
+ put(m, 'TotalTime', p.totalTime);
544
+ put(m, 'DocSecurity', p.docSecurity);
545
+ return m;
546
+ }
547
+ /** Each custom property's typed value, in the part's order; values of other types left out. */
548
+ function customProperties(p) {
549
+ var _a;
550
+ const m = {};
551
+ for (const property of (_a = p.property) !== null && _a !== void 0 ? _a : []) {
552
+ if (property.name === undefined)
553
+ continue;
554
+ put(m, property.name, customValue(property));
555
+ }
556
+ return m;
557
+ }
558
+ function customValue(p) {
559
+ for (const key of ['lpwstr', 'lpstr', 'bstr', 'bool', 'i1', 'i2', 'i4', 'i8', '_int', 'ui1', 'ui2', 'ui4', 'ui8', 'uint', 'r4', 'r8', 'decimal']) {
560
+ const v = p[key];
561
+ if (v !== undefined && v !== null)
562
+ return v;
563
+ }
564
+ if (p.filetime !== undefined)
565
+ return calendarText(p.filetime);
566
+ if (p.date !== undefined)
567
+ return calendarText(p.date);
568
+ if (p.cy !== undefined)
569
+ return p.cy;
570
+ if (p.error !== undefined)
571
+ return p.error;
572
+ return undefined;
573
+ }
574
+ /** An XML calendar as its lexical form. */
575
+ export function calendarText(date) {
576
+ if (date === undefined || date === null)
577
+ return undefined;
578
+ if (typeof date === 'string')
579
+ return date;
580
+ const c = date;
581
+ if (typeof c.year !== 'number')
582
+ return String(date);
583
+ const p2 = (n) => String(n !== null && n !== void 0 ? n : 0).padStart(2, '0');
584
+ let out = `${String(c.year).padStart(4, '0')}-${p2(c.month)}-${p2(c.day)}`;
585
+ if (c.hour !== undefined) {
586
+ out += `T${p2(c.hour)}:${p2(c.minute)}:${p2(c.second)}`;
587
+ if (c.fractionalSecond)
588
+ out += String(c.fractionalSecond).replace(/^0/, '');
589
+ }
590
+ if (c.timezone !== undefined) {
591
+ if (c.timezone === 0)
592
+ out += 'Z';
593
+ else {
594
+ const sign = c.timezone < 0 ? '-' : '+';
595
+ const minutes = Math.abs(c.timezone);
596
+ out += `${sign}${p2(Math.floor(minutes / 60))}:${p2(minutes % 60)}`;
597
+ }
598
+ }
599
+ return out;
600
+ }