@beyondwork/docx-react-component 1.0.28 → 1.0.29

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 (354) hide show
  1. package/dist/canonical-document-BLEbzL2J.d.cts +844 -0
  2. package/dist/canonical-document-BLEbzL2J.d.ts +844 -0
  3. package/dist/chunk-2FJS5GZM.js +763 -0
  4. package/dist/chunk-2FJS5GZM.js.map +1 -0
  5. package/{src/core/commands/section-layout-commands.ts → dist/chunk-2OQBZS3F.js} +106 -340
  6. package/dist/chunk-2OQBZS3F.js.map +1 -0
  7. package/dist/chunk-2S7W4KFO.js +127 -0
  8. package/dist/chunk-2S7W4KFO.js.map +1 -0
  9. package/dist/chunk-2TG72QSW.js +3874 -0
  10. package/dist/chunk-2TG72QSW.js.map +1 -0
  11. package/{src/core/commands/table-structure-commands.ts → dist/chunk-36QNIZBO.js} +126 -315
  12. package/dist/chunk-36QNIZBO.js.map +1 -0
  13. package/dist/chunk-4AQOYAW4.js +3069 -0
  14. package/dist/chunk-4AQOYAW4.js.map +1 -0
  15. package/dist/chunk-4D5EWJ3P.js +77 -0
  16. package/dist/chunk-4D5EWJ3P.js.map +1 -0
  17. package/dist/chunk-5FN54NDH.js +2257 -0
  18. package/dist/chunk-5FN54NDH.js.map +1 -0
  19. package/dist/chunk-BOYGQYRQ.js +7306 -0
  20. package/dist/chunk-BOYGQYRQ.js.map +1 -0
  21. package/dist/chunk-CN3XMECL.js +212 -0
  22. package/dist/chunk-CN3XMECL.js.map +1 -0
  23. package/dist/chunk-EBI3BX6U.js +164 -0
  24. package/dist/chunk-EBI3BX6U.js.map +1 -0
  25. package/dist/chunk-EILUG3VB.js +1275 -0
  26. package/dist/chunk-EILUG3VB.js.map +1 -0
  27. package/dist/chunk-FUDY333O.js +70 -0
  28. package/dist/chunk-FUDY333O.js.map +1 -0
  29. package/dist/chunk-GBVOWFIK.js +1237 -0
  30. package/dist/chunk-GBVOWFIK.js.map +1 -0
  31. package/dist/chunk-H4TQ3H3Y.js +262 -0
  32. package/dist/chunk-H4TQ3H3Y.js.map +1 -0
  33. package/{src/core/commands/style-commands.ts → dist/chunk-JGB3IXZO.js} +40 -113
  34. package/dist/chunk-JGB3IXZO.js.map +1 -0
  35. package/dist/chunk-KD2QRQPY.js +4342 -0
  36. package/dist/chunk-KD2QRQPY.js.map +1 -0
  37. package/dist/chunk-KLMXQVYK.js +369 -0
  38. package/dist/chunk-KLMXQVYK.js.map +1 -0
  39. package/dist/chunk-KZUG5KFQ.js +214 -0
  40. package/dist/chunk-KZUG5KFQ.js.map +1 -0
  41. package/{src/core/state/text-transaction.ts → dist/chunk-QDAQ4CJU.js} +79 -236
  42. package/dist/chunk-QDAQ4CJU.js.map +1 -0
  43. package/{src/legal/bookmarks.ts → dist/chunk-RMH72RZI.js} +44 -130
  44. package/dist/chunk-RMH72RZI.js.map +1 -0
  45. package/dist/chunk-SWKWQZXM.js +117 -0
  46. package/dist/chunk-SWKWQZXM.js.map +1 -0
  47. package/{src/core/commands/formatting-commands.ts → dist/chunk-TJBP2K4T.js} +196 -536
  48. package/dist/chunk-TJBP2K4T.js.map +1 -0
  49. package/dist/chunk-TLCEAQDQ.js +542 -0
  50. package/dist/chunk-TLCEAQDQ.js.map +1 -0
  51. package/{src/core/commands/text-commands.ts → dist/chunk-UZXBISGO.js} +86 -142
  52. package/dist/chunk-UZXBISGO.js.map +1 -0
  53. package/dist/chunk-WGBAKP3Q.js +3220 -0
  54. package/dist/chunk-WGBAKP3Q.js.map +1 -0
  55. package/dist/compare/index.cjs +5475 -0
  56. package/dist/compare/index.cjs.map +1 -0
  57. package/dist/compare/index.d.cts +114 -0
  58. package/dist/compare/index.d.ts +114 -0
  59. package/dist/compare/index.js +731 -0
  60. package/dist/compare/index.js.map +1 -0
  61. package/dist/core/commands/formatting-commands.cjs +828 -0
  62. package/dist/core/commands/formatting-commands.cjs.map +1 -0
  63. package/dist/core/commands/formatting-commands.d.cts +63 -0
  64. package/dist/core/commands/formatting-commands.d.ts +63 -0
  65. package/dist/core/commands/formatting-commands.js +37 -0
  66. package/dist/core/commands/formatting-commands.js.map +1 -0
  67. package/dist/core/commands/image-commands.cjs +2023 -0
  68. package/dist/core/commands/image-commands.cjs.map +1 -0
  69. package/dist/core/commands/image-commands.d.cts +58 -0
  70. package/dist/core/commands/image-commands.d.ts +58 -0
  71. package/dist/core/commands/image-commands.js +18 -0
  72. package/dist/core/commands/image-commands.js.map +1 -0
  73. package/dist/core/commands/section-layout-commands.cjs +477 -0
  74. package/dist/core/commands/section-layout-commands.cjs.map +1 -0
  75. package/dist/core/commands/section-layout-commands.d.cts +62 -0
  76. package/dist/core/commands/section-layout-commands.d.ts +62 -0
  77. package/dist/core/commands/section-layout-commands.js +21 -0
  78. package/dist/core/commands/section-layout-commands.js.map +1 -0
  79. package/dist/core/commands/style-commands.cjs +214 -0
  80. package/dist/core/commands/style-commands.cjs.map +1 -0
  81. package/dist/core/commands/style-commands.d.cts +13 -0
  82. package/dist/core/commands/style-commands.d.ts +13 -0
  83. package/dist/core/commands/style-commands.js +9 -0
  84. package/dist/core/commands/style-commands.js.map +1 -0
  85. package/dist/core/commands/table-structure-commands.cjs +1883 -0
  86. package/dist/core/commands/table-structure-commands.cjs.map +1 -0
  87. package/dist/core/commands/table-structure-commands.d.cts +59 -0
  88. package/dist/core/commands/table-structure-commands.d.ts +59 -0
  89. package/dist/core/commands/table-structure-commands.js +12 -0
  90. package/dist/core/commands/table-structure-commands.js.map +1 -0
  91. package/dist/core/commands/text-commands.cjs +2391 -0
  92. package/dist/core/commands/text-commands.cjs.map +1 -0
  93. package/dist/core/commands/text-commands.d.cts +24 -0
  94. package/dist/core/commands/text-commands.d.ts +24 -0
  95. package/dist/core/commands/text-commands.js +28 -0
  96. package/dist/core/commands/text-commands.js.map +1 -0
  97. package/dist/core/selection/mapping.cjs +200 -0
  98. package/dist/core/selection/mapping.cjs.map +1 -0
  99. package/dist/core/selection/mapping.d.cts +2 -0
  100. package/dist/core/selection/mapping.d.ts +2 -0
  101. package/dist/core/selection/mapping.js +31 -0
  102. package/dist/core/selection/mapping.js.map +1 -0
  103. package/dist/core/state/editor-state.cjs +2278 -0
  104. package/dist/core/state/editor-state.cjs.map +1 -0
  105. package/dist/core/state/editor-state.d.cts +2 -0
  106. package/dist/core/state/editor-state.d.ts +2 -0
  107. package/dist/core/state/editor-state.js +26 -0
  108. package/dist/core/state/editor-state.js.map +1 -0
  109. package/dist/index.cjs +38553 -0
  110. package/dist/index.cjs.map +1 -0
  111. package/dist/index.d.cts +15 -0
  112. package/dist/index.d.ts +15 -0
  113. package/dist/index.js +7856 -0
  114. package/dist/index.js.map +1 -0
  115. package/dist/io/docx-session.cjs +16236 -0
  116. package/dist/io/docx-session.cjs.map +1 -0
  117. package/dist/io/docx-session.d.cts +21 -0
  118. package/dist/io/docx-session.d.ts +21 -0
  119. package/dist/io/docx-session.js +18 -0
  120. package/dist/io/docx-session.js.map +1 -0
  121. package/dist/legal/index.cjs +3900 -0
  122. package/dist/legal/index.cjs.map +1 -0
  123. package/dist/legal/index.d.cts +86 -0
  124. package/dist/legal/index.d.ts +86 -0
  125. package/dist/legal/index.js +616 -0
  126. package/dist/legal/index.js.map +1 -0
  127. package/dist/public-types-7ZL_94cz.d.ts +1573 -0
  128. package/dist/public-types-CeMaDueh.d.cts +1573 -0
  129. package/dist/public-types.cjs +19 -0
  130. package/dist/public-types.cjs.map +1 -0
  131. package/dist/public-types.d.cts +2 -0
  132. package/dist/public-types.d.ts +2 -0
  133. package/dist/public-types.js +1 -0
  134. package/dist/public-types.js.map +1 -0
  135. package/dist/runtime/document-runtime.cjs +11140 -0
  136. package/dist/runtime/document-runtime.cjs.map +1 -0
  137. package/dist/runtime/document-runtime.d.cts +231 -0
  138. package/dist/runtime/document-runtime.d.ts +231 -0
  139. package/dist/runtime/document-runtime.js +21 -0
  140. package/dist/runtime/document-runtime.js.map +1 -0
  141. package/dist/structural-helpers-CilgOVhh.d.cts +10 -0
  142. package/dist/structural-helpers-q0Gd-eBN.d.ts +10 -0
  143. package/dist/ui-tailwind/editor-surface/search-plugin.cjs +313 -0
  144. package/dist/ui-tailwind/editor-surface/search-plugin.cjs.map +1 -0
  145. package/dist/ui-tailwind/editor-surface/search-plugin.d.cts +67 -0
  146. package/dist/ui-tailwind/editor-surface/search-plugin.d.ts +67 -0
  147. package/dist/ui-tailwind/editor-surface/search-plugin.js +23 -0
  148. package/dist/ui-tailwind/editor-surface/search-plugin.js.map +1 -0
  149. package/dist/ui-tailwind/index.cjs +4833 -0
  150. package/dist/ui-tailwind/index.cjs.map +1 -0
  151. package/dist/ui-tailwind/index.d.cts +617 -0
  152. package/dist/ui-tailwind/index.d.ts +617 -0
  153. package/dist/ui-tailwind/index.js +575 -0
  154. package/dist/ui-tailwind/index.js.map +1 -0
  155. package/package.json +61 -41
  156. package/src/README.md +0 -85
  157. package/src/api/README.md +0 -26
  158. package/src/api/public-types.ts +0 -1421
  159. package/src/api/session-state.ts +0 -60
  160. package/src/compare/diff-engine.ts +0 -623
  161. package/src/compare/export-redlines.ts +0 -280
  162. package/src/compare/index.ts +0 -25
  163. package/src/compare/snapshot.ts +0 -97
  164. package/src/component-inventory.md +0 -99
  165. package/src/core/README.md +0 -10
  166. package/src/core/commands/README.md +0 -3
  167. package/src/core/commands/image-commands.ts +0 -373
  168. package/src/core/commands/index.ts +0 -1757
  169. package/src/core/commands/list-commands.ts +0 -565
  170. package/src/core/commands/paragraph-layout-commands.ts +0 -339
  171. package/src/core/commands/review-commands.ts +0 -108
  172. package/src/core/commands/structural-helpers.ts +0 -309
  173. package/src/core/schema/README.md +0 -3
  174. package/src/core/schema/text-schema.ts +0 -516
  175. package/src/core/search/search-text.ts +0 -357
  176. package/src/core/selection/README.md +0 -3
  177. package/src/core/selection/mapping.ts +0 -289
  178. package/src/core/selection/review-anchors.ts +0 -183
  179. package/src/core/state/README.md +0 -3
  180. package/src/core/state/editor-state.ts +0 -892
  181. package/src/formats/xlsx/io/parse-shared-strings.ts +0 -41
  182. package/src/formats/xlsx/io/parse-sheet.ts +0 -459
  183. package/src/formats/xlsx/io/parse-styles.ts +0 -59
  184. package/src/formats/xlsx/io/parse-workbook.ts +0 -75
  185. package/src/formats/xlsx/io/serialize-shared-strings.ts +0 -72
  186. package/src/formats/xlsx/io/serialize-sheet.ts +0 -333
  187. package/src/formats/xlsx/io/serialize-styles.ts +0 -98
  188. package/src/formats/xlsx/io/serialize-workbook.ts +0 -429
  189. package/src/formats/xlsx/io/xlsx-session.ts +0 -314
  190. package/src/formats/xlsx/model/cell.ts +0 -189
  191. package/src/formats/xlsx/model/sheet.ts +0 -326
  192. package/src/formats/xlsx/model/styles.ts +0 -118
  193. package/src/formats/xlsx/model/workbook.ts +0 -453
  194. package/src/formats/xlsx/runtime/cell-commands.ts +0 -567
  195. package/src/formats/xlsx/runtime/sheet-commands.ts +0 -206
  196. package/src/formats/xlsx/runtime/workbook-runtime.ts +0 -177
  197. package/src/formats/xlsx/runtime/workbook-transaction.ts +0 -822
  198. package/src/index.ts +0 -101
  199. package/src/io/README.md +0 -10
  200. package/src/io/docx-session.ts +0 -2882
  201. package/src/io/export/README.md +0 -3
  202. package/src/io/export/export-session.ts +0 -220
  203. package/src/io/export/minimal-docx.ts +0 -115
  204. package/src/io/export/reattach-preserved-parts.ts +0 -54
  205. package/src/io/export/serialize-comments.ts +0 -947
  206. package/src/io/export/serialize-footnotes.ts +0 -399
  207. package/src/io/export/serialize-headers-footers.ts +0 -372
  208. package/src/io/export/serialize-main-document.ts +0 -1376
  209. package/src/io/export/serialize-numbering.ts +0 -118
  210. package/src/io/export/serialize-revisions.ts +0 -389
  211. package/src/io/export/serialize-runtime-revisions.ts +0 -269
  212. package/src/io/export/serialize-tables.ts +0 -174
  213. package/src/io/export/split-review-boundaries.ts +0 -356
  214. package/src/io/normalize/README.md +0 -3
  215. package/src/io/normalize/normalize-text.ts +0 -639
  216. package/src/io/ooxml/README.md +0 -3
  217. package/src/io/ooxml/highlight-colors.ts +0 -39
  218. package/src/io/ooxml/numbering-sentinels.ts +0 -44
  219. package/src/io/ooxml/parse-comments.ts +0 -846
  220. package/src/io/ooxml/parse-complex-content.ts +0 -287
  221. package/src/io/ooxml/parse-fields.ts +0 -834
  222. package/src/io/ooxml/parse-footnotes.ts +0 -896
  223. package/src/io/ooxml/parse-headers-footers.ts +0 -1169
  224. package/src/io/ooxml/parse-inline-media.ts +0 -461
  225. package/src/io/ooxml/parse-main-document.ts +0 -2877
  226. package/src/io/ooxml/parse-numbering.ts +0 -432
  227. package/src/io/ooxml/parse-revisions.ts +0 -931
  228. package/src/io/ooxml/parse-settings.ts +0 -184
  229. package/src/io/ooxml/parse-shapes.ts +0 -296
  230. package/src/io/ooxml/parse-styles.ts +0 -463
  231. package/src/io/ooxml/parse-tables.ts +0 -618
  232. package/src/io/ooxml/parse-theme.ts +0 -346
  233. package/src/io/ooxml/part-manifest.ts +0 -136
  234. package/src/io/ooxml/revision-boundaries.ts +0 -351
  235. package/src/io/opc/README.md +0 -3
  236. package/src/io/opc/corrupt-package.ts +0 -166
  237. package/src/io/opc/docx-package.ts +0 -74
  238. package/src/io/opc/package-reader.ts +0 -325
  239. package/src/io/opc/package-writer.ts +0 -273
  240. package/src/io/source-package-provenance.ts +0 -241
  241. package/src/legal/cross-references.ts +0 -414
  242. package/src/legal/defined-terms.ts +0 -203
  243. package/src/legal/index.ts +0 -32
  244. package/src/legal/signature-blocks.ts +0 -259
  245. package/src/model/README.md +0 -3
  246. package/src/model/canonical-document.ts +0 -2632
  247. package/src/model/cds-1.0.0.ts +0 -212
  248. package/src/model/snapshot.ts +0 -649
  249. package/src/preservation/README.md +0 -3
  250. package/src/preservation/markup-compatibility.ts +0 -48
  251. package/src/preservation/opaque-fragment-store.ts +0 -89
  252. package/src/preservation/opaque-region.ts +0 -233
  253. package/src/preservation/package-preservation.ts +0 -113
  254. package/src/preservation/preserved-part-manifest.ts +0 -56
  255. package/src/preservation/relationship-retention.ts +0 -57
  256. package/src/preservation/store.ts +0 -185
  257. package/src/review/README.md +0 -16
  258. package/src/review/store/README.md +0 -3
  259. package/src/review/store/comment-anchors.ts +0 -70
  260. package/src/review/store/comment-remapping.ts +0 -154
  261. package/src/review/store/comment-store.ts +0 -331
  262. package/src/review/store/comment-thread.ts +0 -109
  263. package/src/review/store/revision-actions.ts +0 -394
  264. package/src/review/store/revision-store.ts +0 -312
  265. package/src/review/store/revision-types.ts +0 -171
  266. package/src/review/store/runtime-comment-store.ts +0 -43
  267. package/src/runtime/README.md +0 -3
  268. package/src/runtime/ai-action-policy.ts +0 -764
  269. package/src/runtime/document-layout.ts +0 -332
  270. package/src/runtime/document-navigation.ts +0 -603
  271. package/src/runtime/document-runtime.ts +0 -3159
  272. package/src/runtime/document-search.ts +0 -145
  273. package/src/runtime/numbering-prefix.ts +0 -216
  274. package/src/runtime/page-layout-estimation.ts +0 -212
  275. package/src/runtime/read-only-diagnostics-runtime.ts +0 -241
  276. package/src/runtime/review-runtime.ts +0 -44
  277. package/src/runtime/revision-runtime.ts +0 -107
  278. package/src/runtime/session-capabilities.ts +0 -192
  279. package/src/runtime/story-context.ts +0 -164
  280. package/src/runtime/story-targeting.ts +0 -162
  281. package/src/runtime/surface-projection.ts +0 -1357
  282. package/src/runtime/table-commands.ts +0 -173
  283. package/src/runtime/table-schema.ts +0 -309
  284. package/src/runtime/view-state.ts +0 -477
  285. package/src/runtime/virtualized-rendering.ts +0 -258
  286. package/src/runtime/workflow-markup.ts +0 -353
  287. package/src/ui/README.md +0 -30
  288. package/src/ui/WordReviewEditor.tsx +0 -4086
  289. package/src/ui/browser-export.ts +0 -52
  290. package/src/ui/comments/README.md +0 -3
  291. package/src/ui/compatibility/README.md +0 -3
  292. package/src/ui/editor-command-bag.ts +0 -120
  293. package/src/ui/editor-runtime-boundary.ts +0 -1457
  294. package/src/ui/editor-shell-view.tsx +0 -142
  295. package/src/ui/editor-surface/README.md +0 -3
  296. package/src/ui/editor-surface-controller.tsx +0 -61
  297. package/src/ui/headless/comment-decoration-model.ts +0 -124
  298. package/src/ui/headless/preserve-editor-selection.ts +0 -5
  299. package/src/ui/headless/revision-decoration-model.ts +0 -128
  300. package/src/ui/headless/selection-helpers.ts +0 -54
  301. package/src/ui/headless/selection-toolbar-model.ts +0 -34
  302. package/src/ui/headless/use-editor-keyboard.ts +0 -103
  303. package/src/ui/review/README.md +0 -3
  304. package/src/ui/runtime-snapshot-selectors.ts +0 -197
  305. package/src/ui/shared/revision-filters.ts +0 -31
  306. package/src/ui/status/README.md +0 -3
  307. package/src/ui/theme/README.md +0 -3
  308. package/src/ui/toolbar/README.md +0 -3
  309. package/src/ui/workflow-surface-blocked-rails.ts +0 -94
  310. package/src/ui-tailwind/chrome/tw-alert-banner.tsx +0 -64
  311. package/src/ui-tailwind/chrome/tw-image-context-toolbar.tsx +0 -129
  312. package/src/ui-tailwind/chrome/tw-layout-panel.tsx +0 -114
  313. package/src/ui-tailwind/chrome/tw-object-context-toolbar.tsx +0 -34
  314. package/src/ui-tailwind/chrome/tw-page-ruler.tsx +0 -386
  315. package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +0 -186
  316. package/src/ui-tailwind/chrome/tw-suggestion-card.tsx +0 -139
  317. package/src/ui-tailwind/chrome/tw-table-context-toolbar.tsx +0 -128
  318. package/src/ui-tailwind/chrome/tw-unsaved-modal.tsx +0 -58
  319. package/src/ui-tailwind/chrome/use-before-unload.ts +0 -20
  320. package/src/ui-tailwind/editor-surface/perf-probe.ts +0 -179
  321. package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +0 -184
  322. package/src/ui-tailwind/editor-surface/pm-contextual-ui.ts +0 -31
  323. package/src/ui-tailwind/editor-surface/pm-decorations.ts +0 -427
  324. package/src/ui-tailwind/editor-surface/pm-position-map.ts +0 -123
  325. package/src/ui-tailwind/editor-surface/pm-schema.ts +0 -876
  326. package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +0 -504
  327. package/src/ui-tailwind/editor-surface/search-plugin.ts +0 -168
  328. package/src/ui-tailwind/editor-surface/surface-build-keys.ts +0 -61
  329. package/src/ui-tailwind/editor-surface/tw-caret.tsx +0 -12
  330. package/src/ui-tailwind/editor-surface/tw-editor-surface.tsx +0 -150
  331. package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +0 -129
  332. package/src/ui-tailwind/editor-surface/tw-opaque-block.tsx +0 -58
  333. package/src/ui-tailwind/editor-surface/tw-paragraph-block.tsx +0 -151
  334. package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +0 -944
  335. package/src/ui-tailwind/editor-surface/tw-segment-view.tsx +0 -111
  336. package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +0 -436
  337. package/src/ui-tailwind/index.ts +0 -62
  338. package/src/ui-tailwind/page-chrome-model.ts +0 -27
  339. package/src/ui-tailwind/review/tw-comment-sidebar.tsx +0 -406
  340. package/src/ui-tailwind/review/tw-health-panel.tsx +0 -149
  341. package/src/ui-tailwind/review/tw-review-rail.tsx +0 -120
  342. package/src/ui-tailwind/review/tw-revision-sidebar.tsx +0 -164
  343. package/src/ui-tailwind/status/tw-status-bar.tsx +0 -61
  344. package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +0 -52
  345. package/src/ui-tailwind/toolbar/tw-toolbar.tsx +0 -1064
  346. package/src/ui-tailwind/tw-review-workspace.tsx +0 -1417
  347. package/src/validation/README.md +0 -3
  348. package/src/validation/compatibility-engine.ts +0 -634
  349. package/src/validation/compatibility-report.ts +0 -161
  350. package/src/validation/diagnostics.ts +0 -204
  351. package/src/validation/docx-comment-proof.ts +0 -707
  352. package/src/validation/import-diagnostics.ts +0 -128
  353. package/src/validation/low-priority-word-surfaces.ts +0 -373
  354. /package/{src → dist}/ui-tailwind/theme/editor-theme.css +0 -0
@@ -1,2877 +0,0 @@
1
- import type {
2
- BorderSpec,
3
- TextMark,
4
- ParagraphBorders,
5
- ParagraphShading,
6
- ParagraphSpacing,
7
- ParagraphIndentation,
8
- TabStop,
9
- TableLook,
10
- TableWidth,
11
- SectionProperties,
12
- PageSize,
13
- PageMargins,
14
- ColumnProperties,
15
- PageNumbering,
16
- HeaderFooterReference,
17
- HeaderFooterVariant,
18
- SectionDocumentGrid,
19
- SectionLineNumbering,
20
- SectionPageBorders,
21
- } from "../../model/canonical-document.ts";
22
- import type { OpcRelationship } from "./part-manifest.ts";
23
- import {
24
- parseInlineMediaXml,
25
- type InlineMediaPart,
26
- } from "./parse-inline-media.ts";
27
- import { toCanonicalNumberingInstanceId } from "./parse-numbering.ts";
28
- import { parseComplexContentXml } from "./parse-complex-content.ts";
29
- import { parseShapeXml, parseVmlXml } from "./parse-shapes.ts";
30
- import { classifyFieldInstruction } from "./parse-fields.ts";
31
- import { resolveHighlightColor } from "./highlight-colors.ts";
32
-
33
- export interface ParsedMainDocument {
34
- blocks: ParsedBlockNode[];
35
- finalSectionProperties?: SectionProperties;
36
- }
37
-
38
- export type ParsedBlockNode =
39
- | ParsedParagraphNode
40
- | ParsedTableBlockNode
41
- | ParsedSdtNode
42
- | ParsedCustomXmlNode
43
- | ParsedAltChunkNode
44
- | ParsedSectionBreakNode
45
- | ParsedOpaqueBlockNode;
46
-
47
- export interface ParsedSectionBreakNode {
48
- type: "section_break";
49
- sectionPropertiesXml: string;
50
- sectionProperties: SectionProperties;
51
- }
52
-
53
- export interface ParsedParagraphNode {
54
- type: "paragraph";
55
- styleId?: string;
56
- numbering?: {
57
- numberingInstanceId: string;
58
- level: number;
59
- };
60
- alignment?: "left" | "center" | "right" | "both" | "distribute";
61
- spacing?: ParagraphSpacing;
62
- contextualSpacing?: boolean;
63
- indentation?: ParagraphIndentation;
64
- tabStops?: TabStop[];
65
- keepNext?: boolean;
66
- keepLines?: boolean;
67
- outlineLevel?: number;
68
- pageBreakBefore?: boolean;
69
- widowControl?: boolean;
70
- borders?: ParagraphBorders;
71
- shading?: ParagraphShading;
72
- bidi?: boolean;
73
- suppressLineNumbers?: boolean;
74
- cnfStyle?: string;
75
- sectionProperties?: SectionProperties;
76
- sectionPropertiesXml?: string;
77
- children: ParsedInlineNode[];
78
- rawXml: string;
79
- }
80
-
81
- export type ParsedInlineNode =
82
- | ParsedTextNode
83
- | ParsedBreakNode
84
- | ParsedColumnBreakNode
85
- | ParsedTabNode
86
- | ParsedSymbolNode
87
- | ParsedImageNode
88
- | ParsedHyperlinkNode
89
- | ParsedOpaqueInlineNode
90
- | ParsedChartPreviewNode
91
- | ParsedSmartArtPreviewNode
92
- | ParsedShapeInlineNode
93
- | ParsedWordArtInlineNode
94
- | ParsedVmlShapeInlineNode
95
- | ParsedBookmarkStartInlineNode
96
- | ParsedBookmarkEndInlineNode
97
- | ParsedFootnoteRefInlineNode
98
- | ParsedFieldInlineNode
99
- | ParsedPermStartInlineNode
100
- | ParsedPermEndInlineNode;
101
-
102
- export interface ParsedTextNode {
103
- type: "text";
104
- text: string;
105
- marks?: TextMark[];
106
- }
107
-
108
- export interface ParsedBreakNode {
109
- type: "hard_break";
110
- }
111
-
112
- export interface ParsedColumnBreakNode {
113
- type: "column_break";
114
- }
115
-
116
- export interface ParsedTabNode {
117
- type: "tab";
118
- }
119
-
120
- export interface ParsedSymbolNode {
121
- type: "symbol";
122
- char: string;
123
- font?: string;
124
- marks?: TextMark[];
125
- }
126
-
127
- export interface ParsedImageNode {
128
- type: "image";
129
- mediaId: string;
130
- relationshipId?: string;
131
- packagePartName?: string;
132
- contentType?: string;
133
- filename?: string;
134
- altText?: string;
135
- widthEmu?: number;
136
- heightEmu?: number;
137
- placementXml?: string;
138
- display?: "inline" | "floating";
139
- floating?: {
140
- horizontalPosition?: {
141
- relativeFrom?: string;
142
- align?: string;
143
- offset?: number;
144
- };
145
- verticalPosition?: {
146
- relativeFrom?: string;
147
- align?: string;
148
- offset?: number;
149
- };
150
- wrap?: "none" | "square" | "tight" | "through" | "topAndBottom";
151
- behindDoc?: boolean;
152
- layoutInCell?: boolean;
153
- allowOverlap?: boolean;
154
- };
155
- }
156
-
157
- export interface ParsedHyperlinkNode {
158
- type: "hyperlink";
159
- href: string;
160
- children: Array<ParsedTextNode | ParsedBreakNode | ParsedColumnBreakNode | ParsedTabNode | ParsedSymbolNode>;
161
- rawXml: string;
162
- }
163
-
164
- export interface ParsedOpaqueInlineNode {
165
- type: "opaque_inline";
166
- rawXml: string;
167
- }
168
-
169
- export interface ParsedChartPreviewNode {
170
- type: "chart_preview";
171
- previewMediaId?: string;
172
- rawXml: string;
173
- }
174
-
175
- export interface ParsedSmartArtPreviewNode {
176
- type: "smartart_preview";
177
- previewMediaId?: string;
178
- rawXml: string;
179
- }
180
-
181
- export interface ParsedShapeInlineNode {
182
- type: "shape";
183
- text?: string;
184
- geometry?: string;
185
- isTextBox?: boolean;
186
- rawXml: string;
187
- }
188
-
189
- export interface ParsedWordArtInlineNode {
190
- type: "wordart";
191
- text: string;
192
- geometry?: string;
193
- rawXml: string;
194
- }
195
-
196
- export interface ParsedVmlShapeInlineNode {
197
- type: "vml_shape";
198
- text?: string;
199
- shapeType?: string;
200
- rawXml: string;
201
- }
202
-
203
- export interface ParsedBookmarkStartInlineNode {
204
- type: "bookmark_start";
205
- bookmarkId: string;
206
- name: string;
207
- rawXml: string;
208
- }
209
-
210
- export interface ParsedBookmarkEndInlineNode {
211
- type: "bookmark_end";
212
- bookmarkId: string;
213
- rawXml: string;
214
- }
215
-
216
- export interface ParsedFootnoteRefInlineNode {
217
- type: "footnote_ref";
218
- noteId: string;
219
- noteKind: "footnote" | "endnote";
220
- }
221
-
222
- export interface ParsedFieldInlineNode {
223
- type: "field";
224
- fieldType: "simple" | "complex";
225
- instruction: string;
226
- contentXml?: string;
227
- children?: Array<ParsedTextNode | ParsedBreakNode | ParsedTabNode>;
228
- rawXml: string;
229
- }
230
-
231
- export interface ParsedPermStartInlineNode {
232
- type: "perm_start";
233
- rangeId: string;
234
- editorGroup?: string;
235
- editor?: string;
236
- rawXml: string;
237
- }
238
-
239
- export interface ParsedPermEndInlineNode {
240
- type: "perm_end";
241
- rangeId: string;
242
- rawXml: string;
243
- }
244
-
245
- export interface ParsedOpaqueBlockNode {
246
- type: "opaque_block";
247
- rawXml: string;
248
- }
249
-
250
- export interface ParsedSdtCheckboxState {
251
- checked: boolean;
252
- checkedChar?: string;
253
- uncheckedChar?: string;
254
- }
255
-
256
- export interface ParsedSdtDatePickerState {
257
- fullDate?: string;
258
- dateFormat?: string;
259
- lid?: string;
260
- }
261
-
262
- export interface ParsedSdtDropdownListItem {
263
- displayText?: string;
264
- value: string;
265
- }
266
-
267
- export interface ParsedSdtNode {
268
- type: "sdt";
269
- properties: {
270
- sdtType?: string;
271
- alias?: string;
272
- tag?: string;
273
- lock?: string;
274
- propertiesXml?: string;
275
- checkbox?: ParsedSdtCheckboxState;
276
- datePicker?: ParsedSdtDatePickerState;
277
- dropdownList?: ParsedSdtDropdownListItem[];
278
- comboBox?: ParsedSdtDropdownListItem[];
279
- showingPlcHdr?: boolean;
280
- };
281
- children: ParsedBlockNode[];
282
- rawXml: string;
283
- }
284
-
285
- export interface ParsedCustomXmlNode {
286
- type: "custom_xml";
287
- uri?: string;
288
- element?: string;
289
- children: ParsedBlockNode[];
290
- rawXml: string;
291
- }
292
-
293
- export interface ParsedAltChunkNode {
294
- type: "alt_chunk";
295
- relationshipId: string;
296
- rawXml: string;
297
- }
298
-
299
- export interface ParsedTableBlockNode {
300
- type: "table";
301
- styleId?: string;
302
- tblLook?: TableLook;
303
- propertiesXml?: string;
304
- gridColumns: number[];
305
- rows: ParsedTableRowNode[];
306
- rawXml: string;
307
- }
308
-
309
- export interface ParsedTableRowNode {
310
- type: "table_row";
311
- propertiesXml?: string;
312
- cells: ParsedTableCellNode[];
313
- gridBefore?: number;
314
- widthBefore?: TableWidth;
315
- gridAfter?: number;
316
- widthAfter?: TableWidth;
317
- rawXml: string;
318
- }
319
-
320
- export interface ParsedTableCellNode {
321
- type: "table_cell";
322
- propertiesXml?: string;
323
- gridSpan?: number;
324
- verticalMerge?: "restart" | "continue";
325
- children: ParsedBlockNode[];
326
- rawXml: string;
327
- }
328
-
329
- interface XmlElementNode {
330
- type: "element";
331
- name: string;
332
- attributes: Record<string, string>;
333
- children: XmlNode[];
334
- start: number;
335
- end: number;
336
- }
337
-
338
- interface XmlTextNode {
339
- type: "text";
340
- text: string;
341
- start: number;
342
- end: number;
343
- }
344
-
345
- type XmlNode = XmlElementNode | XmlTextNode;
346
-
347
- interface RunParseResult {
348
- nodes: Array<ParsedTextNode | ParsedBreakNode | ParsedColumnBreakNode | ParsedTabNode | ParsedSymbolNode>;
349
- supported: boolean;
350
- }
351
-
352
- interface MarksParseResult {
353
- marks: TextMark[];
354
- supported: boolean;
355
- }
356
-
357
- const HYPERLINK_RELATIONSHIP_TYPE =
358
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
359
-
360
- export function parseMainDocumentXml(
361
- xml: string,
362
- relationships: readonly OpcRelationship[] = [],
363
- mediaParts: ReadonlyMap<string, InlineMediaPart> = new Map(),
364
- sourcePartPath = "/word/document.xml",
365
- ): ParsedMainDocument {
366
- const root = parseXml(xml);
367
- const documentElement = findChildElement(root, "document");
368
- const bodyElement = findChildElement(documentElement, "body");
369
- const relationshipMap = new Map(relationships.map((relationship) => [relationship.id, relationship]));
370
-
371
- const allBlocks = bodyElement.children
372
- .filter((node): node is XmlElementNode => node.type === "element")
373
- .map((node) => parseBodyChild(node, xml, relationshipMap, relationships, mediaParts, sourcePartPath));
374
-
375
- // The last body-level sectPr is the final section properties (not an intermediate section break).
376
- // Extract it from the blocks list and store it separately.
377
- let finalSectionProperties: SectionProperties | undefined;
378
- const blocks: ParsedBlockNode[] = [];
379
- for (let i = 0; i < allBlocks.length; i++) {
380
- const block = allBlocks[i];
381
- if (block.type === "section_break" && i === allBlocks.length - 1) {
382
- finalSectionProperties = block.sectionProperties;
383
- } else {
384
- blocks.push(block);
385
- }
386
- }
387
-
388
- return { blocks, finalSectionProperties };
389
- }
390
-
391
- function parseBodyChild(
392
- node: XmlElementNode,
393
- sourceXml: string,
394
- relationshipMap: Map<string, OpcRelationship>,
395
- relationships: readonly OpcRelationship[],
396
- mediaParts: ReadonlyMap<string, InlineMediaPart>,
397
- sourcePartPath: string,
398
- ): ParsedBlockNode {
399
- const nodeType = localName(node.name);
400
-
401
- if (nodeType === "tbl") {
402
- // Tables with revision markup (tracked changes inside cells) stay opaque
403
- // to preserve fidelity until revision-aware table editing is implemented
404
- const rawTableXml = sourceXml.slice(node.start, node.end);
405
- if (tableRequiresOpaquePreservation(rawTableXml)) {
406
- return {
407
- type: "opaque_block",
408
- rawXml: rawTableXml,
409
- };
410
- }
411
- try {
412
- return parseTableElement(node, sourceXml, relationshipMap, relationships, mediaParts, sourcePartPath);
413
- } catch {
414
- // If table parsing fails for any reason, fall back to opaque preservation
415
- return {
416
- type: "opaque_block",
417
- rawXml: rawTableXml,
418
- };
419
- }
420
- }
421
-
422
- if (nodeType === "sdt") {
423
- return parseSdtElement(node, sourceXml, relationshipMap, relationships, mediaParts, sourcePartPath);
424
- }
425
-
426
- if (nodeType === "customXml") {
427
- return parseCustomXmlElement(node, sourceXml, relationshipMap, relationships, mediaParts, sourcePartPath);
428
- }
429
-
430
- if (nodeType === "altChunk") {
431
- return parseAltChunkElement(node, sourceXml);
432
- }
433
-
434
- if (nodeType === "sectPr") {
435
- return parseSectionBreakElement(node, sourceXml);
436
- }
437
-
438
- if (nodeType !== "p") {
439
- return {
440
- type: "opaque_block",
441
- rawXml: sourceXml.slice(node.start, node.end),
442
- };
443
- }
444
-
445
- let styleId: string | undefined;
446
- let numbering: ParsedParagraphNode["numbering"];
447
- let alignment: ParsedParagraphNode["alignment"];
448
- let spacing: ParsedParagraphNode["spacing"];
449
- let contextualSpacing: ParsedParagraphNode["contextualSpacing"];
450
- let indentation: ParsedParagraphNode["indentation"];
451
- let tabStops: ParsedParagraphNode["tabStops"];
452
- let keepNext: ParsedParagraphNode["keepNext"];
453
- let keepLines: ParsedParagraphNode["keepLines"];
454
- let outlineLevel: ParsedParagraphNode["outlineLevel"];
455
- let pageBreakBefore: ParsedParagraphNode["pageBreakBefore"];
456
- let widowControl: ParsedParagraphNode["widowControl"];
457
- let borders: ParsedParagraphNode["borders"];
458
- let shading: ParsedParagraphNode["shading"];
459
- let bidi: ParsedParagraphNode["bidi"];
460
- let suppressLineNumbers: ParsedParagraphNode["suppressLineNumbers"];
461
- let cnfStyle: ParsedParagraphNode["cnfStyle"];
462
- let sectionProperties: SectionProperties | undefined;
463
- let sectionPropertiesXml: string | undefined;
464
- let paragraphSupported = true;
465
- const children: ParsedInlineNode[] = [];
466
- let activeComplexField: {
467
- instruction: string;
468
- children: Array<ParsedTextNode | ParsedBreakNode | ParsedTabNode>;
469
- mode: "instruction" | "result";
470
- } | null = null;
471
-
472
- for (const child of node.children) {
473
- if (child.type !== "element") {
474
- continue;
475
- }
476
-
477
- switch (localName(child.name)) {
478
- case "pPr":
479
- styleId = readParagraphStyleId(child);
480
- numbering = readParagraphNumbering(child);
481
- alignment = readParagraphAlignment(child);
482
- spacing = readParagraphSpacing(child);
483
- contextualSpacing = readOptionalOnOffParagraphProperty(child, "contextualSpacing");
484
- indentation = readParagraphIndentation(child);
485
- tabStops = readParagraphTabStops(child);
486
- keepNext = readOnOffParagraphProperty(child, "keepNext");
487
- keepLines = readOnOffParagraphProperty(child, "keepLines");
488
- outlineLevel = readParagraphOutlineLevel(child);
489
- pageBreakBefore = readOnOffParagraphProperty(child, "pageBreakBefore");
490
- widowControl = readOnOffParagraphProperty(child, "widowControl");
491
- borders = readParagraphBorders(child);
492
- shading = readParagraphShading(child);
493
- bidi = readOnOffParagraphProperty(child, "bidi");
494
- suppressLineNumbers = readOnOffParagraphProperty(child, "suppressLineNumbers");
495
- cnfStyle = readParagraphCnfStyle(child);
496
- sectionProperties = readSectionPropertiesFromPPr(child);
497
- sectionPropertiesXml = readSectionPropertiesXmlFromPPr(child, sourceXml);
498
- paragraphSupported = paragraphSupported && supportsParagraphProperties(child);
499
- break;
500
- case "r":
501
- activeComplexField = appendParagraphRunNodes(
502
- child,
503
- children,
504
- activeComplexField,
505
- sourceXml,
506
- relationships,
507
- mediaParts,
508
- sourcePartPath,
509
- );
510
- break;
511
- case "hyperlink": {
512
- flushActiveComplexField(children, () => {
513
- activeComplexField = null;
514
- }, activeComplexField);
515
- const hyperlink = parseHyperlink(child, sourceXml, relationshipMap);
516
- children.push(hyperlink);
517
- break;
518
- }
519
- case "ins":
520
- case "del": {
521
- flushActiveComplexField(children, () => {
522
- activeComplexField = null;
523
- }, activeComplexField);
524
- children.push(...parseRevisionContainer(child, sourceXml, relationshipMap));
525
- break;
526
- }
527
- case "commentRangeStart":
528
- case "commentRangeEnd":
529
- break;
530
- case "bookmarkStart": {
531
- const bkId = child.attributes["w:id"] ?? child.attributes.id ?? "";
532
- const bkName = child.attributes["w:name"] ?? child.attributes.name ?? "";
533
- if (bkId) {
534
- const bookmarkNode = {
535
- type: "bookmark_start",
536
- bookmarkId: bkId,
537
- name: bkName,
538
- rawXml: sourceXml.slice(child.start, child.end),
539
- } as ParsedBookmarkStartInlineNode;
540
- flushActiveComplexField(children, () => {
541
- activeComplexField = null;
542
- }, activeComplexField);
543
- children.push(bookmarkNode);
544
- } else {
545
- flushActiveComplexField(children, () => {
546
- activeComplexField = null;
547
- }, activeComplexField);
548
- children.push({ type: "opaque_inline", rawXml: sourceXml.slice(child.start, child.end) });
549
- }
550
- break;
551
- }
552
- case "bookmarkEnd": {
553
- const bkEndId = child.attributes["w:id"] ?? child.attributes.id ?? "";
554
- if (bkEndId) {
555
- const bookmarkNode = {
556
- type: "bookmark_end",
557
- bookmarkId: bkEndId,
558
- rawXml: sourceXml.slice(child.start, child.end),
559
- } as ParsedBookmarkEndInlineNode;
560
- flushActiveComplexField(children, () => {
561
- activeComplexField = null;
562
- }, activeComplexField);
563
- children.push(bookmarkNode);
564
- } else {
565
- flushActiveComplexField(children, () => {
566
- activeComplexField = null;
567
- }, activeComplexField);
568
- children.push({ type: "opaque_inline", rawXml: sourceXml.slice(child.start, child.end) });
569
- }
570
- break;
571
- }
572
- case "fldSimple": {
573
- flushActiveComplexField(children, () => {
574
- activeComplexField = null;
575
- }, activeComplexField);
576
- const fieldInstr = child.attributes["w:instr"] ?? child.attributes.instr ?? "";
577
- const fieldContentXml = child.children
578
- .filter((c): c is XmlElementNode => c.type === "element")
579
- .map((c) => sourceXml.slice(c.start, c.end))
580
- .join("");
581
- children.push({
582
- type: "field",
583
- fieldType: "simple",
584
- instruction: fieldInstr,
585
- contentXml: fieldContentXml,
586
- rawXml: sourceXml.slice(child.start, child.end),
587
- } as ParsedFieldInlineNode);
588
- break;
589
- }
590
- case "proofErr":
591
- flushActiveComplexField(children, () => {
592
- activeComplexField = null;
593
- }, activeComplexField);
594
- children.push({
595
- type: "opaque_inline",
596
- rawXml: sourceXml.slice(child.start, child.end),
597
- });
598
- break;
599
- case "permStart":
600
- flushActiveComplexField(children, () => {
601
- activeComplexField = null;
602
- }, activeComplexField);
603
- children.push(parsePermStartNode(child, sourceXml));
604
- break;
605
- case "permEnd":
606
- flushActiveComplexField(children, () => {
607
- activeComplexField = null;
608
- }, activeComplexField);
609
- children.push(parsePermEndNode(child, sourceXml));
610
- break;
611
- case "footnoteReference": {
612
- flushActiveComplexField(children, () => {
613
- activeComplexField = null;
614
- }, activeComplexField);
615
- const noteId = child.attributes["w:id"] ?? child.attributes.id ?? "";
616
- if (noteId) {
617
- children.push({
618
- type: "footnote_ref",
619
- noteId,
620
- noteKind: "footnote",
621
- });
622
- }
623
- break;
624
- }
625
- case "endnoteReference": {
626
- flushActiveComplexField(children, () => {
627
- activeComplexField = null;
628
- }, activeComplexField);
629
- const noteId = child.attributes["w:id"] ?? child.attributes.id ?? "";
630
- if (noteId) {
631
- children.push({
632
- type: "footnote_ref",
633
- noteId,
634
- noteKind: "endnote",
635
- });
636
- }
637
- break;
638
- }
639
- default:
640
- flushActiveComplexField(children, () => {
641
- activeComplexField = null;
642
- }, activeComplexField);
643
- children.push({
644
- type: "opaque_inline",
645
- rawXml: sourceXml.slice(child.start, child.end),
646
- });
647
- break;
648
- }
649
- }
650
-
651
- flushActiveComplexField(children, () => {
652
- activeComplexField = null;
653
- }, activeComplexField);
654
-
655
- if (!paragraphSupported) {
656
- return {
657
- type: "opaque_block",
658
- rawXml: sourceXml.slice(node.start, node.end),
659
- };
660
- }
661
-
662
- return {
663
- type: "paragraph",
664
- styleId,
665
- ...(numbering ? { numbering } : {}),
666
- ...(alignment ? { alignment } : {}),
667
- ...(spacing ? { spacing } : {}),
668
- ...(contextualSpacing !== undefined ? { contextualSpacing } : {}),
669
- ...(indentation ? { indentation } : {}),
670
- ...(tabStops && tabStops.length > 0 ? { tabStops } : {}),
671
- ...(keepNext ? { keepNext } : {}),
672
- ...(keepLines ? { keepLines } : {}),
673
- ...(outlineLevel !== undefined ? { outlineLevel } : {}),
674
- ...(pageBreakBefore ? { pageBreakBefore } : {}),
675
- ...(widowControl ? { widowControl } : {}),
676
- ...(borders ? { borders } : {}),
677
- ...(shading ? { shading } : {}),
678
- ...(bidi ? { bidi } : {}),
679
- ...(suppressLineNumbers ? { suppressLineNumbers } : {}),
680
- ...(cnfStyle ? { cnfStyle } : {}),
681
- ...(sectionProperties ? { sectionProperties } : {}),
682
- ...(sectionPropertiesXml ? { sectionPropertiesXml } : {}),
683
- children,
684
- rawXml: sourceXml.slice(node.start, node.end),
685
- };
686
- }
687
-
688
- function appendParagraphRunNodes(
689
- node: XmlElementNode,
690
- children: ParsedInlineNode[],
691
- activeComplexField: {
692
- instruction: string;
693
- children: Array<ParsedTextNode | ParsedBreakNode | ParsedTabNode>;
694
- mode: "instruction" | "result";
695
- } | null,
696
- sourceXml: string,
697
- relationships: readonly OpcRelationship[],
698
- mediaParts: ReadonlyMap<string, InlineMediaPart>,
699
- sourcePartPath: string,
700
- ): {
701
- instruction: string;
702
- children: Array<ParsedTextNode | ParsedBreakNode | ParsedTabNode>;
703
- mode: "instruction" | "result";
704
- } | null {
705
- const hasFieldMarkers = node.children.some(
706
- (child) =>
707
- child.type === "element" &&
708
- (localName(child.name) === "fldChar" || localName(child.name) === "instrText"),
709
- );
710
-
711
- if (activeComplexField?.mode === "result" && !hasFieldMarkers) {
712
- const run = parseRunContentOnly(node, sourceXml);
713
- if (
714
- run.supported &&
715
- run.nodes.every(
716
- (child) =>
717
- child.type === "text" || child.type === "hard_break" || child.type === "tab",
718
- )
719
- ) {
720
- activeComplexField.children.push(...run.nodes);
721
- return activeComplexField;
722
- }
723
- }
724
-
725
- if (!hasFieldMarkers) {
726
- children.push(...parseRun(node, sourceXml, relationships, mediaParts, sourcePartPath));
727
- return activeComplexField;
728
- }
729
-
730
- for (const child of node.children) {
731
- if (child.type !== "element") {
732
- continue;
733
- }
734
-
735
- const name = localName(child.name);
736
- if (name === "fldChar") {
737
- const fldType = child.attributes["w:fldCharType"] ?? child.attributes.fldCharType;
738
- if (fldType === "begin") {
739
- activeComplexField = { instruction: "", children: [], mode: "instruction" };
740
- } else if (fldType === "separate" && activeComplexField) {
741
- activeComplexField.mode = "result";
742
- } else if (fldType === "end" && activeComplexField) {
743
- flushActiveComplexField(children, () => {
744
- activeComplexField = null;
745
- }, activeComplexField);
746
- }
747
- continue;
748
- }
749
-
750
- if (name === "instrText") {
751
- const instruction = child.children
752
- .filter((entry): entry is XmlTextNode => entry.type === "text")
753
- .map((entry) => entry.text)
754
- .join("");
755
- if (activeComplexField) {
756
- activeComplexField.instruction += instruction;
757
- } else if (instruction.trim().length > 0) {
758
- children.push({
759
- type: "field",
760
- fieldType: "complex",
761
- instruction,
762
- children: [],
763
- rawXml: sourceXml.slice(node.start, node.end),
764
- });
765
- }
766
- continue;
767
- }
768
- }
769
-
770
- return activeComplexField;
771
- }
772
-
773
- function flushActiveComplexField(
774
- children: ParsedInlineNode[],
775
- reset: () => void,
776
- activeComplexField: {
777
- instruction: string;
778
- children: Array<ParsedTextNode | ParsedBreakNode | ParsedTabNode>;
779
- mode: "instruction" | "result";
780
- } | null,
781
- ): void {
782
- if (!activeComplexField || activeComplexField.instruction.trim().length === 0) {
783
- return;
784
- }
785
-
786
- children.push({
787
- type: "field",
788
- fieldType: "complex",
789
- instruction: activeComplexField.instruction,
790
- children: activeComplexField.children,
791
- rawXml: "",
792
- });
793
- reset();
794
- }
795
-
796
- function parseTableElement(
797
- node: XmlElementNode,
798
- sourceXml: string,
799
- relationshipMap: Map<string, OpcRelationship>,
800
- relationships: readonly OpcRelationship[],
801
- mediaParts: ReadonlyMap<string, InlineMediaPart>,
802
- sourcePartPath: string,
803
- ): ParsedTableBlockNode {
804
- let styleId: string | undefined;
805
- let tblLook: TableLook | undefined;
806
- let propertiesXml: string | undefined;
807
- let gridColumns: number[] = [];
808
- const rows: ParsedTableRowNode[] = [];
809
-
810
- for (const child of node.children) {
811
- if (child.type !== "element") continue;
812
-
813
- switch (localName(child.name)) {
814
- case "tblPr": {
815
- propertiesXml = sourceXml.slice(child.start, child.end);
816
- styleId = readTableStyleId(child);
817
- tblLook = readTableLook(child);
818
- break;
819
- }
820
- case "tblGrid": {
821
- gridColumns = readTableGridColumns(child);
822
- break;
823
- }
824
- case "tr": {
825
- rows.push(parseTableRowElement(child, sourceXml, relationshipMap, relationships, mediaParts, sourcePartPath));
826
- break;
827
- }
828
- }
829
- }
830
-
831
- return {
832
- type: "table",
833
- ...(styleId ? { styleId } : {}),
834
- ...(tblLook ? { tblLook } : {}),
835
- ...(propertiesXml ? { propertiesXml } : {}),
836
- gridColumns,
837
- rows,
838
- rawXml: sourceXml.slice(node.start, node.end),
839
- };
840
- }
841
-
842
- function parseTableRowElement(
843
- node: XmlElementNode,
844
- sourceXml: string,
845
- relationshipMap: Map<string, OpcRelationship>,
846
- relationships: readonly OpcRelationship[],
847
- mediaParts: ReadonlyMap<string, InlineMediaPart>,
848
- sourcePartPath: string,
849
- ): ParsedTableRowNode {
850
- let propertiesXml: string | undefined;
851
- let gridBefore: number | undefined;
852
- let widthBefore: TableWidth | undefined;
853
- let gridAfter: number | undefined;
854
- let widthAfter: TableWidth | undefined;
855
- const cells: ParsedTableCellNode[] = [];
856
-
857
- for (const child of node.children) {
858
- if (child.type !== "element") continue;
859
-
860
- switch (localName(child.name)) {
861
- case "trPr":
862
- propertiesXml = sourceXml.slice(child.start, child.end);
863
- gridBefore = readTableRowGridPosition(child, "gridBefore");
864
- widthBefore = readTableRowWidth(child, "wBefore");
865
- gridAfter = readTableRowGridPosition(child, "gridAfter");
866
- widthAfter = readTableRowWidth(child, "wAfter");
867
- break;
868
- case "tc":
869
- cells.push(parseTableCellElement(child, sourceXml, relationshipMap, relationships, mediaParts, sourcePartPath));
870
- break;
871
- }
872
- }
873
-
874
- return {
875
- type: "table_row",
876
- ...(propertiesXml ? { propertiesXml } : {}),
877
- ...(gridBefore !== undefined ? { gridBefore } : {}),
878
- ...(widthBefore ? { widthBefore } : {}),
879
- ...(gridAfter !== undefined ? { gridAfter } : {}),
880
- ...(widthAfter ? { widthAfter } : {}),
881
- cells,
882
- rawXml: sourceXml.slice(node.start, node.end),
883
- };
884
- }
885
-
886
- function parseTableCellElement(
887
- node: XmlElementNode,
888
- sourceXml: string,
889
- relationshipMap: Map<string, OpcRelationship>,
890
- relationships: readonly OpcRelationship[],
891
- mediaParts: ReadonlyMap<string, InlineMediaPart>,
892
- sourcePartPath: string,
893
- ): ParsedTableCellNode {
894
- let propertiesXml: string | undefined;
895
- let gridSpan: number | undefined;
896
- let verticalMerge: "restart" | "continue" | undefined;
897
- const children: ParsedBlockNode[] = [];
898
-
899
- for (const child of node.children) {
900
- if (child.type !== "element") continue;
901
-
902
- switch (localName(child.name)) {
903
- case "tcPr": {
904
- propertiesXml = sourceXml.slice(child.start, child.end);
905
- gridSpan = readCellGridSpan(child);
906
- verticalMerge = readCellVerticalMerge(child);
907
- break;
908
- }
909
- default: {
910
- // Everything else in a cell is a block child (paragraphs, nested tables, etc.)
911
- children.push(parseBodyChild(child, sourceXml, relationshipMap, relationships, mediaParts, sourcePartPath));
912
- break;
913
- }
914
- }
915
- }
916
-
917
- return {
918
- type: "table_cell",
919
- ...(propertiesXml ? { propertiesXml } : {}),
920
- ...(gridSpan ? { gridSpan } : {}),
921
- ...(verticalMerge ? { verticalMerge } : {}),
922
- children,
923
- rawXml: sourceXml.slice(node.start, node.end),
924
- };
925
- }
926
-
927
- function parseSdtElement(
928
- node: XmlElementNode,
929
- sourceXml: string,
930
- relationshipMap: Map<string, OpcRelationship>,
931
- relationships: readonly OpcRelationship[],
932
- mediaParts: ReadonlyMap<string, InlineMediaPart>,
933
- sourcePartPath: string,
934
- ): ParsedBlockNode {
935
- const propertiesNode = node.children.find(
936
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "sdtPr",
937
- );
938
- const contentNode = node.children.find(
939
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "sdtContent",
940
- );
941
-
942
- if (!contentNode) {
943
- return {
944
- type: "opaque_block",
945
- rawXml: sourceXml.slice(node.start, node.end),
946
- };
947
- }
948
-
949
- const children = contentNode.children
950
- .filter((child): child is XmlElementNode => child.type === "element")
951
- .map((child) => parseBodyChild(child, sourceXml, relationshipMap, relationships, mediaParts, sourcePartPath));
952
-
953
- return {
954
- type: "sdt",
955
- properties: readSdtProperties(propertiesNode, sourceXml),
956
- children,
957
- rawXml: sourceXml.slice(node.start, node.end),
958
- };
959
- }
960
-
961
- function parseCustomXmlElement(
962
- node: XmlElementNode,
963
- sourceXml: string,
964
- relationshipMap: Map<string, OpcRelationship>,
965
- relationships: readonly OpcRelationship[],
966
- mediaParts: ReadonlyMap<string, InlineMediaPart>,
967
- sourcePartPath: string,
968
- ): ParsedBlockNode {
969
- const uri = readOptionalAttribute(node, "uri");
970
- const element = readOptionalAttribute(node, "element");
971
- if (!uri && !element) {
972
- return {
973
- type: "opaque_block",
974
- rawXml: sourceXml.slice(node.start, node.end),
975
- };
976
- }
977
-
978
- const children = node.children
979
- .filter(
980
- (child): child is XmlElementNode =>
981
- child.type === "element" && localName(child.name) !== "customXmlPr",
982
- )
983
- .map((child) => parseBodyChild(child, sourceXml, relationshipMap, relationships, mediaParts, sourcePartPath));
984
-
985
- return {
986
- type: "custom_xml",
987
- ...(uri ? { uri } : {}),
988
- ...(element ? { element } : {}),
989
- children,
990
- rawXml: sourceXml.slice(node.start, node.end),
991
- };
992
- }
993
-
994
- function parseAltChunkElement(
995
- node: XmlElementNode,
996
- sourceXml: string,
997
- ): ParsedBlockNode {
998
- const relationshipId = readOptionalAttribute(node, "id");
999
- if (!relationshipId) {
1000
- return {
1001
- type: "opaque_block",
1002
- rawXml: sourceXml.slice(node.start, node.end),
1003
- };
1004
- }
1005
-
1006
- return {
1007
- type: "alt_chunk",
1008
- relationshipId,
1009
- rawXml: sourceXml.slice(node.start, node.end),
1010
- };
1011
- }
1012
-
1013
- function readSdtProperties(
1014
- node: XmlElementNode | undefined,
1015
- sourceXml: string,
1016
- ): ParsedSdtNode["properties"] {
1017
- if (!node) {
1018
- return {};
1019
- }
1020
-
1021
- const properties: ParsedSdtNode["properties"] = {
1022
- propertiesXml: sourceXml.slice(node.start, node.end),
1023
- };
1024
-
1025
- for (const child of node.children) {
1026
- if (child.type !== "element") {
1027
- continue;
1028
- }
1029
-
1030
- const name = localName(child.name);
1031
- if (name === "alias") {
1032
- properties.alias = readOptionalAttribute(child, "val");
1033
- continue;
1034
- }
1035
- if (name === "tag") {
1036
- properties.tag = readOptionalAttribute(child, "val");
1037
- continue;
1038
- }
1039
- if (name === "lock") {
1040
- properties.lock = readOptionalAttribute(child, "val");
1041
- continue;
1042
- }
1043
- if (name === "showingPlcHdr") {
1044
- const val = readOptionalAttribute(child, "val");
1045
- properties.showingPlcHdr = val !== "false" && val !== "0";
1046
- continue;
1047
- }
1048
-
1049
- // Checkbox (w14:checkbox)
1050
- if (name === "checkbox") {
1051
- properties.sdtType = "checkbox";
1052
- const checkedNode = findFirstDescendant(child, "checked");
1053
- const checkedVal = checkedNode ? (readOptionalAttribute(checkedNode, "val") ?? "0") : "0";
1054
- const checkedCharNode = findFirstDescendant(child, "checkedState");
1055
- const uncheckedCharNode = findFirstDescendant(child, "uncheckedState");
1056
- properties.checkbox = {
1057
- checked: checkedVal === "1" || checkedVal === "true",
1058
- ...(checkedCharNode ? { checkedChar: readOptionalAttribute(checkedCharNode, "val") } : {}),
1059
- ...(uncheckedCharNode ? { uncheckedChar: readOptionalAttribute(uncheckedCharNode, "val") } : {}),
1060
- };
1061
- continue;
1062
- }
1063
-
1064
- // Date picker
1065
- if (name === "date") {
1066
- properties.sdtType = "date";
1067
- const fullDate = readOptionalAttribute(child, "fullDate");
1068
- const dateFormatNode = findFirstChild(child, "dateFormat");
1069
- const lidNode = findFirstChild(child, "lid");
1070
- properties.datePicker = {
1071
- ...(fullDate ? { fullDate } : {}),
1072
- ...(dateFormatNode ? { dateFormat: readOptionalAttribute(dateFormatNode, "val") } : {}),
1073
- ...(lidNode ? { lid: readOptionalAttribute(lidNode, "val") } : {}),
1074
- };
1075
- continue;
1076
- }
1077
-
1078
- // Dropdown list
1079
- if (name === "dropDownList") {
1080
- properties.sdtType = "dropDownList";
1081
- properties.dropdownList = readSdtListItems(child);
1082
- continue;
1083
- }
1084
-
1085
- // Combo box
1086
- if (name === "comboBox") {
1087
- properties.sdtType = "comboBox";
1088
- properties.comboBox = readSdtListItems(child);
1089
- continue;
1090
- }
1091
-
1092
- // Plain text
1093
- if (name === "text") {
1094
- properties.sdtType = "plainText";
1095
- continue;
1096
- }
1097
-
1098
- // Rich text (richText element is the default, but if explicitly present, tag it)
1099
- if (name === "richText") {
1100
- properties.sdtType = "richText";
1101
- continue;
1102
- }
1103
-
1104
- if (!properties.sdtType && name !== "id" && name !== "placeholder" && name !== "showingPlcHdr" && name !== "rPr") {
1105
- properties.sdtType = name;
1106
- }
1107
- }
1108
-
1109
- return properties;
1110
- }
1111
-
1112
- function readSdtListItems(node: XmlElementNode): ParsedSdtDropdownListItem[] {
1113
- const items: ParsedSdtDropdownListItem[] = [];
1114
- for (const child of node.children) {
1115
- if (child.type !== "element" || localName(child.name) !== "listItem") continue;
1116
- const value = readOptionalAttribute(child, "value") ?? "";
1117
- const displayText = readOptionalAttribute(child, "displayText");
1118
- items.push({ value, ...(displayText ? { displayText } : {}) });
1119
- }
1120
- return items;
1121
- }
1122
-
1123
- function findFirstChild(node: XmlElementNode, local: string): XmlElementNode | undefined {
1124
- for (const child of node.children) {
1125
- if (child.type === "element" && localName(child.name) === local) return child;
1126
- }
1127
- return undefined;
1128
- }
1129
-
1130
- function findFirstDescendant(node: XmlElementNode, local: string): XmlElementNode | undefined {
1131
- for (const child of node.children) {
1132
- if (child.type !== "element") continue;
1133
- if (localName(child.name) === local) return child;
1134
- const nested = findFirstDescendant(child, local);
1135
- if (nested) return nested;
1136
- }
1137
- return undefined;
1138
- }
1139
-
1140
- function readTableStyleId(node: XmlElementNode): string | undefined {
1141
- for (const child of node.children) {
1142
- if (child.type !== "element" || localName(child.name) !== "tblStyle") continue;
1143
- const styleId = child.attributes["w:val"] ?? child.attributes.val;
1144
- if (styleId) return styleId;
1145
- }
1146
- return undefined;
1147
- }
1148
-
1149
- function readTableLook(node: XmlElementNode): TableLook | undefined {
1150
- const tblLookNode = node.children.find(
1151
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "tblLook",
1152
- );
1153
- if (!tblLookNode) {
1154
- return undefined;
1155
- }
1156
-
1157
- const tableLook: TableLook = {};
1158
- const val = tblLookNode.attributes["w:val"] ?? tblLookNode.attributes.val;
1159
- if (val) {
1160
- tableLook.val = val;
1161
- }
1162
- for (const [attribute, key] of [
1163
- ["w:firstRow", "firstRow"],
1164
- ["w:lastRow", "lastRow"],
1165
- ["w:firstColumn", "firstColumn"],
1166
- ["w:lastColumn", "lastColumn"],
1167
- ["w:noHBand", "noHBand"],
1168
- ["w:noVBand", "noVBand"],
1169
- ] as const) {
1170
- const fallback = attribute.replace("w:", "");
1171
- const raw = tblLookNode.attributes[attribute] ?? tblLookNode.attributes[fallback];
1172
- if (raw !== undefined) {
1173
- tableLook[key] = raw !== "0" && raw !== "false" && raw !== "off";
1174
- }
1175
- }
1176
-
1177
- return Object.keys(tableLook).length > 0 ? tableLook : undefined;
1178
- }
1179
-
1180
- function readTableGridColumns(node: XmlElementNode): number[] {
1181
- return node.children
1182
- .filter((child): child is XmlElementNode => child.type === "element" && localName(child.name) === "gridCol")
1183
- .map((child) => {
1184
- const raw = child.attributes["w:w"] ?? child.attributes.w ?? "0";
1185
- const value = Number.parseInt(raw, 10);
1186
- return Number.isFinite(value) && value > 0 ? value : 0;
1187
- });
1188
- }
1189
-
1190
- /**
1191
- * Check if a table's raw XML contains content that cannot safely round-trip
1192
- * through the parsed table path yet. This includes:
1193
- * - Revision markup (tracked changes inside cells)
1194
- * - Field syntax and smart tags that still lack a safe table-local live model
1195
- * - Grid geometry tags that the table serializer does not reconstruct yet
1196
- *
1197
- * Tables matching this check stay opaque until the respective features
1198
- * are implemented in the table editing path.
1199
- */
1200
- function tableRequiresOpaquePreservation(rawXml: string): boolean {
1201
- // Safe table-local content now includes hyperlinks, bookmarks, comments,
1202
- // nested tables, floating images, VML preview atoms, and bounded field
1203
- // families already owned by the current field slice. Risky table-local
1204
- // semantics still fail closed to preserve-only.
1205
- if (/<w:(ins|del|rPrChange|pPrChange|tblPrChange|trPrChange|tcPrChange|sectPrChange|cellIns|cellDel|cellMerge|smartTag|tblCellSpacing)\b/.test(rawXml)) {
1206
- return true;
1207
- }
1208
-
1209
- const simpleInstructions = [...rawXml.matchAll(/\bw:instr="([^"]*)"/g)].map((match) => match[1] ?? "");
1210
- const complexInstructions = extractComplexFieldInstructions(rawXml);
1211
- for (const instruction of [...simpleInstructions, ...complexInstructions]) {
1212
- const classification = classifyFieldInstruction(instruction);
1213
- if (!isSafeMainStoryTableFieldFamily(classification.family)) {
1214
- return true;
1215
- }
1216
- }
1217
-
1218
- return false;
1219
- }
1220
-
1221
- function isSafeMainStoryTableFieldFamily(family: string): boolean {
1222
- return (
1223
- family === "REF" ||
1224
- family === "PAGEREF" ||
1225
- family === "NOTEREF" ||
1226
- family === "TOC" ||
1227
- family === "PAGE" ||
1228
- family === "NUMPAGES"
1229
- );
1230
- }
1231
-
1232
- function extractComplexFieldInstructions(rawXml: string): string[] {
1233
- const tokenPattern =
1234
- /<(?:\w+:)?fldChar\b[^>]*?(?:\w+:)?fldCharType="(begin|separate|end)"[^>]*?(?:\/>|>[\s\S]*?<\/(?:\w+:)?fldChar>)|<(?:\w+:)?instrText\b[^>]*>([\s\S]*?)<\/(?:\w+:)?instrText>/gu;
1235
- const instructions: string[] = [];
1236
- let activeInstruction = "";
1237
- let capturingInstruction = false;
1238
-
1239
- for (const match of rawXml.matchAll(tokenPattern)) {
1240
- const fldCharType = match[1];
1241
- const instrText = match[2];
1242
- if (fldCharType === "begin") {
1243
- activeInstruction = "";
1244
- capturingInstruction = true;
1245
- continue;
1246
- }
1247
- if (fldCharType === "separate" || fldCharType === "end") {
1248
- if (capturingInstruction && activeInstruction.trim().length > 0) {
1249
- instructions.push(activeInstruction);
1250
- }
1251
- activeInstruction = "";
1252
- capturingInstruction = false;
1253
- continue;
1254
- }
1255
- if (capturingInstruction && instrText !== undefined) {
1256
- activeInstruction += decodeXmlEntities(instrText);
1257
- }
1258
- }
1259
-
1260
- return instructions;
1261
- }
1262
-
1263
- function readTableRowGridPosition(
1264
- node: XmlElementNode,
1265
- local: "gridBefore" | "gridAfter",
1266
- ): number | undefined {
1267
- const positionNode = node.children.find(
1268
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === local,
1269
- );
1270
- if (!positionNode) return undefined;
1271
- const raw = positionNode.attributes["w:val"] ?? positionNode.attributes.val;
1272
- const value = Number.parseInt(raw ?? "", 10);
1273
- return Number.isFinite(value) && value > 0 ? value : 0;
1274
- }
1275
-
1276
- function readTableRowWidth(
1277
- node: XmlElementNode,
1278
- local: "wBefore" | "wAfter",
1279
- ): TableWidth | undefined {
1280
- const widthNode = node.children.find(
1281
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === local,
1282
- );
1283
- if (!widthNode) return undefined;
1284
- const rawValue = widthNode.attributes["w:w"] ?? widthNode.attributes.w;
1285
- const value = Number.parseInt(rawValue ?? "", 10);
1286
- if (!Number.isFinite(value)) {
1287
- return undefined;
1288
- }
1289
- const rawType = (widthNode.attributes["w:type"] ?? widthNode.attributes.type ?? "dxa").toLowerCase();
1290
- const type: TableWidth["type"] =
1291
- rawType === "auto" ? "auto" : rawType === "pct" ? "pct" : rawType === "nil" ? "nil" : "dxa";
1292
- return { value, type };
1293
- }
1294
-
1295
- function readCellGridSpan(node: XmlElementNode): number | undefined {
1296
- const gridSpanNode = node.children.find(
1297
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "gridSpan",
1298
- );
1299
- if (!gridSpanNode) return undefined;
1300
- const raw = gridSpanNode.attributes["w:val"] ?? gridSpanNode.attributes.val;
1301
- const value = Number.parseInt(raw ?? "0", 10);
1302
- return Number.isFinite(value) && value > 1 ? value : undefined;
1303
- }
1304
-
1305
- function readCellVerticalMerge(node: XmlElementNode): "restart" | "continue" | undefined {
1306
- const vMergeNode = node.children.find(
1307
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "vMerge",
1308
- );
1309
- if (!vMergeNode) return undefined;
1310
- const raw = (vMergeNode.attributes["w:val"] ?? vMergeNode.attributes.val ?? "continue").toLowerCase();
1311
- return raw === "restart" ? "restart" : "continue";
1312
- }
1313
-
1314
- function readParagraphAlignment(node: XmlElementNode): ParsedParagraphNode["alignment"] {
1315
- const jcNode = node.children.find(
1316
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "jc",
1317
- );
1318
- if (!jcNode) return undefined;
1319
- const val = (jcNode.attributes["w:val"] ?? jcNode.attributes.val ?? "").toLowerCase();
1320
- if (val === "left" || val === "center" || val === "right" || val === "both" || val === "distribute") {
1321
- return val;
1322
- }
1323
- return undefined;
1324
- }
1325
-
1326
- function readParagraphSpacing(node: XmlElementNode): ParagraphSpacing | undefined {
1327
- const spacingNode = node.children.find(
1328
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "spacing",
1329
- );
1330
- if (!spacingNode) return undefined;
1331
-
1332
- const spacing: ParagraphSpacing = {};
1333
- const before = spacingNode.attributes["w:before"] ?? spacingNode.attributes.before;
1334
- const after = spacingNode.attributes["w:after"] ?? spacingNode.attributes.after;
1335
- const line = spacingNode.attributes["w:line"] ?? spacingNode.attributes.line;
1336
- const lineRule = spacingNode.attributes["w:lineRule"] ?? spacingNode.attributes.lineRule;
1337
-
1338
- if (before !== undefined) {
1339
- const v = Number.parseInt(before, 10);
1340
- if (Number.isFinite(v)) spacing.before = v;
1341
- }
1342
- if (after !== undefined) {
1343
- const v = Number.parseInt(after, 10);
1344
- if (Number.isFinite(v)) spacing.after = v;
1345
- }
1346
- if (line !== undefined) {
1347
- const v = Number.parseInt(line, 10);
1348
- if (Number.isFinite(v)) spacing.line = v;
1349
- }
1350
- if (lineRule !== undefined) {
1351
- const lr = lineRule.toLowerCase();
1352
- if (lr === "auto" || lr === "exact") {
1353
- spacing.lineRule = lr;
1354
- } else if (lr === "atleast") {
1355
- spacing.lineRule = "atLeast";
1356
- }
1357
- }
1358
-
1359
- if (
1360
- spacing.before === undefined &&
1361
- spacing.after === undefined &&
1362
- spacing.line === undefined &&
1363
- spacing.lineRule === undefined
1364
- ) {
1365
- return undefined;
1366
- }
1367
- return spacing;
1368
- }
1369
-
1370
- function readParagraphIndentation(node: XmlElementNode): ParagraphIndentation | undefined {
1371
- const indNode = node.children.find(
1372
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "ind",
1373
- );
1374
- if (!indNode) return undefined;
1375
-
1376
- const indentation: ParagraphIndentation = {};
1377
- const left = indNode.attributes["w:left"] ?? indNode.attributes.left;
1378
- const right = indNode.attributes["w:right"] ?? indNode.attributes.right;
1379
- const firstLine = indNode.attributes["w:firstLine"] ?? indNode.attributes.firstLine;
1380
- const hanging = indNode.attributes["w:hanging"] ?? indNode.attributes.hanging;
1381
-
1382
- if (left !== undefined) {
1383
- const v = Number.parseInt(left, 10);
1384
- if (Number.isFinite(v)) indentation.left = v;
1385
- }
1386
- if (right !== undefined) {
1387
- const v = Number.parseInt(right, 10);
1388
- if (Number.isFinite(v)) indentation.right = v;
1389
- }
1390
- if (firstLine !== undefined) {
1391
- const v = Number.parseInt(firstLine, 10);
1392
- if (Number.isFinite(v)) indentation.firstLine = v;
1393
- }
1394
- if (hanging !== undefined) {
1395
- const v = Number.parseInt(hanging, 10);
1396
- if (Number.isFinite(v)) indentation.hanging = v;
1397
- }
1398
-
1399
- if (
1400
- indentation.left === undefined &&
1401
- indentation.right === undefined &&
1402
- indentation.firstLine === undefined &&
1403
- indentation.hanging === undefined
1404
- ) {
1405
- return undefined;
1406
- }
1407
- return indentation;
1408
- }
1409
-
1410
- function readParagraphTabStops(node: XmlElementNode): TabStop[] | undefined {
1411
- const tabsNode = node.children.find(
1412
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "tabs",
1413
- );
1414
- if (!tabsNode) return undefined;
1415
-
1416
- const tabStops: TabStop[] = [];
1417
- for (const child of tabsNode.children) {
1418
- if (child.type !== "element" || localName(child.name) !== "tab") continue;
1419
- const pos = child.attributes["w:pos"] ?? child.attributes.pos;
1420
- const val = (child.attributes["w:val"] ?? child.attributes.val ?? "left").toLowerCase();
1421
- const leader = (child.attributes["w:leader"] ?? child.attributes.leader ?? "none").toLowerCase();
1422
-
1423
- if (pos === undefined) continue;
1424
- const position = Number.parseInt(pos, 10);
1425
- if (!Number.isFinite(position)) continue;
1426
-
1427
- const align = (["left", "center", "right", "decimal", "bar", "clear"] as const).includes(
1428
- val as "left" | "center" | "right" | "decimal" | "bar" | "clear",
1429
- )
1430
- ? (val as TabStop["align"])
1431
- : "left";
1432
-
1433
- const leaderValue =
1434
- leader === "none" ||
1435
- leader === "dot" ||
1436
- leader === "hyphen" ||
1437
- leader === "underscore" ||
1438
- leader === "heavy"
1439
- ? (leader as Exclude<TabStop["leader"], "middleDot">)
1440
- : leader === "middledot"
1441
- ? "middleDot"
1442
- : undefined;
1443
-
1444
- tabStops.push({
1445
- position,
1446
- align,
1447
- ...(leaderValue && leaderValue !== "none" ? { leader: leaderValue } : {}),
1448
- });
1449
- }
1450
-
1451
- return tabStops.length > 0 ? tabStops : undefined;
1452
- }
1453
-
1454
- function readOnOffParagraphProperty(node: XmlElementNode, name: string): boolean | undefined {
1455
- const propNode = node.children.find(
1456
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === name,
1457
- );
1458
- if (!propNode) return undefined;
1459
- const val = (propNode.attributes["w:val"] ?? propNode.attributes.val ?? "true").toLowerCase();
1460
- return val !== "false" && val !== "0" && val !== "off" ? true : undefined;
1461
- }
1462
-
1463
- function readOptionalOnOffParagraphProperty(
1464
- node: XmlElementNode,
1465
- name: string,
1466
- ): boolean | undefined {
1467
- const propNode = node.children.find(
1468
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === name,
1469
- );
1470
- if (!propNode) return undefined;
1471
- const val = (propNode.attributes["w:val"] ?? propNode.attributes.val ?? "true").toLowerCase();
1472
- return val !== "false" && val !== "0" && val !== "off";
1473
- }
1474
-
1475
- function readParagraphOutlineLevel(node: XmlElementNode): number | undefined {
1476
- const propNode = node.children.find(
1477
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "outlineLvl",
1478
- );
1479
- if (!propNode) return undefined;
1480
- const val = propNode.attributes["w:val"] ?? propNode.attributes.val;
1481
- if (val === undefined) return undefined;
1482
- const level = Number.parseInt(val, 10);
1483
- return Number.isFinite(level) ? level : undefined;
1484
- }
1485
-
1486
- function readParagraphBorders(node: XmlElementNode): ParagraphBorders | undefined {
1487
- const borderContainer = node.children.find(
1488
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "pBdr",
1489
- );
1490
- if (!borderContainer) {
1491
- return undefined;
1492
- }
1493
-
1494
- const borders: ParagraphBorders = {};
1495
- for (const [name, key] of [
1496
- ["top", "top"],
1497
- ["left", "left"],
1498
- ["bottom", "bottom"],
1499
- ["right", "right"],
1500
- ["bar", "bar"],
1501
- ["between", "between"],
1502
- ] as const) {
1503
- const borderNode = borderContainer.children.find(
1504
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === name,
1505
- );
1506
- if (borderNode) {
1507
- const border = readBorder(borderNode);
1508
- if (border) {
1509
- borders[key] = border;
1510
- }
1511
- }
1512
- }
1513
-
1514
- return Object.keys(borders).length > 0 ? borders : undefined;
1515
- }
1516
-
1517
- function readParagraphShading(node: XmlElementNode): ParagraphShading | undefined {
1518
- const shadingNode = node.children.find(
1519
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "shd",
1520
- );
1521
- if (!shadingNode) {
1522
- return undefined;
1523
- }
1524
-
1525
- const shading: ParagraphShading = {};
1526
- const fill = shadingNode.attributes["w:fill"] ?? shadingNode.attributes.fill;
1527
- const color = shadingNode.attributes["w:color"] ?? shadingNode.attributes.color;
1528
- const val = shadingNode.attributes["w:val"] ?? shadingNode.attributes.val;
1529
- if (fill) shading.fill = fill;
1530
- if (color) shading.color = color;
1531
- if (val) shading.val = val;
1532
- return Object.keys(shading).length > 0 ? shading : undefined;
1533
- }
1534
-
1535
- function readParagraphCnfStyle(node: XmlElementNode): string | undefined {
1536
- const cnfStyleNode = node.children.find(
1537
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "cnfStyle",
1538
- );
1539
- return cnfStyleNode?.attributes["w:val"] ?? cnfStyleNode?.attributes.val;
1540
- }
1541
-
1542
- function readBorder(node: XmlElementNode): ParagraphBorders[keyof ParagraphBorders] {
1543
- const border: NonNullable<ParagraphBorders[keyof ParagraphBorders]> = {};
1544
- const value = node.attributes["w:val"] ?? node.attributes.val;
1545
- const size = node.attributes["w:sz"] ?? node.attributes.sz;
1546
- const space = node.attributes["w:space"] ?? node.attributes.space;
1547
- const color = node.attributes["w:color"] ?? node.attributes.color;
1548
- if (value) border.value = value;
1549
- if (size !== undefined) {
1550
- const parsedSize = Number.parseInt(size, 10);
1551
- if (Number.isFinite(parsedSize)) border.size = parsedSize;
1552
- }
1553
- if (space !== undefined) {
1554
- const parsedSpace = Number.parseInt(space, 10);
1555
- if (Number.isFinite(parsedSpace)) border.space = parsedSpace;
1556
- }
1557
- if (color) border.color = color;
1558
- return Object.keys(border).length > 0 ? border : undefined;
1559
- }
1560
-
1561
- function readParagraphStyleId(node: XmlElementNode): string | undefined {
1562
- for (const child of node.children) {
1563
- if (child.type !== "element" || localName(child.name) !== "pStyle") {
1564
- continue;
1565
- }
1566
-
1567
- const styleId = child.attributes["w:val"] ?? child.attributes.val;
1568
- if (styleId) {
1569
- return styleId;
1570
- }
1571
- }
1572
-
1573
- return undefined;
1574
- }
1575
-
1576
- function readParagraphNumbering(
1577
- node: XmlElementNode,
1578
- ): ParsedParagraphNode["numbering"] | undefined {
1579
- const numberingProperties = node.children.find(
1580
- (child): child is XmlElementNode =>
1581
- child.type === "element" && localName(child.name) === "numPr",
1582
- );
1583
- if (!numberingProperties) {
1584
- return undefined;
1585
- }
1586
-
1587
- const levelNode = numberingProperties.children.find(
1588
- (child): child is XmlElementNode =>
1589
- child.type === "element" && localName(child.name) === "ilvl",
1590
- );
1591
- const instanceNode = numberingProperties.children.find(
1592
- (child): child is XmlElementNode =>
1593
- child.type === "element" && localName(child.name) === "numId",
1594
- );
1595
- const rawLevel = levelNode?.attributes["w:val"] ?? levelNode?.attributes.val;
1596
- const rawInstanceId = instanceNode?.attributes["w:val"] ?? instanceNode?.attributes.val;
1597
- if (!rawInstanceId || rawLevel === undefined || !/^-?\d+$/.test(rawLevel)) {
1598
- return undefined;
1599
- }
1600
-
1601
- return {
1602
- numberingInstanceId: toCanonicalNumberingInstanceId(rawInstanceId),
1603
- level: Number.parseInt(rawLevel, 10),
1604
- };
1605
- }
1606
-
1607
- function parseRun(
1608
- node: XmlElementNode,
1609
- sourceXml: string,
1610
- relationships: readonly OpcRelationship[],
1611
- mediaParts: ReadonlyMap<string, InlineMediaPart>,
1612
- sourcePartPath: string,
1613
- ): ParsedInlineNode[] {
1614
- const marksResult = readRunMarks(node, sourceXml);
1615
- if (!marksResult.supported) {
1616
- return [
1617
- {
1618
- type: "opaque_inline",
1619
- rawXml: sourceXml.slice(node.start, node.end),
1620
- },
1621
- ];
1622
- }
1623
-
1624
- const marks = marksResult.marks;
1625
- const result: ParsedInlineNode[] = [];
1626
- let encounteredUnsupportedChild = false;
1627
-
1628
- for (const child of node.children) {
1629
- if (child.type !== "element") {
1630
- continue;
1631
- }
1632
-
1633
- switch (localName(child.name)) {
1634
- case "rPr":
1635
- break;
1636
- case "t": {
1637
- const text = child.children
1638
- .filter((entry): entry is XmlTextNode => entry.type === "text")
1639
- .map((entry) => entry.text)
1640
- .join("");
1641
- result.push({
1642
- type: "text",
1643
- text,
1644
- ...(marks.length > 0 ? { marks } : {}),
1645
- });
1646
- break;
1647
- }
1648
- case "tab":
1649
- result.push({ type: "tab" });
1650
- break;
1651
- case "sym": {
1652
- const symbol = parseSymbolNode(child, marks);
1653
- if (!symbol) {
1654
- encounteredUnsupportedChild = true;
1655
- result.push({
1656
- type: "opaque_inline",
1657
- rawXml: sourceXml.slice(child.start, child.end),
1658
- });
1659
- break;
1660
- }
1661
- result.push(symbol);
1662
- break;
1663
- }
1664
- case "br":
1665
- if (isColumnBreak(child)) {
1666
- result.push({ type: "column_break" });
1667
- } else if (isSimpleLineBreak(child)) {
1668
- result.push({ type: "hard_break" });
1669
- } else {
1670
- result.push({
1671
- type: "opaque_inline",
1672
- rawXml: sourceXml.slice(child.start, child.end),
1673
- });
1674
- }
1675
- break;
1676
- case "drawing": {
1677
- const drawingXml = sourceXml.slice(child.start, child.end);
1678
-
1679
- // Try complex content (charts / SmartArt) first
1680
- try {
1681
- const complexContent = parseComplexContentXml(
1682
- drawingXml,
1683
- relationships,
1684
- mediaParts,
1685
- sourcePartPath,
1686
- );
1687
- if (complexContent) {
1688
- result.push(complexContent);
1689
- break;
1690
- }
1691
- } catch {
1692
- // Fall through to shape / image parsing
1693
- }
1694
-
1695
- // Try shape / WordArt parsing
1696
- try {
1697
- const shapeResult = parseShapeXml(drawingXml);
1698
- if (shapeResult) {
1699
- result.push(shapeResult);
1700
- break;
1701
- }
1702
- } catch {
1703
- // Fall through to image parsing
1704
- }
1705
-
1706
- const parsedMedia = parseInlineMediaXml(
1707
- drawingXml,
1708
- relationships,
1709
- mediaParts,
1710
- sourcePartPath,
1711
- );
1712
- if (parsedMedia.length === 0) {
1713
- encounteredUnsupportedChild = true;
1714
- result.push({
1715
- type: "opaque_inline",
1716
- rawXml: sourceXml.slice(node.start, node.end),
1717
- });
1718
- break;
1719
- }
1720
-
1721
- const semanticChildren = node.children.filter(
1722
- (entry): entry is XmlElementNode =>
1723
- entry.type === "element" && localName(entry.name) !== "rPr",
1724
- );
1725
- const placementXml =
1726
- semanticChildren.length === 1
1727
- ? sourceXml.slice(node.start, node.end)
1728
- : sourceXml.slice(child.start, child.end);
1729
-
1730
- result.push(
1731
- ...parsedMedia.map((media) => ({
1732
- type: "image" as const,
1733
- mediaId: media.mediaId,
1734
- relationshipId: media.relationshipId,
1735
- packagePartName: media.packagePartName,
1736
- contentType: media.contentType,
1737
- filename: media.filename,
1738
- ...(media.altText ? { altText: media.altText } : {}),
1739
- ...(media.widthEmu !== undefined ? { widthEmu: media.widthEmu } : {}),
1740
- ...(media.heightEmu !== undefined ? { heightEmu: media.heightEmu } : {}),
1741
- placementXml,
1742
- ...(media.display ? { display: media.display } : {}),
1743
- ...(media.floating ? { floating: media.floating } : {}),
1744
- })),
1745
- );
1746
- break;
1747
- }
1748
- case "pict": {
1749
- const pictXml = sourceXml.slice(child.start, child.end);
1750
- try {
1751
- const vmlResult = parseVmlXml(pictXml);
1752
- if (vmlResult) {
1753
- // Use the full run XML as rawXml so the export serializer can emit
1754
- // the complete <w:r>...<w:pict>...</w:pict></w:r> wrapper
1755
- result.push({
1756
- ...vmlResult,
1757
- rawXml: sourceXml.slice(node.start, node.end),
1758
- });
1759
- break;
1760
- }
1761
- } catch {
1762
- // Fall through to opaque
1763
- }
1764
- encounteredUnsupportedChild = true;
1765
- result.push({
1766
- type: "opaque_inline",
1767
- rawXml: pictXml,
1768
- });
1769
- break;
1770
- }
1771
- case "commentReference":
1772
- break;
1773
- case "footnoteReference": {
1774
- const noteId = child.attributes["w:id"] ?? child.attributes.id ?? "";
1775
- if (noteId) {
1776
- result.push({
1777
- type: "footnote_ref",
1778
- noteId,
1779
- noteKind: "footnote",
1780
- });
1781
- }
1782
- break;
1783
- }
1784
- case "endnoteReference": {
1785
- const noteId = child.attributes["w:id"] ?? child.attributes.id ?? "";
1786
- if (noteId) {
1787
- result.push({
1788
- type: "footnote_ref",
1789
- noteId,
1790
- noteKind: "endnote",
1791
- });
1792
- }
1793
- break;
1794
- }
1795
- case "lastRenderedPageBreak":
1796
- case "proofErr":
1797
- result.push({
1798
- type: "opaque_inline",
1799
- rawXml: sourceXml.slice(child.start, child.end),
1800
- });
1801
- break;
1802
- default:
1803
- encounteredUnsupportedChild = true;
1804
- result.push({
1805
- type: "opaque_inline",
1806
- rawXml: sourceXml.slice(child.start, child.end),
1807
- });
1808
- break;
1809
- }
1810
- }
1811
-
1812
- if (encounteredUnsupportedChild && result.every((child) => child.type === "opaque_inline")) {
1813
- return [
1814
- {
1815
- type: "opaque_inline",
1816
- rawXml: sourceXml.slice(node.start, node.end),
1817
- },
1818
- ];
1819
- }
1820
-
1821
- return result;
1822
- }
1823
-
1824
- function parseRevisionContainer(
1825
- node: XmlElementNode,
1826
- sourceXml: string,
1827
- relationshipMap: Map<string, OpcRelationship>,
1828
- ): ParsedInlineNode[] {
1829
- const result: ParsedInlineNode[] = [];
1830
- const allowsDeletedText = localName(node.name) === "del";
1831
-
1832
- for (const child of node.children) {
1833
- if (child.type !== "element") {
1834
- continue;
1835
- }
1836
-
1837
- switch (localName(child.name)) {
1838
- case "r": {
1839
- const run = parseRunContentOnly(child, sourceXml, {
1840
- allowDeletedText: allowsDeletedText,
1841
- preserveUnsupportedReviewMarkup: true,
1842
- });
1843
- if (!run.supported) {
1844
- return [
1845
- {
1846
- type: "opaque_inline",
1847
- rawXml: sourceXml.slice(node.start, node.end),
1848
- },
1849
- ];
1850
- }
1851
- result.push(...run.nodes);
1852
- break;
1853
- }
1854
- case "hyperlink": {
1855
- const hyperlink = parseHyperlink(child, sourceXml, relationshipMap, {
1856
- allowDeletedText: allowsDeletedText,
1857
- preserveUnsupportedReviewMarkup: true,
1858
- });
1859
- if (hyperlink.type === "opaque_inline") {
1860
- return [
1861
- {
1862
- type: "opaque_inline",
1863
- rawXml: sourceXml.slice(node.start, node.end),
1864
- },
1865
- ];
1866
- }
1867
- result.push(hyperlink);
1868
- break;
1869
- }
1870
- case "commentRangeStart":
1871
- case "commentRangeEnd":
1872
- case "bookmarkStart":
1873
- case "bookmarkEnd":
1874
- case "proofErr":
1875
- case "lastRenderedPageBreak":
1876
- return [
1877
- {
1878
- type: "opaque_inline",
1879
- rawXml: sourceXml.slice(node.start, node.end),
1880
- },
1881
- ];
1882
- case "permStart":
1883
- result.push(parsePermStartNode(child, sourceXml));
1884
- break;
1885
- case "permEnd":
1886
- result.push(parsePermEndNode(child, sourceXml));
1887
- break;
1888
- default:
1889
- return [
1890
- {
1891
- type: "opaque_inline",
1892
- rawXml: sourceXml.slice(node.start, node.end),
1893
- },
1894
- ];
1895
- }
1896
- }
1897
-
1898
- return result;
1899
- }
1900
-
1901
- function parseHyperlink(
1902
- node: XmlElementNode,
1903
- sourceXml: string,
1904
- relationshipMap: Map<string, OpcRelationship>,
1905
- options: {
1906
- allowDeletedText?: boolean;
1907
- preserveUnsupportedReviewMarkup?: boolean;
1908
- } = {},
1909
- ): ParsedHyperlinkNode | ParsedOpaqueInlineNode {
1910
- const relationshipId = node.attributes["r:id"] ?? node.attributes.id;
1911
- const anchor = node.attributes["w:anchor"] ?? node.attributes.anchor;
1912
- let href: string | undefined;
1913
-
1914
- if (relationshipId) {
1915
- const relationship = relationshipMap.get(relationshipId);
1916
- if (
1917
- relationship &&
1918
- relationship.type === HYPERLINK_RELATIONSHIP_TYPE &&
1919
- relationship.targetMode === "external"
1920
- ) {
1921
- href = relationship.target;
1922
- }
1923
- } else if (anchor) {
1924
- href = `#${anchor}`;
1925
- }
1926
-
1927
- if (!href) {
1928
- return {
1929
- type: "opaque_inline",
1930
- rawXml: sourceXml.slice(node.start, node.end),
1931
- };
1932
- }
1933
-
1934
- const children: Array<ParsedTextNode | ParsedBreakNode | ParsedColumnBreakNode | ParsedTabNode | ParsedSymbolNode> = [];
1935
-
1936
- for (const child of node.children) {
1937
- if (child.type !== "element") {
1938
- continue;
1939
- }
1940
-
1941
- if (localName(child.name) !== "r") {
1942
- return {
1943
- type: "opaque_inline",
1944
- rawXml: sourceXml.slice(node.start, node.end),
1945
- };
1946
- }
1947
-
1948
- const run = parseRunContentOnly(child, sourceXml, {
1949
- allowDeletedText: options.allowDeletedText,
1950
- preserveUnsupportedReviewMarkup: options.preserveUnsupportedReviewMarkup,
1951
- });
1952
- if (!run.supported) {
1953
- return {
1954
- type: "opaque_inline",
1955
- rawXml: sourceXml.slice(node.start, node.end),
1956
- };
1957
- }
1958
-
1959
- children.push(...run.nodes);
1960
- }
1961
-
1962
- return {
1963
- type: "hyperlink",
1964
- href,
1965
- children,
1966
- rawXml: sourceXml.slice(node.start, node.end),
1967
- };
1968
- }
1969
-
1970
- function parseRunContentOnly(
1971
- node: XmlElementNode,
1972
- _sourceXml: string,
1973
- options: {
1974
- allowDeletedText?: boolean;
1975
- preserveUnsupportedReviewMarkup?: boolean;
1976
- } = {},
1977
- ): RunParseResult {
1978
- const marksResult = readRunMarks(node, _sourceXml);
1979
- if (!marksResult.supported) {
1980
- return { nodes: [], supported: false };
1981
- }
1982
-
1983
- const marks = marksResult.marks;
1984
- const nodes: Array<ParsedTextNode | ParsedBreakNode | ParsedColumnBreakNode | ParsedTabNode | ParsedSymbolNode> = [];
1985
-
1986
- for (const child of node.children) {
1987
- if (child.type !== "element") {
1988
- continue;
1989
- }
1990
-
1991
- switch (localName(child.name)) {
1992
- case "rPr":
1993
- break;
1994
- case "t": {
1995
- const text = child.children
1996
- .filter((entry): entry is XmlTextNode => entry.type === "text")
1997
- .map((entry) => entry.text)
1998
- .join("");
1999
- nodes.push({
2000
- type: "text",
2001
- text,
2002
- ...(marks.length > 0 ? { marks } : {}),
2003
- });
2004
- break;
2005
- }
2006
- case "delText":
2007
- case "delInstrText":
2008
- if (!options.allowDeletedText) {
2009
- return { nodes: [], supported: false };
2010
- }
2011
- nodes.push({
2012
- type: "text",
2013
- text: child.children
2014
- .filter((entry): entry is XmlTextNode => entry.type === "text")
2015
- .map((entry) => entry.text)
2016
- .join(""),
2017
- ...(marks.length > 0 ? { marks } : {}),
2018
- });
2019
- break;
2020
- case "tab":
2021
- nodes.push({ type: "tab" });
2022
- break;
2023
- case "sym": {
2024
- const symbol = parseSymbolNode(child, marks);
2025
- if (!symbol) {
2026
- return { nodes: [], supported: false };
2027
- }
2028
- nodes.push(symbol);
2029
- break;
2030
- }
2031
- case "br":
2032
- if (isColumnBreak(child)) {
2033
- nodes.push({ type: "column_break" });
2034
- break;
2035
- }
2036
- if (!isSimpleLineBreak(child)) {
2037
- return { nodes: [], supported: false };
2038
- }
2039
- nodes.push({ type: "hard_break" });
2040
- break;
2041
- case "commentReference":
2042
- case "lastRenderedPageBreak":
2043
- case "proofErr":
2044
- if (options.preserveUnsupportedReviewMarkup) {
2045
- return { nodes: [], supported: false };
2046
- }
2047
- break;
2048
- default:
2049
- return { nodes: [], supported: false };
2050
- }
2051
- }
2052
-
2053
- return { nodes, supported: true };
2054
- }
2055
-
2056
- function readRunMarks(node: XmlElementNode, sourceXml: string): MarksParseResult {
2057
- const properties = node.children.find(
2058
- (child): child is XmlElementNode =>
2059
- child.type === "element" && localName(child.name) === "rPr",
2060
- );
2061
-
2062
- if (!properties) {
2063
- return { marks: [], supported: true };
2064
- }
2065
-
2066
- if (
2067
- properties.children.some(
2068
- (child) =>
2069
- child.type === "element" &&
2070
- DISALLOWED_RUN_PROPERTY_NAMES.has(localName(child.name)),
2071
- )
2072
- ) {
2073
- return {
2074
- marks: [],
2075
- supported: false,
2076
- };
2077
- }
2078
-
2079
- const marks: TextMark[] = [];
2080
- if (hasOnOffProperty(properties, "b")) {
2081
- marks.push({ type: "bold" });
2082
- }
2083
- if (hasOnOffProperty(properties, "i")) {
2084
- marks.push({ type: "italic" });
2085
- }
2086
- if (hasUnderlineProperty(properties)) {
2087
- marks.push({ type: "underline" });
2088
- }
2089
- if (hasOnOffProperty(properties, "strike")) {
2090
- marks.push({ type: "strikethrough" });
2091
- }
2092
- if (hasOnOffProperty(properties, "dstrike")) {
2093
- marks.push({ type: "doubleStrikethrough" });
2094
- }
2095
- if (hasOnOffProperty(properties, "vanish")) {
2096
- marks.push({ type: "vanish" });
2097
- }
2098
-
2099
- const langMark = readRunLang(properties);
2100
- if (langMark) {
2101
- marks.push(langMark);
2102
- }
2103
-
2104
- const backgroundColorMark = readRunBackgroundColor(properties);
2105
- if (backgroundColorMark) {
2106
- marks.push(backgroundColorMark);
2107
- }
2108
-
2109
- const highlightMark = readRunHighlight(properties);
2110
- if (highlightMark) {
2111
- marks.push(highlightMark);
2112
- }
2113
-
2114
- const charSpacingMark = readNumericRunMark(properties, "spacing", "charSpacing");
2115
- if (charSpacingMark) {
2116
- marks.push(charSpacingMark);
2117
- }
2118
-
2119
- const kerningMark = readNumericRunMark(properties, "kern", "kerning");
2120
- if (kerningMark) {
2121
- marks.push(kerningMark);
2122
- }
2123
-
2124
- const positionMark = readNumericRunMark(properties, "position", "position");
2125
- if (positionMark) {
2126
- marks.push(positionMark);
2127
- }
2128
-
2129
- if (hasOnOffProperty(properties, "emboss")) {
2130
- marks.push({ type: "emboss" });
2131
- }
2132
- if (hasOnOffProperty(properties, "imprint")) {
2133
- marks.push({ type: "imprint" });
2134
- }
2135
- if (hasOnOffProperty(properties, "shadow")) {
2136
- marks.push({ type: "shadow" });
2137
- }
2138
-
2139
- const textFillMark = readRunTextFill(properties, sourceXml);
2140
- if (textFillMark) {
2141
- marks.push(textFillMark);
2142
- }
2143
-
2144
- // Font family
2145
- const rFontsNode = properties.children.find(
2146
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "rFonts",
2147
- );
2148
- if (rFontsNode) {
2149
- const family = rFontsNode.attributes["w:ascii"] ?? rFontsNode.attributes["w:hAnsi"] ?? rFontsNode.attributes.ascii ?? rFontsNode.attributes.hAnsi;
2150
- if (family) {
2151
- marks.push({ type: "fontFamily", val: family });
2152
- }
2153
- }
2154
-
2155
- // Font size (half-points)
2156
- const szNode = properties.children.find(
2157
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "sz",
2158
- );
2159
- if (szNode) {
2160
- const szVal = szNode.attributes["w:val"] ?? szNode.attributes.val;
2161
- if (szVal) {
2162
- const size = Number.parseInt(szVal, 10);
2163
- if (Number.isFinite(size) && size > 0) {
2164
- marks.push({ type: "fontSize", val: size });
2165
- }
2166
- }
2167
- }
2168
-
2169
- // Text color
2170
- const colorNode = properties.children.find(
2171
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "color",
2172
- );
2173
- if (colorNode) {
2174
- const colorVal = colorNode.attributes["w:val"] ?? colorNode.attributes.val;
2175
- if (colorVal && colorVal !== "auto") {
2176
- marks.push({ type: "textColor", color: colorVal });
2177
- }
2178
- }
2179
-
2180
- // Small caps
2181
- if (hasOnOffProperty(properties, "smallCaps")) {
2182
- marks.push({ type: "smallCaps" });
2183
- }
2184
-
2185
- // All caps
2186
- if (hasOnOffProperty(properties, "caps")) {
2187
- marks.push({ type: "allCaps" });
2188
- }
2189
-
2190
- return {
2191
- marks,
2192
- supported: true,
2193
- };
2194
- }
2195
-
2196
- function readRunLang(properties: XmlElementNode): TextMark | undefined {
2197
- const langNode = properties.children.find(
2198
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "lang",
2199
- );
2200
- if (!langNode) return undefined;
2201
- const val =
2202
- langNode.attributes["w:val"] ??
2203
- langNode.attributes.val ??
2204
- langNode.attributes["w:bidi"] ??
2205
- langNode.attributes.bidi;
2206
- if (!val) return undefined;
2207
- return { type: "lang", val };
2208
- }
2209
-
2210
- function readRunBackgroundColor(properties: XmlElementNode): TextMark | undefined {
2211
- const shadingNode = properties.children.find(
2212
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "shd",
2213
- );
2214
- if (!shadingNode) {
2215
- return undefined;
2216
- }
2217
-
2218
- const fill = shadingNode.attributes["w:fill"] ?? shadingNode.attributes.fill;
2219
- if (!fill || fill === "auto") {
2220
- return undefined;
2221
- }
2222
-
2223
- return { type: "backgroundColor", color: fill };
2224
- }
2225
-
2226
- function readRunHighlight(properties: XmlElementNode): TextMark | undefined {
2227
- const highlightNode = properties.children.find(
2228
- (child): child is XmlElementNode =>
2229
- child.type === "element" && localName(child.name) === "highlight",
2230
- );
2231
- if (!highlightNode) {
2232
- return undefined;
2233
- }
2234
-
2235
- const highlightValue = highlightNode.attributes["w:val"] ?? highlightNode.attributes.val;
2236
- const resolvedHighlight = resolveHighlightColor(highlightValue);
2237
- if (!resolvedHighlight) {
2238
- return undefined;
2239
- }
2240
-
2241
- return {
2242
- type: "highlight",
2243
- color: resolvedHighlight.color,
2244
- val: resolvedHighlight.val,
2245
- };
2246
- }
2247
-
2248
- function readNumericRunMark(
2249
- properties: XmlElementNode,
2250
- elementName: "spacing" | "kern" | "position",
2251
- markType: "charSpacing" | "kerning" | "position",
2252
- ): TextMark | undefined {
2253
- const propertyNode = properties.children.find(
2254
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === elementName,
2255
- );
2256
- if (!propertyNode) {
2257
- return undefined;
2258
- }
2259
- const rawValue = propertyNode.attributes["w:val"] ?? propertyNode.attributes.val;
2260
- if (rawValue === undefined) {
2261
- return undefined;
2262
- }
2263
- const value = Number.parseInt(rawValue, 10);
2264
- if (!Number.isFinite(value)) {
2265
- return undefined;
2266
- }
2267
- return { type: markType, val: value };
2268
- }
2269
-
2270
- function readRunTextFill(properties: XmlElementNode, sourceXml: string): TextMark | undefined {
2271
- const textFillNode = properties.children.find(
2272
- (child): child is XmlElementNode => child.type === "element" && localName(child.name) === "textFill",
2273
- );
2274
- if (!textFillNode) {
2275
- return undefined;
2276
- }
2277
- return {
2278
- type: "textFill",
2279
- xml: sourceXml.slice(textFillNode.start, textFillNode.end),
2280
- };
2281
- }
2282
-
2283
- function parseSymbolNode(
2284
- node: XmlElementNode,
2285
- marks: TextMark[],
2286
- ): ParsedSymbolNode | undefined {
2287
- const char = node.attributes["w:char"] ?? node.attributes.char;
2288
- if (!char) {
2289
- return undefined;
2290
- }
2291
- const font = node.attributes["w:font"] ?? node.attributes.font;
2292
- return {
2293
- type: "symbol",
2294
- char,
2295
- ...(font ? { font } : {}),
2296
- ...(marks.length > 0 ? { marks } : {}),
2297
- };
2298
- }
2299
-
2300
- function supportsParagraphProperties(node: XmlElementNode): boolean {
2301
- for (const child of node.children) {
2302
- if (child.type !== "element") {
2303
- continue;
2304
- }
2305
-
2306
- const name = localName(child.name);
2307
- if (name === "pPrChange") {
2308
- return false;
2309
- }
2310
-
2311
- if (name === "rPr") {
2312
- if (
2313
- child.children.some(
2314
- (entry) =>
2315
- entry.type === "element" &&
2316
- DISALLOWED_PARAGRAPH_PROPERTY_NAMES.has(localName(entry.name)),
2317
- )
2318
- ) {
2319
- return false;
2320
- }
2321
- }
2322
- }
2323
-
2324
- return true;
2325
- }
2326
-
2327
- const DISALLOWED_RUN_PROPERTY_NAMES = new Set(["rPrChange"]);
2328
- const DISALLOWED_PARAGRAPH_PROPERTY_NAMES = new Set(["pPrChange", "rPrChange"]);
2329
-
2330
- function hasOnOffProperty(properties: XmlElementNode, propertyName: string): boolean {
2331
- const property = properties.children.find(
2332
- (child): child is XmlElementNode =>
2333
- child.type === "element" && localName(child.name) === propertyName,
2334
- );
2335
- if (!property) {
2336
- return false;
2337
- }
2338
-
2339
- const value = (property.attributes["w:val"] ?? property.attributes.val ?? "true").toLowerCase();
2340
- return value !== "false" && value !== "0" && value !== "off";
2341
- }
2342
-
2343
- function hasUnderlineProperty(properties: XmlElementNode): boolean {
2344
- const property = properties.children.find(
2345
- (child): child is XmlElementNode =>
2346
- child.type === "element" && localName(child.name) === "u",
2347
- );
2348
- if (!property) {
2349
- return false;
2350
- }
2351
-
2352
- const value = (property.attributes["w:val"] ?? property.attributes.val ?? "single").toLowerCase();
2353
- return value !== "none";
2354
- }
2355
-
2356
- function isSimpleLineBreak(node: XmlElementNode): boolean {
2357
- const value = (node.attributes["w:type"] ?? node.attributes.type ?? "textWrapping").toLowerCase();
2358
- return value === "textwrapping" || value === "line";
2359
- }
2360
-
2361
- function isColumnBreak(node: XmlElementNode): boolean {
2362
- const value = (node.attributes["w:type"] ?? node.attributes.type ?? "").toLowerCase();
2363
- return value === "column";
2364
- }
2365
-
2366
- function findChildElement(node: XmlElementNode, childLocalName: string): XmlElementNode {
2367
- const child = node.children.find(
2368
- (entry): entry is XmlElementNode =>
2369
- entry.type === "element" && localName(entry.name) === childLocalName,
2370
- );
2371
-
2372
- if (!child) {
2373
- throw new Error(`Expected <${childLocalName}> element in main document XML.`);
2374
- }
2375
-
2376
- return child;
2377
- }
2378
-
2379
- function localName(name: string): string {
2380
- const separatorIndex = name.indexOf(":");
2381
- return separatorIndex >= 0 ? name.slice(separatorIndex + 1) : name;
2382
- }
2383
-
2384
- function readOptionalAttribute(node: XmlElementNode, name: string): string | undefined {
2385
- return node.attributes[`w:${name}`]
2386
- ?? node.attributes[`r:${name}`]
2387
- ?? node.attributes[name];
2388
- }
2389
-
2390
- function parseXml(xml: string): XmlElementNode {
2391
- const root: XmlElementNode = {
2392
- type: "element",
2393
- name: "__root__",
2394
- attributes: {},
2395
- children: [],
2396
- start: 0,
2397
- end: xml.length,
2398
- };
2399
- const stack: XmlElementNode[] = [root];
2400
- let cursor = 0;
2401
-
2402
- while (cursor < xml.length) {
2403
- if (xml.startsWith("<!--", cursor)) {
2404
- const end = xml.indexOf("-->", cursor);
2405
- cursor = end >= 0 ? end + 3 : xml.length;
2406
- continue;
2407
- }
2408
-
2409
- if (xml.startsWith("<?", cursor)) {
2410
- const end = xml.indexOf("?>", cursor);
2411
- cursor = end >= 0 ? end + 2 : xml.length;
2412
- continue;
2413
- }
2414
-
2415
- if (xml.startsWith("<![CDATA[", cursor)) {
2416
- const end = xml.indexOf("]]>", cursor);
2417
- const textEnd = end >= 0 ? end : xml.length;
2418
- stack[stack.length - 1]?.children.push({
2419
- type: "text",
2420
- text: xml.slice(cursor + 9, textEnd),
2421
- start: cursor,
2422
- end: end >= 0 ? end + 3 : xml.length,
2423
- });
2424
- cursor = end >= 0 ? end + 3 : xml.length;
2425
- continue;
2426
- }
2427
-
2428
- const currentChar = xml[cursor];
2429
- if (currentChar !== "<") {
2430
- const nextTag = xml.indexOf("<", cursor);
2431
- const end = nextTag >= 0 ? nextTag : xml.length;
2432
- const text = decodeXmlEntities(xml.slice(cursor, end));
2433
- if (text.length > 0) {
2434
- stack[stack.length - 1]?.children.push({
2435
- type: "text",
2436
- text,
2437
- start: cursor,
2438
- end,
2439
- });
2440
- }
2441
- cursor = end;
2442
- continue;
2443
- }
2444
-
2445
- if (xml[cursor + 1] === "/") {
2446
- const end = xml.indexOf(">", cursor);
2447
- if (end < 0) {
2448
- throw new Error("Malformed XML: missing closing >.");
2449
- }
2450
-
2451
- const name = xml.slice(cursor + 2, end).trim();
2452
- const current = stack.pop();
2453
- if (!current || localName(current.name) !== localName(name)) {
2454
- throw new Error(`Malformed XML: unexpected closing tag </${name}>.`);
2455
- }
2456
- current.end = end + 1;
2457
- cursor = end + 1;
2458
- continue;
2459
- }
2460
-
2461
- const tagEnd = findTagEnd(xml, cursor);
2462
- const tagBody = xml.slice(cursor + 1, tagEnd);
2463
- const selfClosing = /\/\s*$/.test(tagBody);
2464
- const { name, attributes } = parseTag(tagBody.replace(/\/\s*$/, "").trim());
2465
- const element: XmlElementNode = {
2466
- type: "element",
2467
- name,
2468
- attributes,
2469
- children: [],
2470
- start: cursor,
2471
- end: tagEnd + 1,
2472
- };
2473
- stack[stack.length - 1]?.children.push(element);
2474
-
2475
- if (!selfClosing) {
2476
- stack.push(element);
2477
- }
2478
-
2479
- cursor = tagEnd + 1;
2480
- }
2481
-
2482
- if (stack.length !== 1) {
2483
- throw new Error("Malformed XML: unclosed element in main document XML.");
2484
- }
2485
-
2486
- return root;
2487
- }
2488
-
2489
- function parseTag(tagBody: string): { name: string; attributes: Record<string, string> } {
2490
- let cursor = 0;
2491
- while (cursor < tagBody.length && /\s/.test(tagBody[cursor] ?? "")) {
2492
- cursor += 1;
2493
- }
2494
-
2495
- const nameStart = cursor;
2496
- while (cursor < tagBody.length && !/\s/.test(tagBody[cursor] ?? "")) {
2497
- cursor += 1;
2498
- }
2499
- const name = tagBody.slice(nameStart, cursor);
2500
- const attributes: Record<string, string> = {};
2501
-
2502
- while (cursor < tagBody.length) {
2503
- while (cursor < tagBody.length && /\s/.test(tagBody[cursor] ?? "")) {
2504
- cursor += 1;
2505
- }
2506
- if (cursor >= tagBody.length) {
2507
- break;
2508
- }
2509
-
2510
- const keyStart = cursor;
2511
- while (cursor < tagBody.length && !/[\s=]/.test(tagBody[cursor] ?? "")) {
2512
- cursor += 1;
2513
- }
2514
- const key = tagBody.slice(keyStart, cursor);
2515
-
2516
- while (cursor < tagBody.length && /\s/.test(tagBody[cursor] ?? "")) {
2517
- cursor += 1;
2518
- }
2519
-
2520
- if (tagBody[cursor] !== "=") {
2521
- attributes[key] = "";
2522
- continue;
2523
- }
2524
- cursor += 1;
2525
-
2526
- while (cursor < tagBody.length && /\s/.test(tagBody[cursor] ?? "")) {
2527
- cursor += 1;
2528
- }
2529
-
2530
- const quote = tagBody[cursor];
2531
- if (quote !== `"` && quote !== `'`) {
2532
- throw new Error(`Malformed XML attribute ${key}.`);
2533
- }
2534
- cursor += 1;
2535
-
2536
- const valueStart = cursor;
2537
- while (cursor < tagBody.length && tagBody[cursor] !== quote) {
2538
- cursor += 1;
2539
- }
2540
- const rawValue = tagBody.slice(valueStart, cursor);
2541
- attributes[key] = decodeXmlEntities(rawValue);
2542
- cursor += 1;
2543
- }
2544
-
2545
- return { name, attributes };
2546
- }
2547
-
2548
- function findTagEnd(xml: string, start: number): number {
2549
- let cursor = start + 1;
2550
- let quote: string | null = null;
2551
-
2552
- while (cursor < xml.length) {
2553
- const current = xml[cursor];
2554
- if (quote) {
2555
- if (current === quote) {
2556
- quote = null;
2557
- }
2558
- cursor += 1;
2559
- continue;
2560
- }
2561
-
2562
- if (current === `"` || current === `'`) {
2563
- quote = current;
2564
- cursor += 1;
2565
- continue;
2566
- }
2567
-
2568
- if (current === ">") {
2569
- return cursor;
2570
- }
2571
-
2572
- cursor += 1;
2573
- }
2574
-
2575
- throw new Error("Malformed XML: missing >.");
2576
- }
2577
-
2578
- function decodeXmlEntities(value: string): string {
2579
- return value.replace(/&(#x[0-9a-fA-F]+|#\d+|amp|lt|gt|quot|apos);/g, (match, entity) => {
2580
- switch (entity) {
2581
- case "amp":
2582
- return "&";
2583
- case "lt":
2584
- return "<";
2585
- case "gt":
2586
- return ">";
2587
- case "quot":
2588
- return `"`;
2589
- case "apos":
2590
- return "'";
2591
- default:
2592
- if (entity.startsWith("#x")) {
2593
- return String.fromCodePoint(Number.parseInt(entity.slice(2), 16));
2594
- }
2595
- if (entity.startsWith("#")) {
2596
- return String.fromCodePoint(Number.parseInt(entity.slice(1), 10));
2597
- }
2598
- return match;
2599
- }
2600
- });
2601
- }
2602
-
2603
- // ---- Section properties parsing ----
2604
-
2605
- function parseSectionBreakElement(
2606
- node: XmlElementNode,
2607
- sourceXml: string,
2608
- ): ParsedSectionBreakNode {
2609
- const props = parseSectionPropertiesFromElement(node);
2610
- return {
2611
- type: "section_break",
2612
- sectionPropertiesXml: sourceXml.slice(node.start, node.end),
2613
- sectionProperties: props,
2614
- };
2615
- }
2616
-
2617
- function readSectionPropertiesFromPPr(
2618
- pPrNode: XmlElementNode,
2619
- ): SectionProperties | undefined {
2620
- for (const child of pPrNode.children) {
2621
- if (child.type === "element" && localName(child.name) === "sectPr") {
2622
- return parseSectionPropertiesFromElement(child);
2623
- }
2624
- }
2625
- return undefined;
2626
- }
2627
-
2628
- function readSectionPropertiesXmlFromPPr(
2629
- pPrNode: XmlElementNode,
2630
- sourceXml: string,
2631
- ): string | undefined {
2632
- for (const child of pPrNode.children) {
2633
- if (child.type === "element" && localName(child.name) === "sectPr") {
2634
- return sourceXml.slice(child.start, child.end);
2635
- }
2636
- }
2637
- return undefined;
2638
- }
2639
-
2640
- export function parseSectionPropertiesFromElement(
2641
- node: XmlElementNode,
2642
- ): SectionProperties {
2643
- const props: SectionProperties = {};
2644
-
2645
- for (const child of node.children) {
2646
- if (child.type !== "element") continue;
2647
- const name = localName(child.name);
2648
-
2649
- switch (name) {
2650
- case "pgSz": {
2651
- const w = safeParseInt(child.attributes["w:w"]);
2652
- const h = safeParseInt(child.attributes["w:h"]);
2653
- if (w !== undefined && h !== undefined) {
2654
- const pageSize: PageSize = { width: w, height: h };
2655
- const orient = child.attributes["w:orient"];
2656
- if (orient === "landscape" || orient === "portrait") {
2657
- pageSize.orientation = orient;
2658
- }
2659
- props.pageSize = pageSize;
2660
- }
2661
- break;
2662
- }
2663
- case "pgMar": {
2664
- const top = safeParseInt(child.attributes["w:top"]);
2665
- const right = safeParseInt(child.attributes["w:right"]);
2666
- const bottom = safeParseInt(child.attributes["w:bottom"]);
2667
- const left = safeParseInt(child.attributes["w:left"]);
2668
- if (top !== undefined && right !== undefined && bottom !== undefined && left !== undefined) {
2669
- const margins: PageMargins = { top, right, bottom, left };
2670
- const header = safeParseInt(child.attributes["w:header"]);
2671
- const footer = safeParseInt(child.attributes["w:footer"]);
2672
- const gutter = safeParseInt(child.attributes["w:gutter"]);
2673
- if (header !== undefined) margins.header = header;
2674
- if (footer !== undefined) margins.footer = footer;
2675
- if (gutter !== undefined) margins.gutter = gutter;
2676
- props.pageMargins = margins;
2677
- }
2678
- break;
2679
- }
2680
- case "cols": {
2681
- const columns: ColumnProperties = {};
2682
- const num = safeParseInt(child.attributes["w:num"]);
2683
- const space = safeParseInt(child.attributes["w:space"]);
2684
- const equalWidth = child.attributes["w:equalWidth"];
2685
- const sep = child.attributes["w:sep"];
2686
- if (num !== undefined) columns.count = num;
2687
- if (space !== undefined) columns.space = space;
2688
- if (equalWidth !== undefined) columns.equalWidth = equalWidth !== "0" && equalWidth !== "false";
2689
- if (sep === "1" || sep === "true") columns.separator = true;
2690
- const colDefs: Array<{ width: number; space?: number }> = [];
2691
- for (const colChild of child.children) {
2692
- if (colChild.type === "element" && localName(colChild.name) === "col") {
2693
- const colW = safeParseInt(colChild.attributes["w:w"]);
2694
- const colSpace = safeParseInt(colChild.attributes["w:space"]);
2695
- if (colW !== undefined) {
2696
- colDefs.push(colSpace !== undefined ? { width: colW, space: colSpace } : { width: colW });
2697
- }
2698
- }
2699
- }
2700
- if (colDefs.length > 0) columns.columns = colDefs;
2701
- if (Object.keys(columns).length > 0) props.columns = columns;
2702
- break;
2703
- }
2704
- case "pgNumType": {
2705
- const numbering: PageNumbering = {};
2706
- const fmt = child.attributes["w:fmt"];
2707
- const start = safeParseInt(child.attributes["w:start"]);
2708
- const chapStyle = child.attributes["w:chapStyle"];
2709
- const chapSep = child.attributes["w:chapSep"];
2710
- if (fmt) numbering.format = fmt;
2711
- if (start !== undefined) numbering.start = start;
2712
- if (chapStyle) numbering.chapStyle = chapStyle;
2713
- if (chapSep) numbering.chapSep = chapSep;
2714
- if (Object.keys(numbering).length > 0) props.pageNumbering = numbering;
2715
- break;
2716
- }
2717
- case "lnNumType": {
2718
- const lineNumbering: SectionLineNumbering = {};
2719
- const countBy = safeParseInt(child.attributes["w:countBy"]);
2720
- const start = safeParseInt(child.attributes["w:start"]);
2721
- const distance = safeParseInt(child.attributes["w:distance"]);
2722
- const restart = child.attributes["w:restart"];
2723
- if (countBy !== undefined) lineNumbering.countBy = countBy;
2724
- if (start !== undefined) lineNumbering.start = start;
2725
- if (distance !== undefined) lineNumbering.distance = distance;
2726
- if (
2727
- restart === "newPage" ||
2728
- restart === "newSection" ||
2729
- restart === "continuous"
2730
- ) {
2731
- lineNumbering.restart = restart;
2732
- }
2733
- if (Object.keys(lineNumbering).length > 0) {
2734
- props.lineNumbering = lineNumbering;
2735
- }
2736
- break;
2737
- }
2738
- case "pgBorders": {
2739
- const pageBorders: SectionPageBorders = {};
2740
- const offsetFrom = child.attributes["w:offsetFrom"];
2741
- const display = child.attributes["w:display"];
2742
- const zOrder = child.attributes["w:zOrder"];
2743
- if (offsetFrom === "page" || offsetFrom === "text") {
2744
- pageBorders.offsetFrom = offsetFrom;
2745
- }
2746
- if (
2747
- display === "allPages" ||
2748
- display === "firstPage" ||
2749
- display === "notFirstPage"
2750
- ) {
2751
- pageBorders.display = display;
2752
- }
2753
- if (zOrder === "front" || zOrder === "back") {
2754
- pageBorders.zOrder = zOrder;
2755
- }
2756
- for (const borderChild of child.children) {
2757
- if (borderChild.type !== "element") continue;
2758
- const borderName = localName(borderChild.name);
2759
- const border = parseBorderSpec(borderChild);
2760
- if (!border) continue;
2761
- if (
2762
- borderName === "top" ||
2763
- borderName === "left" ||
2764
- borderName === "bottom" ||
2765
- borderName === "right"
2766
- ) {
2767
- pageBorders[borderName] = border;
2768
- }
2769
- }
2770
- if (Object.keys(pageBorders).length > 0) {
2771
- props.pageBorders = pageBorders;
2772
- }
2773
- break;
2774
- }
2775
- case "docGrid": {
2776
- const documentGrid: SectionDocumentGrid = {};
2777
- const type = child.attributes["w:type"];
2778
- const linePitch = safeParseInt(child.attributes["w:linePitch"]);
2779
- const charSpace = safeParseInt(child.attributes["w:charSpace"]);
2780
- if (
2781
- type === "default" ||
2782
- type === "lines" ||
2783
- type === "linesAndChars" ||
2784
- type === "snapToChars"
2785
- ) {
2786
- documentGrid.type = type;
2787
- }
2788
- if (linePitch !== undefined) documentGrid.linePitch = linePitch;
2789
- if (charSpace !== undefined) documentGrid.charSpace = charSpace;
2790
- if (Object.keys(documentGrid).length > 0) {
2791
- props.documentGrid = documentGrid;
2792
- }
2793
- break;
2794
- }
2795
- case "headerReference": {
2796
- const variant = child.attributes["w:type"] as HeaderFooterVariant | undefined;
2797
- const rId = child.attributes["r:id"];
2798
- if (variant && rId) {
2799
- if (!props.headerReferences) props.headerReferences = [];
2800
- props.headerReferences.push({ variant, relationshipId: rId });
2801
- }
2802
- break;
2803
- }
2804
- case "footerReference": {
2805
- const variant = child.attributes["w:type"] as HeaderFooterVariant | undefined;
2806
- const rId = child.attributes["r:id"];
2807
- if (variant && rId) {
2808
- if (!props.footerReferences) props.footerReferences = [];
2809
- props.footerReferences.push({ variant, relationshipId: rId });
2810
- }
2811
- break;
2812
- }
2813
- case "type": {
2814
- const val = child.attributes["w:val"];
2815
- if (val === "continuous" || val === "nextPage" || val === "evenPage" || val === "oddPage" || val === "nextColumn") {
2816
- props.sectionType = val;
2817
- }
2818
- break;
2819
- }
2820
- case "titlePg": {
2821
- const val = child.attributes["w:val"];
2822
- props.titlePage = val !== "false" && val !== "0";
2823
- break;
2824
- }
2825
- }
2826
- }
2827
-
2828
- return props;
2829
- }
2830
-
2831
- function safeParseInt(value: string | undefined): number | undefined {
2832
- if (value === undefined) return undefined;
2833
- const n = Number.parseInt(value, 10);
2834
- return Number.isFinite(n) ? n : undefined;
2835
- }
2836
-
2837
- function parseBorderSpec(node: XmlElementNode): BorderSpec | undefined {
2838
- const border: BorderSpec = {};
2839
- const value = node.attributes["w:val"];
2840
- const size = safeParseInt(node.attributes["w:sz"]);
2841
- const space = safeParseInt(node.attributes["w:space"]);
2842
- const color = node.attributes["w:color"];
2843
-
2844
- if (value) border.value = value;
2845
- if (size !== undefined) border.size = size;
2846
- if (space !== undefined) border.space = space;
2847
- if (color) border.color = color;
2848
-
2849
- return Object.keys(border).length > 0 ? border : undefined;
2850
- }
2851
-
2852
- function parsePermStartNode(
2853
- node: XmlElementNode,
2854
- sourceXml: string,
2855
- ): ParsedPermStartInlineNode {
2856
- const rangeId = node.attributes["w:id"] ?? node.attributes.id ?? "";
2857
- const edGrp = node.attributes["w:edGrp"] ?? node.attributes.edGrp;
2858
- const ed = node.attributes["w:ed"] ?? node.attributes.ed;
2859
- return {
2860
- type: "perm_start",
2861
- rangeId,
2862
- ...(edGrp ? { editorGroup: edGrp } : {}),
2863
- ...(ed ? { editor: ed } : {}),
2864
- rawXml: sourceXml.slice(node.start, node.end),
2865
- };
2866
- }
2867
-
2868
- function parsePermEndNode(
2869
- node: XmlElementNode,
2870
- sourceXml: string,
2871
- ): ParsedPermEndInlineNode {
2872
- return {
2873
- type: "perm_end",
2874
- rangeId: node.attributes["w:id"] ?? node.attributes.id ?? "",
2875
- rawXml: sourceXml.slice(node.start, node.end),
2876
- };
2877
- }