docxmlater 10.1.4 → 10.1.6

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 (372) hide show
  1. package/README.md +759 -754
  2. package/dist/constants/legacyCompatFlags.js +1 -1
  3. package/dist/constants/legacyCompatFlags.js.map +1 -1
  4. package/dist/constants/limits.js.map +1 -1
  5. package/dist/core/Document.d.ts +51 -50
  6. package/dist/core/Document.d.ts.map +1 -1
  7. package/dist/core/Document.js +486 -471
  8. package/dist/core/Document.js.map +1 -1
  9. package/dist/core/DocumentContent.d.ts +9 -9
  10. package/dist/core/DocumentContent.d.ts.map +1 -1
  11. package/dist/core/DocumentContent.js +1 -1
  12. package/dist/core/DocumentContent.js.map +1 -1
  13. package/dist/core/DocumentGenerator.d.ts +11 -11
  14. package/dist/core/DocumentGenerator.d.ts.map +1 -1
  15. package/dist/core/DocumentGenerator.js +251 -251
  16. package/dist/core/DocumentGenerator.js.map +1 -1
  17. package/dist/core/DocumentIdManager.js.map +1 -1
  18. package/dist/core/DocumentParser.d.ts +15 -15
  19. package/dist/core/DocumentParser.d.ts.map +1 -1
  20. package/dist/core/DocumentParser.js +2123 -2155
  21. package/dist/core/DocumentParser.js.map +1 -1
  22. package/dist/core/DocumentValidator.d.ts.map +1 -1
  23. package/dist/core/DocumentValidator.js +2 -5
  24. package/dist/core/DocumentValidator.js.map +1 -1
  25. package/dist/core/Relationship.js.map +1 -1
  26. package/dist/core/RelationshipManager.d.ts.map +1 -1
  27. package/dist/core/RelationshipManager.js +3 -3
  28. package/dist/core/RelationshipManager.js.map +1 -1
  29. package/dist/elements/AlternateContent.js.map +1 -1
  30. package/dist/elements/Bookmark.d.ts.map +1 -1
  31. package/dist/elements/Bookmark.js +3 -1
  32. package/dist/elements/Bookmark.js.map +1 -1
  33. package/dist/elements/BookmarkManager.d.ts.map +1 -1
  34. package/dist/elements/BookmarkManager.js.map +1 -1
  35. package/dist/elements/Comment.d.ts.map +1 -1
  36. package/dist/elements/Comment.js +9 -6
  37. package/dist/elements/Comment.js.map +1 -1
  38. package/dist/elements/CommentManager.d.ts.map +1 -1
  39. package/dist/elements/CommentManager.js +18 -17
  40. package/dist/elements/CommentManager.js.map +1 -1
  41. package/dist/elements/CommonTypes.d.ts +21 -21
  42. package/dist/elements/CommonTypes.d.ts.map +1 -1
  43. package/dist/elements/CommonTypes.js +56 -56
  44. package/dist/elements/CommonTypes.js.map +1 -1
  45. package/dist/elements/CustomXml.js.map +1 -1
  46. package/dist/elements/Endnote.d.ts.map +1 -1
  47. package/dist/elements/Endnote.js +6 -6
  48. package/dist/elements/Endnote.js.map +1 -1
  49. package/dist/elements/EndnoteManager.d.ts.map +1 -1
  50. package/dist/elements/EndnoteManager.js +6 -7
  51. package/dist/elements/EndnoteManager.js.map +1 -1
  52. package/dist/elements/Field.d.ts.map +1 -1
  53. package/dist/elements/Field.js +82 -25
  54. package/dist/elements/Field.js.map +1 -1
  55. package/dist/elements/FieldHelpers.d.ts.map +1 -1
  56. package/dist/elements/FieldHelpers.js.map +1 -1
  57. package/dist/elements/FontManager.d.ts.map +1 -1
  58. package/dist/elements/FontManager.js +1 -1
  59. package/dist/elements/FontManager.js.map +1 -1
  60. package/dist/elements/Footer.js +2 -2
  61. package/dist/elements/Footer.js.map +1 -1
  62. package/dist/elements/Footnote.d.ts.map +1 -1
  63. package/dist/elements/Footnote.js +6 -6
  64. package/dist/elements/Footnote.js.map +1 -1
  65. package/dist/elements/FootnoteManager.d.ts.map +1 -1
  66. package/dist/elements/FootnoteManager.js +6 -7
  67. package/dist/elements/FootnoteManager.js.map +1 -1
  68. package/dist/elements/Header.js +2 -2
  69. package/dist/elements/Header.js.map +1 -1
  70. package/dist/elements/HeaderFooterManager.js.map +1 -1
  71. package/dist/elements/Hyperlink.d.ts +5 -3
  72. package/dist/elements/Hyperlink.d.ts.map +1 -1
  73. package/dist/elements/Hyperlink.js +134 -76
  74. package/dist/elements/Hyperlink.js.map +1 -1
  75. package/dist/elements/Image.d.ts.map +1 -1
  76. package/dist/elements/Image.js +238 -106
  77. package/dist/elements/Image.js.map +1 -1
  78. package/dist/elements/ImageManager.d.ts.map +1 -1
  79. package/dist/elements/ImageManager.js +1 -1
  80. package/dist/elements/ImageManager.js.map +1 -1
  81. package/dist/elements/ImageRun.js +1 -1
  82. package/dist/elements/ImageRun.js.map +1 -1
  83. package/dist/elements/MathElement.js.map +1 -1
  84. package/dist/elements/Paragraph.d.ts +24 -24
  85. package/dist/elements/Paragraph.d.ts.map +1 -1
  86. package/dist/elements/Paragraph.js +181 -188
  87. package/dist/elements/Paragraph.js.map +1 -1
  88. package/dist/elements/PreservedElement.js.map +1 -1
  89. package/dist/elements/PropertyChangeTypes.d.ts.map +1 -1
  90. package/dist/elements/PropertyChangeTypes.js +6 -6
  91. package/dist/elements/PropertyChangeTypes.js.map +1 -1
  92. package/dist/elements/RangeMarker.d.ts.map +1 -1
  93. package/dist/elements/RangeMarker.js.map +1 -1
  94. package/dist/elements/Revision.d.ts.map +1 -1
  95. package/dist/elements/Revision.js +4 -5
  96. package/dist/elements/Revision.js.map +1 -1
  97. package/dist/elements/RevisionContent.js.map +1 -1
  98. package/dist/elements/RevisionManager.d.ts.map +1 -1
  99. package/dist/elements/RevisionManager.js +40 -48
  100. package/dist/elements/RevisionManager.js.map +1 -1
  101. package/dist/elements/Run.d.ts +16 -16
  102. package/dist/elements/Run.d.ts.map +1 -1
  103. package/dist/elements/Run.js +256 -238
  104. package/dist/elements/Run.js.map +1 -1
  105. package/dist/elements/Section.d.ts.map +1 -1
  106. package/dist/elements/Section.js +36 -11
  107. package/dist/elements/Section.js.map +1 -1
  108. package/dist/elements/Shape.d.ts.map +1 -1
  109. package/dist/elements/Shape.js.map +1 -1
  110. package/dist/elements/StructuredDocumentTag.d.ts +6 -6
  111. package/dist/elements/StructuredDocumentTag.d.ts.map +1 -1
  112. package/dist/elements/StructuredDocumentTag.js +99 -104
  113. package/dist/elements/StructuredDocumentTag.js.map +1 -1
  114. package/dist/elements/Table.d.ts +11 -11
  115. package/dist/elements/Table.d.ts.map +1 -1
  116. package/dist/elements/Table.js +102 -107
  117. package/dist/elements/Table.js.map +1 -1
  118. package/dist/elements/TableCell.d.ts +10 -10
  119. package/dist/elements/TableCell.d.ts.map +1 -1
  120. package/dist/elements/TableCell.js +105 -106
  121. package/dist/elements/TableCell.js.map +1 -1
  122. package/dist/elements/TableGridChange.d.ts.map +1 -1
  123. package/dist/elements/TableGridChange.js.map +1 -1
  124. package/dist/elements/TableOfContents.d.ts.map +1 -1
  125. package/dist/elements/TableOfContents.js +4 -4
  126. package/dist/elements/TableOfContents.js.map +1 -1
  127. package/dist/elements/TableOfContentsElement.js.map +1 -1
  128. package/dist/elements/TableRow.d.ts.map +1 -1
  129. package/dist/elements/TableRow.js +13 -6
  130. package/dist/elements/TableRow.js.map +1 -1
  131. package/dist/elements/TextBox.d.ts.map +1 -1
  132. package/dist/elements/TextBox.js +3 -5
  133. package/dist/elements/TextBox.js.map +1 -1
  134. package/dist/formatting/AbstractNumbering.d.ts +4 -4
  135. package/dist/formatting/AbstractNumbering.d.ts.map +1 -1
  136. package/dist/formatting/AbstractNumbering.js +54 -49
  137. package/dist/formatting/AbstractNumbering.js.map +1 -1
  138. package/dist/formatting/NumberingInstance.d.ts.map +1 -1
  139. package/dist/formatting/NumberingInstance.js +1 -3
  140. package/dist/formatting/NumberingInstance.js.map +1 -1
  141. package/dist/formatting/NumberingLevel.d.ts +5 -5
  142. package/dist/formatting/NumberingLevel.d.ts.map +1 -1
  143. package/dist/formatting/NumberingLevel.js +119 -125
  144. package/dist/formatting/NumberingLevel.js.map +1 -1
  145. package/dist/formatting/NumberingManager.d.ts +1 -0
  146. package/dist/formatting/NumberingManager.d.ts.map +1 -1
  147. package/dist/formatting/NumberingManager.js +27 -9
  148. package/dist/formatting/NumberingManager.js.map +1 -1
  149. package/dist/formatting/Style.d.ts +11 -11
  150. package/dist/formatting/Style.d.ts.map +1 -1
  151. package/dist/formatting/Style.js +219 -247
  152. package/dist/formatting/Style.js.map +1 -1
  153. package/dist/formatting/StylesManager.d.ts +2 -2
  154. package/dist/formatting/StylesManager.d.ts.map +1 -1
  155. package/dist/formatting/StylesManager.js +96 -102
  156. package/dist/formatting/StylesManager.js.map +1 -1
  157. package/dist/helpers/CleanupHelper.d.ts +1 -1
  158. package/dist/helpers/CleanupHelper.d.ts.map +1 -1
  159. package/dist/helpers/CleanupHelper.js +6 -6
  160. package/dist/helpers/CleanupHelper.js.map +1 -1
  161. package/dist/images/ImageOptimizer.js +7 -7
  162. package/dist/images/ImageOptimizer.js.map +1 -1
  163. package/dist/index.d.ts +9 -9
  164. package/dist/index.d.ts.map +1 -1
  165. package/dist/index.js.map +1 -1
  166. package/dist/managers/DrawingManager.js.map +1 -1
  167. package/dist/tracking/DocumentTrackingContext.d.ts.map +1 -1
  168. package/dist/tracking/DocumentTrackingContext.js +23 -7
  169. package/dist/tracking/DocumentTrackingContext.js.map +1 -1
  170. package/dist/tracking/TrackingContext.d.ts.map +1 -1
  171. package/dist/tracking/TrackingContext.js.map +1 -1
  172. package/dist/types/compatibility-types.js.map +1 -1
  173. package/dist/types/formatting.js.map +1 -1
  174. package/dist/types/list-types.d.ts +6 -6
  175. package/dist/types/list-types.js.map +1 -1
  176. package/dist/types/settings-types.js.map +1 -1
  177. package/dist/types/styleConfig.d.ts +2 -2
  178. package/dist/types/styleConfig.js.map +1 -1
  179. package/dist/utils/ChangelogGenerator.d.ts.map +1 -1
  180. package/dist/utils/ChangelogGenerator.js +97 -101
  181. package/dist/utils/ChangelogGenerator.js.map +1 -1
  182. package/dist/utils/CompatibilityUpgrader.d.ts.map +1 -1
  183. package/dist/utils/CompatibilityUpgrader.js +1 -1
  184. package/dist/utils/CompatibilityUpgrader.js.map +1 -1
  185. package/dist/utils/InMemoryRevisionAcceptor.d.ts.map +1 -1
  186. package/dist/utils/InMemoryRevisionAcceptor.js +1 -6
  187. package/dist/utils/InMemoryRevisionAcceptor.js.map +1 -1
  188. package/dist/utils/MoveOperationHelper.d.ts.map +1 -1
  189. package/dist/utils/MoveOperationHelper.js +1 -1
  190. package/dist/utils/MoveOperationHelper.js.map +1 -1
  191. package/dist/utils/RevisionAwareProcessor.d.ts.map +1 -1
  192. package/dist/utils/RevisionAwareProcessor.js +2 -4
  193. package/dist/utils/RevisionAwareProcessor.js.map +1 -1
  194. package/dist/utils/RevisionWalker.d.ts.map +1 -1
  195. package/dist/utils/RevisionWalker.js +4 -12
  196. package/dist/utils/RevisionWalker.js.map +1 -1
  197. package/dist/utils/SelectiveRevisionAcceptor.d.ts.map +1 -1
  198. package/dist/utils/SelectiveRevisionAcceptor.js +2 -6
  199. package/dist/utils/SelectiveRevisionAcceptor.js.map +1 -1
  200. package/dist/utils/ShadingResolver.d.ts.map +1 -1
  201. package/dist/utils/ShadingResolver.js +1 -1
  202. package/dist/utils/ShadingResolver.js.map +1 -1
  203. package/dist/utils/acceptRevisions.d.ts.map +1 -1
  204. package/dist/utils/acceptRevisions.js +23 -12
  205. package/dist/utils/acceptRevisions.js.map +1 -1
  206. package/dist/utils/cnfStyleDecoder.d.ts +1 -1
  207. package/dist/utils/cnfStyleDecoder.d.ts.map +1 -1
  208. package/dist/utils/cnfStyleDecoder.js +40 -40
  209. package/dist/utils/cnfStyleDecoder.js.map +1 -1
  210. package/dist/utils/corruptionDetection.d.ts.map +1 -1
  211. package/dist/utils/corruptionDetection.js.map +1 -1
  212. package/dist/utils/dateFormatting.js.map +1 -1
  213. package/dist/utils/deepClone.js +1 -1
  214. package/dist/utils/deepClone.js.map +1 -1
  215. package/dist/utils/diagnostics.d.ts.map +1 -1
  216. package/dist/utils/diagnostics.js +1 -1
  217. package/dist/utils/diagnostics.js.map +1 -1
  218. package/dist/utils/errorHandling.js.map +1 -1
  219. package/dist/utils/formatting.d.ts.map +1 -1
  220. package/dist/utils/formatting.js +10 -2
  221. package/dist/utils/formatting.js.map +1 -1
  222. package/dist/utils/list-detection.d.ts +2 -2
  223. package/dist/utils/list-detection.d.ts.map +1 -1
  224. package/dist/utils/list-detection.js +21 -23
  225. package/dist/utils/list-detection.js.map +1 -1
  226. package/dist/utils/logger.d.ts.map +1 -1
  227. package/dist/utils/logger.js +12 -7
  228. package/dist/utils/logger.js.map +1 -1
  229. package/dist/utils/parsingHelpers.js.map +1 -1
  230. package/dist/utils/stripTrackedChanges.d.ts.map +1 -1
  231. package/dist/utils/stripTrackedChanges.js +3 -3
  232. package/dist/utils/stripTrackedChanges.js.map +1 -1
  233. package/dist/utils/textDiff.d.ts +1 -1
  234. package/dist/utils/textDiff.js +8 -8
  235. package/dist/utils/textDiff.js.map +1 -1
  236. package/dist/utils/units.js.map +1 -1
  237. package/dist/utils/validation.d.ts.map +1 -1
  238. package/dist/utils/validation.js +24 -7
  239. package/dist/utils/validation.js.map +1 -1
  240. package/dist/utils/xmlSanitization.d.ts.map +1 -1
  241. package/dist/utils/xmlSanitization.js +3 -3
  242. package/dist/utils/xmlSanitization.js.map +1 -1
  243. package/dist/validation/RevisionAutoFixer.d.ts.map +1 -1
  244. package/dist/validation/RevisionAutoFixer.js +5 -5
  245. package/dist/validation/RevisionAutoFixer.js.map +1 -1
  246. package/dist/validation/RevisionValidator.d.ts.map +1 -1
  247. package/dist/validation/RevisionValidator.js +7 -9
  248. package/dist/validation/RevisionValidator.js.map +1 -1
  249. package/dist/validation/ValidationRules.js +3 -3
  250. package/dist/validation/ValidationRules.js.map +1 -1
  251. package/dist/validation/index.js.map +1 -1
  252. package/dist/xml/XMLBuilder.d.ts +1 -1
  253. package/dist/xml/XMLBuilder.d.ts.map +1 -1
  254. package/dist/xml/XMLBuilder.js +98 -100
  255. package/dist/xml/XMLBuilder.js.map +1 -1
  256. package/dist/xml/XMLParser.d.ts.map +1 -1
  257. package/dist/xml/XMLParser.js +61 -66
  258. package/dist/xml/XMLParser.js.map +1 -1
  259. package/dist/zip/ZipHandler.d.ts.map +1 -1
  260. package/dist/zip/ZipHandler.js.map +1 -1
  261. package/dist/zip/ZipReader.d.ts.map +1 -1
  262. package/dist/zip/ZipReader.js +1 -3
  263. package/dist/zip/ZipReader.js.map +1 -1
  264. package/dist/zip/ZipWriter.d.ts +1 -1
  265. package/dist/zip/ZipWriter.d.ts.map +1 -1
  266. package/dist/zip/ZipWriter.js +28 -36
  267. package/dist/zip/ZipWriter.js.map +1 -1
  268. package/dist/zip/types.js +1 -1
  269. package/dist/zip/types.js.map +1 -1
  270. package/package.json +92 -92
  271. package/src/__tests__/helper-methods.test.ts +512 -512
  272. package/src/constants/legacyCompatFlags.ts +138 -138
  273. package/src/constants/limits.ts +50 -50
  274. package/src/core/Document.ts +1010 -1145
  275. package/src/core/DocumentContent.ts +461 -467
  276. package/src/core/DocumentGenerator.ts +1133 -1104
  277. package/src/core/DocumentIdManager.ts +158 -158
  278. package/src/core/DocumentParser.ts +2347 -2716
  279. package/src/core/DocumentValidator.ts +363 -372
  280. package/src/core/Relationship.ts +367 -367
  281. package/src/core/RelationshipManager.ts +429 -428
  282. package/src/elements/AlternateContent.ts +42 -42
  283. package/src/elements/Bookmark.ts +212 -210
  284. package/src/elements/BookmarkManager.ts +247 -250
  285. package/src/elements/Comment.ts +356 -359
  286. package/src/elements/CommentManager.ts +499 -502
  287. package/src/elements/CommonTypes.ts +524 -549
  288. package/src/elements/CustomXml.ts +36 -36
  289. package/src/elements/Endnote.ts +221 -217
  290. package/src/elements/EndnoteManager.ts +246 -249
  291. package/src/elements/Field.ts +1292 -1233
  292. package/src/elements/FieldHelpers.ts +329 -333
  293. package/src/elements/FontManager.ts +336 -339
  294. package/src/elements/Footer.ts +269 -269
  295. package/src/elements/Footnote.ts +221 -217
  296. package/src/elements/FootnoteManager.ts +246 -249
  297. package/src/elements/Header.ts +269 -269
  298. package/src/elements/HeaderFooterManager.ts +219 -219
  299. package/src/elements/Hyperlink.ts +1288 -1193
  300. package/src/elements/Image.ts +1982 -1756
  301. package/src/elements/ImageManager.ts +437 -432
  302. package/src/elements/ImageRun.ts +59 -59
  303. package/src/elements/MathElement.ts +65 -65
  304. package/src/elements/Paragraph.ts +4347 -4287
  305. package/src/elements/PreservedElement.ts +53 -53
  306. package/src/elements/PropertyChangeTypes.ts +458 -442
  307. package/src/elements/RangeMarker.ts +382 -400
  308. package/src/elements/Revision.ts +1198 -1217
  309. package/src/elements/RevisionContent.ts +73 -73
  310. package/src/elements/RevisionManager.ts +1070 -1070
  311. package/src/elements/Run.ts +3103 -3073
  312. package/src/elements/Section.ts +1521 -1421
  313. package/src/elements/Shape.ts +884 -873
  314. package/src/elements/StructuredDocumentTag.ts +1176 -1207
  315. package/src/elements/Table.ts +2468 -2524
  316. package/src/elements/TableCell.ts +1617 -1621
  317. package/src/elements/TableGridChange.ts +149 -151
  318. package/src/elements/TableOfContents.ts +701 -691
  319. package/src/elements/TableOfContentsElement.ts +89 -89
  320. package/src/elements/TableRow.ts +960 -929
  321. package/src/elements/TextBox.ts +766 -768
  322. package/src/formatting/AbstractNumbering.ts +580 -579
  323. package/src/formatting/NumberingInstance.ts +295 -299
  324. package/src/formatting/NumberingLevel.ts +981 -1040
  325. package/src/formatting/NumberingManager.ts +875 -827
  326. package/src/formatting/Style.ts +1785 -1879
  327. package/src/formatting/StylesManager.ts +1090 -1130
  328. package/src/helpers/CleanupHelper.ts +524 -524
  329. package/src/images/ImageOptimizer.ts +274 -274
  330. package/src/index.ts +559 -554
  331. package/src/managers/DrawingManager.ts +319 -319
  332. package/src/tracking/DocumentTrackingContext.ts +687 -674
  333. package/src/tracking/TrackingContext.ts +175 -173
  334. package/src/types/compatibility-types.ts +49 -49
  335. package/src/types/formatting.ts +210 -210
  336. package/src/types/list-types.ts +14 -14
  337. package/src/types/settings-types.ts +59 -59
  338. package/src/types/styleConfig.ts +189 -189
  339. package/src/utils/ChangelogGenerator.ts +1583 -1581
  340. package/src/utils/CompatibilityUpgrader.ts +235 -237
  341. package/src/utils/InMemoryRevisionAcceptor.ts +691 -696
  342. package/src/utils/MoveOperationHelper.ts +233 -238
  343. package/src/utils/RevisionAwareProcessor.ts +518 -526
  344. package/src/utils/RevisionWalker.ts +427 -457
  345. package/src/utils/SelectiveRevisionAcceptor.ts +662 -683
  346. package/src/utils/ShadingResolver.ts +105 -107
  347. package/src/utils/acceptRevisions.ts +723 -714
  348. package/src/utils/cnfStyleDecoder.ts +212 -217
  349. package/src/utils/corruptionDetection.ts +346 -345
  350. package/src/utils/dateFormatting.ts +20 -20
  351. package/src/utils/deepClone.ts +77 -78
  352. package/src/utils/diagnostics.ts +125 -129
  353. package/src/utils/errorHandling.ts +80 -80
  354. package/src/utils/formatting.ts +220 -213
  355. package/src/utils/list-detection.ts +32 -42
  356. package/src/utils/logger.ts +412 -404
  357. package/src/utils/parsingHelpers.ts +190 -190
  358. package/src/utils/stripTrackedChanges.ts +356 -353
  359. package/src/utils/textDiff.ts +100 -100
  360. package/src/utils/units.ts +421 -421
  361. package/src/utils/validation.ts +553 -542
  362. package/src/utils/xmlSanitization.ts +179 -182
  363. package/src/validation/RevisionAutoFixer.ts +541 -542
  364. package/src/validation/RevisionValidator.ts +470 -460
  365. package/src/validation/ValidationRules.ts +338 -338
  366. package/src/validation/index.ts +30 -30
  367. package/src/xml/XMLBuilder.ts +857 -871
  368. package/src/xml/XMLParser.ts +877 -919
  369. package/src/zip/ZipHandler.ts +629 -637
  370. package/src/zip/ZipReader.ts +295 -299
  371. package/src/zip/ZipWriter.ts +374 -390
  372. package/src/zip/types.ts +116 -116
@@ -1,526 +1,518 @@
1
- /**
2
- * RevisionAwareProcessor - Handles document revisions before and during processing
3
- *
4
- * Provides three modes for handling existing Word tracked changes:
5
- * - accept_all: Accept all revisions before processing (default)
6
- * - preserve: Preserve revisions, skip conflicting operations
7
- * - preserve_and_wrap: Preserve revisions, wrap conflicts in new revisions
8
- *
9
- * Implements ECMA-376 compliant revision handling.
10
- *
11
- * @module RevisionAwareProcessor
12
- */
13
-
14
- import type { Document } from '../core/Document';
15
- import { Revision, RevisionType } from '../elements/Revision';
16
- import { RevisionManager } from '../elements/RevisionManager';
17
- import { ChangeCategory, ChangeLocation } from './ChangelogGenerator';
18
-
19
- /**
20
- * Revision handling modes for document processing.
21
- */
22
- export type RevisionHandlingMode =
23
- | 'accept_all' // Accept all revisions before processing
24
- | 'preserve' // Preserve revisions, skip conflicting operations
25
- | 'preserve_and_wrap'; // Preserve revisions, wrap conflicts in new revisions
26
-
27
- /**
28
- * Options for revision-aware processing.
29
- */
30
- export interface RevisionProcessingOptions {
31
- /** How to handle existing revisions */
32
- mode: RevisionHandlingMode;
33
- /** Author name for new revisions (required for 'preserve_and_wrap' mode) */
34
- author?: string;
35
- /** Selective acceptance criteria (optional, for partial acceptance) */
36
- acceptCriteria?: SelectionCriteria;
37
- }
38
-
39
- /**
40
- * Criteria for selecting specific revisions.
41
- */
42
- export interface SelectionCriteria {
43
- /** Specific revision IDs */
44
- ids?: number[];
45
- /** Filter by revision types */
46
- types?: RevisionType[];
47
- /** Filter by authors */
48
- authors?: string[];
49
- /** Filter by date range */
50
- dateRange?: { start: Date; end: Date };
51
- /** Filter by category */
52
- categories?: ChangeCategory[];
53
- /** Custom filter function */
54
- custom?: (revision: Revision) => boolean;
55
- }
56
-
57
- /**
58
- * Result of revision-aware processing.
59
- */
60
- export interface RevisionProcessingResult {
61
- /** Whether processing completed successfully */
62
- success: boolean;
63
- /** IDs of revisions that were accepted */
64
- acceptedRevisions: string[];
65
- /** IDs of revisions that were preserved */
66
- preservedRevisions: string[];
67
- /** IDs of new revisions created (preserve_and_wrap mode) */
68
- newRevisions: string[];
69
- /** Conflicts encountered during processing */
70
- conflicts: ConflictInfo[];
71
- /** Processing log entries */
72
- log: ProcessingLogEntry[];
73
- }
74
-
75
- /**
76
- * Information about a conflict between an operation and a revision.
77
- */
78
- export interface ConflictInfo {
79
- /** Description of the operation that conflicted */
80
- operation: string;
81
- /** ID of the conflicting revision */
82
- revisionId: string;
83
- /** How the conflict was resolved */
84
- resolution: 'accepted' | 'skipped' | 'wrapped';
85
- /** Location of the conflict */
86
- location: ChangeLocation;
87
- }
88
-
89
- /**
90
- * Log entry for processing actions.
91
- */
92
- export interface ProcessingLogEntry {
93
- timestamp: Date;
94
- action: string;
95
- details: string;
96
- revisionId?: string;
97
- }
98
-
99
- /**
100
- * Handles document revisions before and during processing.
101
- * Implements ECMA-376 compliant revision handling.
102
- */
103
- export class RevisionAwareProcessor {
104
- /**
105
- * Prepare document for processing by handling revisions.
106
- * Call this before applying any document modifications.
107
- *
108
- * @param doc - Document to prepare
109
- * @param options - Processing options
110
- * @returns Processing result with details of actions taken
111
- */
112
- static async prepare(
113
- doc: Document,
114
- options: RevisionProcessingOptions
115
- ): Promise<RevisionProcessingResult> {
116
- const log: ProcessingLogEntry[] = [];
117
- const acceptedRevisions: string[] = [];
118
- const preservedRevisions: string[] = [];
119
- const newRevisions: string[] = [];
120
- const conflicts: ConflictInfo[] = [];
121
-
122
- const addLog = (action: string, details: string, revisionId?: string) => {
123
- log.push({
124
- timestamp: new Date(),
125
- action,
126
- details,
127
- revisionId,
128
- });
129
- };
130
-
131
- addLog('start', `Starting revision processing with mode: ${options.mode}`);
132
-
133
- try {
134
- const revisionManager = doc.getRevisionManager();
135
- const hasInMemoryRevisions = revisionManager?.hasRevisions() ?? false;
136
- const hasRawXmlRevisions = doc.hasRawXmlRevisions();
137
-
138
- // Check both in-memory model AND raw XML for revisions
139
- // Raw XML may contain revisions that weren't fully parsed into memory
140
- if (!hasInMemoryRevisions && !hasRawXmlRevisions) {
141
- addLog('complete', 'No revisions found in document');
142
- return {
143
- success: true,
144
- acceptedRevisions,
145
- preservedRevisions,
146
- newRevisions,
147
- conflicts,
148
- log,
149
- };
150
- }
151
-
152
- const allRevisions = revisionManager?.getAllRevisions() ?? [];
153
- const inMemoryCount = allRevisions.length;
154
- const rawXmlNote = hasRawXmlRevisions ? ' (raw XML contains revision markup)' : '';
155
- addLog('info', `Found ${inMemoryCount} parsed revisions in document${rawXmlNote}`);
156
-
157
- switch (options.mode) {
158
- case 'accept_all':
159
- // Accept all revisions
160
- await this.acceptAllRevisions(doc, allRevisions, acceptedRevisions, addLog);
161
- break;
162
-
163
- case 'preserve':
164
- // Preserve all revisions by preventing document.xml regeneration
165
- // This keeps the original XML with w:ins/w:del elements intact
166
- for (const rev of allRevisions) {
167
- preservedRevisions.push(rev.getId().toString());
168
- }
169
- doc.preserveRawXml();
170
- addLog('complete', `Preserved ${preservedRevisions.length} revisions (raw XML preservation enabled)`);
171
- break;
172
-
173
- case 'preserve_and_wrap':
174
- // Preserve all revisions, track for later wrapping
175
- // Also enable raw XML preservation to keep existing revisions
176
- for (const rev of allRevisions) {
177
- preservedRevisions.push(rev.getId().toString());
178
- }
179
- doc.preserveRawXml();
180
- addLog('info', `Will preserve and wrap conflicts for ${preservedRevisions.length} revisions`);
181
- addLog('info', `New revisions will be authored by: ${options.author || 'Unknown'}`);
182
- break;
183
-
184
- default:
185
- addLog('error', `Unknown processing mode: ${options.mode}`);
186
- return {
187
- success: false,
188
- acceptedRevisions,
189
- preservedRevisions,
190
- newRevisions,
191
- conflicts,
192
- log,
193
- };
194
- }
195
-
196
- addLog('complete', 'Revision processing completed successfully');
197
-
198
- return {
199
- success: true,
200
- acceptedRevisions,
201
- preservedRevisions,
202
- newRevisions,
203
- conflicts,
204
- log,
205
- };
206
- } catch (error: unknown) {
207
- const errorMessage = error instanceof Error ? error.message : 'Unknown error';
208
- addLog('error', `Processing failed: ${errorMessage}`);
209
-
210
- return {
211
- success: false,
212
- acceptedRevisions,
213
- preservedRevisions,
214
- newRevisions,
215
- conflicts,
216
- log,
217
- };
218
- }
219
- }
220
-
221
- /**
222
- * Accept all revisions in the document using Document.acceptAllRevisions().
223
- *
224
- * This delegates to Document.acceptAllRevisions() which uses in-memory DOM transformation
225
- * (the industry-standard approach used by OpenXML PowerTools, Aspose.Words, etc.):
226
- *
227
- * 1. Transforms Revision objects in paragraph.content arrays
228
- * 2. Insertions (w:ins): Unwraps content - keeps Runs/Hyperlinks, removes Revision wrapper
229
- * 3. Deletions (w:del): Removes content entirely from the model
230
- * 4. Move operations: moveFrom removed, moveTo unwrapped
231
- * 5. Property changes: Removes change metadata, keeps current formatting
232
- *
233
- * This approach allows subsequent modifications to be saved correctly because the
234
- * in-memory model is transformed (not raw XML), so save() regenerates document.xml
235
- * with both the accepted changes AND any subsequent modifications.
236
- *
237
- * @see https://github.com/OfficeDev/Open-Xml-PowerTools - RevisionAccepter.cs
238
- * @see https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/ee836138
239
- */
240
- private static async acceptAllRevisions(
241
- doc: Document,
242
- revisions: Revision[],
243
- acceptedIds: string[],
244
- addLog: (action: string, details: string, revisionId?: string) => void
245
- ): Promise<void> {
246
- addLog('info', 'Accepting all revisions using in-memory DOM transformation');
247
-
248
- // Track accepted revision IDs before acceptance (for logging)
249
- for (const rev of revisions) {
250
- acceptedIds.push(rev.getId().toString());
251
- }
252
-
253
- // Use Document.acceptAllRevisions() which uses in-memory transformation
254
- // This allows subsequent modifications to be saved correctly
255
- await doc.acceptAllRevisions();
256
-
257
- // Log each accepted revision
258
- for (const id of acceptedIds) {
259
- addLog('accept', `Accepted revision ${id}`, id);
260
- }
261
-
262
- addLog('complete', `Accepted ${acceptedIds.length} revisions (in-memory transformation)`);
263
- }
264
-
265
- /**
266
- * Accept revisions matching the given criteria.
267
- * Uses existing RevisionAcceptor under the hood.
268
- *
269
- * @param doc - Document to process
270
- * @param criteria - Selection criteria
271
- * @returns Array of accepted revision IDs
272
- */
273
- static acceptSelective(
274
- doc: Document,
275
- criteria: SelectionCriteria
276
- ): string[] {
277
- const revisionManager = doc.getRevisionManager();
278
- if (!revisionManager) {
279
- return [];
280
- }
281
-
282
- const allRevisions = revisionManager.getAllRevisions();
283
- const matchingRevisions = this.filterRevisions(allRevisions, criteria);
284
-
285
- const acceptedIds: string[] = [];
286
- for (const rev of matchingRevisions) {
287
- acceptedIds.push(rev.getId().toString());
288
- // Note: Actual acceptance would require XML manipulation
289
- // For now, this returns which revisions WOULD be accepted
290
- }
291
-
292
- return acceptedIds;
293
- }
294
-
295
- /**
296
- * Filter revisions based on criteria.
297
- */
298
- private static filterRevisions(
299
- revisions: Revision[],
300
- criteria: SelectionCriteria
301
- ): Revision[] {
302
- return revisions.filter(rev => {
303
- // Filter by IDs
304
- if (criteria.ids && !criteria.ids.includes(rev.getId())) {
305
- return false;
306
- }
307
-
308
- // Filter by types
309
- if (criteria.types && !criteria.types.includes(rev.getType())) {
310
- return false;
311
- }
312
-
313
- // Filter by authors
314
- if (criteria.authors && !criteria.authors.includes(rev.getAuthor())) {
315
- return false;
316
- }
317
-
318
- // Filter by date range
319
- if (criteria.dateRange) {
320
- const date = rev.getDate();
321
- if (date < criteria.dateRange.start || date > criteria.dateRange.end) {
322
- return false;
323
- }
324
- }
325
-
326
- // Filter by categories
327
- if (criteria.categories) {
328
- const category = this.getRevisionCategory(rev);
329
- if (!criteria.categories.includes(category)) {
330
- return false;
331
- }
332
- }
333
-
334
- // Custom filter
335
- if (criteria.custom && !criteria.custom(rev)) {
336
- return false;
337
- }
338
-
339
- return true;
340
- });
341
- }
342
-
343
- /**
344
- * Get the semantic category of a revision.
345
- */
346
- private static getRevisionCategory(revision: Revision): ChangeCategory {
347
- const type = revision.getType();
348
-
349
- if (type === 'insert' || type === 'delete') {
350
- return 'content';
351
- }
352
- if (
353
- type === 'runPropertiesChange' ||
354
- type === 'paragraphPropertiesChange' ||
355
- type === 'numberingChange'
356
- ) {
357
- return 'formatting';
358
- }
359
- if (
360
- type === 'moveFrom' ||
361
- type === 'moveTo' ||
362
- type === 'sectionPropertiesChange'
363
- ) {
364
- return 'structural';
365
- }
366
- if (
367
- type === 'tablePropertiesChange' ||
368
- type === 'tableExceptionPropertiesChange' ||
369
- type === 'tableRowPropertiesChange' ||
370
- type === 'tableCellPropertiesChange' ||
371
- type === 'tableCellInsert' ||
372
- type === 'tableCellDelete' ||
373
- type === 'tableCellMerge'
374
- ) {
375
- return 'table';
376
- }
377
-
378
- return 'content';
379
- }
380
-
381
- /**
382
- * Check if an operation at the given location would conflict with any revision.
383
- *
384
- * @param doc - Document to check
385
- * @param paragraphIndex - Paragraph index
386
- * @param runIndex - Optional run index
387
- * @returns Conflicting revision or null
388
- */
389
- static checkConflict(
390
- doc: Document,
391
- paragraphIndex: number,
392
- runIndex?: number
393
- ): Revision | null {
394
- const revisionManager = doc.getRevisionManager();
395
- if (!revisionManager) {
396
- return null;
397
- }
398
-
399
- // Get revisions that might affect this paragraph
400
- const revisions = revisionManager.getRevisionsForParagraph(paragraphIndex);
401
-
402
- // For now, return the first revision at this location
403
- // In a more complete implementation, we would check run-level conflicts
404
- return revisions.length > 0 ? revisions[0]! : null;
405
- }
406
-
407
- /**
408
- * Get all revisions that would be affected by an operation.
409
- *
410
- * @param doc - Document to check
411
- * @param paragraphIndex - Paragraph index
412
- * @param runIndex - Optional run index
413
- * @returns Array of affected revisions
414
- */
415
- static getAffectedRevisions(
416
- doc: Document,
417
- paragraphIndex: number,
418
- runIndex?: number
419
- ): Revision[] {
420
- const revisionManager = doc.getRevisionManager();
421
- if (!revisionManager) {
422
- return [];
423
- }
424
-
425
- return revisionManager.getRevisionsForParagraph(paragraphIndex);
426
- }
427
-
428
- /**
429
- * Wrap content in a new revision (for preserve_and_wrap mode).
430
- * Creates a new revision tracking the change.
431
- *
432
- * @param doc - Document to modify
433
- * @param paragraphIndex - Paragraph index
434
- * @param runIndex - Run index
435
- * @param author - Author for the new revision
436
- * @param type - Type of revision to create
437
- * @returns ID of the new revision
438
- */
439
- static wrapInRevision(
440
- doc: Document,
441
- paragraphIndex: number,
442
- runIndex: number,
443
- author: string,
444
- type: 'insert' | 'delete'
445
- ): string {
446
- const revisionManager = doc.getRevisionManager();
447
- if (!revisionManager) {
448
- throw new Error('Document does not have a revision manager');
449
- }
450
-
451
- // Get the paragraphs from the document
452
- const paragraphs = doc.getParagraphs();
453
- if (paragraphIndex < 0 || paragraphIndex >= paragraphs.length) {
454
- throw new Error(`Invalid paragraph index: ${paragraphIndex}`);
455
- }
456
-
457
- const paragraph = paragraphs[paragraphIndex];
458
- if (!paragraph) {
459
- throw new Error(`Paragraph at index ${paragraphIndex} not found`);
460
- }
461
-
462
- const runs = paragraph.getRuns();
463
- if (runIndex < 0 || runIndex >= runs.length) {
464
- throw new Error(`Invalid run index: ${runIndex}`);
465
- }
466
-
467
- const run = runs[runIndex];
468
- if (!run) {
469
- throw new Error(`Run at index ${runIndex} not found`);
470
- }
471
-
472
- // Create new revision
473
- const revision = new Revision({
474
- author,
475
- type,
476
- content: run,
477
- date: new Date(),
478
- });
479
-
480
- // Register with manager
481
- revisionManager.register(revision);
482
-
483
- return revision.getId().toString();
484
- }
485
-
486
- /**
487
- * Check if document has any tracked changes.
488
- *
489
- * @param doc - Document to check
490
- * @returns True if document has tracked changes
491
- */
492
- static hasTrackedChanges(doc: Document): boolean {
493
- const revisionManager = doc.getRevisionManager();
494
- return revisionManager ? revisionManager.hasRevisions() : false;
495
- }
496
-
497
- /**
498
- * Get summary of tracked changes in document.
499
- *
500
- * @param doc - Document to summarize
501
- * @returns Summary object or null if no revision manager
502
- */
503
- static getTrackedChangesSummary(doc: Document): {
504
- total: number;
505
- insertions: number;
506
- deletions: number;
507
- formatting: number;
508
- moves: number;
509
- authors: string[];
510
- } | null {
511
- const revisionManager = doc.getRevisionManager();
512
- if (!revisionManager) {
513
- return null;
514
- }
515
-
516
- const summary = revisionManager.getSummary();
517
- return {
518
- total: summary.total,
519
- insertions: summary.byType.insertions,
520
- deletions: summary.byType.deletions,
521
- formatting: summary.byType.propertyChanges,
522
- moves: summary.byType.moves,
523
- authors: summary.authors,
524
- };
525
- }
526
- }
1
+ /**
2
+ * RevisionAwareProcessor - Handles document revisions before and during processing
3
+ *
4
+ * Provides three modes for handling existing Word tracked changes:
5
+ * - accept_all: Accept all revisions before processing (default)
6
+ * - preserve: Preserve revisions, skip conflicting operations
7
+ * - preserve_and_wrap: Preserve revisions, wrap conflicts in new revisions
8
+ *
9
+ * Implements ECMA-376 compliant revision handling.
10
+ *
11
+ * @module RevisionAwareProcessor
12
+ */
13
+
14
+ import type { Document } from '../core/Document';
15
+ import { Revision, RevisionType } from '../elements/Revision';
16
+ import { RevisionManager } from '../elements/RevisionManager';
17
+ import { ChangeCategory, ChangeLocation } from './ChangelogGenerator';
18
+
19
+ /**
20
+ * Revision handling modes for document processing.
21
+ */
22
+ export type RevisionHandlingMode =
23
+ | 'accept_all' // Accept all revisions before processing
24
+ | 'preserve' // Preserve revisions, skip conflicting operations
25
+ | 'preserve_and_wrap'; // Preserve revisions, wrap conflicts in new revisions
26
+
27
+ /**
28
+ * Options for revision-aware processing.
29
+ */
30
+ export interface RevisionProcessingOptions {
31
+ /** How to handle existing revisions */
32
+ mode: RevisionHandlingMode;
33
+ /** Author name for new revisions (required for 'preserve_and_wrap' mode) */
34
+ author?: string;
35
+ /** Selective acceptance criteria (optional, for partial acceptance) */
36
+ acceptCriteria?: SelectionCriteria;
37
+ }
38
+
39
+ /**
40
+ * Criteria for selecting specific revisions.
41
+ */
42
+ export interface SelectionCriteria {
43
+ /** Specific revision IDs */
44
+ ids?: number[];
45
+ /** Filter by revision types */
46
+ types?: RevisionType[];
47
+ /** Filter by authors */
48
+ authors?: string[];
49
+ /** Filter by date range */
50
+ dateRange?: { start: Date; end: Date };
51
+ /** Filter by category */
52
+ categories?: ChangeCategory[];
53
+ /** Custom filter function */
54
+ custom?: (revision: Revision) => boolean;
55
+ }
56
+
57
+ /**
58
+ * Result of revision-aware processing.
59
+ */
60
+ export interface RevisionProcessingResult {
61
+ /** Whether processing completed successfully */
62
+ success: boolean;
63
+ /** IDs of revisions that were accepted */
64
+ acceptedRevisions: string[];
65
+ /** IDs of revisions that were preserved */
66
+ preservedRevisions: string[];
67
+ /** IDs of new revisions created (preserve_and_wrap mode) */
68
+ newRevisions: string[];
69
+ /** Conflicts encountered during processing */
70
+ conflicts: ConflictInfo[];
71
+ /** Processing log entries */
72
+ log: ProcessingLogEntry[];
73
+ }
74
+
75
+ /**
76
+ * Information about a conflict between an operation and a revision.
77
+ */
78
+ export interface ConflictInfo {
79
+ /** Description of the operation that conflicted */
80
+ operation: string;
81
+ /** ID of the conflicting revision */
82
+ revisionId: string;
83
+ /** How the conflict was resolved */
84
+ resolution: 'accepted' | 'skipped' | 'wrapped';
85
+ /** Location of the conflict */
86
+ location: ChangeLocation;
87
+ }
88
+
89
+ /**
90
+ * Log entry for processing actions.
91
+ */
92
+ export interface ProcessingLogEntry {
93
+ timestamp: Date;
94
+ action: string;
95
+ details: string;
96
+ revisionId?: string;
97
+ }
98
+
99
+ /**
100
+ * Handles document revisions before and during processing.
101
+ * Implements ECMA-376 compliant revision handling.
102
+ */
103
+ export class RevisionAwareProcessor {
104
+ /**
105
+ * Prepare document for processing by handling revisions.
106
+ * Call this before applying any document modifications.
107
+ *
108
+ * @param doc - Document to prepare
109
+ * @param options - Processing options
110
+ * @returns Processing result with details of actions taken
111
+ */
112
+ static async prepare(
113
+ doc: Document,
114
+ options: RevisionProcessingOptions
115
+ ): Promise<RevisionProcessingResult> {
116
+ const log: ProcessingLogEntry[] = [];
117
+ const acceptedRevisions: string[] = [];
118
+ const preservedRevisions: string[] = [];
119
+ const newRevisions: string[] = [];
120
+ const conflicts: ConflictInfo[] = [];
121
+
122
+ const addLog = (action: string, details: string, revisionId?: string) => {
123
+ log.push({
124
+ timestamp: new Date(),
125
+ action,
126
+ details,
127
+ revisionId,
128
+ });
129
+ };
130
+
131
+ addLog('start', `Starting revision processing with mode: ${options.mode}`);
132
+
133
+ try {
134
+ const revisionManager = doc.getRevisionManager();
135
+ const hasInMemoryRevisions = revisionManager?.hasRevisions() ?? false;
136
+ const hasRawXmlRevisions = doc.hasRawXmlRevisions();
137
+
138
+ // Check both in-memory model AND raw XML for revisions
139
+ // Raw XML may contain revisions that weren't fully parsed into memory
140
+ if (!hasInMemoryRevisions && !hasRawXmlRevisions) {
141
+ addLog('complete', 'No revisions found in document');
142
+ return {
143
+ success: true,
144
+ acceptedRevisions,
145
+ preservedRevisions,
146
+ newRevisions,
147
+ conflicts,
148
+ log,
149
+ };
150
+ }
151
+
152
+ const allRevisions = revisionManager?.getAllRevisions() ?? [];
153
+ const inMemoryCount = allRevisions.length;
154
+ const rawXmlNote = hasRawXmlRevisions ? ' (raw XML contains revision markup)' : '';
155
+ addLog('info', `Found ${inMemoryCount} parsed revisions in document${rawXmlNote}`);
156
+
157
+ switch (options.mode) {
158
+ case 'accept_all':
159
+ // Accept all revisions
160
+ await this.acceptAllRevisions(doc, allRevisions, acceptedRevisions, addLog);
161
+ break;
162
+
163
+ case 'preserve':
164
+ // Preserve all revisions by preventing document.xml regeneration
165
+ // This keeps the original XML with w:ins/w:del elements intact
166
+ for (const rev of allRevisions) {
167
+ preservedRevisions.push(rev.getId().toString());
168
+ }
169
+ doc.preserveRawXml();
170
+ addLog(
171
+ 'complete',
172
+ `Preserved ${preservedRevisions.length} revisions (raw XML preservation enabled)`
173
+ );
174
+ break;
175
+
176
+ case 'preserve_and_wrap':
177
+ // Preserve all revisions, track for later wrapping
178
+ // Also enable raw XML preservation to keep existing revisions
179
+ for (const rev of allRevisions) {
180
+ preservedRevisions.push(rev.getId().toString());
181
+ }
182
+ doc.preserveRawXml();
183
+ addLog(
184
+ 'info',
185
+ `Will preserve and wrap conflicts for ${preservedRevisions.length} revisions`
186
+ );
187
+ addLog('info', `New revisions will be authored by: ${options.author || 'Unknown'}`);
188
+ break;
189
+
190
+ default:
191
+ addLog('error', `Unknown processing mode: ${options.mode}`);
192
+ return {
193
+ success: false,
194
+ acceptedRevisions,
195
+ preservedRevisions,
196
+ newRevisions,
197
+ conflicts,
198
+ log,
199
+ };
200
+ }
201
+
202
+ addLog('complete', 'Revision processing completed successfully');
203
+
204
+ return {
205
+ success: true,
206
+ acceptedRevisions,
207
+ preservedRevisions,
208
+ newRevisions,
209
+ conflicts,
210
+ log,
211
+ };
212
+ } catch (error: unknown) {
213
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error';
214
+ addLog('error', `Processing failed: ${errorMessage}`);
215
+
216
+ return {
217
+ success: false,
218
+ acceptedRevisions,
219
+ preservedRevisions,
220
+ newRevisions,
221
+ conflicts,
222
+ log,
223
+ };
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Accept all revisions in the document using Document.acceptAllRevisions().
229
+ *
230
+ * This delegates to Document.acceptAllRevisions() which uses in-memory DOM transformation
231
+ * (the industry-standard approach used by OpenXML PowerTools, Aspose.Words, etc.):
232
+ *
233
+ * 1. Transforms Revision objects in paragraph.content arrays
234
+ * 2. Insertions (w:ins): Unwraps content - keeps Runs/Hyperlinks, removes Revision wrapper
235
+ * 3. Deletions (w:del): Removes content entirely from the model
236
+ * 4. Move operations: moveFrom removed, moveTo unwrapped
237
+ * 5. Property changes: Removes change metadata, keeps current formatting
238
+ *
239
+ * This approach allows subsequent modifications to be saved correctly because the
240
+ * in-memory model is transformed (not raw XML), so save() regenerates document.xml
241
+ * with both the accepted changes AND any subsequent modifications.
242
+ *
243
+ * @see https://github.com/OfficeDev/Open-Xml-PowerTools - RevisionAccepter.cs
244
+ * @see https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/ee836138
245
+ */
246
+ private static async acceptAllRevisions(
247
+ doc: Document,
248
+ revisions: Revision[],
249
+ acceptedIds: string[],
250
+ addLog: (action: string, details: string, revisionId?: string) => void
251
+ ): Promise<void> {
252
+ addLog('info', 'Accepting all revisions using in-memory DOM transformation');
253
+
254
+ // Track accepted revision IDs before acceptance (for logging)
255
+ for (const rev of revisions) {
256
+ acceptedIds.push(rev.getId().toString());
257
+ }
258
+
259
+ // Use Document.acceptAllRevisions() which uses in-memory transformation
260
+ // This allows subsequent modifications to be saved correctly
261
+ await doc.acceptAllRevisions();
262
+
263
+ // Log each accepted revision
264
+ for (const id of acceptedIds) {
265
+ addLog('accept', `Accepted revision ${id}`, id);
266
+ }
267
+
268
+ addLog('complete', `Accepted ${acceptedIds.length} revisions (in-memory transformation)`);
269
+ }
270
+
271
+ /**
272
+ * Accept revisions matching the given criteria.
273
+ * Uses existing RevisionAcceptor under the hood.
274
+ *
275
+ * @param doc - Document to process
276
+ * @param criteria - Selection criteria
277
+ * @returns Array of accepted revision IDs
278
+ */
279
+ static acceptSelective(doc: Document, criteria: SelectionCriteria): string[] {
280
+ const revisionManager = doc.getRevisionManager();
281
+ if (!revisionManager) {
282
+ return [];
283
+ }
284
+
285
+ const allRevisions = revisionManager.getAllRevisions();
286
+ const matchingRevisions = this.filterRevisions(allRevisions, criteria);
287
+
288
+ const acceptedIds: string[] = [];
289
+ for (const rev of matchingRevisions) {
290
+ acceptedIds.push(rev.getId().toString());
291
+ // Note: Actual acceptance would require XML manipulation
292
+ // For now, this returns which revisions WOULD be accepted
293
+ }
294
+
295
+ return acceptedIds;
296
+ }
297
+
298
+ /**
299
+ * Filter revisions based on criteria.
300
+ */
301
+ private static filterRevisions(revisions: Revision[], criteria: SelectionCriteria): Revision[] {
302
+ return revisions.filter((rev) => {
303
+ // Filter by IDs
304
+ if (criteria.ids && !criteria.ids.includes(rev.getId())) {
305
+ return false;
306
+ }
307
+
308
+ // Filter by types
309
+ if (criteria.types && !criteria.types.includes(rev.getType())) {
310
+ return false;
311
+ }
312
+
313
+ // Filter by authors
314
+ if (criteria.authors && !criteria.authors.includes(rev.getAuthor())) {
315
+ return false;
316
+ }
317
+
318
+ // Filter by date range
319
+ if (criteria.dateRange) {
320
+ const date = rev.getDate();
321
+ if (date < criteria.dateRange.start || date > criteria.dateRange.end) {
322
+ return false;
323
+ }
324
+ }
325
+
326
+ // Filter by categories
327
+ if (criteria.categories) {
328
+ const category = this.getRevisionCategory(rev);
329
+ if (!criteria.categories.includes(category)) {
330
+ return false;
331
+ }
332
+ }
333
+
334
+ // Custom filter
335
+ if (criteria.custom && !criteria.custom(rev)) {
336
+ return false;
337
+ }
338
+
339
+ return true;
340
+ });
341
+ }
342
+
343
+ /**
344
+ * Get the semantic category of a revision.
345
+ */
346
+ private static getRevisionCategory(revision: Revision): ChangeCategory {
347
+ const type = revision.getType();
348
+
349
+ if (type === 'insert' || type === 'delete') {
350
+ return 'content';
351
+ }
352
+ if (
353
+ type === 'runPropertiesChange' ||
354
+ type === 'paragraphPropertiesChange' ||
355
+ type === 'numberingChange'
356
+ ) {
357
+ return 'formatting';
358
+ }
359
+ if (type === 'moveFrom' || type === 'moveTo' || type === 'sectionPropertiesChange') {
360
+ return 'structural';
361
+ }
362
+ if (
363
+ type === 'tablePropertiesChange' ||
364
+ type === 'tableExceptionPropertiesChange' ||
365
+ type === 'tableRowPropertiesChange' ||
366
+ type === 'tableCellPropertiesChange' ||
367
+ type === 'tableCellInsert' ||
368
+ type === 'tableCellDelete' ||
369
+ type === 'tableCellMerge'
370
+ ) {
371
+ return 'table';
372
+ }
373
+
374
+ return 'content';
375
+ }
376
+
377
+ /**
378
+ * Check if an operation at the given location would conflict with any revision.
379
+ *
380
+ * @param doc - Document to check
381
+ * @param paragraphIndex - Paragraph index
382
+ * @param runIndex - Optional run index
383
+ * @returns Conflicting revision or null
384
+ */
385
+ static checkConflict(doc: Document, paragraphIndex: number, runIndex?: number): Revision | null {
386
+ const revisionManager = doc.getRevisionManager();
387
+ if (!revisionManager) {
388
+ return null;
389
+ }
390
+
391
+ // Get revisions that might affect this paragraph
392
+ const revisions = revisionManager.getRevisionsForParagraph(paragraphIndex);
393
+
394
+ // For now, return the first revision at this location
395
+ // In a more complete implementation, we would check run-level conflicts
396
+ return revisions.length > 0 ? revisions[0]! : null;
397
+ }
398
+
399
+ /**
400
+ * Get all revisions that would be affected by an operation.
401
+ *
402
+ * @param doc - Document to check
403
+ * @param paragraphIndex - Paragraph index
404
+ * @param runIndex - Optional run index
405
+ * @returns Array of affected revisions
406
+ */
407
+ static getAffectedRevisions(
408
+ doc: Document,
409
+ paragraphIndex: number,
410
+ runIndex?: number
411
+ ): Revision[] {
412
+ const revisionManager = doc.getRevisionManager();
413
+ if (!revisionManager) {
414
+ return [];
415
+ }
416
+
417
+ return revisionManager.getRevisionsForParagraph(paragraphIndex);
418
+ }
419
+
420
+ /**
421
+ * Wrap content in a new revision (for preserve_and_wrap mode).
422
+ * Creates a new revision tracking the change.
423
+ *
424
+ * @param doc - Document to modify
425
+ * @param paragraphIndex - Paragraph index
426
+ * @param runIndex - Run index
427
+ * @param author - Author for the new revision
428
+ * @param type - Type of revision to create
429
+ * @returns ID of the new revision
430
+ */
431
+ static wrapInRevision(
432
+ doc: Document,
433
+ paragraphIndex: number,
434
+ runIndex: number,
435
+ author: string,
436
+ type: 'insert' | 'delete'
437
+ ): string {
438
+ const revisionManager = doc.getRevisionManager();
439
+ if (!revisionManager) {
440
+ throw new Error('Document does not have a revision manager');
441
+ }
442
+
443
+ // Get the paragraphs from the document
444
+ const paragraphs = doc.getParagraphs();
445
+ if (paragraphIndex < 0 || paragraphIndex >= paragraphs.length) {
446
+ throw new Error(`Invalid paragraph index: ${paragraphIndex}`);
447
+ }
448
+
449
+ const paragraph = paragraphs[paragraphIndex];
450
+ if (!paragraph) {
451
+ throw new Error(`Paragraph at index ${paragraphIndex} not found`);
452
+ }
453
+
454
+ const runs = paragraph.getRuns();
455
+ if (runIndex < 0 || runIndex >= runs.length) {
456
+ throw new Error(`Invalid run index: ${runIndex}`);
457
+ }
458
+
459
+ const run = runs[runIndex];
460
+ if (!run) {
461
+ throw new Error(`Run at index ${runIndex} not found`);
462
+ }
463
+
464
+ // Create new revision
465
+ const revision = new Revision({
466
+ author,
467
+ type,
468
+ content: run,
469
+ date: new Date(),
470
+ });
471
+
472
+ // Register with manager
473
+ revisionManager.register(revision);
474
+
475
+ return revision.getId().toString();
476
+ }
477
+
478
+ /**
479
+ * Check if document has any tracked changes.
480
+ *
481
+ * @param doc - Document to check
482
+ * @returns True if document has tracked changes
483
+ */
484
+ static hasTrackedChanges(doc: Document): boolean {
485
+ const revisionManager = doc.getRevisionManager();
486
+ return revisionManager ? revisionManager.hasRevisions() : false;
487
+ }
488
+
489
+ /**
490
+ * Get summary of tracked changes in document.
491
+ *
492
+ * @param doc - Document to summarize
493
+ * @returns Summary object or null if no revision manager
494
+ */
495
+ static getTrackedChangesSummary(doc: Document): {
496
+ total: number;
497
+ insertions: number;
498
+ deletions: number;
499
+ formatting: number;
500
+ moves: number;
501
+ authors: string[];
502
+ } | null {
503
+ const revisionManager = doc.getRevisionManager();
504
+ if (!revisionManager) {
505
+ return null;
506
+ }
507
+
508
+ const summary = revisionManager.getSummary();
509
+ return {
510
+ total: summary.total,
511
+ insertions: summary.byType.insertions,
512
+ deletions: summary.byType.deletions,
513
+ formatting: summary.byType.propertyChanges,
514
+ moves: summary.byType.moves,
515
+ authors: summary.authors,
516
+ };
517
+ }
518
+ }