@ansonlai/docx-redline-js 0.8.2 → 0.8.4
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/CHANGELOG.md +75 -1
- package/README.md +28 -13
- package/core/list-targeting.js +12 -10
- package/dist/docx-redline-js.esm.js +837 -549
- package/dist/docx-redline-js.esm.js.map +4 -4
- package/dist/docx-redline-js.esm.min.js +93 -90
- package/dist/docx-redline-js.esm.min.js.map +4 -4
- package/dist/docx-redline.bundle.cjs +837 -549
- package/dist/docx-redline.bundle.cjs.map +4 -4
- package/dist/docx-redline.bundle.js +837 -549
- package/dist/docx-redline.bundle.js.map +4 -4
- package/docs/AGENT_KNOWLEDGE_BASE.md +4 -1
- package/docs/schemas/document-operations.schema.json +168 -168
- package/engine/oxml-engine.js +215 -195
- package/engine/reconstruction-mode.js +183 -183
- package/index.d.ts +4 -0
- package/index.js +36 -1
- package/node/cli.js +2 -1
- package/orchestration/list-structural-fallback.js +530 -518
- package/package.json +1 -1
- package/pipeline/list-generation.js +203 -168
- package/pipeline/serialization.js +111 -94
- package/services/batch-operation-orchestrator.js +6 -0
- package/services/document-inspection.js +10 -7
- package/services/document-operation-applier.js +2 -0
- package/services/document-operation-contract.js +553 -537
- package/services/document-operation-mutations.js +1140 -1011
- package/services/numbering-helpers.js +108 -0
- package/services/operation-batch-compiler.js +440 -436
- package/services/operation-heuristics.js +197 -173
- package/services/revision-comment-management.js +601 -594
- package/services/standalone-operation-runner.d.ts +429 -427
- package/services/table-reconciliation.js +48 -46
|
@@ -1,594 +1,601 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Revision/comment management utilities for OOXML payloads.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { NS_W } from '../core/types.js';
|
|
6
|
-
import { createSerializer, parseOoxmlSafe } 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
|
-
let rawOxml = typeof oxml === 'string' ? oxml.replace(/^\uFEFF/, '').trim() : '';
|
|
66
|
-
let isFragmentWrapped = false;
|
|
67
|
-
let parsed = parseOoxmlSafe(rawOxml, 'application/xml');
|
|
68
|
-
if (parsed.error && (parsed.error.message.includes('HierarchyRequestError') || parsed.error.message.includes('Only one element'))) {
|
|
69
|
-
const wrapped = `<w:body xmlns:w="${NS_W}">${rawOxml}</w:body>`;
|
|
70
|
-
const wrappedParsed = parseOoxmlSafe(wrapped, 'application/xml');
|
|
71
|
-
if (!wrappedParsed.error) {
|
|
72
|
-
parsed = wrappedParsed;
|
|
73
|
-
isFragmentWrapped = true;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
const parseError = parsed.doc ? getXmlParseError(parsed.doc) : null;
|
|
77
|
-
if (parsed.error || parseError) {
|
|
78
|
-
const message = parsed.error?.message || parseError?.textContent || 'parse error';
|
|
79
|
-
return {
|
|
80
|
-
xmlDoc: null,
|
|
81
|
-
serializer: null,
|
|
82
|
-
isFragmentWrapped: false,
|
|
83
|
-
warning: `${parseFailurePrefix}: ${message}`,
|
|
84
|
-
warnings: parsed.warnings,
|
|
85
|
-
error: { code: 'PARSE_ERROR', message }
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return {
|
|
89
|
-
xmlDoc: parsed.doc,
|
|
90
|
-
serializer: createSerializer(),
|
|
91
|
-
isFragmentWrapped,
|
|
92
|
-
warning: null,
|
|
93
|
-
warnings: parsed.warnings,
|
|
94
|
-
error: null
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function removeNode(node) {
|
|
99
|
-
if (node?.parentNode) {
|
|
100
|
-
node.parentNode.removeChild(node);
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function unwrapNode(node) {
|
|
107
|
-
const parent = node?.parentNode;
|
|
108
|
-
if (!parent) return false;
|
|
109
|
-
while (node.firstChild) {
|
|
110
|
-
parent.insertBefore(node.firstChild, node);
|
|
111
|
-
}
|
|
112
|
-
parent.removeChild(node);
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function nextElementSibling(node) {
|
|
117
|
-
let sibling = node?.nextSibling || null;
|
|
118
|
-
while (sibling && sibling.nodeType !== 1) sibling = sibling.nextSibling;
|
|
119
|
-
return sibling;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function revisionMetadataWithoutId(node) {
|
|
123
|
-
return Array.from(node?.attributes || [])
|
|
124
|
-
.filter(attribute => String(attribute.localName || attribute.name || '').toLowerCase() !== 'id')
|
|
125
|
-
.map(attribute => `${attribute.namespaceURI || ''}|${attribute.localName || attribute.name}=${attribute.value}`)
|
|
126
|
-
.sort()
|
|
127
|
-
.join('\n');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function coalesceAdjacentCompatibleInsertions(xmlDoc) {
|
|
131
|
-
let coalesced = 0;
|
|
132
|
-
const insertions = getWordElementsByLocalName(xmlDoc, 'ins');
|
|
133
|
-
|
|
134
|
-
for (const insertion of insertions) {
|
|
135
|
-
if (!insertion.parentNode || isParagraphMarkRevisionMarker(insertion)) continue;
|
|
136
|
-
let sibling = nextElementSibling(insertion);
|
|
137
|
-
while (
|
|
138
|
-
isWordElement(sibling, 'ins')
|
|
139
|
-
&& normalizeAuthor(getAttributeByLocalName(sibling, 'author'))
|
|
140
|
-
=== normalizeAuthor(getAttributeByLocalName(insertion, 'author'))
|
|
141
|
-
&& revisionMetadataWithoutId(sibling) === revisionMetadataWithoutId(insertion)
|
|
142
|
-
) {
|
|
143
|
-
const next = nextElementSibling(sibling);
|
|
144
|
-
while (sibling.firstChild) insertion.appendChild(sibling.firstChild);
|
|
145
|
-
sibling.parentNode?.removeChild(sibling);
|
|
146
|
-
coalesced += 1;
|
|
147
|
-
sibling = next;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return coalesced;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function isTableRowRevisionMarker(node) {
|
|
155
|
-
const parent = node?.parentNode;
|
|
156
|
-
return isWordElement(parent, 'trPr') && isWordElement(parent?.parentNode, 'tr');
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function isParagraphMarkRevisionMarker(node) {
|
|
160
|
-
const rPr = node?.parentNode;
|
|
161
|
-
const pPr = rPr?.parentNode;
|
|
162
|
-
const paragraph = pPr?.parentNode;
|
|
163
|
-
return isWordElement(rPr, 'rPr') && isWordElement(pPr, 'pPr') && isWordElement(paragraph, 'p');
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function getContainingParagraphMarkRevision(node) {
|
|
167
|
-
return isParagraphMarkRevisionMarker(node) ? node.parentNode.parentNode.parentNode : null;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function getNextWordParagraph(paragraph) {
|
|
171
|
-
let cursor = paragraph?.nextSibling || null;
|
|
172
|
-
while (cursor) {
|
|
173
|
-
if (isWordElement(cursor, 'p')) return cursor;
|
|
174
|
-
cursor = cursor.nextSibling;
|
|
175
|
-
}
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function mergeParagraphIntoNextAndRemove(paragraph) {
|
|
180
|
-
if (!paragraph?.parentNode) return false;
|
|
181
|
-
const nextParagraph = getNextWordParagraph(paragraph);
|
|
182
|
-
if (!nextParagraph) {
|
|
183
|
-
if (isWordElement(paragraph.parentNode, 'tc') && getWordElementsByLocalName(paragraph.parentNode, 'p').length <= 1) {
|
|
184
|
-
return false;
|
|
185
|
-
}
|
|
186
|
-
return removeNode(paragraph);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const childrenToMove = Array.from(paragraph.childNodes || []).filter(child => !isWordElement(child, 'pPr'));
|
|
190
|
-
let insertionPoint = nextParagraph.firstChild || null;
|
|
191
|
-
if (isWordElement(insertionPoint, 'pPr')) {
|
|
192
|
-
insertionPoint = insertionPoint.nextSibling || null;
|
|
193
|
-
}
|
|
194
|
-
for (const child of childrenToMove) {
|
|
195
|
-
nextParagraph.insertBefore(child, insertionPoint);
|
|
196
|
-
}
|
|
197
|
-
return removeNode(paragraph);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Accepts tracked changes (`w:ins`, `w:del`, and *PrChange tags) for one author
|
|
202
|
-
* or all authors in the provided OOXML payload.
|
|
203
|
-
*
|
|
204
|
-
* @param {string} oxml
|
|
205
|
-
* @param {{ author?: string, allAuthors?: boolean }} [options]
|
|
206
|
-
* @returns {{ oxml: string, hasChanges: boolean, acceptedCount: number, warnings: string[] }}
|
|
207
|
-
*/
|
|
208
|
-
export function acceptTrackedChangesInOoxml(oxml, options = {}) {
|
|
209
|
-
const warnings = [];
|
|
210
|
-
const filter = resolveAuthorFilter(options);
|
|
211
|
-
if (!filter.valid) {
|
|
212
|
-
return { oxml, hasChanges: false, acceptedCount: 0, warnings: [filter.warning] };
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const parseResult = parseXmlWithWarnings(oxml, 'Failed to parse OOXML');
|
|
216
|
-
if (!parseResult.xmlDoc) {
|
|
217
|
-
return {
|
|
218
|
-
oxml,
|
|
219
|
-
hasChanges: false,
|
|
220
|
-
acceptedCount: 0,
|
|
221
|
-
status: 'error',
|
|
222
|
-
error: parseResult.error,
|
|
223
|
-
warnings: [...(parseResult.warnings || []), parseResult.warning]
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const { xmlDoc, serializer } = parseResult;
|
|
228
|
-
warnings.push(...(parseResult.warnings || []));
|
|
229
|
-
let acceptedCount = 0;
|
|
230
|
-
|
|
231
|
-
for (const insNode of getWordElementsByLocalName(xmlDoc, 'ins')) {
|
|
232
|
-
if (!insNode.parentNode || !authorMatchesNode(insNode, filter)) continue;
|
|
233
|
-
if (isParagraphMarkRevisionMarker(insNode)) {
|
|
234
|
-
if (removeNode(insNode)) acceptedCount += 1;
|
|
235
|
-
continue;
|
|
236
|
-
}
|
|
237
|
-
if (isTableRowRevisionMarker(insNode)) {
|
|
238
|
-
if (removeNode(insNode)) acceptedCount += 1;
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
if (unwrapNode(insNode)) acceptedCount += 1;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
for (const delNode of getWordElementsByLocalName(xmlDoc, 'del')) {
|
|
245
|
-
if (!delNode.parentNode || !authorMatchesNode(delNode, filter)) continue;
|
|
246
|
-
const paragraphMark = getContainingParagraphMarkRevision(delNode);
|
|
247
|
-
if (paragraphMark) {
|
|
248
|
-
if (mergeParagraphIntoNextAndRemove(paragraphMark)) acceptedCount += 1;
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
if (isTableRowRevisionMarker(delNode)) {
|
|
252
|
-
const rowNode = delNode.parentNode?.parentNode;
|
|
253
|
-
if (removeNode(rowNode)) acceptedCount += 1;
|
|
254
|
-
continue;
|
|
255
|
-
}
|
|
256
|
-
if (removeNode(delNode)) acceptedCount += 1;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
for (const moveFromNode of getWordElementsByLocalName(xmlDoc, 'moveFrom')) {
|
|
260
|
-
if (!moveFromNode.parentNode || !authorMatchesNode(moveFromNode, filter)) continue;
|
|
261
|
-
if (removeNode(moveFromNode)) acceptedCount += 1;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
for (const moveToNode of getWordElementsByLocalName(xmlDoc, 'moveTo')) {
|
|
265
|
-
if (!moveToNode.parentNode || !authorMatchesNode(moveToNode, filter)) continue;
|
|
266
|
-
if (unwrapNode(moveToNode)) acceptedCount += 1;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
acceptedCount += removeMoveRangeMarkers(xmlDoc, filter);
|
|
270
|
-
|
|
271
|
-
const changeTags = ['rPrChange', 'pPrChange', 'tblPrChange', 'trPrChange', 'tcPrChange'];
|
|
272
|
-
for (const localName of changeTags) {
|
|
273
|
-
for (const changeNode of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
274
|
-
if (!changeNode.parentNode || !authorMatchesNode(changeNode, filter)) continue;
|
|
275
|
-
if (removeNode(changeNode)) acceptedCount += 1;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
const serializedOxml = parseResult.isFragmentWrapped
|
|
280
|
-
? Array.from(xmlDoc.documentElement.childNodes).map(n => serializer.serializeToString(n)).join('')
|
|
281
|
-
: serializer.serializeToString(xmlDoc);
|
|
282
|
-
|
|
283
|
-
return {
|
|
284
|
-
oxml: serializedOxml,
|
|
285
|
-
hasChanges: acceptedCount > 0,
|
|
286
|
-
acceptedCount,
|
|
287
|
-
warnings
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
function convertDeletionTextNodes(xmlDoc, delNode) {
|
|
292
|
-
for (const delTextNode of Array.from(delNode.getElementsByTagNameNS(NS_W, 'delText'))) {
|
|
293
|
-
const normalText = createWordElement(xmlDoc, 'w:t');
|
|
294
|
-
const spaceValue = delTextNode.getAttribute('xml:space');
|
|
295
|
-
if (spaceValue) {
|
|
296
|
-
normalText.setAttribute('xml:space', spaceValue);
|
|
297
|
-
}
|
|
298
|
-
while (delTextNode.firstChild) {
|
|
299
|
-
normalText.appendChild(delTextNode.firstChild);
|
|
300
|
-
}
|
|
301
|
-
delTextNode.parentNode?.replaceChild(normalText, delTextNode);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
function rejectPropertyChangeNode(changeNode, localName) {
|
|
306
|
-
const parent = changeNode?.parentNode;
|
|
307
|
-
if (!parent) return false;
|
|
308
|
-
|
|
309
|
-
const baseLocalName = localName.endsWith('Change')
|
|
310
|
-
? localName.slice(0, -'Change'.length)
|
|
311
|
-
: '';
|
|
312
|
-
|
|
313
|
-
if (
|
|
314
|
-
!baseLocalName
|
|
315
|
-
|| String(parent.localName || '').toLowerCase() !== baseLocalName.toLowerCase()
|
|
316
|
-
|| parent.namespaceURI !== NS_W
|
|
317
|
-
) {
|
|
318
|
-
return removeNode(changeNode);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
const historicalNode = Array.from(changeNode.childNodes || []).find(
|
|
322
|
-
child => child.nodeType === 1 && child.namespaceURI === NS_W
|
|
323
|
-
&& String(child.localName || '').toLowerCase() === baseLocalName.toLowerCase()
|
|
324
|
-
);
|
|
325
|
-
|
|
326
|
-
if (!historicalNode) {
|
|
327
|
-
return removeNode(changeNode);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const toAppend = Array.from(historicalNode.childNodes || []);
|
|
331
|
-
while (parent.firstChild) {
|
|
332
|
-
parent.removeChild(parent.firstChild);
|
|
333
|
-
}
|
|
334
|
-
for (const node of toAppend) {
|
|
335
|
-
const clone = xmlDocImportNode(parent.ownerDocument, node);
|
|
336
|
-
parent.appendChild(clone);
|
|
337
|
-
}
|
|
338
|
-
return true;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function xmlDocImportNode(xmlDoc, node) {
|
|
342
|
-
if (xmlDoc && typeof xmlDoc.importNode === 'function') {
|
|
343
|
-
return xmlDoc.importNode(node, true);
|
|
344
|
-
}
|
|
345
|
-
return node.cloneNode(true);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
function collectMoveRangeStartIds(xmlDoc, localName, filter) {
|
|
349
|
-
const ids = new Set();
|
|
350
|
-
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
351
|
-
if (!authorMatchesNode(node, filter)) continue;
|
|
352
|
-
const id = getAttributeByLocalName(node, 'id');
|
|
353
|
-
if (id) ids.add(id);
|
|
354
|
-
}
|
|
355
|
-
return ids;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
function removeMoveRangeMarkers(xmlDoc, filter) {
|
|
359
|
-
let removed = 0;
|
|
360
|
-
const moveFromIds = collectMoveRangeStartIds(xmlDoc, 'moveFromRangeStart', filter);
|
|
361
|
-
const moveToIds = collectMoveRangeStartIds(xmlDoc, 'moveToRangeStart', filter);
|
|
362
|
-
const markerSpecs = [
|
|
363
|
-
['moveFromRangeStart', moveFromIds, true],
|
|
364
|
-
['moveFromRangeEnd', moveFromIds, false],
|
|
365
|
-
['moveToRangeStart', moveToIds, true],
|
|
366
|
-
['moveToRangeEnd', moveToIds, false]
|
|
367
|
-
];
|
|
368
|
-
|
|
369
|
-
for (const [localName, ids, isStart] of markerSpecs) {
|
|
370
|
-
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
371
|
-
if (!node.parentNode) continue;
|
|
372
|
-
const id = getAttributeByLocalName(node, 'id');
|
|
373
|
-
if (!id) continue;
|
|
374
|
-
if (filter.allAuthors || ids.has(id) || (isStart && authorMatchesNode(node, filter))) {
|
|
375
|
-
if (removeNode(node)) removed += 1;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
return removed;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
/**
|
|
384
|
-
* Rejects tracked changes (`w:ins`, `w:del`, and *PrChange tags) for one author
|
|
385
|
-
* or all authors in the provided OOXML payload.
|
|
386
|
-
*
|
|
387
|
-
* @param {string} oxml
|
|
388
|
-
* @param {{ author?: string, allAuthors?: boolean }} [options]
|
|
389
|
-
* @returns {{ oxml: string, hasChanges: boolean, rejectedCount: number, warnings: string[] }}
|
|
390
|
-
*/
|
|
391
|
-
export function rejectTrackedChangesInOoxml(oxml, options = {}) {
|
|
392
|
-
const warnings = [];
|
|
393
|
-
const filter = resolveAuthorFilter(options);
|
|
394
|
-
if (!filter.valid) {
|
|
395
|
-
return { oxml, hasChanges: false, rejectedCount: 0, warnings: [filter.warning] };
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
const parseResult = parseXmlWithWarnings(oxml, 'Failed to parse OOXML');
|
|
399
|
-
if (!parseResult.xmlDoc) {
|
|
400
|
-
return {
|
|
401
|
-
oxml,
|
|
402
|
-
hasChanges: false,
|
|
403
|
-
rejectedCount: 0,
|
|
404
|
-
status: 'error',
|
|
405
|
-
error: parseResult.error,
|
|
406
|
-
warnings: [...(parseResult.warnings || []), parseResult.warning]
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
const { xmlDoc, serializer } = parseResult;
|
|
411
|
-
warnings.push(...(parseResult.warnings || []));
|
|
412
|
-
let rejectedCount = 0;
|
|
413
|
-
|
|
414
|
-
for (const insNode of getWordElementsByLocalName(xmlDoc, 'ins')) {
|
|
415
|
-
if (!insNode.parentNode || !authorMatchesNode(insNode, filter)) continue;
|
|
416
|
-
const paragraphMark = getContainingParagraphMarkRevision(insNode);
|
|
417
|
-
if (paragraphMark) {
|
|
418
|
-
if (mergeParagraphIntoNextAndRemove(paragraphMark)) rejectedCount += 1;
|
|
419
|
-
continue;
|
|
420
|
-
}
|
|
421
|
-
if (isTableRowRevisionMarker(insNode)) {
|
|
422
|
-
const rowNode = insNode.parentNode?.parentNode;
|
|
423
|
-
if (removeNode(rowNode)) rejectedCount += 1;
|
|
424
|
-
continue;
|
|
425
|
-
}
|
|
426
|
-
if (removeNode(insNode)) rejectedCount += 1;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
if (
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
if (
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Revision/comment management utilities for OOXML payloads.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { NS_W } from '../core/types.js';
|
|
6
|
+
import { createSerializer, parseOoxmlSafe } 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
|
+
let rawOxml = typeof oxml === 'string' ? oxml.replace(/^\uFEFF/, '').trim() : '';
|
|
66
|
+
let isFragmentWrapped = false;
|
|
67
|
+
let parsed = parseOoxmlSafe(rawOxml, 'application/xml');
|
|
68
|
+
if (parsed.error && (parsed.error.message.includes('HierarchyRequestError') || parsed.error.message.includes('Only one element'))) {
|
|
69
|
+
const wrapped = `<w:body xmlns:w="${NS_W}">${rawOxml}</w:body>`;
|
|
70
|
+
const wrappedParsed = parseOoxmlSafe(wrapped, 'application/xml');
|
|
71
|
+
if (!wrappedParsed.error) {
|
|
72
|
+
parsed = wrappedParsed;
|
|
73
|
+
isFragmentWrapped = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const parseError = parsed.doc ? getXmlParseError(parsed.doc) : null;
|
|
77
|
+
if (parsed.error || parseError) {
|
|
78
|
+
const message = parsed.error?.message || parseError?.textContent || 'parse error';
|
|
79
|
+
return {
|
|
80
|
+
xmlDoc: null,
|
|
81
|
+
serializer: null,
|
|
82
|
+
isFragmentWrapped: false,
|
|
83
|
+
warning: `${parseFailurePrefix}: ${message}`,
|
|
84
|
+
warnings: parsed.warnings,
|
|
85
|
+
error: { code: 'PARSE_ERROR', message }
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
xmlDoc: parsed.doc,
|
|
90
|
+
serializer: createSerializer(),
|
|
91
|
+
isFragmentWrapped,
|
|
92
|
+
warning: null,
|
|
93
|
+
warnings: parsed.warnings,
|
|
94
|
+
error: null
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function removeNode(node) {
|
|
99
|
+
if (node?.parentNode) {
|
|
100
|
+
node.parentNode.removeChild(node);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function unwrapNode(node) {
|
|
107
|
+
const parent = node?.parentNode;
|
|
108
|
+
if (!parent) return false;
|
|
109
|
+
while (node.firstChild) {
|
|
110
|
+
parent.insertBefore(node.firstChild, node);
|
|
111
|
+
}
|
|
112
|
+
parent.removeChild(node);
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function nextElementSibling(node) {
|
|
117
|
+
let sibling = node?.nextSibling || null;
|
|
118
|
+
while (sibling && sibling.nodeType !== 1) sibling = sibling.nextSibling;
|
|
119
|
+
return sibling;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function revisionMetadataWithoutId(node) {
|
|
123
|
+
return Array.from(node?.attributes || [])
|
|
124
|
+
.filter(attribute => String(attribute.localName || attribute.name || '').toLowerCase() !== 'id')
|
|
125
|
+
.map(attribute => `${attribute.namespaceURI || ''}|${attribute.localName || attribute.name}=${attribute.value}`)
|
|
126
|
+
.sort()
|
|
127
|
+
.join('\n');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function coalesceAdjacentCompatibleInsertions(xmlDoc) {
|
|
131
|
+
let coalesced = 0;
|
|
132
|
+
const insertions = getWordElementsByLocalName(xmlDoc, 'ins');
|
|
133
|
+
|
|
134
|
+
for (const insertion of insertions) {
|
|
135
|
+
if (!insertion.parentNode || isParagraphMarkRevisionMarker(insertion)) continue;
|
|
136
|
+
let sibling = nextElementSibling(insertion);
|
|
137
|
+
while (
|
|
138
|
+
isWordElement(sibling, 'ins')
|
|
139
|
+
&& normalizeAuthor(getAttributeByLocalName(sibling, 'author'))
|
|
140
|
+
=== normalizeAuthor(getAttributeByLocalName(insertion, 'author'))
|
|
141
|
+
&& revisionMetadataWithoutId(sibling) === revisionMetadataWithoutId(insertion)
|
|
142
|
+
) {
|
|
143
|
+
const next = nextElementSibling(sibling);
|
|
144
|
+
while (sibling.firstChild) insertion.appendChild(sibling.firstChild);
|
|
145
|
+
sibling.parentNode?.removeChild(sibling);
|
|
146
|
+
coalesced += 1;
|
|
147
|
+
sibling = next;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return coalesced;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function isTableRowRevisionMarker(node) {
|
|
155
|
+
const parent = node?.parentNode;
|
|
156
|
+
return isWordElement(parent, 'trPr') && isWordElement(parent?.parentNode, 'tr');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function isParagraphMarkRevisionMarker(node) {
|
|
160
|
+
const rPr = node?.parentNode;
|
|
161
|
+
const pPr = rPr?.parentNode;
|
|
162
|
+
const paragraph = pPr?.parentNode;
|
|
163
|
+
return isWordElement(rPr, 'rPr') && isWordElement(pPr, 'pPr') && isWordElement(paragraph, 'p');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function getContainingParagraphMarkRevision(node) {
|
|
167
|
+
return isParagraphMarkRevisionMarker(node) ? node.parentNode.parentNode.parentNode : null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function getNextWordParagraph(paragraph) {
|
|
171
|
+
let cursor = paragraph?.nextSibling || null;
|
|
172
|
+
while (cursor) {
|
|
173
|
+
if (isWordElement(cursor, 'p')) return cursor;
|
|
174
|
+
cursor = cursor.nextSibling;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function mergeParagraphIntoNextAndRemove(paragraph) {
|
|
180
|
+
if (!paragraph?.parentNode) return false;
|
|
181
|
+
const nextParagraph = getNextWordParagraph(paragraph);
|
|
182
|
+
if (!nextParagraph) {
|
|
183
|
+
if (isWordElement(paragraph.parentNode, 'tc') && getWordElementsByLocalName(paragraph.parentNode, 'p').length <= 1) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return removeNode(paragraph);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const childrenToMove = Array.from(paragraph.childNodes || []).filter(child => !isWordElement(child, 'pPr'));
|
|
190
|
+
let insertionPoint = nextParagraph.firstChild || null;
|
|
191
|
+
if (isWordElement(insertionPoint, 'pPr')) {
|
|
192
|
+
insertionPoint = insertionPoint.nextSibling || null;
|
|
193
|
+
}
|
|
194
|
+
for (const child of childrenToMove) {
|
|
195
|
+
nextParagraph.insertBefore(child, insertionPoint);
|
|
196
|
+
}
|
|
197
|
+
return removeNode(paragraph);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Accepts tracked changes (`w:ins`, `w:del`, and *PrChange tags) for one author
|
|
202
|
+
* or all authors in the provided OOXML payload.
|
|
203
|
+
*
|
|
204
|
+
* @param {string} oxml
|
|
205
|
+
* @param {{ author?: string, allAuthors?: boolean }} [options]
|
|
206
|
+
* @returns {{ oxml: string, hasChanges: boolean, acceptedCount: number, warnings: string[] }}
|
|
207
|
+
*/
|
|
208
|
+
export function acceptTrackedChangesInOoxml(oxml, options = {}) {
|
|
209
|
+
const warnings = [];
|
|
210
|
+
const filter = resolveAuthorFilter(options);
|
|
211
|
+
if (!filter.valid) {
|
|
212
|
+
return { oxml, hasChanges: false, acceptedCount: 0, warnings: [filter.warning] };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const parseResult = parseXmlWithWarnings(oxml, 'Failed to parse OOXML');
|
|
216
|
+
if (!parseResult.xmlDoc) {
|
|
217
|
+
return {
|
|
218
|
+
oxml,
|
|
219
|
+
hasChanges: false,
|
|
220
|
+
acceptedCount: 0,
|
|
221
|
+
status: 'error',
|
|
222
|
+
error: parseResult.error,
|
|
223
|
+
warnings: [...(parseResult.warnings || []), parseResult.warning]
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const { xmlDoc, serializer } = parseResult;
|
|
228
|
+
warnings.push(...(parseResult.warnings || []));
|
|
229
|
+
let acceptedCount = 0;
|
|
230
|
+
|
|
231
|
+
for (const insNode of getWordElementsByLocalName(xmlDoc, 'ins')) {
|
|
232
|
+
if (!insNode.parentNode || !authorMatchesNode(insNode, filter)) continue;
|
|
233
|
+
if (isParagraphMarkRevisionMarker(insNode)) {
|
|
234
|
+
if (removeNode(insNode)) acceptedCount += 1;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
if (isTableRowRevisionMarker(insNode)) {
|
|
238
|
+
if (removeNode(insNode)) acceptedCount += 1;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (unwrapNode(insNode)) acceptedCount += 1;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
for (const delNode of getWordElementsByLocalName(xmlDoc, 'del')) {
|
|
245
|
+
if (!delNode.parentNode || !authorMatchesNode(delNode, filter)) continue;
|
|
246
|
+
const paragraphMark = getContainingParagraphMarkRevision(delNode);
|
|
247
|
+
if (paragraphMark) {
|
|
248
|
+
if (mergeParagraphIntoNextAndRemove(paragraphMark)) acceptedCount += 1;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (isTableRowRevisionMarker(delNode)) {
|
|
252
|
+
const rowNode = delNode.parentNode?.parentNode;
|
|
253
|
+
if (removeNode(rowNode)) acceptedCount += 1;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (removeNode(delNode)) acceptedCount += 1;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
for (const moveFromNode of getWordElementsByLocalName(xmlDoc, 'moveFrom')) {
|
|
260
|
+
if (!moveFromNode.parentNode || !authorMatchesNode(moveFromNode, filter)) continue;
|
|
261
|
+
if (removeNode(moveFromNode)) acceptedCount += 1;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
for (const moveToNode of getWordElementsByLocalName(xmlDoc, 'moveTo')) {
|
|
265
|
+
if (!moveToNode.parentNode || !authorMatchesNode(moveToNode, filter)) continue;
|
|
266
|
+
if (unwrapNode(moveToNode)) acceptedCount += 1;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
acceptedCount += removeMoveRangeMarkers(xmlDoc, filter);
|
|
270
|
+
|
|
271
|
+
const changeTags = ['rPrChange', 'pPrChange', 'tblPrChange', 'trPrChange', 'tcPrChange'];
|
|
272
|
+
for (const localName of changeTags) {
|
|
273
|
+
for (const changeNode of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
274
|
+
if (!changeNode.parentNode || !authorMatchesNode(changeNode, filter)) continue;
|
|
275
|
+
if (removeNode(changeNode)) acceptedCount += 1;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const serializedOxml = parseResult.isFragmentWrapped
|
|
280
|
+
? Array.from(xmlDoc.documentElement.childNodes).map(n => serializer.serializeToString(n)).join('')
|
|
281
|
+
: serializer.serializeToString(xmlDoc);
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
oxml: serializedOxml,
|
|
285
|
+
hasChanges: acceptedCount > 0,
|
|
286
|
+
acceptedCount,
|
|
287
|
+
warnings
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function convertDeletionTextNodes(xmlDoc, delNode) {
|
|
292
|
+
for (const delTextNode of Array.from(delNode.getElementsByTagNameNS(NS_W, 'delText'))) {
|
|
293
|
+
const normalText = createWordElement(xmlDoc, 'w:t');
|
|
294
|
+
const spaceValue = delTextNode.getAttribute('xml:space');
|
|
295
|
+
if (spaceValue) {
|
|
296
|
+
normalText.setAttribute('xml:space', spaceValue);
|
|
297
|
+
}
|
|
298
|
+
while (delTextNode.firstChild) {
|
|
299
|
+
normalText.appendChild(delTextNode.firstChild);
|
|
300
|
+
}
|
|
301
|
+
delTextNode.parentNode?.replaceChild(normalText, delTextNode);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function rejectPropertyChangeNode(changeNode, localName) {
|
|
306
|
+
const parent = changeNode?.parentNode;
|
|
307
|
+
if (!parent) return false;
|
|
308
|
+
|
|
309
|
+
const baseLocalName = localName.endsWith('Change')
|
|
310
|
+
? localName.slice(0, -'Change'.length)
|
|
311
|
+
: '';
|
|
312
|
+
|
|
313
|
+
if (
|
|
314
|
+
!baseLocalName
|
|
315
|
+
|| String(parent.localName || '').toLowerCase() !== baseLocalName.toLowerCase()
|
|
316
|
+
|| parent.namespaceURI !== NS_W
|
|
317
|
+
) {
|
|
318
|
+
return removeNode(changeNode);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const historicalNode = Array.from(changeNode.childNodes || []).find(
|
|
322
|
+
child => child.nodeType === 1 && child.namespaceURI === NS_W
|
|
323
|
+
&& String(child.localName || '').toLowerCase() === baseLocalName.toLowerCase()
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
if (!historicalNode) {
|
|
327
|
+
return removeNode(changeNode);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const toAppend = Array.from(historicalNode.childNodes || []);
|
|
331
|
+
while (parent.firstChild) {
|
|
332
|
+
parent.removeChild(parent.firstChild);
|
|
333
|
+
}
|
|
334
|
+
for (const node of toAppend) {
|
|
335
|
+
const clone = xmlDocImportNode(parent.ownerDocument, node);
|
|
336
|
+
parent.appendChild(clone);
|
|
337
|
+
}
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function xmlDocImportNode(xmlDoc, node) {
|
|
342
|
+
if (xmlDoc && typeof xmlDoc.importNode === 'function') {
|
|
343
|
+
return xmlDoc.importNode(node, true);
|
|
344
|
+
}
|
|
345
|
+
return node.cloneNode(true);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function collectMoveRangeStartIds(xmlDoc, localName, filter) {
|
|
349
|
+
const ids = new Set();
|
|
350
|
+
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
351
|
+
if (!authorMatchesNode(node, filter)) continue;
|
|
352
|
+
const id = getAttributeByLocalName(node, 'id');
|
|
353
|
+
if (id) ids.add(id);
|
|
354
|
+
}
|
|
355
|
+
return ids;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function removeMoveRangeMarkers(xmlDoc, filter) {
|
|
359
|
+
let removed = 0;
|
|
360
|
+
const moveFromIds = collectMoveRangeStartIds(xmlDoc, 'moveFromRangeStart', filter);
|
|
361
|
+
const moveToIds = collectMoveRangeStartIds(xmlDoc, 'moveToRangeStart', filter);
|
|
362
|
+
const markerSpecs = [
|
|
363
|
+
['moveFromRangeStart', moveFromIds, true],
|
|
364
|
+
['moveFromRangeEnd', moveFromIds, false],
|
|
365
|
+
['moveToRangeStart', moveToIds, true],
|
|
366
|
+
['moveToRangeEnd', moveToIds, false]
|
|
367
|
+
];
|
|
368
|
+
|
|
369
|
+
for (const [localName, ids, isStart] of markerSpecs) {
|
|
370
|
+
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
371
|
+
if (!node.parentNode) continue;
|
|
372
|
+
const id = getAttributeByLocalName(node, 'id');
|
|
373
|
+
if (!id) continue;
|
|
374
|
+
if (filter.allAuthors || ids.has(id) || (isStart && authorMatchesNode(node, filter))) {
|
|
375
|
+
if (removeNode(node)) removed += 1;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return removed;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Rejects tracked changes (`w:ins`, `w:del`, and *PrChange tags) for one author
|
|
385
|
+
* or all authors in the provided OOXML payload.
|
|
386
|
+
*
|
|
387
|
+
* @param {string} oxml
|
|
388
|
+
* @param {{ author?: string, allAuthors?: boolean }} [options]
|
|
389
|
+
* @returns {{ oxml: string, hasChanges: boolean, rejectedCount: number, warnings: string[] }}
|
|
390
|
+
*/
|
|
391
|
+
export function rejectTrackedChangesInOoxml(oxml, options = {}) {
|
|
392
|
+
const warnings = [];
|
|
393
|
+
const filter = resolveAuthorFilter(options);
|
|
394
|
+
if (!filter.valid) {
|
|
395
|
+
return { oxml, hasChanges: false, rejectedCount: 0, warnings: [filter.warning] };
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const parseResult = parseXmlWithWarnings(oxml, 'Failed to parse OOXML');
|
|
399
|
+
if (!parseResult.xmlDoc) {
|
|
400
|
+
return {
|
|
401
|
+
oxml,
|
|
402
|
+
hasChanges: false,
|
|
403
|
+
rejectedCount: 0,
|
|
404
|
+
status: 'error',
|
|
405
|
+
error: parseResult.error,
|
|
406
|
+
warnings: [...(parseResult.warnings || []), parseResult.warning]
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const { xmlDoc, serializer } = parseResult;
|
|
411
|
+
warnings.push(...(parseResult.warnings || []));
|
|
412
|
+
let rejectedCount = 0;
|
|
413
|
+
|
|
414
|
+
for (const insNode of getWordElementsByLocalName(xmlDoc, 'ins')) {
|
|
415
|
+
if (!insNode.parentNode || !authorMatchesNode(insNode, filter)) continue;
|
|
416
|
+
const paragraphMark = getContainingParagraphMarkRevision(insNode);
|
|
417
|
+
if (paragraphMark) {
|
|
418
|
+
if (mergeParagraphIntoNextAndRemove(paragraphMark)) rejectedCount += 1;
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
if (isTableRowRevisionMarker(insNode)) {
|
|
422
|
+
const rowNode = insNode.parentNode?.parentNode;
|
|
423
|
+
if (removeNode(rowNode)) rejectedCount += 1;
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (removeNode(insNode)) rejectedCount += 1;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// A table whose every row was an inserted row is itself an inserted table: drop the empty shell.
|
|
430
|
+
for (const tableNode of getWordElementsByLocalName(xmlDoc, 'tbl')) {
|
|
431
|
+
if (!tableNode.parentNode) continue;
|
|
432
|
+
const hasRow = Array.from(tableNode.childNodes || []).some(child => isWordElement(child, 'tr'));
|
|
433
|
+
if (!hasRow && removeNode(tableNode)) rejectedCount += 1;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
for (const delNode of getWordElementsByLocalName(xmlDoc, 'del')) {
|
|
437
|
+
if (!delNode.parentNode || !authorMatchesNode(delNode, filter)) continue;
|
|
438
|
+
if (isParagraphMarkRevisionMarker(delNode)) {
|
|
439
|
+
if (removeNode(delNode)) rejectedCount += 1;
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
if (isTableRowRevisionMarker(delNode)) {
|
|
443
|
+
if (removeNode(delNode)) rejectedCount += 1;
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
convertDeletionTextNodes(xmlDoc, delNode);
|
|
447
|
+
if (unwrapNode(delNode)) rejectedCount += 1;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
for (const moveFromNode of getWordElementsByLocalName(xmlDoc, 'moveFrom')) {
|
|
451
|
+
if (!moveFromNode.parentNode || !authorMatchesNode(moveFromNode, filter)) continue;
|
|
452
|
+
convertDeletionTextNodes(xmlDoc, moveFromNode);
|
|
453
|
+
if (unwrapNode(moveFromNode)) rejectedCount += 1;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
for (const moveToNode of getWordElementsByLocalName(xmlDoc, 'moveTo')) {
|
|
457
|
+
if (!moveToNode.parentNode || !authorMatchesNode(moveToNode, filter)) continue;
|
|
458
|
+
if (removeNode(moveToNode)) rejectedCount += 1;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
rejectedCount += removeMoveRangeMarkers(xmlDoc, filter);
|
|
462
|
+
|
|
463
|
+
const changeTags = ['rPrChange', 'pPrChange', 'tblPrChange', 'trPrChange', 'tcPrChange'];
|
|
464
|
+
for (const localName of changeTags) {
|
|
465
|
+
for (const changeNode of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
466
|
+
if (!changeNode.parentNode || !authorMatchesNode(changeNode, filter)) continue;
|
|
467
|
+
if (rejectPropertyChangeNode(changeNode, localName)) rejectedCount += 1;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
coalesceAdjacentCompatibleInsertions(xmlDoc);
|
|
472
|
+
|
|
473
|
+
const serializedOxml = parseResult.isFragmentWrapped
|
|
474
|
+
? Array.from(xmlDoc.documentElement.childNodes).map(n => serializer.serializeToString(n)).join('')
|
|
475
|
+
: serializer.serializeToString(xmlDoc);
|
|
476
|
+
|
|
477
|
+
return {
|
|
478
|
+
oxml: serializedOxml,
|
|
479
|
+
hasChanges: rejectedCount > 0,
|
|
480
|
+
rejectedCount,
|
|
481
|
+
warnings
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function collectCommentTargetIds(xmlDoc, filter) {
|
|
486
|
+
const targetIds = new Set();
|
|
487
|
+
const commentNodes = getWordElementsByLocalName(xmlDoc, 'comment');
|
|
488
|
+
|
|
489
|
+
for (const commentNode of commentNodes) {
|
|
490
|
+
if (!authorMatchesNode(commentNode, filter)) continue;
|
|
491
|
+
const id = getAttributeByLocalName(commentNode, 'id');
|
|
492
|
+
if (id) targetIds.add(id);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return { targetIds, commentNodes };
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function removeCommentNodesById(commentNodes, targetIds) {
|
|
499
|
+
let removed = 0;
|
|
500
|
+
for (const commentNode of commentNodes) {
|
|
501
|
+
const id = getAttributeByLocalName(commentNode, 'id');
|
|
502
|
+
if (!id || !targetIds.has(id)) continue;
|
|
503
|
+
if (removeNode(commentNode)) removed += 1;
|
|
504
|
+
}
|
|
505
|
+
return removed;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function runIsOnlyCommentReference(runNode) {
|
|
509
|
+
if (!isWordElement(runNode, 'r')) return false;
|
|
510
|
+
const meaningfulChildren = Array.from(runNode.childNodes || []).filter(child => {
|
|
511
|
+
if (child.nodeType === 3) return String(child.nodeValue || '').trim().length > 0;
|
|
512
|
+
if (child.nodeType !== 1) return false;
|
|
513
|
+
if (child.namespaceURI !== NS_W) return true;
|
|
514
|
+
const local = String(child.localName || '').toLowerCase();
|
|
515
|
+
return local !== 'rpr' && local !== 'commentreference';
|
|
516
|
+
});
|
|
517
|
+
return meaningfulChildren.length === 0;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function removeCommentAnchors(xmlDoc, targetIds) {
|
|
521
|
+
let removed = 0;
|
|
522
|
+
const anchorTags = ['commentRangeStart', 'commentRangeEnd', 'commentReference'];
|
|
523
|
+
|
|
524
|
+
for (const localName of anchorTags) {
|
|
525
|
+
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
526
|
+
if (!node.parentNode) continue;
|
|
527
|
+
const id = getAttributeByLocalName(node, 'id');
|
|
528
|
+
if (!id || !targetIds.has(id)) continue;
|
|
529
|
+
|
|
530
|
+
if (localName === 'commentReference' && runIsOnlyCommentReference(node.parentNode)) {
|
|
531
|
+
if (removeNode(node.parentNode)) {
|
|
532
|
+
removed += 1;
|
|
533
|
+
}
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
if (removeNode(node)) {
|
|
537
|
+
removed += 1;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return removed;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Deletes comments authored by one user (or all users) and removes matching
|
|
546
|
+
* comment anchors/references from the OOXML payload.
|
|
547
|
+
*
|
|
548
|
+
* @param {string} oxml
|
|
549
|
+
* @param {{ author?: string, allAuthors?: boolean }} [options]
|
|
550
|
+
* @returns {{ oxml: string, hasChanges: boolean, commentsRemoved: number, referencesRemoved: number, warnings: string[] }}
|
|
551
|
+
*/
|
|
552
|
+
export function deleteCommentsByAuthorInOoxml(oxml, options = {}) {
|
|
553
|
+
const warnings = [];
|
|
554
|
+
const filter = resolveAuthorFilter(options);
|
|
555
|
+
if (!filter.valid) {
|
|
556
|
+
return {
|
|
557
|
+
oxml,
|
|
558
|
+
hasChanges: false,
|
|
559
|
+
commentsRemoved: 0,
|
|
560
|
+
referencesRemoved: 0,
|
|
561
|
+
warnings: [filter.warning]
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const parseResult = parseXmlWithWarnings(oxml, 'Failed to parse OOXML');
|
|
566
|
+
if (!parseResult.xmlDoc) {
|
|
567
|
+
return {
|
|
568
|
+
oxml,
|
|
569
|
+
hasChanges: false,
|
|
570
|
+
commentsRemoved: 0,
|
|
571
|
+
referencesRemoved: 0,
|
|
572
|
+
status: 'error',
|
|
573
|
+
error: parseResult.error,
|
|
574
|
+
warnings: [...(parseResult.warnings || []), parseResult.warning]
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const { xmlDoc, serializer } = parseResult;
|
|
579
|
+
warnings.push(...(parseResult.warnings || []));
|
|
580
|
+
const { targetIds, commentNodes } = collectCommentTargetIds(xmlDoc, filter);
|
|
581
|
+
|
|
582
|
+
if (filter.allAuthors) {
|
|
583
|
+
for (const localName of ['commentRangeStart', 'commentRangeEnd', 'commentReference']) {
|
|
584
|
+
for (const node of getWordElementsByLocalName(xmlDoc, localName)) {
|
|
585
|
+
const id = getAttributeByLocalName(node, 'id');
|
|
586
|
+
if (id) targetIds.add(id);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const commentsRemoved = removeCommentNodesById(commentNodes, targetIds);
|
|
592
|
+
const referencesRemoved = removeCommentAnchors(xmlDoc, targetIds);
|
|
593
|
+
|
|
594
|
+
return {
|
|
595
|
+
oxml: serializer.serializeToString(xmlDoc),
|
|
596
|
+
hasChanges: commentsRemoved > 0 || referencesRemoved > 0,
|
|
597
|
+
commentsRemoved,
|
|
598
|
+
referencesRemoved,
|
|
599
|
+
warnings
|
|
600
|
+
};
|
|
601
|
+
}
|