@ansonlai/docx-redline-js 0.1.3 → 0.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.
- package/AGENTS.md +91 -5
- package/ARCHITECTURE.md +62 -9
- package/README.md +94 -3
- package/core/types.js +35 -8
- package/core/word-xml.js +90 -0
- package/dist/docx-redline-js.esm.js +1149 -367
- package/dist/docx-redline-js.esm.js.map +4 -4
- package/dist/docx-redline-js.esm.min.js +78 -74
- package/dist/docx-redline-js.esm.min.js.map +4 -4
- package/docs/VALIDATION.md +48 -0
- package/docs/plans/2026-03-01-release-0.1.4-design.md +31 -0
- package/docs/plans/2026-03-01-release-0.1.4.md +108 -0
- package/engine/format-application.js +13 -14
- package/engine/format-span-application.js +7 -6
- package/engine/formatting-removal.js +15 -12
- package/engine/oxml-engine.js +146 -55
- package/engine/reconstruction-mapper.js +35 -8
- package/engine/reconstruction-mode.js +14 -13
- package/engine/reconstruction-writer.js +97 -78
- package/engine/rpr-helpers.js +34 -32
- package/engine/run-builders.js +150 -39
- package/engine/surgical-diff-application.js +216 -0
- package/engine/surgical-mode.js +84 -519
- package/engine/surgical-run-splitting.js +96 -0
- package/engine/surgical-spans.js +169 -0
- package/engine/table-cell-context.js +15 -13
- package/engine/table-mode.js +39 -35
- package/index.d.ts +148 -0
- package/index.js +26 -19
- package/package.json +8 -1
- package/pipeline/ingestion-export.js +1 -0
- package/pipeline/ingestion-paragraph.js +37 -12
- package/pipeline/ingestion-table.js +11 -8
- package/scripts/build.mjs +35 -0
- package/scripts/check-types.mjs +28 -0
- package/scripts/export-validation-fixtures.mjs +68 -0
- package/scripts/run-tests.mjs +43 -0
- package/scripts/word-com-smoke.ps1 +48 -0
- package/services/comment-locator.js +10 -9
- package/services/revision-comment-management.js +501 -0
- package/services/standalone-operation-runner.js +119 -69
- package/services/table-reconciliation.js +7 -8
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Revision/comment management utilities for OOXML payloads.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { NS_W } from '../core/types.js';
|
|
6
|
+
import { createParser, createSerializer } from '../adapters/xml-adapter.js';
|
|
7
|
+
import { createWordElement } from '../core/word-xml.js';
|
|
8
|
+
import { getXmlParseError } from '../core/xml-query.js';
|
|
9
|
+
|
|
10
|
+
function getAttributeByLocalName(node, localName) {
|
|
11
|
+
if (!node || !node.attributes) return '';
|
|
12
|
+
for (const attr of Array.from(node.attributes)) {
|
|
13
|
+
if ((attr.localName || '').toLowerCase() === localName.toLowerCase()) {
|
|
14
|
+
return String(attr.value || '');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return String(
|
|
18
|
+
node.getAttribute?.(`w:${localName}`)
|
|
19
|
+
|| node.getAttribute?.(localName)
|
|
20
|
+
|| ''
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function normalizeAuthor(author) {
|
|
25
|
+
return typeof author === 'string' ? author.trim().toLowerCase() : '';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isElement(node) {
|
|
29
|
+
return !!node && node.nodeType === 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isWordElement(node, localName) {
|
|
33
|
+
return isElement(node)
|
|
34
|
+
&& node.namespaceURI === NS_W
|
|
35
|
+
&& String(node.localName || '').toLowerCase() === localName.toLowerCase();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function getWordElementsByLocalName(xmlDoc, localName) {
|
|
39
|
+
return Array.from(xmlDoc.getElementsByTagNameNS(NS_W, localName));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function resolveAuthorFilter(options = {}) {
|
|
43
|
+
if (options?.allAuthors === true) {
|
|
44
|
+
return { valid: true, allAuthors: true, normalizedAuthor: '' };
|
|
45
|
+
}
|
|
46
|
+
const normalizedAuthor = normalizeAuthor(options?.author);
|
|
47
|
+
if (!normalizedAuthor) {
|
|
48
|
+
return {
|
|
49
|
+
valid: false,
|
|
50
|
+
allAuthors: false,
|
|
51
|
+
normalizedAuthor: '',
|
|
52
|
+
warning: 'No author provided. Pass { author } or set { allAuthors: true }.'
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return { valid: true, allAuthors: false, normalizedAuthor };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function authorMatchesNode(node, filter) {
|
|
59
|
+
if (filter.allAuthors) return true;
|
|
60
|
+
const nodeAuthor = normalizeAuthor(getAttributeByLocalName(node, 'author'));
|
|
61
|
+
return !!nodeAuthor && nodeAuthor === filter.normalizedAuthor;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function parseXmlWithWarnings(oxml, parseFailurePrefix) {
|
|
65
|
+
const parser = createParser();
|
|
66
|
+
const xmlDoc = parser.parseFromString(oxml, 'application/xml');
|
|
67
|
+
const parseError = getXmlParseError(xmlDoc);
|
|
68
|
+
if (parseError) {
|
|
69
|
+
return {
|
|
70
|
+
xmlDoc: null,
|
|
71
|
+
serializer: null,
|
|
72
|
+
warning: `${parseFailurePrefix}: ${parseError.textContent || 'parse error'}`
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return { xmlDoc, serializer: createSerializer(), warning: null };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function removeNode(node) {
|
|
79
|
+
if (node?.parentNode) {
|
|
80
|
+
node.parentNode.removeChild(node);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function unwrapNode(node) {
|
|
87
|
+
const parent = node?.parentNode;
|
|
88
|
+
if (!parent) return false;
|
|
89
|
+
while (node.firstChild) {
|
|
90
|
+
parent.insertBefore(node.firstChild, node);
|
|
91
|
+
}
|
|
92
|
+
parent.removeChild(node);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isTableRowRevisionMarker(node) {
|
|
97
|
+
const parent = node?.parentNode;
|
|
98
|
+
return isWordElement(parent, 'trPr') && isWordElement(parent?.parentNode, 'tr');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function isParagraphMarkRevisionMarker(node) {
|
|
102
|
+
const rPr = node?.parentNode;
|
|
103
|
+
const pPr = rPr?.parentNode;
|
|
104
|
+
const paragraph = pPr?.parentNode;
|
|
105
|
+
return isWordElement(rPr, 'rPr') && isWordElement(pPr, 'pPr') && isWordElement(paragraph, 'p');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function getContainingParagraphMarkRevision(node) {
|
|
109
|
+
return isParagraphMarkRevisionMarker(node) ? node.parentNode.parentNode.parentNode : null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function getNextWordParagraph(paragraph) {
|
|
113
|
+
let cursor = paragraph?.nextSibling || null;
|
|
114
|
+
while (cursor) {
|
|
115
|
+
if (isWordElement(cursor, 'p')) return cursor;
|
|
116
|
+
cursor = cursor.nextSibling;
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function mergeParagraphIntoNextAndRemove(paragraph) {
|
|
122
|
+
if (!paragraph?.parentNode) return false;
|
|
123
|
+
const nextParagraph = getNextWordParagraph(paragraph);
|
|
124
|
+
if (!nextParagraph) {
|
|
125
|
+
return removeNode(paragraph);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const childrenToMove = Array.from(paragraph.childNodes || []).filter(child => !isWordElement(child, 'pPr'));
|
|
129
|
+
const insertionPoint = nextParagraph.firstChild || null;
|
|
130
|
+
for (const child of childrenToMove) {
|
|
131
|
+
nextParagraph.insertBefore(child, insertionPoint);
|
|
132
|
+
}
|
|
133
|
+
return removeNode(paragraph);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Accepts tracked changes (`w:ins`, `w:del`, and *PrChange tags) for one author
|
|
138
|
+
* or all authors in the provided OOXML payload.
|
|
139
|
+
*
|
|
140
|
+
* @param {string} oxml
|
|
141
|
+
* @param {{ author?: string, allAuthors?: boolean }} [options]
|
|
142
|
+
* @returns {{ oxml: string, hasChanges: boolean, acceptedCount: number, warnings: string[] }}
|
|
143
|
+
*/
|
|
144
|
+
export function acceptTrackedChangesInOoxml(oxml, options = {}) {
|
|
145
|
+
const warnings = [];
|
|
146
|
+
const filter = resolveAuthorFilter(options);
|
|
147
|
+
if (!filter.valid) {
|
|
148
|
+
return { oxml, hasChanges: false, acceptedCount: 0, warnings: [filter.warning] };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const parseResult = parseXmlWithWarnings(oxml, 'Failed to parse OOXML');
|
|
152
|
+
if (!parseResult.xmlDoc) {
|
|
153
|
+
return { oxml, hasChanges: false, acceptedCount: 0, warnings: [parseResult.warning] };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const { xmlDoc, serializer } = parseResult;
|
|
157
|
+
let acceptedCount = 0;
|
|
158
|
+
|
|
159
|
+
for (const insNode of getWordElementsByLocalName(xmlDoc, 'ins')) {
|
|
160
|
+
if (!insNode.parentNode || !authorMatchesNode(insNode, filter)) continue;
|
|
161
|
+
if (isParagraphMarkRevisionMarker(insNode)) {
|
|
162
|
+
if (removeNode(insNode)) acceptedCount += 1;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (isTableRowRevisionMarker(insNode)) {
|
|
166
|
+
if (removeNode(insNode)) acceptedCount += 1;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (unwrapNode(insNode)) acceptedCount += 1;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
for (const delNode of getWordElementsByLocalName(xmlDoc, 'del')) {
|
|
173
|
+
if (!delNode.parentNode || !authorMatchesNode(delNode, filter)) continue;
|
|
174
|
+
const paragraphMark = getContainingParagraphMarkRevision(delNode);
|
|
175
|
+
if (paragraphMark) {
|
|
176
|
+
if (mergeParagraphIntoNextAndRemove(paragraphMark)) acceptedCount += 1;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (isTableRowRevisionMarker(delNode)) {
|
|
180
|
+
const rowNode = delNode.parentNode?.parentNode;
|
|
181
|
+
if (removeNode(rowNode)) acceptedCount += 1;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (removeNode(delNode)) acceptedCount += 1;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
for (const moveFromNode of getWordElementsByLocalName(xmlDoc, 'moveFrom')) {
|
|
188
|
+
if (!moveFromNode.parentNode || !authorMatchesNode(moveFromNode, filter)) continue;
|
|
189
|
+
if (removeNode(moveFromNode)) acceptedCount += 1;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
for (const moveToNode of getWordElementsByLocalName(xmlDoc, 'moveTo')) {
|
|
193
|
+
if (!moveToNode.parentNode || !authorMatchesNode(moveToNode, filter)) continue;
|
|
194
|
+
if (unwrapNode(moveToNode)) acceptedCount += 1;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
acceptedCount += removeMoveRangeMarkers(xmlDoc, filter);
|
|
198
|
+
|
|
199
|
+
const changeTags = ['rPrChange', 'pPrChange', 'tblPrChange', 'trPrChange', 'tcPrChange'];
|
|
200
|
+
for (const localName of changeTags) {
|
|
201
|
+
for (const changeNode of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
202
|
+
if (!changeNode.parentNode || !authorMatchesNode(changeNode, filter)) continue;
|
|
203
|
+
if (removeNode(changeNode)) acceptedCount += 1;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
oxml: serializer.serializeToString(xmlDoc),
|
|
209
|
+
hasChanges: acceptedCount > 0,
|
|
210
|
+
acceptedCount,
|
|
211
|
+
warnings
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function convertDeletionTextNodes(xmlDoc, delNode) {
|
|
216
|
+
for (const delTextNode of Array.from(delNode.getElementsByTagNameNS(NS_W, 'delText'))) {
|
|
217
|
+
const normalText = createWordElement(xmlDoc, 'w:t');
|
|
218
|
+
const spaceValue = delTextNode.getAttribute('xml:space');
|
|
219
|
+
if (spaceValue) {
|
|
220
|
+
normalText.setAttribute('xml:space', spaceValue);
|
|
221
|
+
}
|
|
222
|
+
while (delTextNode.firstChild) {
|
|
223
|
+
normalText.appendChild(delTextNode.firstChild);
|
|
224
|
+
}
|
|
225
|
+
delTextNode.parentNode?.replaceChild(normalText, delTextNode);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function rejectPropertyChangeNode(changeNode, localName) {
|
|
230
|
+
const parent = changeNode?.parentNode;
|
|
231
|
+
if (!parent) return false;
|
|
232
|
+
|
|
233
|
+
const baseLocalName = localName.endsWith('Change')
|
|
234
|
+
? localName.slice(0, -'Change'.length)
|
|
235
|
+
: '';
|
|
236
|
+
|
|
237
|
+
if (
|
|
238
|
+
!baseLocalName
|
|
239
|
+
|| String(parent.localName || '').toLowerCase() !== baseLocalName.toLowerCase()
|
|
240
|
+
|| parent.namespaceURI !== NS_W
|
|
241
|
+
) {
|
|
242
|
+
return removeNode(changeNode);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const historicalNode = Array.from(changeNode.childNodes || []).find(
|
|
246
|
+
child => child.nodeType === 1 && child.namespaceURI === NS_W
|
|
247
|
+
&& String(child.localName || '').toLowerCase() === baseLocalName.toLowerCase()
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
if (!historicalNode) {
|
|
251
|
+
return removeNode(changeNode);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const toAppend = Array.from(historicalNode.childNodes || []);
|
|
255
|
+
while (parent.firstChild) {
|
|
256
|
+
parent.removeChild(parent.firstChild);
|
|
257
|
+
}
|
|
258
|
+
for (const node of toAppend) {
|
|
259
|
+
const clone = xmlDocImportNode(parent.ownerDocument, node);
|
|
260
|
+
parent.appendChild(clone);
|
|
261
|
+
}
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function xmlDocImportNode(xmlDoc, node) {
|
|
266
|
+
if (xmlDoc && typeof xmlDoc.importNode === 'function') {
|
|
267
|
+
return xmlDoc.importNode(node, true);
|
|
268
|
+
}
|
|
269
|
+
return node.cloneNode(true);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function collectMoveRangeStartIds(xmlDoc, localName, filter) {
|
|
273
|
+
const ids = new Set();
|
|
274
|
+
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
275
|
+
if (!authorMatchesNode(node, filter)) continue;
|
|
276
|
+
const id = getAttributeByLocalName(node, 'id');
|
|
277
|
+
if (id) ids.add(id);
|
|
278
|
+
}
|
|
279
|
+
return ids;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function removeMoveRangeMarkers(xmlDoc, filter) {
|
|
283
|
+
let removed = 0;
|
|
284
|
+
const moveFromIds = collectMoveRangeStartIds(xmlDoc, 'moveFromRangeStart', filter);
|
|
285
|
+
const moveToIds = collectMoveRangeStartIds(xmlDoc, 'moveToRangeStart', filter);
|
|
286
|
+
const markerSpecs = [
|
|
287
|
+
['moveFromRangeStart', moveFromIds, true],
|
|
288
|
+
['moveFromRangeEnd', moveFromIds, false],
|
|
289
|
+
['moveToRangeStart', moveToIds, true],
|
|
290
|
+
['moveToRangeEnd', moveToIds, false]
|
|
291
|
+
];
|
|
292
|
+
|
|
293
|
+
for (const [localName, ids, isStart] of markerSpecs) {
|
|
294
|
+
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
295
|
+
if (!node.parentNode) continue;
|
|
296
|
+
const id = getAttributeByLocalName(node, 'id');
|
|
297
|
+
if (!id) continue;
|
|
298
|
+
if (filter.allAuthors || ids.has(id) || (isStart && authorMatchesNode(node, filter))) {
|
|
299
|
+
if (removeNode(node)) removed += 1;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return removed;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Rejects tracked changes (`w:ins`, `w:del`, and *PrChange tags) for one author
|
|
309
|
+
* or all authors in the provided OOXML payload.
|
|
310
|
+
*
|
|
311
|
+
* @param {string} oxml
|
|
312
|
+
* @param {{ author?: string, allAuthors?: boolean }} [options]
|
|
313
|
+
* @returns {{ oxml: string, hasChanges: boolean, rejectedCount: number, warnings: string[] }}
|
|
314
|
+
*/
|
|
315
|
+
export function rejectTrackedChangesInOoxml(oxml, options = {}) {
|
|
316
|
+
const warnings = [];
|
|
317
|
+
const filter = resolveAuthorFilter(options);
|
|
318
|
+
if (!filter.valid) {
|
|
319
|
+
return { oxml, hasChanges: false, rejectedCount: 0, warnings: [filter.warning] };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const parseResult = parseXmlWithWarnings(oxml, 'Failed to parse OOXML');
|
|
323
|
+
if (!parseResult.xmlDoc) {
|
|
324
|
+
return { oxml, hasChanges: false, rejectedCount: 0, warnings: [parseResult.warning] };
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const { xmlDoc, serializer } = parseResult;
|
|
328
|
+
let rejectedCount = 0;
|
|
329
|
+
|
|
330
|
+
for (const insNode of getWordElementsByLocalName(xmlDoc, 'ins')) {
|
|
331
|
+
if (!insNode.parentNode || !authorMatchesNode(insNode, filter)) continue;
|
|
332
|
+
const paragraphMark = getContainingParagraphMarkRevision(insNode);
|
|
333
|
+
if (paragraphMark) {
|
|
334
|
+
if (mergeParagraphIntoNextAndRemove(paragraphMark)) rejectedCount += 1;
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (isTableRowRevisionMarker(insNode)) {
|
|
338
|
+
const rowNode = insNode.parentNode?.parentNode;
|
|
339
|
+
if (removeNode(rowNode)) rejectedCount += 1;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (removeNode(insNode)) rejectedCount += 1;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
for (const delNode of getWordElementsByLocalName(xmlDoc, 'del')) {
|
|
346
|
+
if (!delNode.parentNode || !authorMatchesNode(delNode, filter)) continue;
|
|
347
|
+
if (isParagraphMarkRevisionMarker(delNode)) {
|
|
348
|
+
if (removeNode(delNode)) rejectedCount += 1;
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
if (isTableRowRevisionMarker(delNode)) {
|
|
352
|
+
if (removeNode(delNode)) rejectedCount += 1;
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
convertDeletionTextNodes(xmlDoc, delNode);
|
|
356
|
+
if (unwrapNode(delNode)) rejectedCount += 1;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
for (const moveFromNode of getWordElementsByLocalName(xmlDoc, 'moveFrom')) {
|
|
360
|
+
if (!moveFromNode.parentNode || !authorMatchesNode(moveFromNode, filter)) continue;
|
|
361
|
+
convertDeletionTextNodes(xmlDoc, moveFromNode);
|
|
362
|
+
if (unwrapNode(moveFromNode)) rejectedCount += 1;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
for (const moveToNode of getWordElementsByLocalName(xmlDoc, 'moveTo')) {
|
|
366
|
+
if (!moveToNode.parentNode || !authorMatchesNode(moveToNode, filter)) continue;
|
|
367
|
+
if (removeNode(moveToNode)) rejectedCount += 1;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
rejectedCount += removeMoveRangeMarkers(xmlDoc, filter);
|
|
371
|
+
|
|
372
|
+
const changeTags = ['rPrChange', 'pPrChange', 'tblPrChange', 'trPrChange', 'tcPrChange'];
|
|
373
|
+
for (const localName of changeTags) {
|
|
374
|
+
for (const changeNode of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
375
|
+
if (!changeNode.parentNode || !authorMatchesNode(changeNode, filter)) continue;
|
|
376
|
+
if (rejectPropertyChangeNode(changeNode, localName)) rejectedCount += 1;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return {
|
|
381
|
+
oxml: serializer.serializeToString(xmlDoc),
|
|
382
|
+
hasChanges: rejectedCount > 0,
|
|
383
|
+
rejectedCount,
|
|
384
|
+
warnings
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function collectCommentTargetIds(xmlDoc, filter) {
|
|
389
|
+
const targetIds = new Set();
|
|
390
|
+
const commentNodes = getWordElementsByLocalName(xmlDoc, 'comment');
|
|
391
|
+
|
|
392
|
+
for (const commentNode of commentNodes) {
|
|
393
|
+
if (!authorMatchesNode(commentNode, filter)) continue;
|
|
394
|
+
const id = getAttributeByLocalName(commentNode, 'id');
|
|
395
|
+
if (id) targetIds.add(id);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return { targetIds, commentNodes };
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function removeCommentNodesById(commentNodes, targetIds) {
|
|
402
|
+
let removed = 0;
|
|
403
|
+
for (const commentNode of commentNodes) {
|
|
404
|
+
const id = getAttributeByLocalName(commentNode, 'id');
|
|
405
|
+
if (!id || !targetIds.has(id)) continue;
|
|
406
|
+
if (removeNode(commentNode)) removed += 1;
|
|
407
|
+
}
|
|
408
|
+
return removed;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function runIsOnlyCommentReference(runNode) {
|
|
412
|
+
if (!isWordElement(runNode, 'r')) return false;
|
|
413
|
+
const meaningfulChildren = Array.from(runNode.childNodes || []).filter(child => {
|
|
414
|
+
if (child.nodeType === 3) return String(child.nodeValue || '').trim().length > 0;
|
|
415
|
+
if (child.nodeType !== 1) return false;
|
|
416
|
+
if (child.namespaceURI !== NS_W) return true;
|
|
417
|
+
const local = String(child.localName || '').toLowerCase();
|
|
418
|
+
return local !== 'rpr' && local !== 'commentreference';
|
|
419
|
+
});
|
|
420
|
+
return meaningfulChildren.length === 0;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function removeCommentAnchors(xmlDoc, targetIds) {
|
|
424
|
+
let removed = 0;
|
|
425
|
+
const anchorTags = ['commentRangeStart', 'commentRangeEnd', 'commentReference'];
|
|
426
|
+
|
|
427
|
+
for (const localName of anchorTags) {
|
|
428
|
+
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
429
|
+
if (!node.parentNode) continue;
|
|
430
|
+
const id = getAttributeByLocalName(node, 'id');
|
|
431
|
+
if (!id || !targetIds.has(id)) continue;
|
|
432
|
+
|
|
433
|
+
if (localName === 'commentReference' && runIsOnlyCommentReference(node.parentNode)) {
|
|
434
|
+
if (removeNode(node.parentNode)) {
|
|
435
|
+
removed += 1;
|
|
436
|
+
}
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (removeNode(node)) {
|
|
440
|
+
removed += 1;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return removed;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Deletes comments authored by one user (or all users) and removes matching
|
|
449
|
+
* comment anchors/references from the OOXML payload.
|
|
450
|
+
*
|
|
451
|
+
* @param {string} oxml
|
|
452
|
+
* @param {{ author?: string, allAuthors?: boolean }} [options]
|
|
453
|
+
* @returns {{ oxml: string, hasChanges: boolean, commentsRemoved: number, referencesRemoved: number, warnings: string[] }}
|
|
454
|
+
*/
|
|
455
|
+
export function deleteCommentsByAuthorInOoxml(oxml, options = {}) {
|
|
456
|
+
const warnings = [];
|
|
457
|
+
const filter = resolveAuthorFilter(options);
|
|
458
|
+
if (!filter.valid) {
|
|
459
|
+
return {
|
|
460
|
+
oxml,
|
|
461
|
+
hasChanges: false,
|
|
462
|
+
commentsRemoved: 0,
|
|
463
|
+
referencesRemoved: 0,
|
|
464
|
+
warnings: [filter.warning]
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const parseResult = parseXmlWithWarnings(oxml, 'Failed to parse OOXML');
|
|
469
|
+
if (!parseResult.xmlDoc) {
|
|
470
|
+
return {
|
|
471
|
+
oxml,
|
|
472
|
+
hasChanges: false,
|
|
473
|
+
commentsRemoved: 0,
|
|
474
|
+
referencesRemoved: 0,
|
|
475
|
+
warnings: [parseResult.warning]
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const { xmlDoc, serializer } = parseResult;
|
|
480
|
+
const { targetIds, commentNodes } = collectCommentTargetIds(xmlDoc, filter);
|
|
481
|
+
|
|
482
|
+
if (filter.allAuthors) {
|
|
483
|
+
for (const localName of ['commentRangeStart', 'commentRangeEnd', 'commentReference']) {
|
|
484
|
+
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
485
|
+
const id = getAttributeByLocalName(node, 'id');
|
|
486
|
+
if (id) targetIds.add(id);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const commentsRemoved = removeCommentNodesById(commentNodes, targetIds);
|
|
492
|
+
const referencesRemoved = removeCommentAnchors(xmlDoc, targetIds);
|
|
493
|
+
|
|
494
|
+
return {
|
|
495
|
+
oxml: serializer.serializeToString(xmlDoc),
|
|
496
|
+
hasChanges: commentsRemoved > 0 || referencesRemoved > 0,
|
|
497
|
+
commentsRemoved,
|
|
498
|
+
referencesRemoved,
|
|
499
|
+
warnings
|
|
500
|
+
};
|
|
501
|
+
}
|