@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,1169 +0,0 @@
1
- import type {
2
- BlockNode,
3
- FootnoteRefNode,
4
- HeaderFooterVariant,
5
- InlineNode,
6
- ParagraphIndentation,
7
- ParagraphNode,
8
- ParagraphSpacing,
9
- TabStop,
10
- TableCellNode,
11
- TableNode,
12
- TableRowNode,
13
- TextMark,
14
- } from "../../model/canonical-document.ts";
15
- import { resolveHighlightColor } from "./highlight-colors.ts";
16
- import { classifyFieldInstruction } from "./parse-fields.ts";
17
- import { parseShapeXml, parseVmlXml } from "./parse-shapes.ts";
18
-
19
- // ---- Public types ----
20
-
21
- export interface ParsedHeaderFooterReference {
22
- variant: HeaderFooterVariant;
23
- relationshipId: string;
24
- kind: "header" | "footer";
25
- sectionIndex?: number;
26
- }
27
-
28
- export interface ParsedHeaderFooterDocument {
29
- blocks: BlockNode[];
30
- }
31
-
32
- // ---- XML node types (inline, no external dep) ----
33
-
34
- interface XmlElementNode {
35
- type: "element";
36
- name: string;
37
- attributes: Record<string, string>;
38
- children: XmlNode[];
39
- start: number;
40
- end: number;
41
- }
42
-
43
- interface XmlTextNode {
44
- type: "text";
45
- text: string;
46
- start: number;
47
- end: number;
48
- }
49
-
50
- type XmlNode = XmlElementNode | XmlTextNode;
51
-
52
- let currentSourceXml = "";
53
-
54
- // ---- Public API ----
55
-
56
- /**
57
- * Scan a document body XML for w:headerReference / w:footerReference elements
58
- * inside w:sectPr and return the relationship references.
59
- */
60
- export function parseHeaderFooterReferences(
61
- documentXml: string,
62
- ): ParsedHeaderFooterReference[] {
63
- const root = parseXml(documentXml);
64
- const documentElement = findChildElementOptional(root, "document");
65
- if (!documentElement) {
66
- return [];
67
- }
68
-
69
- const bodyElement = findChildElementOptional(documentElement, "body");
70
- if (!bodyElement) {
71
- return [];
72
- }
73
-
74
- const refs: ParsedHeaderFooterReference[] = [];
75
-
76
- // Collect all sectPr elements (can appear in paragraph pPr and at body level)
77
- collectSectPrReferences(bodyElement, refs);
78
-
79
- return refs;
80
- }
81
-
82
- /**
83
- * Parse a headerN.xml part (<w:hdr> root) into block nodes.
84
- */
85
- export function parseHeaderXml(xml: string): ParsedHeaderFooterDocument {
86
- return parseHdrFtrXml(xml, "hdr");
87
- }
88
-
89
- /**
90
- * Parse a footerN.xml part (<w:ftr> root) into block nodes.
91
- */
92
- export function parseFooterXml(xml: string): ParsedHeaderFooterDocument {
93
- return parseHdrFtrXml(xml, "ftr");
94
- }
95
-
96
- // ---- Internal helpers ----
97
-
98
- function collectSectPrReferences(
99
- element: XmlElementNode,
100
- refs: ParsedHeaderFooterReference[],
101
- ): void {
102
- let sectionIndex = 0;
103
- for (const child of element.children) {
104
- if (child.type !== "element") {
105
- continue;
106
- }
107
-
108
- const name = localName(child.name);
109
-
110
- if (name === "sectPr") {
111
- // Body-level sectPr is the final section
112
- extractSectPrRefs(child, refs, sectionIndex);
113
- } else if (name === "p") {
114
- // Check paragraph properties for sectPr (non-final section break)
115
- const pPr = findChildElementOptional(child, "pPr");
116
- if (pPr) {
117
- const sectPr = findChildElementOptional(pPr, "sectPr");
118
- if (sectPr) {
119
- extractSectPrRefs(sectPr, refs, sectionIndex);
120
- sectionIndex++;
121
- }
122
- }
123
- }
124
- }
125
- }
126
-
127
- function extractSectPrRefs(
128
- sectPr: XmlElementNode,
129
- refs: ParsedHeaderFooterReference[],
130
- sectionIndex: number,
131
- ): void {
132
- for (const child of sectPr.children) {
133
- if (child.type !== "element") {
134
- continue;
135
- }
136
-
137
- const name = localName(child.name);
138
- if (name === "headerReference" || name === "footerReference") {
139
- const kind: "header" | "footer" = name === "headerReference" ? "header" : "footer";
140
- const rawType =
141
- child.attributes["w:type"] ?? child.attributes.type ?? "default";
142
- const variant = toHeaderFooterVariant(rawType);
143
- const relationshipId =
144
- child.attributes["r:id"] ??
145
- child.attributes["r:Id"] ??
146
- child.attributes.id ??
147
- child.attributes.Id ??
148
- "";
149
-
150
- if (relationshipId) {
151
- // Avoid duplicates (multiple sectPr may reference same header)
152
- const dedupeKey = `${kind}:${variant}:${relationshipId}`;
153
- const alreadyAdded = refs.some(
154
- (ref) => `${ref.kind}:${ref.variant}:${ref.relationshipId}` === dedupeKey,
155
- );
156
- if (!alreadyAdded) {
157
- refs.push({ variant, relationshipId, kind, sectionIndex });
158
- }
159
- }
160
- }
161
- }
162
- }
163
-
164
- function toHeaderFooterVariant(raw: string): HeaderFooterVariant {
165
- if (raw === "first") {
166
- return "first";
167
- }
168
- if (raw === "even") {
169
- return "even";
170
- }
171
- return "default";
172
- }
173
-
174
- function parseHdrFtrXml(
175
- xml: string,
176
- rootLocalName: "hdr" | "ftr",
177
- ): ParsedHeaderFooterDocument {
178
- const root = parseXml(xml);
179
- const hdrFtrElement = findChildElementOptional(root, rootLocalName);
180
- if (!hdrFtrElement) {
181
- return { blocks: [] };
182
- }
183
-
184
- const blocks: BlockNode[] = [];
185
-
186
- for (const child of hdrFtrElement.children) {
187
- if (child.type !== "element") {
188
- continue;
189
- }
190
-
191
- const name = localName(child.name);
192
-
193
- if (name === "p") {
194
- blocks.push(parseParagraphElement(child));
195
- } else if (name === "tbl") {
196
- // Simple tables (no revisions, fields, or nested tables) are promoted
197
- // to supported-roundtrip; structurally risky tables stay opaque.
198
- if (isSimpleSecondaryStoryTable(child)) {
199
- blocks.push(parseSimpleTableElement(child));
200
- } else {
201
- blocks.push({
202
- type: "opaque_block",
203
- fragmentId: "fragment:hdrftr-tbl",
204
- warningId: "warning:hdrftr-opaque-table",
205
- rawXml: serializeElementToXml(child),
206
- });
207
- }
208
- } else {
209
- // Other block-level elements: treat as opaque
210
- blocks.push({
211
- type: "opaque_block",
212
- fragmentId: "fragment:hdrftr-opaque",
213
- warningId: "warning:hdrftr-opaque-block",
214
- rawXml: serializeElementToXml(child),
215
- });
216
- }
217
- }
218
-
219
- return { blocks };
220
- }
221
-
222
- function parseParagraphElement(pElement: XmlElementNode): ParagraphNode {
223
- let styleId: string | undefined;
224
- let alignment: ParagraphNode["alignment"];
225
- let spacing: ParagraphNode["spacing"];
226
- let indentation: ParagraphNode["indentation"];
227
- let tabStops: ParagraphNode["tabStops"];
228
- const children: InlineNode[] = [];
229
- let activeComplexField: {
230
- instruction: string;
231
- children: Array<Extract<InlineNode, { type: "text" | "hard_break" | "tab" }>>;
232
- mode: "instruction" | "result";
233
- } | null = null;
234
-
235
- for (const child of pElement.children) {
236
- if (child.type !== "element") {
237
- continue;
238
- }
239
-
240
- const name = localName(child.name);
241
-
242
- if (name === "pPr") {
243
- const pStyle = findChildElementOptional(child, "pStyle");
244
- styleId = pStyle?.attributes["w:val"] ?? pStyle?.attributes.val;
245
- const jc = findChildElementOptional(child, "jc");
246
- const jcVal = jc?.attributes["w:val"] ?? jc?.attributes.val;
247
- if (jcVal === "left" || jcVal === "center" || jcVal === "right" || jcVal === "both" || jcVal === "distribute") {
248
- alignment = jcVal;
249
- }
250
- spacing = readParagraphSpacing(child);
251
- indentation = readParagraphIndentation(child);
252
- tabStops = readParagraphTabStops(child);
253
- } else if (name === "r") {
254
- activeComplexField = appendRunNodes(child, children, activeComplexField);
255
- } else if (name === "hyperlink") {
256
- children.push(parseHyperlinkElement(child));
257
- } else if (name === "bookmarkStart" || name === "bookmarkEnd") {
258
- children.push(parseBookmarkElement(child));
259
- } else if (name === "fldSimple") {
260
- if (activeComplexField && activeComplexField.instruction.trim().length > 0) {
261
- children.push({
262
- type: "field",
263
- fieldType: "complex",
264
- instruction: activeComplexField.instruction,
265
- children: activeComplexField.children,
266
- });
267
- activeComplexField = null;
268
- }
269
- pushFieldNode(children, child, "simple");
270
- }
271
- }
272
-
273
- if (activeComplexField && activeComplexField.instruction.trim().length > 0) {
274
- children.push({
275
- type: "field",
276
- fieldType: "complex",
277
- instruction: activeComplexField.instruction,
278
- children: activeComplexField.children,
279
- });
280
- }
281
-
282
- return {
283
- type: "paragraph",
284
- ...(styleId ? { styleId } : {}),
285
- ...(alignment ? { alignment } : {}),
286
- ...(spacing ? { spacing } : {}),
287
- ...(indentation ? { indentation } : {}),
288
- ...(tabStops && tabStops.length > 0 ? { tabStops } : {}),
289
- children,
290
- };
291
- }
292
-
293
- function appendRunNodes(
294
- rElement: XmlElementNode,
295
- nodes: InlineNode[],
296
- activeComplexField: {
297
- instruction: string;
298
- children: Array<Extract<InlineNode, { type: "text" | "hard_break" | "tab" }>>;
299
- mode: "instruction" | "result";
300
- } | null,
301
- ): {
302
- instruction: string;
303
- children: Array<Extract<InlineNode, { type: "text" | "hard_break" | "tab" }>>;
304
- mode: "instruction" | "result";
305
- } | null {
306
- const marks: TextMark[] = parseRunProperties(rElement);
307
-
308
- for (const child of rElement.children) {
309
- if (child.type !== "element") {
310
- continue;
311
- }
312
-
313
- const name = localName(child.name);
314
- if (name === "fldChar") {
315
- const fldType = child.attributes["w:fldCharType"] ?? child.attributes.fldCharType;
316
- if (fldType === "begin") {
317
- activeComplexField = { instruction: "", children: [], mode: "instruction" };
318
- } else if (fldType === "separate" && activeComplexField) {
319
- activeComplexField.mode = "result";
320
- } else if (fldType === "end" && activeComplexField) {
321
- if (activeComplexField.instruction.trim().length > 0) {
322
- nodes.push({
323
- type: "field",
324
- fieldType: "complex",
325
- instruction: activeComplexField.instruction,
326
- children: activeComplexField.children,
327
- });
328
- }
329
- activeComplexField = null;
330
- }
331
- continue;
332
- }
333
-
334
- if (name === "instrText") {
335
- if (activeComplexField) {
336
- activeComplexField.instruction += extractTextContent(child);
337
- } else {
338
- pushFieldNode(nodes, child, "complex");
339
- }
340
- continue;
341
- }
342
-
343
- const inlineNode = parseRunChildNode(child, marks);
344
- if (!inlineNode) {
345
- continue;
346
- }
347
-
348
- if (activeComplexField?.mode === "result") {
349
- if (
350
- inlineNode.type === "text" ||
351
- inlineNode.type === "hard_break" ||
352
- inlineNode.type === "tab"
353
- ) {
354
- activeComplexField.children.push(inlineNode);
355
- }
356
- continue;
357
- }
358
-
359
- nodes.push(inlineNode);
360
- }
361
-
362
- return activeComplexField;
363
- }
364
-
365
- function parseRunElement(rElement: XmlElementNode): InlineNode[] {
366
- const nodes: InlineNode[] = [];
367
- const marks: TextMark[] = parseRunProperties(rElement);
368
-
369
- for (const child of rElement.children) {
370
- if (child.type !== "element") {
371
- continue;
372
- }
373
-
374
- const name = localName(child.name);
375
-
376
- if (name === "t") {
377
- const text = extractTextContent(child);
378
- if (text.length > 0) {
379
- nodes.push({
380
- type: "text",
381
- text,
382
- ...(marks.length > 0 ? { marks } : {}),
383
- });
384
- }
385
- } else if (name === "br") {
386
- nodes.push({ type: "hard_break" });
387
- } else if (name === "tab") {
388
- nodes.push({ type: "tab" });
389
- } else if (name === "footnoteReference") {
390
- const noteId =
391
- child.attributes["w:id"] ?? child.attributes.id ?? "";
392
- if (noteId) {
393
- const ref: FootnoteRefNode = {
394
- type: "footnote_ref",
395
- noteId,
396
- noteKind: "footnote",
397
- };
398
- nodes.push(ref);
399
- }
400
- } else if (name === "endnoteReference") {
401
- const noteId =
402
- child.attributes["w:id"] ?? child.attributes.id ?? "";
403
- if (noteId) {
404
- const ref: FootnoteRefNode = {
405
- type: "footnote_ref",
406
- noteId,
407
- noteKind: "endnote",
408
- };
409
- nodes.push(ref);
410
- }
411
- } else if (name === "bookmarkStart" || name === "bookmarkEnd") {
412
- nodes.push(parseBookmarkElement(child));
413
- } else if (name === "instrText") {
414
- pushFieldNode(nodes, child, "complex");
415
- } else if (name === "drawing") {
416
- const drawingXml = currentSourceXml.slice(child.start, child.end);
417
- const shapeResult = parseShapeXml(drawingXml);
418
- if (shapeResult) {
419
- nodes.push(shapeResult);
420
- }
421
- } else if (name === "pict") {
422
- const pictXml = currentSourceXml.slice(child.start, child.end);
423
- const vmlResult = parseVmlXml(pictXml);
424
- if (vmlResult) {
425
- nodes.push(vmlResult);
426
- }
427
- } else if (name === "AlternateContent") {
428
- const drawingNode = findFirstDescendant(child, "drawing");
429
- if (drawingNode) {
430
- const drawingXml = currentSourceXml.slice(drawingNode.start, drawingNode.end);
431
- const shapeResult = parseShapeXml(drawingXml);
432
- if (shapeResult) {
433
- nodes.push({
434
- ...shapeResult,
435
- rawXml: currentSourceXml.slice(child.start, child.end),
436
- });
437
- continue;
438
- }
439
- }
440
- const pictNode = findFirstDescendant(child, "pict");
441
- if (pictNode) {
442
- const pictXml = currentSourceXml.slice(pictNode.start, pictNode.end);
443
- const vmlResult = parseVmlXml(pictXml);
444
- if (vmlResult) {
445
- nodes.push({
446
- ...vmlResult,
447
- rawXml: currentSourceXml.slice(child.start, child.end),
448
- });
449
- }
450
- }
451
- }
452
- }
453
-
454
- return nodes;
455
- }
456
-
457
- function parseRunChildNode(
458
- child: XmlElementNode,
459
- marks: TextMark[],
460
- ): InlineNode | null {
461
- const name = localName(child.name);
462
-
463
- if (name === "t") {
464
- const text = extractTextContent(child);
465
- if (text.length > 0) {
466
- return {
467
- type: "text",
468
- text,
469
- ...(marks.length > 0 ? { marks } : {}),
470
- };
471
- }
472
- return null;
473
- }
474
- if (name === "br") {
475
- return { type: "hard_break" };
476
- }
477
- if (name === "tab") {
478
- return { type: "tab" };
479
- }
480
- if (name === "footnoteReference") {
481
- const noteId =
482
- child.attributes["w:id"] ?? child.attributes.id ?? "";
483
- if (noteId) {
484
- const ref: FootnoteRefNode = {
485
- type: "footnote_ref",
486
- noteId,
487
- noteKind: "footnote",
488
- };
489
- return ref;
490
- }
491
- return null;
492
- }
493
- if (name === "endnoteReference") {
494
- const noteId =
495
- child.attributes["w:id"] ?? child.attributes.id ?? "";
496
- if (noteId) {
497
- const ref: FootnoteRefNode = {
498
- type: "footnote_ref",
499
- noteId,
500
- noteKind: "endnote",
501
- };
502
- return ref;
503
- }
504
- return null;
505
- }
506
- if (name === "bookmarkStart" || name === "bookmarkEnd") {
507
- return parseBookmarkElement(child);
508
- }
509
- if (name === "drawing") {
510
- const drawingXml = currentSourceXml.slice(child.start, child.end);
511
- return parseShapeXml(drawingXml);
512
- }
513
- if (name === "pict") {
514
- const pictXml = currentSourceXml.slice(child.start, child.end);
515
- return parseVmlXml(pictXml);
516
- }
517
- if (name === "AlternateContent") {
518
- const drawingNode = findFirstDescendant(child, "drawing");
519
- if (drawingNode) {
520
- const drawingXml = currentSourceXml.slice(drawingNode.start, drawingNode.end);
521
- const shapeResult = parseShapeXml(drawingXml);
522
- if (shapeResult) {
523
- return {
524
- ...shapeResult,
525
- rawXml: currentSourceXml.slice(child.start, child.end),
526
- };
527
- }
528
- }
529
- const pictNode = findFirstDescendant(child, "pict");
530
- if (pictNode) {
531
- const pictXml = currentSourceXml.slice(pictNode.start, pictNode.end);
532
- const vmlResult = parseVmlXml(pictXml);
533
- if (vmlResult) {
534
- return {
535
- ...vmlResult,
536
- rawXml: currentSourceXml.slice(child.start, child.end),
537
- };
538
- }
539
- }
540
- }
541
-
542
- return null;
543
- }
544
-
545
- function parseHyperlinkElement(element: XmlElementNode): Extract<InlineNode, { type: "hyperlink" }> {
546
- const href = element.attributes["w:anchor"]
547
- ? `#${element.attributes["w:anchor"]}`
548
- : element.attributes["r:id"] ?? "relationship:unknown";
549
- const children: Array<Extract<InlineNode, { type: "text" | "hard_break" | "tab" }>> = [];
550
-
551
- for (const child of element.children) {
552
- if (child.type === "element" && localName(child.name) === "r") {
553
- for (const runChild of parseRunElement(child)) {
554
- if (runChild.type === "text" || runChild.type === "hard_break" || runChild.type === "tab") {
555
- children.push(runChild);
556
- }
557
- }
558
- }
559
- }
560
-
561
- return {
562
- type: "hyperlink",
563
- href,
564
- children,
565
- };
566
- }
567
-
568
- function parseBookmarkElement(
569
- element: XmlElementNode,
570
- ): Extract<InlineNode, { type: "bookmark_start" | "bookmark_end" }> {
571
- const bookmarkId = element.attributes["w:id"] ?? element.attributes.id ?? "0";
572
- if (localName(element.name) === "bookmarkStart") {
573
- return {
574
- type: "bookmark_start",
575
- bookmarkId,
576
- name: element.attributes["w:name"] ?? element.attributes.name ?? "",
577
- };
578
- }
579
-
580
- return {
581
- type: "bookmark_end",
582
- bookmarkId,
583
- };
584
- }
585
-
586
- function pushFieldNode(
587
- nodes: InlineNode[],
588
- element: XmlElementNode,
589
- fieldType: "simple" | "complex",
590
- ): void {
591
- const instruction = readFieldInstruction(element);
592
- if (!instruction) {
593
- return;
594
- }
595
-
596
- nodes.push({
597
- type: "field",
598
- fieldType,
599
- instruction,
600
- children: [],
601
- });
602
- }
603
-
604
- function readFieldInstruction(element: XmlElementNode): string | undefined {
605
- const instruction =
606
- element.attributes["w:instr"] ??
607
- element.attributes.instr ??
608
- extractTextContent(element);
609
- return instruction.trim().length > 0 ? instruction : undefined;
610
- }
611
-
612
- function parseRunProperties(rElement: XmlElementNode): TextMark[] {
613
- const rPr = findChildElementOptional(rElement, "rPr");
614
- if (!rPr) {
615
- return [];
616
- }
617
-
618
- const marks: TextMark[] = [];
619
-
620
- for (const child of rPr.children) {
621
- if (child.type !== "element") {
622
- continue;
623
- }
624
-
625
- const name = localName(child.name);
626
- const val = child.attributes["w:val"] ?? child.attributes.val ?? "true";
627
-
628
- switch (name) {
629
- case "b":
630
- if (val !== "0" && val !== "false") {
631
- marks.push({ type: "bold" });
632
- }
633
- break;
634
- case "i":
635
- if (val !== "0" && val !== "false") {
636
- marks.push({ type: "italic" });
637
- }
638
- break;
639
- case "u":
640
- if (val !== "none" && val !== "0") {
641
- marks.push({ type: "underline" });
642
- }
643
- break;
644
- case "strike":
645
- if (val !== "0" && val !== "false") {
646
- marks.push({ type: "strikethrough" });
647
- }
648
- break;
649
- case "dstrike":
650
- if (val !== "0" && val !== "false") {
651
- marks.push({ type: "doubleStrikethrough" });
652
- }
653
- break;
654
- case "rFonts": {
655
- const family =
656
- child.attributes["w:ascii"] ??
657
- child.attributes["w:hAnsi"] ??
658
- child.attributes.ascii ??
659
- child.attributes.hAnsi;
660
- if (family) {
661
- marks.push({ type: "fontFamily", val: family });
662
- }
663
- break;
664
- }
665
- case "sz": {
666
- const szVal = child.attributes["w:val"] ?? child.attributes.val;
667
- if (szVal) {
668
- const size = Number.parseInt(szVal, 10);
669
- if (Number.isFinite(size) && size > 0) {
670
- marks.push({ type: "fontSize", val: size });
671
- }
672
- }
673
- break;
674
- }
675
- case "color": {
676
- const colorVal = child.attributes["w:val"] ?? child.attributes.val;
677
- if (colorVal && colorVal !== "auto") {
678
- marks.push({ type: "textColor", color: colorVal });
679
- }
680
- break;
681
- }
682
- case "shd": {
683
- const fill = child.attributes["w:fill"] ?? child.attributes.fill;
684
- if (fill && fill !== "auto") {
685
- marks.push({ type: "backgroundColor", color: fill });
686
- }
687
- break;
688
- }
689
- case "highlight": {
690
- const resolvedHighlight = resolveHighlightColor(
691
- child.attributes["w:val"] ?? child.attributes.val,
692
- );
693
- if (resolvedHighlight) {
694
- marks.push({
695
- type: "highlight",
696
- color: resolvedHighlight.color,
697
- val: resolvedHighlight.val,
698
- });
699
- }
700
- break;
701
- }
702
- case "smallCaps":
703
- if (val !== "0" && val !== "false") {
704
- marks.push({ type: "smallCaps" });
705
- }
706
- break;
707
- case "caps":
708
- if (val !== "0" && val !== "false") {
709
- marks.push({ type: "allCaps" });
710
- }
711
- break;
712
- }
713
- }
714
-
715
- return marks;
716
- }
717
-
718
- function readParagraphSpacing(pPr: XmlElementNode): ParagraphSpacing | undefined {
719
- const spacingNode = findChildElementOptional(pPr, "spacing");
720
- if (!spacingNode) return undefined;
721
- const result: ParagraphSpacing = {};
722
- const before = spacingNode.attributes["w:before"] ?? spacingNode.attributes.before;
723
- if (before) result.before = Number.parseInt(before, 10);
724
- const after = spacingNode.attributes["w:after"] ?? spacingNode.attributes.after;
725
- if (after) result.after = Number.parseInt(after, 10);
726
- const line = spacingNode.attributes["w:line"] ?? spacingNode.attributes.line;
727
- if (line) result.line = Number.parseInt(line, 10);
728
- const lineRule = spacingNode.attributes["w:lineRule"] ?? spacingNode.attributes.lineRule;
729
- if (lineRule === "auto" || lineRule === "exact" || lineRule === "atLeast") {
730
- result.lineRule = lineRule;
731
- }
732
- return Object.keys(result).length > 0 ? result : undefined;
733
- }
734
-
735
- function readParagraphIndentation(pPr: XmlElementNode): ParagraphIndentation | undefined {
736
- const indNode = findChildElementOptional(pPr, "ind");
737
- if (!indNode) return undefined;
738
- const result: ParagraphIndentation = {};
739
- const left = indNode.attributes["w:left"] ?? indNode.attributes.left;
740
- if (left) result.left = Number.parseInt(left, 10);
741
- const right = indNode.attributes["w:right"] ?? indNode.attributes.right;
742
- if (right) result.right = Number.parseInt(right, 10);
743
- const firstLine = indNode.attributes["w:firstLine"] ?? indNode.attributes.firstLine;
744
- if (firstLine) result.firstLine = Number.parseInt(firstLine, 10);
745
- const hanging = indNode.attributes["w:hanging"] ?? indNode.attributes.hanging;
746
- if (hanging) result.hanging = Number.parseInt(hanging, 10);
747
- return Object.keys(result).length > 0 ? result : undefined;
748
- }
749
-
750
- function readParagraphTabStops(pPr: XmlElementNode): TabStop[] | undefined {
751
- const tabsNode = findChildElementOptional(pPr, "tabs");
752
- if (!tabsNode) return undefined;
753
-
754
- const tabStops: TabStop[] = [];
755
- for (const child of tabsNode.children) {
756
- if (child.type !== "element" || localName(child.name) !== "tab") continue;
757
- const pos = child.attributes["w:pos"] ?? child.attributes.pos;
758
- const val = (child.attributes["w:val"] ?? child.attributes.val ?? "left").toLowerCase();
759
- const leader = (child.attributes["w:leader"] ?? child.attributes.leader ?? "none").toLowerCase();
760
-
761
- if (pos === undefined) continue;
762
- const position = Number.parseInt(pos, 10);
763
- if (!Number.isFinite(position)) continue;
764
-
765
- const align = (["left", "center", "right", "decimal", "bar", "clear"] as const).includes(
766
- val as "left" | "center" | "right" | "decimal" | "bar" | "clear",
767
- )
768
- ? (val as TabStop["align"])
769
- : "left";
770
-
771
- const leaderValue =
772
- leader === "none" ||
773
- leader === "dot" ||
774
- leader === "hyphen" ||
775
- leader === "underscore" ||
776
- leader === "heavy"
777
- ? (leader as Exclude<TabStop["leader"], "middleDot">)
778
- : leader === "middledot"
779
- ? "middleDot"
780
- : undefined;
781
-
782
- tabStops.push({
783
- position,
784
- align,
785
- ...(leaderValue && leaderValue !== "none" ? { leader: leaderValue } : {}),
786
- });
787
- }
788
-
789
- return tabStops.length > 0 ? tabStops : undefined;
790
- }
791
-
792
- function extractTextContent(tElement: XmlElementNode): string {
793
- let text = "";
794
- for (const child of tElement.children) {
795
- if (child.type === "text") {
796
- text += child.text;
797
- }
798
- }
799
- return text;
800
- }
801
-
802
- function findChildElementOptional(
803
- node: XmlElementNode,
804
- childLocalName: string,
805
- ): XmlElementNode | undefined {
806
- return node.children.find(
807
- (entry): entry is XmlElementNode =>
808
- entry.type === "element" && localName(entry.name) === childLocalName,
809
- );
810
- }
811
-
812
- function findFirstDescendant(
813
- node: XmlElementNode,
814
- childLocalName: string,
815
- ): XmlElementNode | undefined {
816
- for (const child of node.children) {
817
- if (child.type !== "element") continue;
818
- if (localName(child.name) === childLocalName) {
819
- return child;
820
- }
821
- const nested = findFirstDescendant(child, childLocalName);
822
- if (nested) {
823
- return nested;
824
- }
825
- }
826
- return undefined;
827
- }
828
-
829
- function localName(name: string): string {
830
- const separatorIndex = name.indexOf(":");
831
- return separatorIndex >= 0 ? name.slice(separatorIndex + 1) : name;
832
- }
833
-
834
- // ---- Simple secondary-story table support ----
835
-
836
- /**
837
- * Revision-bearing, field-bearing, or structurally risky elements that
838
- * disqualify a secondary-story table from supported-roundtrip.
839
- */
840
- const RISKY_TABLE_ELEMENT_NAMES = new Set([
841
- "ins",
842
- "del",
843
- "moveFrom",
844
- "moveTo",
845
- "tblPrChange",
846
- "trPrChange",
847
- "tcPrChange",
848
- "rPrChange",
849
- "pPrChange",
850
- "sectPrChange",
851
- "sdt",
852
- "customXml",
853
- ]);
854
-
855
- function isSimpleSecondaryStoryTable(tblElement: XmlElementNode): boolean {
856
- return !containsRiskyElement(tblElement);
857
- }
858
-
859
- function containsRiskyElement(element: XmlElementNode): boolean {
860
- for (const child of element.children) {
861
- if (child.type !== "element") {
862
- continue;
863
- }
864
- const name = localName(child.name);
865
- if (name === "fldSimple" || name === "instrText") {
866
- const instruction =
867
- child.attributes["w:instr"] ??
868
- child.attributes.instr ??
869
- extractTextContent(child);
870
- const classification = classifyFieldInstruction(instruction);
871
- if (!isSafeSecondaryStoryFieldFamily(classification.family)) {
872
- return true;
873
- }
874
- continue;
875
- }
876
- if (RISKY_TABLE_ELEMENT_NAMES.has(name)) {
877
- return true;
878
- }
879
- if (name === "fldChar") {
880
- continue;
881
- }
882
- // Nested tables remain risky
883
- if (name === "tbl") {
884
- return true;
885
- }
886
- if (containsRiskyElement(child)) {
887
- return true;
888
- }
889
- }
890
- return false;
891
- }
892
-
893
- function isSafeSecondaryStoryFieldFamily(family: string): boolean {
894
- return (
895
- family === "REF" ||
896
- family === "PAGEREF" ||
897
- family === "NOTEREF" ||
898
- family === "TOC" ||
899
- family === "PAGE" ||
900
- family === "NUMPAGES"
901
- );
902
- }
903
-
904
- function parseSimpleTableElement(tblElement: XmlElementNode): TableNode {
905
- let gridColumns: number[] = [];
906
- const rows: TableRowNode[] = [];
907
- let propertiesXml: string | undefined;
908
- let styleId: string | undefined;
909
-
910
- for (const child of tblElement.children) {
911
- if (child.type !== "element") continue;
912
- const name = localName(child.name);
913
-
914
- if (name === "tblPr") {
915
- propertiesXml = serializeElementToXml(child);
916
- const pStyle = findChildElementOptional(child, "tblStyle");
917
- styleId = pStyle?.attributes["w:val"] ?? pStyle?.attributes.val;
918
- } else if (name === "tblGrid") {
919
- gridColumns = readGridColumns(child);
920
- } else if (name === "tr") {
921
- rows.push(parseSimpleTableRow(child));
922
- }
923
- }
924
-
925
- return {
926
- type: "table",
927
- ...(styleId ? { styleId } : {}),
928
- ...(propertiesXml ? { propertiesXml } : {}),
929
- gridColumns,
930
- rows,
931
- };
932
- }
933
-
934
- function readGridColumns(tblGrid: XmlElementNode): number[] {
935
- const columns: number[] = [];
936
- for (const child of tblGrid.children) {
937
- if (child.type !== "element") continue;
938
- if (localName(child.name) === "gridCol") {
939
- const w = child.attributes["w:w"] ?? child.attributes.w ?? "0";
940
- columns.push(Number.parseInt(w, 10) || 0);
941
- }
942
- }
943
- return columns;
944
- }
945
-
946
- function parseSimpleTableRow(trElement: XmlElementNode): TableRowNode {
947
- const cells: TableCellNode[] = [];
948
- let propertiesXml: string | undefined;
949
-
950
- for (const child of trElement.children) {
951
- if (child.type !== "element") continue;
952
- const name = localName(child.name);
953
-
954
- if (name === "trPr") {
955
- propertiesXml = serializeElementToXml(child);
956
- } else if (name === "tc") {
957
- cells.push(parseSimpleTableCell(child));
958
- }
959
- }
960
-
961
- return {
962
- type: "table_row",
963
- ...(propertiesXml ? { propertiesXml } : {}),
964
- cells,
965
- };
966
- }
967
-
968
- function parseSimpleTableCell(tcElement: XmlElementNode): TableCellNode {
969
- const children: BlockNode[] = [];
970
- let propertiesXml: string | undefined;
971
- let gridSpan: number | undefined;
972
- let verticalMerge: "restart" | "continue" | undefined;
973
-
974
- for (const child of tcElement.children) {
975
- if (child.type !== "element") continue;
976
- const name = localName(child.name);
977
-
978
- if (name === "tcPr") {
979
- propertiesXml = serializeElementToXml(child);
980
- const gsEl = findChildElementOptional(child, "gridSpan");
981
- const gsVal = gsEl?.attributes["w:val"] ?? gsEl?.attributes.val;
982
- if (gsVal) gridSpan = Number.parseInt(gsVal, 10) || undefined;
983
-
984
- const vmEl = findChildElementOptional(child, "vMerge");
985
- if (vmEl) {
986
- const vmVal = vmEl.attributes["w:val"] ?? vmEl.attributes.val ?? "continue";
987
- verticalMerge = vmVal === "restart" ? "restart" : "continue";
988
- }
989
- } else if (name === "p") {
990
- children.push(parseParagraphElement(child));
991
- }
992
- }
993
-
994
- return {
995
- type: "table_cell",
996
- ...(propertiesXml ? { propertiesXml } : {}),
997
- ...(gridSpan ? { gridSpan } : {}),
998
- ...(verticalMerge ? { verticalMerge } : {}),
999
- children: children.length > 0 ? children : [{ type: "paragraph", children: [] }],
1000
- };
1001
- }
1002
-
1003
- /**
1004
- * Serialize an XmlElementNode back to XML string for propertiesXml preservation.
1005
- */
1006
- function serializeElementToXml(element: XmlElementNode): string {
1007
- const attrs = Object.entries(element.attributes)
1008
- .map(([key, value]) => ` ${key}="${escapeXmlAttribute(value)}"`)
1009
- .join("");
1010
- const children = element.children
1011
- .map((child) => {
1012
- if (child.type === "text") {
1013
- return escapeXmlText(child.text);
1014
- }
1015
- return serializeElementToXml(child);
1016
- })
1017
- .join("");
1018
- if (children.length === 0) {
1019
- return `<${element.name}${attrs}/>`;
1020
- }
1021
- return `<${element.name}${attrs}>${children}</${element.name}>`;
1022
- }
1023
-
1024
- function escapeXmlAttribute(text: string): string {
1025
- return text
1026
- .replace(/&/g, "&amp;")
1027
- .replace(/"/g, "&quot;")
1028
- .replace(/</g, "&lt;")
1029
- .replace(/>/g, "&gt;");
1030
- }
1031
-
1032
- function escapeXmlText(text: string): string {
1033
- return text
1034
- .replace(/&/g, "&amp;")
1035
- .replace(/</g, "&lt;")
1036
- .replace(/>/g, "&gt;");
1037
- }
1038
-
1039
- // ---- Minimal XML parser (same pattern as parse-numbering.ts) ----
1040
-
1041
- function parseXml(xml: string): XmlElementNode {
1042
- currentSourceXml = xml;
1043
- const root: XmlElementNode = {
1044
- type: "element",
1045
- name: "__root__",
1046
- attributes: {},
1047
- children: [],
1048
- start: 0,
1049
- end: xml.length,
1050
- };
1051
- const stack: XmlElementNode[] = [root];
1052
- let cursor = 0;
1053
-
1054
- while (cursor < xml.length) {
1055
- if (xml.startsWith("<!--", cursor)) {
1056
- const end = xml.indexOf("-->", cursor);
1057
- cursor = end >= 0 ? end + 3 : xml.length;
1058
- continue;
1059
- }
1060
-
1061
- if (xml.startsWith("<?", cursor)) {
1062
- const end = xml.indexOf("?>", cursor);
1063
- cursor = end >= 0 ? end + 2 : xml.length;
1064
- continue;
1065
- }
1066
-
1067
- if (xml.startsWith("<![CDATA[", cursor)) {
1068
- const end = xml.indexOf("]]>", cursor);
1069
- const textEnd = end >= 0 ? end : xml.length;
1070
- stack[stack.length - 1]?.children.push({
1071
- type: "text",
1072
- text: xml.slice(cursor + 9, textEnd),
1073
- start: cursor,
1074
- end: end >= 0 ? end + 3 : xml.length,
1075
- });
1076
- cursor = end >= 0 ? end + 3 : xml.length;
1077
- continue;
1078
- }
1079
-
1080
- if (xml[cursor] !== "<") {
1081
- const nextTag = xml.indexOf("<", cursor);
1082
- const end = nextTag >= 0 ? nextTag : xml.length;
1083
- const text = decodeXmlEntities(xml.slice(cursor, end));
1084
- if (text.trim().length > 0 || (text.length > 0 && stack.length > 1)) {
1085
- stack[stack.length - 1]?.children.push({ type: "text", text, start: cursor, end });
1086
- }
1087
- cursor = end;
1088
- continue;
1089
- }
1090
-
1091
- // Closing tag
1092
- if (xml[cursor + 1] === "/") {
1093
- const end = xml.indexOf(">", cursor);
1094
- if (end < 0) {
1095
- break;
1096
- }
1097
- const current = stack.pop();
1098
- if (current) {
1099
- current.end = end + 1;
1100
- }
1101
- cursor = end + 1;
1102
- continue;
1103
- }
1104
-
1105
- // Open or self-closing tag
1106
- const tagEnd = xml.indexOf(">", cursor);
1107
- if (tagEnd < 0) {
1108
- break;
1109
- }
1110
-
1111
- const tagContent = xml.slice(cursor + 1, tagEnd);
1112
- const selfClosing = tagContent.endsWith("/");
1113
- const normalized = selfClosing ? tagContent.slice(0, -1).trimEnd() : tagContent;
1114
-
1115
- const spaceIndex = normalized.search(/\s/);
1116
- const tagName =
1117
- spaceIndex >= 0 ? normalized.slice(0, spaceIndex) : normalized;
1118
- const attrString =
1119
- spaceIndex >= 0 ? normalized.slice(spaceIndex + 1) : "";
1120
- const attributes = parseAttributes(attrString);
1121
-
1122
- const element: XmlElementNode = {
1123
- type: "element",
1124
- name: tagName,
1125
- attributes,
1126
- children: [],
1127
- start: cursor,
1128
- end: tagEnd + 1,
1129
- };
1130
-
1131
- stack[stack.length - 1]?.children.push(element);
1132
-
1133
- if (!selfClosing) {
1134
- stack.push(element);
1135
- }
1136
-
1137
- cursor = tagEnd + 1;
1138
- }
1139
-
1140
- return root;
1141
- }
1142
-
1143
- function parseAttributes(attrString: string): Record<string, string> {
1144
- const attrs: Record<string, string> = {};
1145
- const pattern = /([A-Za-z_:][A-Za-z0-9:._-]*)\s*=\s*("([^"]*)"|'([^']*)')/gu;
1146
-
1147
- for (const match of attrString.matchAll(pattern)) {
1148
- const name = match[1];
1149
- const value = match[3] ?? match[4] ?? "";
1150
- if (name) {
1151
- attrs[name] = decodeXmlEntities(value);
1152
- }
1153
- }
1154
-
1155
- return attrs;
1156
- }
1157
-
1158
- function decodeXmlEntities(text: string): string {
1159
- return text
1160
- .replace(/&amp;/g, "&")
1161
- .replace(/&lt;/g, "<")
1162
- .replace(/&gt;/g, ">")
1163
- .replace(/&quot;/g, '"')
1164
- .replace(/&apos;/g, "'")
1165
- .replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(Number.parseInt(dec, 10)))
1166
- .replace(/&#x([0-9a-fA-F]+);/g, (_, hex) =>
1167
- String.fromCodePoint(Number.parseInt(hex, 16)),
1168
- );
1169
- }