onionskin-cli 0.0.0-stage → 0.0.1
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 +136 -0
- package/INSTALL.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE +46 -0
- package/README.md +88 -2
- package/docs/contracts/batch.schema.json +839 -0
- package/docs/contracts/history.schema.json +126 -0
- package/docs/contracts/ops.schema.json +541 -0
- package/package.json +54 -4
- package/scripts/checker/check.py +411 -0
- package/scripts/checker/requirements.txt +5 -0
- package/scripts/install-check.mjs +9 -0
- package/scripts/requirements.txt +5 -0
- package/scripts/rewriter/apple/main.swift +135 -0
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +6 -0
- package/skills/onionskin/SKILL.md +117 -0
- package/skills/onionskin/reference/batch.md +94 -0
- package/skills/onionskin/reference/formats.md +15 -0
- package/skills/onionskin/reference/ops.md +253 -0
- package/skills/onionskin/reference/troubleshooting.md +73 -0
- package/skills/onionskin/reference/voice-guide.md +86 -0
- package/skills/onionskin/voice.example.md +51 -0
- package/src/agent-detect.js +149 -0
- package/src/agent-events.js +159 -0
- package/src/agent-prompt.js +27 -0
- package/src/agent-runner.js +593 -0
- package/src/analyze.js +17 -0
- package/src/anchors.js +190 -0
- package/src/apply.js +387 -0
- package/src/auth.js +130 -0
- package/src/batch.js +322 -0
- package/src/check-worker.js +8 -0
- package/src/checker.js +293 -0
- package/src/cli.js +516 -0
- package/src/constants.js +20 -0
- package/src/diff.js +267 -0
- package/src/doctor.js +247 -0
- package/src/docx.js +438 -0
- package/src/fmt.js +30 -0
- package/src/fsutil.js +348 -0
- package/src/history-worker.js +16 -0
- package/src/history.js +576 -0
- package/src/lease.js +79 -0
- package/src/local.js +242 -0
- package/src/merge-pptx.js +198 -0
- package/src/merge-xlsx.js +453 -0
- package/src/merge.js +410 -0
- package/src/offload.js +34 -0
- package/src/ops/common.js +48 -0
- package/src/ops/docx-table.js +323 -0
- package/src/ops/docx.js +503 -0
- package/src/ops/engine.js +41 -0
- package/src/ops/pptx.js +336 -0
- package/src/ops/run.js +115 -0
- package/src/ops/runs.js +127 -0
- package/src/ops/schema.js +109 -0
- package/src/ops/xlsx.js +277 -0
- package/src/package.js +189 -0
- package/src/paths.js +64 -0
- package/src/person.js +66 -0
- package/src/pptx.js +194 -0
- package/src/question.js +47 -0
- package/src/quick-plan.js +107 -0
- package/src/quick.js +164 -0
- package/src/redline-check.js +251 -0
- package/src/redline-worker.js +29 -0
- package/src/redline.js +438 -0
- package/src/reply.js +410 -0
- package/src/review.js +274 -0
- package/src/rewrite-check.js +1014 -0
- package/src/rewriter-cleanup.js +105 -0
- package/src/rewriter-setup.js +104 -0
- package/src/rewriter.js +396 -0
- package/src/sanitize.js +42 -0
- package/src/sentence-span.js +25 -0
- package/src/server-entry.js +13 -0
- package/src/server.js +576 -0
- package/src/session.js +1910 -0
- package/src/settings.js +161 -0
- package/src/setup.js +160 -0
- package/src/stamp-words.js +10 -0
- package/src/stamps.js +59 -0
- package/src/style.js +110 -0
- package/src/suggest-reply.js +84 -0
- package/src/suggest.js +261 -0
- package/src/text.js +31 -0
- package/src/tracked-words.js +43 -0
- package/src/voice-guide.js +182 -0
- package/src/voice-learn.js +314 -0
- package/src/voice-sample-worker.js +95 -0
- package/src/voice-samples.js +329 -0
- package/src/voice-store.js +305 -0
- package/src/voice-worker.js +28 -0
- package/src/voice.js +297 -0
- package/src/xlsx.js +454 -0
- package/src/xml-tree.js +118 -0
- package/src/xml.js +127 -0
- package/src/zip-write.js +74 -0
- package/src/zip.js +88 -0
- package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
- package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
- package/vendor/engine/collab-BVynXMqc.js +2591 -0
- package/vendor/engine/collab.js +2 -0
- package/vendor/engine/core.js +4 -0
- package/vendor/engine/default-worker-DogNT09A.js +35625 -0
- package/vendor/engine/embed.js +107 -0
- package/vendor/engine/index.js +5 -0
- package/vendor/engine/office-engine.embed.js +184 -0
- package/vendor/engine/office-engine.worker.js +36030 -0
- package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
- package/vendor/engine-node/LICENSE +21 -0
- package/vendor/engine-node/engine-node.mjs +46618 -0
- package/vendor/engine.lock.json +26 -0
- package/web/dist/assets/index-CFISATkn.js +197 -0
- package/web/dist/assets/index-spXE4oTz.css +1 -0
- package/web/dist/index.html +14 -0
package/src/redline.js
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { diffRaw } from "./diff.js";
|
|
2
|
+
import { parseDocxParagraphs } from "./docx.js";
|
|
3
|
+
import { matchParagraphs } from "./history.js";
|
|
4
|
+
import { normText } from "./text.js";
|
|
5
|
+
import { fmtNote } from "./fmt.js";
|
|
6
|
+
import { openZip } from "./zip.js";
|
|
7
|
+
import { replaceEntry } from "./zip-write.js";
|
|
8
|
+
import { walkXml } from "./xml.js";
|
|
9
|
+
import { attr, attrs, descendants, elements, escAttr, escText, parse, serialize, textOf } from "./xml-tree.js";
|
|
10
|
+
|
|
11
|
+
// Builds the tracked-changes copy (docs/contracts/tracked-changes.md): the AFTER file with Word revision markup written into
|
|
12
|
+
// word/document.xml, so Accept All in Word gives AFTER and Reject All gives BEFORE (text and paragraph structure). Pure: bytes in, bytes or
|
|
13
|
+
// refusal codes out. Every gate below refuses the whole copy (all or nothing); the caller still runs src/redline-check.js on the result.
|
|
14
|
+
//
|
|
15
|
+
// Markup (contract section 4): run revisions <w:del><w:r><w:rPr/><w:delText/></w:r></w:del> then <w:ins><w:r>..<w:t/></w:r></w:ins>;
|
|
16
|
+
// an added paragraph has its own mark inserted, a removed one is rebuilt from BEFORE with its own mark deleted and placed before the AFTER
|
|
17
|
+
// match of the next kept BEFORE paragraph. Ids start at 1 + the largest w:id in any part; no rsid attributes on what is written here.
|
|
18
|
+
|
|
19
|
+
export const AUTHORS = { agent: "Onionskin agent", mixed: "Onionskin" };
|
|
20
|
+
const MAX_ID = 2147483647;
|
|
21
|
+
|
|
22
|
+
const two = (n) => String(n).padStart(2, "0");
|
|
23
|
+
/** Word's own w:date convention: the local wall-clock time written with a Z, seconds 00 (contract M2). */
|
|
24
|
+
export const wordDate = (ms) => {
|
|
25
|
+
const d = new Date(ms);
|
|
26
|
+
return `${d.getFullYear()}-${two(d.getMonth() + 1)}-${two(d.getDate())}T${two(d.getHours())}:${two(d.getMinutes())}:00Z`;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const REVISIONS = new Set(["w:ins", "w:del", "w:moveFrom", "w:moveTo", "w:moveFromRangeStart", "w:moveToRangeStart", "w:rPrChange", "w:pPrChange", "w:cellIns", "w:cellDel", "w:cellMerge", "w:numberingChange", "w:sectPrChange", "w:tblPrChange", "w:tblGridChange", "w:trPrChange", "w:tcPrChange", "w:tblPrExChange"]);
|
|
30
|
+
const P_ANCHORS = new Set(["w:bookmarkStart", "w:bookmarkEnd", "w:commentRangeStart", "w:commentRangeEnd", "w:proofErr"]);
|
|
31
|
+
const RUN_KIDS = new Set(["w:rPr", "w:t", "w:tab", "w:br", "w:lastRenderedPageBreak", "w:commentReference"]);
|
|
32
|
+
// direct run properties a deleted run may carry over from BEFORE (anything else falls back to the neighboring AFTER properties)
|
|
33
|
+
const DIRECT_RPR = new Set(["w:b", "w:bCs", "w:i", "w:iCs", "w:u", "w:strike", "w:dstrike", "w:color", "w:sz", "w:szCs", "w:rFonts", "w:highlight", "w:vertAlign", "w:lang", "w:caps", "w:smallCaps"]);
|
|
34
|
+
// paragraph properties a removed paragraph keeps from BEFORE, in their source order (which is the schema order)
|
|
35
|
+
const KEEP_PPR = new Set(["w:pStyle", "w:keepNext", "w:keepLines", "w:pageBreakBefore", "w:numPr", "w:spacing", "w:ind", "w:contextualSpacing", "w:jc", "w:outlineLvl"]);
|
|
36
|
+
// characters XML 1.0 does not allow (a decoded  would make the copy unreadable)
|
|
37
|
+
const BAD_CHAR = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
|
|
38
|
+
const NOTES_PART = /^word\/(header|footer|footnotes|endnotes|comments)[^/]*\.xml$/;
|
|
39
|
+
|
|
40
|
+
class Refused extends Error {
|
|
41
|
+
constructor(codes) {
|
|
42
|
+
super(codes.join(", "));
|
|
43
|
+
this.codes = codes;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ---- reading the paragraphs out of the tree -------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
/** Every paragraph in document order, as parseDocxParagraphs lists them (paragraphs inside a paragraph, text boxes, are not listed). */
|
|
50
|
+
function listParagraphs(root) {
|
|
51
|
+
const body = [...descendants(root)].find((e) => e.name === "w:body");
|
|
52
|
+
if (!body) throw new Refused(["unreadable"]);
|
|
53
|
+
const out = [];
|
|
54
|
+
const tables = [];
|
|
55
|
+
let nestedTables = 0;
|
|
56
|
+
const visit = (el, depth) => {
|
|
57
|
+
for (const k of elements(el)) {
|
|
58
|
+
if (k.name === "w:p") {
|
|
59
|
+
let ctx = "other";
|
|
60
|
+
if (k.parent === body) ctx = "body";
|
|
61
|
+
else if (depth === 1 && k.parent.name === "w:tc") ctx = "cell";
|
|
62
|
+
else if (depth > 1) ctx = "nested";
|
|
63
|
+
out.push({ node: k, ctx, seg: tables.length - nestedTables }); // seg: top-level tables before it
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (k.name === "w:tbl" && depth > 0) nestedTables++;
|
|
67
|
+
if (k.name === "w:tbl") {
|
|
68
|
+
const t = { rows: [] };
|
|
69
|
+
tables.push(t);
|
|
70
|
+
for (const tr of elements(k).filter((x) => x.name === "w:tr")) t.rows.push(elements(tr).filter((x) => x.name === "w:tc").length);
|
|
71
|
+
}
|
|
72
|
+
visit(k, depth + (k.name === "w:tbl" ? 1 : 0));
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
visit(body, 0);
|
|
76
|
+
return { body, paras: out, tables };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A paragraph's content as pieces: {kind: "text", rPr, tag: "t"|"tab"|"br", text} and {kind: "anchor", raw} (bookmarks, comment marks,
|
|
81
|
+
* proofErr, a comment reference run). null when it holds anything else (links, fields, objects, special characters...).
|
|
82
|
+
*/
|
|
83
|
+
function piecesOf(p) {
|
|
84
|
+
const pieces = [];
|
|
85
|
+
let pPr = null;
|
|
86
|
+
for (const k of p.kids) {
|
|
87
|
+
if (k.t === "text") {
|
|
88
|
+
if (k.raw.trim()) return null;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (k.t !== "el") return null;
|
|
92
|
+
if (k.name === "w:pPr") {
|
|
93
|
+
if (pPr || pieces.length) return null;
|
|
94
|
+
pPr = k;
|
|
95
|
+
} else if (P_ANCHORS.has(k.name)) pieces.push({ kind: "anchor", raw: serialize(k) });
|
|
96
|
+
else if (k.name === "w:r") {
|
|
97
|
+
let rPr = "";
|
|
98
|
+
const kids = elements(k);
|
|
99
|
+
if (k.kids.some((x) => x.t !== "el" && !(x.t === "text" && !x.raw.trim()))) return null;
|
|
100
|
+
for (const c of kids) {
|
|
101
|
+
if (!RUN_KIDS.has(c.name)) return null;
|
|
102
|
+
if (c.name === "w:rPr") rPr = serialize(c);
|
|
103
|
+
else if (c.name === "w:t") {
|
|
104
|
+
if (c.kids.some((x) => x.t !== "text" && x.t !== "cdata")) return null;
|
|
105
|
+
const text = c.kids.map(textOf).join("");
|
|
106
|
+
if (text) pieces.push({ kind: "text", rPr, tag: "t", text });
|
|
107
|
+
} else if (c.name === "w:tab") pieces.push({ kind: "text", rPr, tag: "tab", text: "\t" });
|
|
108
|
+
else if (c.name === "w:br") {
|
|
109
|
+
if (attrs(c).length) return null; // a typed break (page, column) or a clear break is not plain text
|
|
110
|
+
pieces.push({ kind: "text", rPr, tag: "br", text: "\n" });
|
|
111
|
+
} else if (c.name === "w:commentReference") pieces.push({ kind: "anchor", raw: `<w:r>${rPr}${serialize(c)}</w:r>` });
|
|
112
|
+
// w:lastRenderedPageBreak is a layout hint Word writes again on save: dropped from rewritten paragraphs
|
|
113
|
+
}
|
|
114
|
+
} else return null;
|
|
115
|
+
}
|
|
116
|
+
return { pPr, pieces };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const textOfPieces = (pieces) => pieces.filter((x) => x.kind === "text").map((x) => x.text).join("");
|
|
120
|
+
|
|
121
|
+
// ---- writing markup ---------------------------------------------------------------------------------------------------
|
|
122
|
+
|
|
123
|
+
const space = (s) => (/^\s|\s$/.test(s) ? ' xml:space="preserve"' : "");
|
|
124
|
+
const tagXml = (pc, del) => (pc.tag === "tab" ? "<w:tab/>" : pc.tag === "br" ? "<w:br/>" : del ? `<w:delText${space(pc.text)}>${escText(pc.text)}</w:delText>` : `<w:t${space(pc.text)}>${escText(pc.text)}</w:t>`);
|
|
125
|
+
/** pieces -> runs; neighbors with the same properties share one run */
|
|
126
|
+
function runsXml(pieces, del) {
|
|
127
|
+
let out = "";
|
|
128
|
+
let cur = null;
|
|
129
|
+
let body = "";
|
|
130
|
+
for (const pc of pieces) {
|
|
131
|
+
if (cur !== null && pc.rPr !== cur) (out += `<w:r>${cur}${body}</w:r>`, (body = ""));
|
|
132
|
+
cur = pc.rPr;
|
|
133
|
+
body += tagXml(pc, del);
|
|
134
|
+
}
|
|
135
|
+
if (cur !== null) out += `<w:r>${cur}${body}</w:r>`;
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Splits text pieces at character offsets [s, e) of the paragraph text. */
|
|
140
|
+
function slice(pieces, s, e) {
|
|
141
|
+
const out = [];
|
|
142
|
+
let at = 0;
|
|
143
|
+
for (const pc of pieces) {
|
|
144
|
+
if (pc.kind !== "text") continue;
|
|
145
|
+
const a = at;
|
|
146
|
+
const b = at + pc.text.length;
|
|
147
|
+
at = b;
|
|
148
|
+
if (b <= s || a >= e) continue;
|
|
149
|
+
const from = Math.max(s, a) - a;
|
|
150
|
+
const to = Math.min(e, b) - a;
|
|
151
|
+
out.push({ ...pc, text: pc.text.slice(from, to) });
|
|
152
|
+
}
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
/** Offsets of the anchors among the pieces: [{at, raw}] */
|
|
156
|
+
function anchorsOf(pieces) {
|
|
157
|
+
const out = [];
|
|
158
|
+
let at = 0;
|
|
159
|
+
for (const pc of pieces) {
|
|
160
|
+
if (pc.kind === "text") at += pc.text.length;
|
|
161
|
+
else out.push({ at, raw: pc.raw });
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The open tag of a paragraph that gets content: an empty <w:p .../> becomes <w:p ...>. */
|
|
167
|
+
const openOf = (node) => (node.close ? node.open : node.open.replace(/\s*\/>$/, ">"));
|
|
168
|
+
|
|
169
|
+
/** Adds an empty w:ins / w:del as the first child of the paragraph mark's w:rPr (contract M7, M8). `pPrXml` is "" or a serialized w:pPr. */
|
|
170
|
+
function markPPr(pPrNode, mark) {
|
|
171
|
+
if (!pPrNode) return `<w:pPr><w:rPr>${mark}</w:rPr></w:pPr>`;
|
|
172
|
+
const kids = pPrNode.kids;
|
|
173
|
+
const rPr = kids.find((k) => k.t === "el" && k.name === "w:rPr");
|
|
174
|
+
const inner = (k) => (k.t === "el" && k.name === "w:rPr" ? (k.close ? k.open + mark + k.kids.map(serialize).join("") + k.close : `${k.open.replace(/\s*\/>$/, ">")}${mark}</w:rPr>`) : serialize(k));
|
|
175
|
+
let body;
|
|
176
|
+
if (rPr) body = kids.map(inner).join("");
|
|
177
|
+
else {
|
|
178
|
+
const at = kids.findIndex((k) => k.t === "el" && (k.name === "w:sectPr" || k.name === "w:pPrChange"));
|
|
179
|
+
const list = kids.map(serialize);
|
|
180
|
+
list.splice(at < 0 ? list.length : at, 0, `<w:rPr>${mark}</w:rPr>`);
|
|
181
|
+
body = list.join("");
|
|
182
|
+
}
|
|
183
|
+
const open = pPrNode.close ? pPrNode.open : pPrNode.open.replace(/\s*\/>$/, ">");
|
|
184
|
+
return `${open}${body}</w:pPr>`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ---- the build --------------------------------------------------------------------------------------------------------
|
|
188
|
+
|
|
189
|
+
/** XML text of every part, for the largest id. */
|
|
190
|
+
function maxIdOf(zip) {
|
|
191
|
+
let max = 0;
|
|
192
|
+
for (const n of zip.names) {
|
|
193
|
+
if (!/\.xml$/i.test(n)) continue;
|
|
194
|
+
walkXml(zip.readText(n), {
|
|
195
|
+
onOpen(_, a) {
|
|
196
|
+
const v = a["w:id"];
|
|
197
|
+
if (v != null && /^\d{1,12}$/.test(v)) max = Math.max(max, Number(v));
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return max;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function styleIds(xml) {
|
|
205
|
+
const ids = new Set();
|
|
206
|
+
if (!xml) return ids;
|
|
207
|
+
walkXml(xml, { onOpen: (name, a) => name === "w:style" && a["w:styleId"] && ids.add(a["w:styleId"]) });
|
|
208
|
+
return ids;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* {before, after}: DOCX bytes; {author, date}: what every revision carries. Returns {bytes, counts: {ins, del, paras_added, paras_removed,
|
|
213
|
+
* moved, formatting}} or {refused: [codes]}. `formatting` is how many paragraphs Reject All leaves with formatting other than BEFORE's.
|
|
214
|
+
*/
|
|
215
|
+
export function buildTracked({ before, after, author, date }) {
|
|
216
|
+
try {
|
|
217
|
+
return build(before, after, author, date);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
if (err instanceof Refused) return { refused: [...new Set(err.codes)] };
|
|
220
|
+
return { refused: ["unreadable"], detail: String(err?.message || err).slice(0, 200) };
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function build(beforeBytes, afterBytes, author, date) {
|
|
225
|
+
const zb = openZip(beforeBytes);
|
|
226
|
+
const za = openZip(afterBytes);
|
|
227
|
+
const codes = [];
|
|
228
|
+
// ---- parts: only word/document.xml may differ (contract section 6: the tolerated list is empty)
|
|
229
|
+
const names = new Set([...zb.names, ...za.names]);
|
|
230
|
+
for (const n of names) {
|
|
231
|
+
if (n === "word/document.xml") continue;
|
|
232
|
+
const same = zb.entries.has(n) && za.entries.has(n) && (zb.raw(n).equals(za.raw(n)) || zb.read(n, 512 * 1024 * 1024).equals(za.read(n, 512 * 1024 * 1024)));
|
|
233
|
+
if (!same) codes.push(NOTES_PART.test(n) ? "notes_part" : "other_part");
|
|
234
|
+
}
|
|
235
|
+
const xb = zb.readText("word/document.xml");
|
|
236
|
+
const xa = za.readText("word/document.xml");
|
|
237
|
+
if (xb == null || xa == null) throw new Refused(["unreadable"]);
|
|
238
|
+
const tb = parse(xb);
|
|
239
|
+
const ta = parse(xa);
|
|
240
|
+
for (const t of [tb, ta]) for (const e of descendants(t)) if (REVISIONS.has(e.name)) throw new Refused(["has_revisions"]); // its own message
|
|
241
|
+
const LB = listParagraphs(tb);
|
|
242
|
+
const LA = listParagraphs(ta);
|
|
243
|
+
const A = parseDocxParagraphs(xb);
|
|
244
|
+
const B = parseDocxParagraphs(xa);
|
|
245
|
+
// the tree walk and the shared reader must list the same paragraphs, or nothing here can be trusted
|
|
246
|
+
if (A.length !== LB.paras.length || B.length !== LA.paras.length) throw new Refused(["unreadable"]);
|
|
247
|
+
for (const p of [...A, ...B]) if (BAD_CHAR.test(p.text)) codes.push("bad_char");
|
|
248
|
+
if (JSON.stringify(LB.tables) !== JSON.stringify(LA.tables)) codes.push("table_shape");
|
|
249
|
+
const stylesAfter = styleIds(za.readText("word/styles.xml"));
|
|
250
|
+
|
|
251
|
+
// ---- pairing. Table shapes are equal (gated), so a cell paragraph pairs with the one in the same table, row, column and position.
|
|
252
|
+
// Every other paragraph goes through History's matcher; a pair that sits between other tables, or crosses an earlier pair, is a move
|
|
253
|
+
// (removed at the old place, added at the new one).
|
|
254
|
+
const pairB = new Int32Array(B.length).fill(-1);
|
|
255
|
+
const pairA = new Int32Array(A.length).fill(-1);
|
|
256
|
+
const cellKey = (P, L, k) => (L.paras[k].ctx === "cell" ? `${P[k].tableNo}|${P[k].path.slice(1).join(",")}` : null);
|
|
257
|
+
const cellsA = new Map();
|
|
258
|
+
A.forEach((_, i) => cellKey(A, LB, i) && cellsA.set(cellKey(A, LB, i), i));
|
|
259
|
+
B.forEach((_, j) => {
|
|
260
|
+
const i = cellsA.get(cellKey(B, LA, j));
|
|
261
|
+
if (cellKey(B, LA, j) && i != null) (pairB[j] = i), (pairA[i] = j);
|
|
262
|
+
});
|
|
263
|
+
const restA = A.map((_, i) => i).filter((i) => LB.paras[i].ctx !== "cell");
|
|
264
|
+
const restB = B.map((_, j) => j).filter((j) => LA.paras[j].ctx !== "cell");
|
|
265
|
+
const m = matchParagraphs(restA.map((i) => A[i]), restB.map((j) => B[j]));
|
|
266
|
+
// Ceiling: the demotion is greedy in AFTER order, so one pair matched far forward turns the later pairs into moves (a correct copy, just a
|
|
267
|
+
// noisier one). Upgrade path: keep the longest increasing subsequence of the pairs instead.
|
|
268
|
+
let lastI = -1;
|
|
269
|
+
const movedPairs = new Set();
|
|
270
|
+
restB.forEach((j, jj) => {
|
|
271
|
+
if (m.mapB[jj] < 0) return;
|
|
272
|
+
const i = restA[m.mapB[jj]];
|
|
273
|
+
if (m.moved.has(jj) || i < lastI || LB.paras[i].ctx !== LA.paras[j].ctx || LB.paras[i].seg !== LA.paras[j].seg) return movedPairs.add(j);
|
|
274
|
+
pairB[j] = i;
|
|
275
|
+
pairA[i] = j;
|
|
276
|
+
lastI = i;
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const plainA = A.map((_, i) => piecesOf(LB.paras[i].node));
|
|
280
|
+
const plainB = B.map((_, j) => piecesOf(LA.paras[j].node));
|
|
281
|
+
const gate = (side, k, { whole }) => {
|
|
282
|
+
const L = side === "A" ? LB : LA;
|
|
283
|
+
const P = side === "A" ? A : B;
|
|
284
|
+
const plain = side === "A" ? plainA : plainB;
|
|
285
|
+
const { ctx, node } = L.paras[k];
|
|
286
|
+
if (ctx === "nested") return codes.push("nested_table");
|
|
287
|
+
if (ctx === "other") return codes.push("sdt");
|
|
288
|
+
if (P[k].protected || !plain[k]) return codes.push("protected");
|
|
289
|
+
if (textOfPieces(plain[k].pieces) !== P[k].text) return codes.push("unreadable");
|
|
290
|
+
if (!whole) return;
|
|
291
|
+
if (ctx === "cell") return codes.push("table_para");
|
|
292
|
+
if (plain[k].pPr && elements(plain[k].pPr).some((e) => e.name === "w:sectPr")) return codes.push("section");
|
|
293
|
+
const sib = node.parent.kids.slice(node.parent.kids.indexOf(node) + 1).find((x) => x.t === "el");
|
|
294
|
+
if (!sib || sib.name === "w:sectPr") return codes.push("at_end");
|
|
295
|
+
if (sib.name !== "w:p") return codes.push("next_to_table");
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const modified = new Map(); // j -> i
|
|
299
|
+
for (let j = 0; j < B.length; j++) {
|
|
300
|
+
const i = pairB[j];
|
|
301
|
+
if (i < 0) continue;
|
|
302
|
+
if (A[i].text !== B[j].text) {
|
|
303
|
+
gate("A", i, { whole: false });
|
|
304
|
+
gate("B", j, { whole: false });
|
|
305
|
+
modified.set(j, i);
|
|
306
|
+
} else if ((A[i].protected || B[j].protected) && serialize(LB.paras[i].node) !== serialize(LA.paras[j].node)) codes.push("protected"); // a link or field changed under the same text
|
|
307
|
+
}
|
|
308
|
+
const added = [];
|
|
309
|
+
for (let j = 0; j < B.length; j++) if (pairB[j] < 0) (gate("B", j, { whole: true }), added.push(j));
|
|
310
|
+
const removed = [];
|
|
311
|
+
for (let i = 0; i < A.length; i++) if (pairA[i] < 0) (gate("A", i, { whole: true }), removed.push(i));
|
|
312
|
+
if (codes.length) throw new Refused(codes);
|
|
313
|
+
if (!modified.size && !added.length && !removed.length) throw new Refused(["no_changes"]);
|
|
314
|
+
|
|
315
|
+
// ---- where each removed paragraph goes: before the AFTER match of the next kept BEFORE paragraph (its siblings are paragraphs: gated)
|
|
316
|
+
const insertBefore = new Map(); // AFTER paragraph index -> [BEFORE indexes, in order]
|
|
317
|
+
for (const i of removed) {
|
|
318
|
+
let k = i + 1;
|
|
319
|
+
while (k < A.length && pairA[k] < 0) k++;
|
|
320
|
+
const j = k < A.length ? pairA[k] : -1;
|
|
321
|
+
if (j < 0 || LA.paras[j].ctx !== "body" || LB.paras[k].ctx !== "body") throw new Refused(["at_end"]);
|
|
322
|
+
(insertBefore.get(j) || insertBefore.set(j, []).get(j)).push(i);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// ---- markup, generated in document order so ids rise through the document
|
|
326
|
+
let nextId = Math.max(maxIdOf(za), maxIdOf(zb)) + 1;
|
|
327
|
+
const counts = { ins: 0, del: 0, paras_added: added.length, paras_removed: removed.length, moved: movedPairs.size, formatting: 0 };
|
|
328
|
+
const rev = (kind) => {
|
|
329
|
+
if (nextId > MAX_ID) throw new Refused(["id_overflow"]);
|
|
330
|
+
counts[kind]++;
|
|
331
|
+
return `w:id="${nextId++}" w:author="${escAttr(author)}" w:date="${escAttr(date)}"`;
|
|
332
|
+
};
|
|
333
|
+
const stylesOk = (rPrXml) => {
|
|
334
|
+
if (!rPrXml) return true;
|
|
335
|
+
const t = parse(rPrXml);
|
|
336
|
+
const rPr = elements(t)[0];
|
|
337
|
+
return elements(rPr).every((e) => DIRECT_RPR.has(e.name) || (e.name === "w:rStyle" && stylesAfter.has(attr(e, "w:val"))));
|
|
338
|
+
};
|
|
339
|
+
const predicted = new Map(); // BEFORE index -> the paragraph Reject All gives, as XML (for the formatting count)
|
|
340
|
+
|
|
341
|
+
const removedXml = (i) => {
|
|
342
|
+
const { pPr, pieces } = plainA[i];
|
|
343
|
+
const keep = pPr ? elements(pPr).filter((e) => KEEP_PPR.has(e.name) && (e.name !== "w:pStyle" || stylesAfter.has(attr(e, "w:val")))) : [];
|
|
344
|
+
const keptPPr = keep.map(serialize).join("");
|
|
345
|
+
const text = pieces.filter((x) => x.kind === "text").map((pc) => (stylesOk(pc.rPr) ? pc : { ...pc, rPr: "" }));
|
|
346
|
+
predicted.set(i, `<w:p><w:pPr>${keptPPr}</w:pPr>${runsXml(text, false)}</w:p>`);
|
|
347
|
+
const mark = `<w:del ${rev("del")}/>`;
|
|
348
|
+
return `<w:p><w:pPr>${keptPPr}<w:rPr>${mark}</w:rPr></w:pPr>${text.length ? `<w:del ${rev("del")}>${runsXml(text, true)}</w:del>` : ""}</w:p>`;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const addedXml = (j) => {
|
|
352
|
+
const node = LA.paras[j].node;
|
|
353
|
+
const { pPr, pieces } = plainB[j];
|
|
354
|
+
const mark = `<w:ins ${rev("ins")}/>`;
|
|
355
|
+
let body = "";
|
|
356
|
+
let group = [];
|
|
357
|
+
const flush = () => {
|
|
358
|
+
if (group.length) body += `<w:ins ${rev("ins")}>${runsXml(group, false)}</w:ins>`;
|
|
359
|
+
group = [];
|
|
360
|
+
};
|
|
361
|
+
for (const pc of pieces) {
|
|
362
|
+
if (pc.kind === "anchor") (flush(), (body += pc.raw));
|
|
363
|
+
else group.push(pc);
|
|
364
|
+
}
|
|
365
|
+
flush();
|
|
366
|
+
return `${openOf(node)}${markPPr(pPr, mark)}${body}</w:p>`;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const modifiedXml = (j, i) => {
|
|
370
|
+
const node = LA.paras[j].node;
|
|
371
|
+
const { pPr, pieces: pb } = plainB[j];
|
|
372
|
+
const pa = plainA[i].pieces;
|
|
373
|
+
const ops = diffRaw(A[i].text, B[j].text);
|
|
374
|
+
const anchors = anchorsOf(pb);
|
|
375
|
+
let ai = 0;
|
|
376
|
+
let out = "";
|
|
377
|
+
let rejected = ""; // the runs Reject All gives back: kept AFTER text plus restored BEFORE text
|
|
378
|
+
const flushAnchors = (upto) => {
|
|
379
|
+
while (ai < anchors.length && anchors[ai].at <= upto) out += anchors[ai++].raw;
|
|
380
|
+
};
|
|
381
|
+
const emitAfter = (s, e, wrap) => {
|
|
382
|
+
const cuts = [s, ...anchors.map((x) => x.at).filter((x) => x > s && x < e), e];
|
|
383
|
+
for (let c = 0; c + 1 < cuts.length; c++) {
|
|
384
|
+
flushAnchors(cuts[c]);
|
|
385
|
+
const runs = runsXml(slice(pb, cuts[c], cuts[c + 1]), false);
|
|
386
|
+
if (!runs) continue;
|
|
387
|
+
out += wrap ? `<w:ins ${rev("ins")}>${runs}</w:ins>` : runs;
|
|
388
|
+
if (!wrap) rejected += runs;
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
let aPos = 0;
|
|
392
|
+
let bPos = 0;
|
|
393
|
+
for (const [t, text] of ops) {
|
|
394
|
+
const n = text.length;
|
|
395
|
+
if (t === "=") (emitAfter(bPos, bPos + n, false), (aPos += n), (bPos += n));
|
|
396
|
+
else if (t === "+") (emitAfter(bPos, bPos + n, true), (bPos += n));
|
|
397
|
+
else {
|
|
398
|
+
flushAnchors(bPos);
|
|
399
|
+
// BEFORE's run properties when they are plain direct formatting; otherwise the AFTER properties next to the place
|
|
400
|
+
const near = slice(pb, Math.max(0, bPos - 1), bPos + 1)[0]?.rPr ?? "";
|
|
401
|
+
const pieces = slice(pa, aPos, aPos + n).map((pc) => (stylesOk(pc.rPr) ? pc : { ...pc, rPr: near }));
|
|
402
|
+
out += `<w:del ${rev("del")}>${runsXml(pieces, true)}</w:del>`;
|
|
403
|
+
rejected += runsXml(pieces, false);
|
|
404
|
+
aPos += n;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
flushAnchors(Infinity);
|
|
408
|
+
predicted.set(i, `<w:p>${pPr ? serialize(pPr) : ""}${rejected}</w:p>`);
|
|
409
|
+
return `${openOf(node)}${pPr ? serialize(pPr) : ""}${out}</w:p>`;
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
// replace the changed AFTER paragraphs with lazily built markup and slot the removed BEFORE paragraphs in; serialize builds in order
|
|
413
|
+
const addedSet = new Set(added);
|
|
414
|
+
for (let j = 0; j < B.length; j++) {
|
|
415
|
+
const node = LA.paras[j].node;
|
|
416
|
+
const lazies = [];
|
|
417
|
+
for (const i of insertBefore.get(j) || []) lazies.push({ t: "lazy", parent: node.parent, build: () => removedXml(i) });
|
|
418
|
+
let self = null;
|
|
419
|
+
if (addedSet.has(j)) self = { t: "lazy", parent: node.parent, build: () => addedXml(j) };
|
|
420
|
+
else if (modified.has(j)) self = { t: "lazy", parent: node.parent, build: () => modifiedXml(j, modified.get(j)) };
|
|
421
|
+
if (!lazies.length && !self) continue;
|
|
422
|
+
const list = node.parent.kids;
|
|
423
|
+
list.splice(list.indexOf(node), 1, ...lazies, self || node);
|
|
424
|
+
}
|
|
425
|
+
const xml = serialize(ta);
|
|
426
|
+
|
|
427
|
+
// ---- the formatting count: paragraphs Reject All gives back with formatting other than BEFORE's (History's comparison)
|
|
428
|
+
for (let i = 0; i < A.length; i++) if (!predicted.has(i) && pairA[i] >= 0) predicted.set(i, null);
|
|
429
|
+
const order = [...predicted.keys()].filter((i) => predicted.get(i) !== null);
|
|
430
|
+
const probe = order.length ? parseDocxParagraphs(`<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>${order.map((i) => predicted.get(i)).join("")}</w:body></w:document>`) : [];
|
|
431
|
+
const slot = new Map(order.map((i, k) => [i, k]));
|
|
432
|
+
const fmtOf = (p) => ({ props: p.fmt, style: p.style, align: p.align });
|
|
433
|
+
for (const [i, x] of predicted) {
|
|
434
|
+
const back = x === null ? B[pairA[i]] : probe[slot.get(i)];
|
|
435
|
+
if (normText(A[i].text) && fmtNote(fmtOf(A[i]), fmtOf(back))) counts.formatting++;
|
|
436
|
+
}
|
|
437
|
+
return { bytes: replaceEntry(afterBytes, "word/document.xml", xml), counts };
|
|
438
|
+
}
|