onionskin-cli 0.0.2 → 0.0.5
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 +111 -0
- package/INSTALL.md +5 -1
- package/NOTICE +34 -0
- package/README.md +101 -33
- package/docs/contracts/batch.schema.json +27 -4
- package/docs/contracts/ops.schema.json +117 -5
- package/package.json +4 -2
- package/skills/onionskin/SKILL.md +9 -6
- package/skills/onionskin/reference/batch.md +5 -3
- package/skills/onionskin/reference/formats.md +5 -1
- package/skills/onionskin/reference/ops.md +47 -3
- package/skills/onionskin/reference/troubleshooting.md +31 -0
- package/src/agent-detect.js +28 -2
- package/src/agent-prompt.js +12 -1
- package/src/agent-runner.js +77 -19
- package/src/anchor-work.js +154 -0
- package/src/anchor-worker.js +38 -0
- package/src/anchors.js +61 -11
- package/src/apply.js +119 -47
- package/src/batch.js +32 -8
- package/src/check-worker.js +8 -5
- package/src/cli.js +91 -25
- package/src/constants.js +34 -1
- package/src/diff.js +3 -1
- package/src/docx.js +368 -58
- package/src/edit-hash.js +19 -0
- package/src/fsutil.js +1 -1
- package/src/history-worker.js +8 -8
- package/src/history.js +135 -31
- package/src/html-active.js +384 -0
- package/src/html-envelope.js +51 -0
- package/src/html-parser.js +8 -0
- package/src/html-render-worker.js +25 -0
- package/src/html-render.js +880 -0
- package/src/html-utf8.js +65 -0
- package/src/html-write-worker.js +14 -0
- package/src/html-write.js +380 -0
- package/src/html.js +1257 -0
- package/src/local.js +68 -4
- package/src/merge-package.js +1110 -0
- package/src/merge-pptx.js +98 -38
- package/src/merge-proof.js +1019 -0
- package/src/merge-worker.js +60 -0
- package/src/merge-xlsx.js +84 -29
- package/src/merge.js +286 -83
- package/src/nfc.js +46 -0
- package/src/notes.js +122 -0
- package/src/offload.js +400 -1
- package/src/open-worker.js +27 -0
- package/src/ops/common.js +2 -1
- package/src/ops/docx.js +58 -29
- package/src/ops/html.js +394 -0
- package/src/ops/pptx.js +74 -21
- package/src/ops/run.js +23 -3
- package/src/ops/runs.js +215 -13
- package/src/ops/schema.js +8 -1
- package/src/ops/xlsx.js +12 -7
- package/src/ops-worker.js +17 -0
- package/src/package.js +26 -2
- package/src/person.js +8 -1
- package/src/pptx.js +85 -21
- package/src/quick-plan.js +13 -6
- package/src/redline-worker.js +10 -14
- package/src/reply.js +46 -19
- package/src/retain.js +223 -0
- package/src/review.js +32 -18
- package/src/rewrite-check.js +2 -2
- package/src/sanitize.js +16 -3
- package/src/server.js +88 -37
- package/src/session.js +1143 -208
- package/src/stop-gate.js +34 -0
- package/src/suggest-reply.js +42 -7
- package/src/suggest.js +14 -29
- package/src/text.js +161 -11
- package/src/update.js +288 -0
- package/src/voice-guide.js +4 -3
- package/src/voice-learn.js +4 -3
- package/src/voice-sample-worker.js +7 -6
- package/src/voice-worker.js +4 -6
- package/src/watch.js +188 -0
- package/src/worker-host.js +23 -0
- package/src/xlsx.js +355 -94
- package/src/xml-tree.js +6 -3
- package/src/xml.js +82 -34
- package/src/zip-write.js +2 -1
- package/src/zip.js +9 -5
- package/vendor/parse5/LICENSE.txt +32 -0
- package/vendor/parse5/parse5.mjs +7822 -0
- package/vendor/parse5.lock.json +23 -0
- package/web/dist/assets/index-DkNx0LSH.css +1 -0
- package/web/dist/assets/{index-BUtofdtr.js → index-DytKkMdi.js} +117 -107
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-Dfizlg0i.css +0 -1
|
@@ -0,0 +1,1019 @@
|
|
|
1
|
+
import { paraIdentity } from "./history.js";
|
|
2
|
+
import { byFormat } from "./constants.js";
|
|
3
|
+
import { readDocxParagraphs } from "./docx.js";
|
|
4
|
+
import { parseDeck } from "./pptx.js";
|
|
5
|
+
import { parseWorkbook } from "./xlsx.js";
|
|
6
|
+
import { untrusted } from "./sanitize.js";
|
|
7
|
+
import { Interner, cellStyles, docxUnits, lookCarried, lookLooseSame, numberingOf, partLabel, readPackage, residueCarried, residueLooseSame, sameLookKept, sameResidue, sameStored, sharedRich, sheetUnits, slideUnits, twinSame, unitCarried, unitLooseSame, unitSame, sameVolatile, looseSame } from "./merge-package.js";
|
|
8
|
+
|
|
9
|
+
// The proof of a merge (src/merge-worker.js runs it after every merge, before anything is written or returned). The planners
|
|
10
|
+
// (src/merge.js, src/merge-pptx.js, src/merge-xlsx.js) decide what to carry; this file checks the outcome on its own terms, from the
|
|
11
|
+
// raw content of the four versions: base (what the page loaded), mine (the person's), theirs (the agent's) and the merged result read
|
|
12
|
+
// back from its bytes (theirs when the merge wrote nothing). It shares only the readers and the paragraph identity rule
|
|
13
|
+
// (paraIdentity in src/history.js) with the planners: no matcher, no hash of normalized text, no report of the engine.
|
|
14
|
+
//
|
|
15
|
+
// Every difference between base and mine (raw text with every space, tab and line break as written, the formatting the planner
|
|
16
|
+
// claims to carry, order, inserts and deletes with their multiplicity) must be in exactly one of two states: in the merged result at
|
|
17
|
+
// the same identity, or handed back in `unplaced` by an entry that names it (`of`: the refs below) and, for text, holds the text.
|
|
18
|
+
// A kind of change the merge does not carry is no exception: it must be handed back too. The other direction holds as well: nothing
|
|
19
|
+
// in the result may read differently from the agent's version except where the person's change was carried, and nothing the agent
|
|
20
|
+
// wrote may be missing. When the accounting does not balance, the merge returns no file (the caller's existing "could not be merged"
|
|
21
|
+
// outcome: nothing is written, the person's edits stay in the page and a copy of their version is saved).
|
|
22
|
+
//
|
|
23
|
+
// Refs (set by the planners on each unplaced entry as `of`, stripped before the answer leaves the worker):
|
|
24
|
+
// DOCX "m<n>" paragraph n of mine, "b<n>" paragraph n of base (n: the reader's ordinal)
|
|
25
|
+
// PPTX "s<slide id>" a slide, "h<slide id>/<shape key>" a shape on it, "order" the order of slides
|
|
26
|
+
// XLSX "S<sheet key>" a sheet, "c<sheet key>!<cell key>" a cell, "sheets" the list of sheets
|
|
27
|
+
// An entry for a whole slide or sheet covers text only when the merge also keeps a copy of the person's file (`copy`).
|
|
28
|
+
//
|
|
29
|
+
// Complete by construction (src/merge-package.js): beyond the units modeled one by one above, every part of the package is compared
|
|
30
|
+
// whole between the versions (headers, footers, footnotes, comments, styles, lists, media, charts, relationships, properties...), the
|
|
31
|
+
// main parts outside their units too (tables and sections, a slide's background, a sheet's columns, rows and merged ranges), and each
|
|
32
|
+
// unit's residue: whatever its canonical XML holds beyond what is modeled (a paragraph's objects and other properties, a shape's place,
|
|
33
|
+
// size and geometry, a cell's whole style). Equal means equal canonical XML; the few attributes the engine rewrites on its own
|
|
34
|
+
// (VOLATILE, measured) are judged by one rule (src/merge-package.js sameVolatile): a difference between base and mine is the person's. A difference of a kind no planner carries is handed back by handBack (src/merge-worker.js
|
|
35
|
+
// runs it before the proof: one plain entry per kind, and a copy of the person's file); one it cannot name refuses the merge. The proof
|
|
36
|
+
// works from the bytes of the four versions and the merge's answer only: never from the page's list of edits.
|
|
37
|
+
//
|
|
38
|
+
// Cost: linear in the size of the four versions, except the longest increasing subsequence that tells moved paragraphs and shapes
|
|
39
|
+
// from the rest (n log n). The merged result is read once more (the planners' readings are not reused for it, so nothing they did to
|
|
40
|
+
// them counts); the package is read once for handBack and the proof together (prepareMerge). A part or slide or sheet whose stored
|
|
41
|
+
// bytes are the same between base and mine and between theirs and the result is not read at all.
|
|
42
|
+
|
|
43
|
+
const CAP = 2000; // the planners cut the text of an unplaced entry here (src/merge.js, src/merge-pptx.js, src/merge-xlsx.js)
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Does the text of an unplaced entry hold `want`: all of it, or the first CAP characters when the planner cut it and the person's
|
|
47
|
+
* file is kept (`copy`: the rest is only there; src/merge-worker.js keeps one whenever an entry's text was cut).
|
|
48
|
+
*/
|
|
49
|
+
const holds = (got, want, copy) => typeof got === "string" && (got === want || (want.length > CAP && copy && got.startsWith(want.slice(0, CAP))));
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* An index of the unplaced entries by ref, and the check that a difference is handed back: by an entry that names the unit and, for
|
|
53
|
+
* text, holds it; or by an entry that names the whole slide or sheet (`box`), which holds text only when the person's file is kept.
|
|
54
|
+
*/
|
|
55
|
+
function handedBack(unplaced, copy) {
|
|
56
|
+
// entries that say a kind (`as`, src/merge-proof.js handBack) cover that kind only, never text, a move or anything else
|
|
57
|
+
const by = new Map();
|
|
58
|
+
const byKind = new Map();
|
|
59
|
+
for (const u of unplaced || []) for (const ref of Array.isArray(u?.of) ? u.of : []) {
|
|
60
|
+
const into = u.as ? byKind : by;
|
|
61
|
+
(into.get(ref) || into.set(ref, []).get(ref)).push(u);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* refs: the unit's own refs; text: what must be shown (null: nothing to show); box: the slide or sheet a whole entry may name; look:
|
|
65
|
+
* the difference is formatting, the one thing a line that only asks to check (`check: true`, said of an edit that WAS carried) covers
|
|
66
|
+
*/
|
|
67
|
+
const back = (refs, text = null, box = null, look = false) => {
|
|
68
|
+
for (const ref of refs) for (const u of by.get(ref) || []) if ((look || !u.check) && (text === null || holds(u.text, text, copy))) return true;
|
|
69
|
+
return !!(box && by.has(box) && (copy || text === null));
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* A difference of a kind the planners do not carry (src/merge-package.js: "look", formatting; any other kind: an object, a shape's
|
|
73
|
+
* place or size, a table's layout, a part): handed back by an entry that names the unit and says that kind (`as`), or for formatting
|
|
74
|
+
* by any entry that names the unit; or by an entry for the whole slide or sheet when the person's file is kept.
|
|
75
|
+
*/
|
|
76
|
+
back.kind = (refs, kind, box = null) => {
|
|
77
|
+
for (const ref of refs) {
|
|
78
|
+
for (const u of byKind.get(ref) || []) if (u.as === kind) return true;
|
|
79
|
+
if (kind === "look" && by.has(ref)) return true;
|
|
80
|
+
}
|
|
81
|
+
return !!(box && by.has(box) && copy);
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* named by an entry for the whole slide or sheet that says this kind of change (a planner's "You changed formatting on the sheet"),
|
|
85
|
+
* which covers it once the person's file is kept: handBack then asks for the copy instead of a second line
|
|
86
|
+
*/
|
|
87
|
+
const SAYS = { look: /formatting/i, layout: /row or column sizes|layout/i, shape: /shape/i };
|
|
88
|
+
back.boxed = (box, kind) => !!(box && SAYS[kind] && (by.get(box) || []).some((u) => !u.check && SAYS[kind].test(u.why ?? "")));
|
|
89
|
+
return back;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The tally of what did not balance; `fail` is a short reason with counts, never document text. */
|
|
93
|
+
class Tally {
|
|
94
|
+
/** `noun`: what a unit is in this format (paragraphs, shapes, cells) */
|
|
95
|
+
constructor(noun) {
|
|
96
|
+
this.noun = noun;
|
|
97
|
+
this.kinds = new Map();
|
|
98
|
+
}
|
|
99
|
+
add(kind) {
|
|
100
|
+
kind = kind.replace(/\bunits\b/g, this.noun);
|
|
101
|
+
this.kinds.set(kind, (this.kinds.get(kind) || 0) + 1);
|
|
102
|
+
}
|
|
103
|
+
get fail() {
|
|
104
|
+
if (!this.kinds.size) return null;
|
|
105
|
+
return [...this.kinds].map(([k, n]) => `${k}: ${n}`).join("; ");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Longest increasing subsequence of `seq` (numbers): a Uint8Array marking the members of one. n log n. */
|
|
110
|
+
function lis(seq) {
|
|
111
|
+
const tails = [];
|
|
112
|
+
const tailAt = [];
|
|
113
|
+
const prev = new Int32Array(seq.length).fill(-1);
|
|
114
|
+
for (let k = 0; k < seq.length; k++) {
|
|
115
|
+
let lo = 0;
|
|
116
|
+
let hi = tails.length;
|
|
117
|
+
while (lo < hi) {
|
|
118
|
+
const mid = (lo + hi) >> 1;
|
|
119
|
+
if (tails[mid] < seq[k]) lo = mid + 1;
|
|
120
|
+
else hi = mid;
|
|
121
|
+
}
|
|
122
|
+
tails[lo] = seq[k];
|
|
123
|
+
tailAt[lo] = k;
|
|
124
|
+
prev[k] = lo > 0 ? tailAt[lo - 1] : -1;
|
|
125
|
+
}
|
|
126
|
+
const keep = new Uint8Array(seq.length);
|
|
127
|
+
for (let k = tails.length ? tailAt[tails.length - 1] : -1; k >= 0; k = prev[k]) keep[k] = 1;
|
|
128
|
+
return keep;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The three-way accounting over one list of units (DOCX paragraphs, the shapes of one slide). Units are {raw, ref, parts: {name:
|
|
133
|
+
* value}}; `bm`, `bt`, `tr` pair base with mine, base with theirs, theirs with the result (Int32Array, -1: none). `ordered`: the order
|
|
134
|
+
* of the list is content (a move is a difference). `runs(u)` (DOCX): the run formatting of a unit by character, or null.
|
|
135
|
+
*/
|
|
136
|
+
function account({ B, M, T, R, bm, bt, tr, ordered, box = null, runs = null, twin = null }, back, tally) {
|
|
137
|
+
// mine's run formatting in the result: the same, or the engine's paired property (twinSame) where it reads the same
|
|
138
|
+
const sameMine = (m, r) => sameRuns(m, r, runs, twin);
|
|
139
|
+
const inv = (map, n) => {
|
|
140
|
+
const out = new Int32Array(n).fill(-1);
|
|
141
|
+
map.forEach((j, i) => j >= 0 && (out[j] = i));
|
|
142
|
+
return out;
|
|
143
|
+
};
|
|
144
|
+
const mb = inv(bm, M.length);
|
|
145
|
+
const tb = inv(bt, T.length);
|
|
146
|
+
const rt = inv(tr, R.length);
|
|
147
|
+
const need = (refs, text, kind) => back(refs, text, box, kind.includes("formatting")) || tally.add(kind);
|
|
148
|
+
const partNames = (u) => Object.keys(u.parts);
|
|
149
|
+
const shown = (u) => u.show ?? u.raw; // the text an entry handed back must hold
|
|
150
|
+
|
|
151
|
+
// Where an added unit belongs. The result's index of mine's unit j, when base, theirs and the result all have it:
|
|
152
|
+
const fullR = new Int32Array(M.length).fill(-1);
|
|
153
|
+
for (let j = 0; j < M.length; j++) {
|
|
154
|
+
const x = mb[j];
|
|
155
|
+
if (x >= 0 && bt[x] >= 0 && tr[bt[x]] >= 0) fullR[j] = tr[bt[x]];
|
|
156
|
+
}
|
|
157
|
+
// An added unit is in its place when the unit of the result right before it (other added units and the agent's new ones skipped) is
|
|
158
|
+
// the unit before it in mine, or the one right after it is the unit after it in mine. A neighbor in mine that the merge hands back
|
|
159
|
+
// (its move or its text is the person's to redo) has no settled place: then any unit back to the nearest one not handed back counts.
|
|
160
|
+
const toMine = new Int32Array(R.length).fill(-1);
|
|
161
|
+
for (let j = 0; j < M.length; j++) if (fullR[j] >= 0) toMine[fullR[j]] = j;
|
|
162
|
+
const prevFull = new Int32Array(R.length);
|
|
163
|
+
const nextFull = new Int32Array(R.length);
|
|
164
|
+
for (let r = 0, last = -1; r < R.length; r++) (prevFull[r] = last), toMine[r] >= 0 && (last = toMine[r]);
|
|
165
|
+
for (let r = R.length - 1, last = M.length; r >= 0; r--) (nextFull[r] = last), toMine[r] >= 0 && (last = toMine[r]);
|
|
166
|
+
const lo = new Int32Array(M.length);
|
|
167
|
+
const hi = new Int32Array(M.length);
|
|
168
|
+
const settled = (j) => fullR[j] >= 0 && !back([M[j].ref], null, box);
|
|
169
|
+
for (let j = 0, last = -1; j < M.length; j++) (lo[j] = last), settled(j) && (last = j);
|
|
170
|
+
for (let j = M.length - 1, last = M.length; j >= 0; j--) (hi[j] = last), settled(j) && (last = j);
|
|
171
|
+
const inPlace = (j, r) => !ordered || (prevFull[r] < j && prevFull[r] >= lo[j]) || (nextFull[r] > j && nextFull[r] <= hi[j]);
|
|
172
|
+
const group = (L, ok) => {
|
|
173
|
+
const g = new Map();
|
|
174
|
+
L.forEach((u, n) => ok(n) && (g.get(u.raw) || g.set(u.raw, []).get(u.raw)).push(n));
|
|
175
|
+
return g;
|
|
176
|
+
};
|
|
177
|
+
const added = group(M, (j) => mb[j] < 0); // the person's additions, by text
|
|
178
|
+
const fresh = group(R, (r) => rt[r] < 0); // the units the merge added, by text
|
|
179
|
+
const agentNew = group(T, (t) => tb[t] < 0);
|
|
180
|
+
// A unit rewritten whole has no identity left where it carries no id: a rewrite may be carried as a new unit in place of the old one,
|
|
181
|
+
// and a deletion next to an addition as the addition written into the deleted unit. Either counts when the text is the person's and
|
|
182
|
+
// the place is the person's (`take` claims the unit and the addition once).
|
|
183
|
+
const claimed = new Set(); // indexes in R of units claimed as the person's
|
|
184
|
+
const carried = new Set(); // indexes in M of additions claimed as carried
|
|
185
|
+
// the first entry of `list` that `ok` accepts; entries already claimed at its head are skipped once, not on every call (many units
|
|
186
|
+
// rewritten into one same text stay linear). Ceiling: an entry `ok` refuses for its place is tried again by the next call.
|
|
187
|
+
const head = new Map();
|
|
188
|
+
const take = (list, ok) => {
|
|
189
|
+
if (!list) return -1;
|
|
190
|
+
let k = head.get(list) || 0;
|
|
191
|
+
while (k < list.length && (claimed.has(list[k]) || carried.has(list[k]))) k++;
|
|
192
|
+
head.set(list, k);
|
|
193
|
+
for (; k < list.length; k++) if (ok(list[k])) return list[k];
|
|
194
|
+
return -1;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
for (let x = 0; x < B.length; x++) {
|
|
198
|
+
const b = B[x];
|
|
199
|
+
const m = bm[x];
|
|
200
|
+
const t = bt[x];
|
|
201
|
+
let r = t >= 0 ? tr[t] : -1;
|
|
202
|
+
const refs = m >= 0 ? [M[m].ref, b.ref] : [b.ref];
|
|
203
|
+
const pText = m >= 0 && M[m].raw !== b.raw;
|
|
204
|
+
const pDel = m < 0;
|
|
205
|
+
const aText = t >= 0 && T[t].raw !== b.raw;
|
|
206
|
+
let rewritten = false; // the person's text is in a unit the merge added in place of this one
|
|
207
|
+
if (t >= 0) {
|
|
208
|
+
if (r < 0) {
|
|
209
|
+
// gone from the result: a deletion of the person's of a unit the agent left as it was, or the person's rewrite carried as a new unit
|
|
210
|
+
const n = pText && !aText ? take(fresh.get(M[m].raw), (n) => !claimed.has(n) && inPlace(m, n)) : -1;
|
|
211
|
+
if (n >= 0) (claimed.add(n), (r = n), (rewritten = true));
|
|
212
|
+
else if (!(pDel && !aText)) tally.add("the agent's units missing from the result");
|
|
213
|
+
} else if (R[r].raw !== T[t].raw && !(pText && !aText && R[r].raw === M[m].raw)) {
|
|
214
|
+
// the result changed the agent's unit: to the person's text, or to an addition of the person's written in place of a deletion
|
|
215
|
+
const j = pDel && !aText ? take(added.get(R[r].raw), (j) => !carried.has(j) && inPlace(j, r)) : -1;
|
|
216
|
+
if (j >= 0) {
|
|
217
|
+
carried.add(j);
|
|
218
|
+
// the unit now is the person's addition: its formatting is the addition's
|
|
219
|
+
if (partNames(M[j]).some((k) => R[r].parts[k] !== M[j].parts[k]) || (runs && !sameMine(M[j], R[r]))) need([M[j].ref], null, "formatting of added units neither carried nor handed back");
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
tally.add("the agent's units changed into text neither side wrote there");
|
|
223
|
+
}
|
|
224
|
+
const kept = r >= 0 && R[r].raw === T[t].raw;
|
|
225
|
+
if (pText && kept && M[m].raw !== T[t].raw) {
|
|
226
|
+
// the rewrite carried as a new unit next to the old one, which stays (a deletion not carried, so it must be said)
|
|
227
|
+
const n = take(fresh.get(M[m].raw), (n) => !claimed.has(n) && inPlace(m, n));
|
|
228
|
+
if (n >= 0) (claimed.add(n), need(refs, null, "deletions neither carried nor handed back"));
|
|
229
|
+
else need(refs, shown(M[m]), "text edits neither carried nor handed back");
|
|
230
|
+
}
|
|
231
|
+
if (pDel && r >= 0 && R[r].raw === T[t].raw) need(refs, null, "deletions neither carried nor handed back");
|
|
232
|
+
} else if (pText) {
|
|
233
|
+
// the agent removed the unit: the person's text handed back, or carried as a new unit in its place
|
|
234
|
+
const n = take(fresh.get(M[m].raw), (n) => !claimed.has(n) && inPlace(m, n));
|
|
235
|
+
if (n >= 0) claimed.add(n);
|
|
236
|
+
else need(refs, shown(M[m]), "text edits of units the agent removed, not handed back");
|
|
237
|
+
}
|
|
238
|
+
// formatting the person changed: in the result (or the agent set the same), else handed back; the agent's own formatting is kept
|
|
239
|
+
for (const k of partNames(b)) {
|
|
240
|
+
const changed = m >= 0 && M[m].parts[k] !== b.parts[k];
|
|
241
|
+
const got = r >= 0 ? R[r].parts[k] : undefined;
|
|
242
|
+
// the place of a paragraph (the body, or which table and which cell) is no formatting: a line that only asks to check never covers it
|
|
243
|
+
if ((changed || rewritten) && got !== M[m].parts[k]) need(refs, null, k === "box" ? "moves into another table or cell neither carried nor handed back" : "formatting changes neither carried nor handed back");
|
|
244
|
+
if (r >= 0 && !rewritten && got !== T[t].parts[k] && !(changed && got === M[m].parts[k] && T[t].parts[k] === b.parts[k])) tally.add("formatting changes in the result that neither side made");
|
|
245
|
+
}
|
|
246
|
+
if (rewritten) {
|
|
247
|
+
if (runs && !sameMine(M[m], R[r])) need(refs, null, "formatting changes neither carried nor handed back");
|
|
248
|
+
} else if (runs && m >= 0 && r >= 0) runsCheck(b, M[m], T[t], R[r], refs, need, tally, runs, twin);
|
|
249
|
+
else if (runs && m >= 0 && runChanged(b, M[m], runs)) need(refs, null, "formatting changes neither carried nor handed back");
|
|
250
|
+
else if (runs && r >= 0 && !sameRuns(T[t], R[r], runs)) tally.add("formatting changes in the result that neither side made");
|
|
251
|
+
}
|
|
252
|
+
// what the agent added is in the result as it wrote it, formatting included
|
|
253
|
+
for (let t = 0; t < T.length; t++) {
|
|
254
|
+
if (tb[t] >= 0) continue;
|
|
255
|
+
const r = tr[t];
|
|
256
|
+
if (r < 0 || R[r].raw !== T[t].raw) tally.add("the agent's new units missing from the result");
|
|
257
|
+
else if (partNames(T[t]).some((k) => R[r].parts[k] !== T[t].parts[k]) || (runs && !sameRuns(T[t], R[r], runs))) tally.add("formatting changes in the result that neither side made");
|
|
258
|
+
}
|
|
259
|
+
// what the person added: in the result, in its place, once per copy; or handed back. A unit of the result that is in neither the
|
|
260
|
+
// agent's version nor among the person's additions is text nobody wrote.
|
|
261
|
+
for (const [raw, rs] of fresh) if (rs.filter((n) => !claimed.has(n)).length > (added.get(raw)?.filter((j) => !carried.has(j)).length || 0)) tally.add("units in the result that neither side wrote");
|
|
262
|
+
for (const [raw, js] of added) {
|
|
263
|
+
const rs = (fresh.get(raw) || []).filter((n) => !claimed.has(n));
|
|
264
|
+
let same = agentNew.get(raw)?.length || 0; // the agent added the same text: the person's copy may be that one
|
|
265
|
+
let k = 0;
|
|
266
|
+
for (const j of js) {
|
|
267
|
+
if (carried.has(j)) continue;
|
|
268
|
+
const r = k < rs.length ? rs[k++] : -1;
|
|
269
|
+
if (r >= 0 && inPlace(j, r)) {
|
|
270
|
+
for (const name of partNames(M[j])) if (R[r].parts[name] !== M[j].parts[name]) need([M[j].ref], null, "formatting of added units neither carried nor handed back");
|
|
271
|
+
if (runs && !sameMine(M[j], R[r])) need([M[j].ref], null, "formatting of added units neither carried nor handed back");
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (back([M[j].ref], shown(M[j]), box)) continue;
|
|
275
|
+
if (r < 0 && same > 0) {
|
|
276
|
+
same--;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
tally.add(r >= 0 ? "added units out of place" : "added units neither carried nor handed back");
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (ordered) orderCheck({ M, mb, bt, fullR }, back, box, tally);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Order: of the units base, mine, theirs and the result all have, the person moved those outside a longest run kept in base order. The
|
|
287
|
+
* rest keep the agent's order in the result; a moved one sits next to the neighbor it has in mine (before or after it), or is handed
|
|
288
|
+
* back. Which units moved is ambiguous (of two swapped, either one did): the order balances when it balances for one of four choices, the
|
|
289
|
+
* latest or the earliest longest run, over all of them or over those the merge does not hand back (a move it named is then the move).
|
|
290
|
+
*/
|
|
291
|
+
function orderCheck({ M, mb, bt, fullR }, back, box, tally) {
|
|
292
|
+
const js = [];
|
|
293
|
+
for (let j = 0; j < M.length; j++) if (fullR[j] >= 0) js.push(j);
|
|
294
|
+
const covered = js.map((j) => back([M[j].ref], null, box));
|
|
295
|
+
const order = js.map((j) => fullR[j]).sort((a, b) => a - b);
|
|
296
|
+
const at = new Map(order.map((r, k) => [r, k]));
|
|
297
|
+
// a longest run kept in base order, the latest one (`late`) or the earliest one (found on the sequence reversed and negated)
|
|
298
|
+
const movedBy = (pool, late) => {
|
|
299
|
+
const seq = pool.map((k) => mb[js[k]]);
|
|
300
|
+
const keep = late ? lis(seq) : lis(seq.reverse().map((v) => -v)).reverse();
|
|
301
|
+
return new Set(pool.filter((_, n) => !keep[n]));
|
|
302
|
+
};
|
|
303
|
+
/** the kinds that do not balance when the units at these indexes of js are the ones the person moved */
|
|
304
|
+
const judge = (moved) => {
|
|
305
|
+
const bad = [];
|
|
306
|
+
const byT = js.map((j, k) => ({ k, t: bt[mb[j]], r: fullR[j] })).filter((x) => !moved.has(x.k));
|
|
307
|
+
byT.sort((a, b) => a.t - b.t);
|
|
308
|
+
for (let n = 1; n < byT.length; n++) if (byT[n].r < byT[n - 1].r && !covered[byT[n].k] && !covered[byT[n - 1].k]) {
|
|
309
|
+
bad.push("units out of the agent's order");
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
for (const k of moved) {
|
|
313
|
+
const k2 = at.get(fullR[js[k]]);
|
|
314
|
+
const before = k > 0 ? fullR[js[k - 1]] : -1;
|
|
315
|
+
const after = k < js.length - 1 ? fullR[js[k + 1]] : -1;
|
|
316
|
+
if (before === (k2 > 0 ? order[k2 - 1] : -1) || after === (k2 < order.length - 1 ? order[k2 + 1] : -1) || covered[k]) continue;
|
|
317
|
+
bad.push("moves neither carried nor handed back");
|
|
318
|
+
}
|
|
319
|
+
return bad;
|
|
320
|
+
};
|
|
321
|
+
const all = js.map((_, k) => k);
|
|
322
|
+
const free = all.filter((k) => !covered[k]);
|
|
323
|
+
const first = judge(movedBy(all, true));
|
|
324
|
+
if (!first.length || [movedBy(all, false), movedBy(free, true), movedBy(free, false)].some((moved) => !judge(moved).length)) return;
|
|
325
|
+
for (const kind of first) tally.add(kind);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ---- DOCX ------------------------------------------------------------------------------------------------------------------------
|
|
329
|
+
|
|
330
|
+
// A paragraph's style, alignment and list as written (src/merge-package.js docxUnits `mod`: the canonical w:pStyle, w:jc and
|
|
331
|
+
// w:numPr), the list's w:numId written out as the definition it names (numberingOf). A style by its id: the definitions are compared
|
|
332
|
+
// where they are written, in word/styles.xml (an outside review: two lists of the same kind, or a style named the same, read as the
|
|
333
|
+
// same before). Ceiling: whether a list paragraph continues the numbering of the one before it is not compared, since it depends on
|
|
334
|
+
// the other side's lists too (a numbered paragraph the agent adds above renumbers the person's, measured: comparing it refused that
|
|
335
|
+
// ordinary merge); a list restarted on the same definition is not seen. Upgrade path: compare it only against the list paragraphs
|
|
336
|
+
// base, mine and the result all have.
|
|
337
|
+
const NUM_ID = /<\{[^}]*\}numId \{[^}]*\}val="([^"]*)"><\/>/;
|
|
338
|
+
const docxLists = (units, numbering) => units.map((u) => (u.mod["w:numPr"] ? u.mod["w:numPr"].replace(NUM_ID, (_m, v) => `<numId ${numbering(v)}>`) : ""));
|
|
339
|
+
// where a paragraph is: the body, or which table (counted over the whole document, nested ones too), row and cell, at every depth
|
|
340
|
+
// (src/merge-package.js docxUnits: two paragraphs in cell 0 0 of two different tables are in two different places)
|
|
341
|
+
const docxUnit = (p, side, u, list) => ({ raw: p.text, ref: `${side}${p.ordinal}`, parts: { box: u.box || "body", style: u.mod["w:pStyle"] ?? "", align: u.mod["w:jc"] ?? "", list, ppr: p.ppr || "" }, look: u.look });
|
|
342
|
+
|
|
343
|
+
/** Run formatting by character (src/merge-package.js docxUnits: every run property, as written, interned): an Int32Array. */
|
|
344
|
+
const looks = (u) => u.look;
|
|
345
|
+
/** `twin(want, got, ch)`: mine's run formatting `want` reads as `got` on character `ch` (twinSame); null: only equal counts */
|
|
346
|
+
const sameRuns = (a, b, runs, twin = null) => {
|
|
347
|
+
const x = runs(a);
|
|
348
|
+
const y = runs(b);
|
|
349
|
+
return x.length === y.length && x.every((v, k) => v === y[k] || (twin !== null && twin(v, y[k], a.raw[k])));
|
|
350
|
+
};
|
|
351
|
+
/** Did the run formatting change from `b` to `m`, on the characters both have (the common start and end of their texts)? */
|
|
352
|
+
function runChanged(b, m, runs) {
|
|
353
|
+
const x = runs(b);
|
|
354
|
+
const y = runs(m);
|
|
355
|
+
const { p, s } = ends(b.raw, m.raw);
|
|
356
|
+
for (let k = 0; k < p; k++) if (x[k] !== y[k]) return true;
|
|
357
|
+
for (let k = 1; k <= s; k++) if (x[x.length - k] !== y[y.length - k]) return true;
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
function ends(a, b) {
|
|
361
|
+
let p = 0;
|
|
362
|
+
const max = Math.min(a.length, b.length);
|
|
363
|
+
while (p < max && a[p] === b[p]) p++;
|
|
364
|
+
let s = 0;
|
|
365
|
+
while (s < max - p && a[a.length - 1 - s] === b[b.length - 1 - s]) s++;
|
|
366
|
+
return { p, s };
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Run formatting by character, three ways. Where the person's text is in the result and the agent left the text alone, each character
|
|
370
|
+
* of the result has the person's formatting where the person changed it (or typed it), else the agent's. Anything else the person
|
|
371
|
+
* changed is handed back; a character of the agent's whose formatting changed otherwise is a change nobody made.
|
|
372
|
+
*/
|
|
373
|
+
function runsCheck(b, m, t, r, refs, need, tally, runs, twin = null) {
|
|
374
|
+
const changed = runChanged(b, m, runs);
|
|
375
|
+
const [xb, xm, xt, xr] = [b, m, t, r].map(runs);
|
|
376
|
+
if (r.raw === t.raw && (r.raw !== m.raw || t.raw !== b.raw || !changed)) {
|
|
377
|
+
// nothing of the person's landed in this unit's text: its formatting is the agent's
|
|
378
|
+
if (!sameRuns(t, r, runs)) tally.add("formatting changes in the result that neither side made");
|
|
379
|
+
if (changed && !(t.raw === m.raw && sameRuns(t, m, runs))) need(refs, null, "formatting changes neither carried nor handed back");
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (r.raw !== m.raw || t.raw !== b.raw) return; // a text change of the result that the text accounting already judged
|
|
383
|
+
if (!xb || !xm || !xt || !xr) {
|
|
384
|
+
if (!sameRuns(m, r, runs) && changed) need(refs, null, "formatting changes neither carried nor handed back");
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const { p, s } = ends(b.raw, m.raw);
|
|
388
|
+
let bad = false;
|
|
389
|
+
let foreign = false;
|
|
390
|
+
for (let k = 0; k < m.raw.length; k++) {
|
|
391
|
+
const kb = k < p ? k : k >= m.raw.length - s ? b.raw.length - (m.raw.length - k) : -1; // the base character it is, or -1: typed
|
|
392
|
+
const mineSet = kb < 0 || xm[k] !== xb[kb];
|
|
393
|
+
const want = mineSet ? xm[k] : xt[kb];
|
|
394
|
+
if (xr[k] === want || (mineSet && twin !== null && twin(want, xr[k], m.raw[k]))) continue;
|
|
395
|
+
if (mineSet) bad = true;
|
|
396
|
+
else foreign = true;
|
|
397
|
+
}
|
|
398
|
+
if (bad) need(refs, null, "formatting changes neither carried nor handed back");
|
|
399
|
+
if (foreign) tally.add("formatting changes in the result that neither side made");
|
|
400
|
+
}
|
|
401
|
+
/** Pairs of DOCX paragraphs: a paragraph id unique on both sides first, then the same raw text in order where the ids allow it. */
|
|
402
|
+
function pairDocx(A, B, strict) {
|
|
403
|
+
const { may } = paraIdentity(A, B, { strict });
|
|
404
|
+
const ab = new Int32Array(A.length).fill(-1);
|
|
405
|
+
const ba = new Int32Array(B.length).fill(-1);
|
|
406
|
+
const tally = (L) => {
|
|
407
|
+
const m = new Map();
|
|
408
|
+
for (const p of L) if (p.paraId) m.set(p.paraId, (m.get(p.paraId) || 0) + 1);
|
|
409
|
+
return m;
|
|
410
|
+
};
|
|
411
|
+
const [ca, cb] = [tally(A), tally(B)];
|
|
412
|
+
const at = new Map();
|
|
413
|
+
B.forEach((p, j) => p.paraId && cb.get(p.paraId) === 1 && ca.get(p.paraId) === 1 && at.set(p.paraId, j));
|
|
414
|
+
A.forEach((p, i) => {
|
|
415
|
+
const j = p.paraId ? at.get(p.paraId) : undefined;
|
|
416
|
+
if (j !== undefined) (ab[i] = j), (ba[j] = i);
|
|
417
|
+
});
|
|
418
|
+
const byText = new Map();
|
|
419
|
+
B.forEach((p, j) => ba[j] < 0 && (byText.get(p.text) || byText.set(p.text, []).get(p.text)).push(j));
|
|
420
|
+
const next = new Map();
|
|
421
|
+
A.forEach((p, i) => {
|
|
422
|
+
if (ab[i] >= 0) return;
|
|
423
|
+
const list = byText.get(p.text);
|
|
424
|
+
const k = next.get(p.text) || 0;
|
|
425
|
+
if (!list || k >= list.length || !may(i, list[k])) return;
|
|
426
|
+
(ab[i] = list[k]), (ba[list[k]] = i);
|
|
427
|
+
next.set(p.text, k + 1);
|
|
428
|
+
});
|
|
429
|
+
// then an id that several paragraphs carry (one copied with it), in order: the same id still says more than nothing
|
|
430
|
+
const byId = new Map();
|
|
431
|
+
B.forEach((p, j) => ba[j] < 0 && p.paraId && (byId.get(p.paraId) || byId.set(p.paraId, []).get(p.paraId)).push(j));
|
|
432
|
+
const nextId = new Map();
|
|
433
|
+
A.forEach((p, i) => {
|
|
434
|
+
if (ab[i] >= 0 || !p.paraId) return;
|
|
435
|
+
const list = byId.get(p.paraId);
|
|
436
|
+
const k = nextId.get(p.paraId) || 0;
|
|
437
|
+
if (!list || k >= list.length) return;
|
|
438
|
+
(ab[i] = list[k]), (ba[list[k]] = i);
|
|
439
|
+
nextId.set(p.paraId, k + 1);
|
|
440
|
+
});
|
|
441
|
+
// last, a paragraph rewritten in place (where the ids allow it strictly: an id of its own that the other side, which has ids, lacks
|
|
442
|
+
// was removed): a run of unpaired paragraphs between two paired neighbors that face a run of the same length
|
|
443
|
+
// between the same neighbors on the other side are those paragraphs, in order (without this, a paragraph whose text changed and that
|
|
444
|
+
// carries no id would be a deletion and an insertion, and a change of its formatting could not be followed)
|
|
445
|
+
const sure = strict ? may : paraIdentity(A, B, { strict: true }).may;
|
|
446
|
+
let i0 = -1;
|
|
447
|
+
for (let i = 0; i <= A.length; i++) {
|
|
448
|
+
if (i < A.length && ab[i] < 0) continue;
|
|
449
|
+
const j0 = i0 < 0 ? -1 : ab[i0];
|
|
450
|
+
const j1 = i < A.length ? ab[i] : B.length;
|
|
451
|
+
if (i - i0 > 1 && j1 - j0 === i - i0) {
|
|
452
|
+
let free = true;
|
|
453
|
+
for (let j = j0 + 1; j < j1 && free; j++) free = ba[j] < 0;
|
|
454
|
+
for (let k = 1; free && k < i - i0; k++) free = sure(i0 + k, j0 + k);
|
|
455
|
+
if (free) for (let k = 1; k < i - i0; k++) (ab[i0 + k] = j0 + k), (ba[j0 + k] = i0 + k);
|
|
456
|
+
}
|
|
457
|
+
i0 = i;
|
|
458
|
+
}
|
|
459
|
+
return ab;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** The four versions of a DOCX merge read once, for the proof and for handBack: paragraphs, pairs and the package. */
|
|
463
|
+
function prepareDocx({ base, mine, theirs, merged, read = null }) {
|
|
464
|
+
const reading = (bytes) => readDocxParagraphs(bytes, { detail: true });
|
|
465
|
+
const [pb, pm, pt] = read ? [read.base, read.mine, read.theirs] : [base, mine, theirs].map(reading);
|
|
466
|
+
const pr = merged === theirs ? pt : reading(merged);
|
|
467
|
+
const pk = packages(base, mine, theirs, merged);
|
|
468
|
+
const I = new Interner();
|
|
469
|
+
const U = pk.map((P, n) => (n === 3 && merged === theirs ? null : docxUnits(P.text("word/document.xml"), I)));
|
|
470
|
+
U[3] ||= U[2];
|
|
471
|
+
const L = [pb, pm, pt, pr];
|
|
472
|
+
// the package reading lists the paragraphs as the reader does; when it does not (a document it reads otherwise), nothing is proven
|
|
473
|
+
const whole = U.every((u, n) => u.paras.length === L[n].length && u.paras.every((x, k) => x.text === L[n][k].text));
|
|
474
|
+
return { L, pk, U, I, whole, bm: pairDocx(pb, pm, false), bt: pairDocx(pb, pt, true), tr: pairDocx(pt, pr, true) };
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export function proveDocx(args) {
|
|
478
|
+
const ctx = args.ctx ?? prepareDocx(args);
|
|
479
|
+
const tally = new Tally("paragraphs");
|
|
480
|
+
if (!ctx.whole) {
|
|
481
|
+
tally.add("documents the proof could not read in full");
|
|
482
|
+
return tally.fail;
|
|
483
|
+
}
|
|
484
|
+
const back = handedBack(args.unplaced, args.copy);
|
|
485
|
+
const [B, M, T, R] = ctx.L.map((L, n) => {
|
|
486
|
+
const lists = docxLists(ctx.U[n].paras, numberingOf(ctx.pk[n].text("word/numbering.xml")));
|
|
487
|
+
return L.map((p, k) => docxUnit(p, "bmtr"[n], ctx.U[n].paras[k], lists[k]));
|
|
488
|
+
});
|
|
489
|
+
const twin = (want, got, ch) => twinSame(ctx.I, want, got, ch);
|
|
490
|
+
account({ B, M, T, R, bm: ctx.bm, bt: ctx.bt, tr: ctx.tr, ordered: true, runs: looks, twin }, back, tally);
|
|
491
|
+
restDocx(ctx, new Sink(back, tally));
|
|
492
|
+
return tally.fail;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// ---- PPTX ------------------------------------------------------------------------------------------------------------------------
|
|
496
|
+
|
|
497
|
+
const sid = (s) => String(s.id ?? `#${s.index}`);
|
|
498
|
+
const shapeKey = (sh) => `${sh.id}#${sh.ordinal}`;
|
|
499
|
+
|
|
500
|
+
/** Pairs by key: `keyOf` gives each unit's key; a key found more than once on a side pairs nothing. */
|
|
501
|
+
function pairByKey(A, B, keyOf) {
|
|
502
|
+
const count = (L) => L.reduce((m, u) => m.set(keyOf(u), (m.get(keyOf(u)) || 0) + 1), new Map());
|
|
503
|
+
const [ca, cb] = [count(A), count(B)];
|
|
504
|
+
const at = new Map();
|
|
505
|
+
B.forEach((u, j) => cb.get(keyOf(u)) === 1 && at.set(keyOf(u), j));
|
|
506
|
+
return Int32Array.from(A, (u) => (ca.get(keyOf(u)) === 1 && at.has(keyOf(u)) ? at.get(keyOf(u)) : -1));
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function preparePptx({ base, mine, theirs, merged, read = null }) {
|
|
510
|
+
const deck = (bytes) => {
|
|
511
|
+
const d = parseDeck(bytes);
|
|
512
|
+
d.ensure();
|
|
513
|
+
return d;
|
|
514
|
+
};
|
|
515
|
+
const three = read ? [read.base, read.mine, read.theirs] : [base, mine, theirs].map(deck);
|
|
516
|
+
return { decks: [...three, merged === theirs ? three[2] : deck(merged)].map((d) => d.slides), pk: packages(base, mine, theirs, merged), I: new Interner(), merged, theirs };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
export function provePptx(args) {
|
|
520
|
+
const ctx = args.ctx ?? preparePptx(args);
|
|
521
|
+
const decks = ctx.decks;
|
|
522
|
+
const [B, M, T, R] = decks;
|
|
523
|
+
const copy = !!args.copy;
|
|
524
|
+
const back = handedBack(args.unplaced, copy);
|
|
525
|
+
const tally = new Tally("shapes");
|
|
526
|
+
const by = (L) => new Map(L.map((s) => [sid(s), s]));
|
|
527
|
+
const [inB, inM, inT, inR] = decks.map(by);
|
|
528
|
+
// slides: the person's added, removed and moved slides are never carried, so each one is handed back; the agent's slide list is
|
|
529
|
+
// the result's
|
|
530
|
+
if (T.map(sid).join(" ") !== R.map(sid).join(" ")) tally.add("slide lists in the result different from the agent's");
|
|
531
|
+
for (const s of M) if (!inB.has(sid(s)) && !(back([`s${sid(s)}`], null) && (copy || !s.shapes.some((x) => x.text)))) tally.add("added slides not handed back");
|
|
532
|
+
for (const s of B) if (!inM.has(sid(s)) && !back([`s${sid(s)}`], null)) tally.add("removed slides not handed back");
|
|
533
|
+
const common = (L, other) => L.map(sid).filter((x) => inB.has(x) && inM.has(x) && other.has(x));
|
|
534
|
+
if (common(B, inM).join(" ") !== common(M, inB).join(" ") && !back(["order"], null)) tally.add("slide moves not handed back");
|
|
535
|
+
const unit = (side, s) => (sh) => ({ raw: sh.text, ref: side === "b" || side === "m" ? `h${sid(s)}/${shapeKey(sh)}` : "", parts: { fmt: JSON.stringify(sh.fmt ?? null), name: sh.name, kind: sh.kind, placeholder: sh.placeholder ?? "" }, key: shapeKey(sh) });
|
|
536
|
+
for (const b of B) {
|
|
537
|
+
const m = inM.get(sid(b));
|
|
538
|
+
if (!m) continue;
|
|
539
|
+
const t = inT.get(sid(b));
|
|
540
|
+
const r = t ? inR.get(sid(b)) : null;
|
|
541
|
+
const box = `s${sid(b)}`;
|
|
542
|
+
const U = [["b", b], ["m", m], ["t", t], ["r", r]].map(([side, s]) => (s ? s.shapes.map(unit(side, s)) : []));
|
|
543
|
+
const key = (u) => u.key;
|
|
544
|
+
account({ B: U[0], M: U[1], T: U[2], R: U[3], bm: pairByKey(U[0], U[1], key), bt: pairByKey(U[0], U[2], key), tr: pairByKey(U[2], U[3], key), ordered: true, box }, back, tally);
|
|
545
|
+
}
|
|
546
|
+
// the agent's slides that base or mine lack are in the result as the agent wrote them
|
|
547
|
+
for (const t of T) {
|
|
548
|
+
if (inB.has(sid(t)) && inM.has(sid(t))) continue;
|
|
549
|
+
const r = inR.get(sid(t));
|
|
550
|
+
if (!r || r.shapes.map((x) => `${shapeKey(x)} ${x.text}`).join("\n") !== t.shapes.map((x) => `${shapeKey(x)} ${x.text}`).join("\n")) tally.add("the agent's slides changed in the result");
|
|
551
|
+
}
|
|
552
|
+
agentOnly(ctx, T, R, (t) => inB.has(sid(t)) && inM.has(sid(t)), new Sink(back, tally), "the agent's slides changed in the result");
|
|
553
|
+
restPptx(ctx, new Sink(back, tally));
|
|
554
|
+
return tally.fail;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* A slide or sheet of the agent's that base or mine lack (or that the person removed): the merge never writes into it, so the result
|
|
559
|
+
* holds its part as the agent wrote it, everything in it (not its text alone), but for VOLATILE.
|
|
560
|
+
*/
|
|
561
|
+
function agentOnly(ctx, T, R, inside, sink, words) {
|
|
562
|
+
const [, , PT, PR] = ctx.pk;
|
|
563
|
+
if (PT === PR) return;
|
|
564
|
+
for (const t of T) {
|
|
565
|
+
if (inside(t) || !t.part) continue;
|
|
566
|
+
const r = R.find((x) => x.part === t.part);
|
|
567
|
+
if (!r) continue; // missing: the list checks count it
|
|
568
|
+
if (sameStored(PT, PR, t.part)) continue;
|
|
569
|
+
const [ut, ur] = [PT.units(t.part), PR.units(t.part)];
|
|
570
|
+
if (!ut || !ur) continue;
|
|
571
|
+
for (const k of new Set([...ut.keys(), ...ur.keys()])) if (!unitLooseSame(ut.get(k), ur.get(k))) return void sink.foreign(words);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// ---- XLSX ------------------------------------------------------------------------------------------------------------------------
|
|
576
|
+
|
|
577
|
+
const sheetKey = (s) => `${s.sheetId} ${s.name}`;
|
|
578
|
+
|
|
579
|
+
function prepareXlsx({ base, mine, theirs, merged, read = null }) {
|
|
580
|
+
const three = read ? [read.base, read.mine, read.theirs] : [base, mine, theirs].map(parseWorkbook);
|
|
581
|
+
return { books: [...three, merged === theirs ? three[2] : parseWorkbook(merged)], pk: packages(base, mine, theirs, merged), merged, theirs };
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export function proveXlsx(args) {
|
|
585
|
+
const ctx = args.ctx ?? prepareXlsx(args);
|
|
586
|
+
const books = ctx.books;
|
|
587
|
+
const [B, M, T, R] = books;
|
|
588
|
+
const copy = !!args.copy;
|
|
589
|
+
const back = handedBack(args.unplaced, copy);
|
|
590
|
+
const tally = new Tally("cells");
|
|
591
|
+
const by = (wb) => new Map(wb.sheets.map((s) => [sheetKey(s), s]));
|
|
592
|
+
const [inB, inM, inT, inR] = books.map(by);
|
|
593
|
+
if (T.sheets.map(sheetKey).join("\n") !== R.sheets.map(sheetKey).join("\n")) tally.add("sheet lists in the result different from the agent's");
|
|
594
|
+
for (const s of M.sheets) if (!inB.has(sheetKey(s)) && !(back([`S${sheetKey(s)}`], null) && (copy || !M.sheetCells(s).cells.size))) tally.add("added or renamed sheets not handed back");
|
|
595
|
+
for (const s of B.sheets) if (!inM.has(sheetKey(s)) && !back([`S${sheetKey(s)}`], null)) tally.add("removed or renamed sheets not handed back");
|
|
596
|
+
const common = (wb, other) => wb.sheets.map(sheetKey).filter((k) => inB.has(k) && other.has(k));
|
|
597
|
+
if (common(B, inM).join("\n") !== common(M, inB).join("\n") && !back(["sheets"], null)) tally.add("sheet moves not handed back");
|
|
598
|
+
for (const b of B.sheets) {
|
|
599
|
+
const m = inM.get(sheetKey(b));
|
|
600
|
+
// as written: hidden and very hidden (shown only by a macro) are two states
|
|
601
|
+
if (m && b.state !== m.state && !back(["sheets", `S${sheetKey(b)}`], null)) tally.add("sheets hidden or shown, not handed back");
|
|
602
|
+
}
|
|
603
|
+
// Cells, by address (the cell is its address on every side: a row or column inserted shows as the cells that changed), each one
|
|
604
|
+
// three ways as in account(), in one pass without a list per sheet (a sheet holds up to a million cells): the value as written (a
|
|
605
|
+
// formula as its text) and the number format.
|
|
606
|
+
const value = (c) => (!c ? "" : c.shared ? "\u0001shared" : `${c.k}\u0001${c.text}`);
|
|
607
|
+
for (const b of B.sheets) {
|
|
608
|
+
const m = inM.get(sheetKey(b));
|
|
609
|
+
if (!m) continue;
|
|
610
|
+
const k = sheetKey(b);
|
|
611
|
+
const box = `S${k}`;
|
|
612
|
+
const t = inT.get(k);
|
|
613
|
+
const r = t ? inR.get(k) : null;
|
|
614
|
+
const [cb, cm, ct, cr] = [[B, b], [M, m], [T, t], [R, r]].map(([wb, s]) => (s ? wb.sheetCells(s).cells : null));
|
|
615
|
+
if ([[B, b], [M, m], [T, t], [R, r]].some(([wb, s]) => s && wb.sheetCells(s).truncated)) {
|
|
616
|
+
if (!back([box], null)) tally.add("sheets too large to check, not handed back");
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
if (JSON.stringify(B.sheetCells(b).merges) !== JSON.stringify(M.sheetCells(m).merges) && !back([box], null) && !back.kind([box], "layout", box)) tally.add("merged cells changed, not handed back");
|
|
620
|
+
const cell = (at) => {
|
|
621
|
+
const [xb, xm, xt, xr] = [cb, cm, ct, cr].map((c) => c?.get(at));
|
|
622
|
+
const [vb, vm, vt, vr] = [xb, xm, xt, xr].map(value);
|
|
623
|
+
const refs = [`c${k}!${at}`];
|
|
624
|
+
const pText = vm !== vb;
|
|
625
|
+
if (ct && cr) {
|
|
626
|
+
const aText = vt !== vb;
|
|
627
|
+
if (vr !== vt && !(pText && !aText && vr === vm)) tally.add("the agent's cells changed into values neither side wrote there");
|
|
628
|
+
if (pText && vr === vt && vm !== vt && !back(refs, xm?.text ?? "", box)) tally.add("value edits neither carried nor handed back");
|
|
629
|
+
} else if (pText && !back(refs, xm?.text ?? "", box)) tally.add("value edits on sheets the agent removed, not handed back");
|
|
630
|
+
// the number format, where the cell holds a value on each side compared (a value typed into an empty cell takes the format the
|
|
631
|
+
// engine gives it)
|
|
632
|
+
if (!xt || !xr) return;
|
|
633
|
+
const [fb, fm, ft, fr] = [[B, xb], [M, xm], [T, xt], [R, xr]].map(([wb, c]) => (c ? wb.numberFormat(c.s) : null));
|
|
634
|
+
const changed = xb && xm && fb !== fm;
|
|
635
|
+
if (changed && fr !== fm && !back(refs, null, box)) tally.add("number formats neither carried nor handed back");
|
|
636
|
+
if (fr !== ft && !(changed && fr === fm && ft === fb)) tally.add("number formats in the result that neither side set");
|
|
637
|
+
};
|
|
638
|
+
for (const at of cb.keys()) cell(at);
|
|
639
|
+
for (const at of cm.keys()) if (!cb.has(at)) cell(at);
|
|
640
|
+
if (ct) for (const at of ct.keys()) if (!cb.has(at) && !cm.has(at)) cell(at);
|
|
641
|
+
if (cr) for (const at of cr.keys()) if (!cb.has(at) && !cm.has(at) && !ct?.has(at)) cell(at);
|
|
642
|
+
}
|
|
643
|
+
for (const t of T.sheets) {
|
|
644
|
+
if (inB.has(sheetKey(t)) && inM.has(sheetKey(t))) continue;
|
|
645
|
+
const r = inR.get(sheetKey(t));
|
|
646
|
+
const cells = (wb, s) => [...wb.sheetCells(s).cells].map(([at, c]) => `${at} ${value(c)}`).join("\n");
|
|
647
|
+
if (!r || cells(T, t) !== cells(R, r)) tally.add("the agent's sheets changed in the result");
|
|
648
|
+
}
|
|
649
|
+
agentOnly(ctx, T.sheets, R.sheets, (t) => inB.has(sheetKey(t)) && inM.has(sheetKey(t)), new Sink(back, tally), "the agent's sheets changed in the result");
|
|
650
|
+
restXlsx(ctx, new Sink(back, tally));
|
|
651
|
+
return tally.fail;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// ---- the rest: every part of the package, the layout of the main parts, and every unit's residue ----------------------------------
|
|
655
|
+
// (src/merge-package.js reads them.) Each difference between base and mine is in the result or handed back by an entry that names the
|
|
656
|
+
// unit and its kind (`as`); every other difference in the result is one the agent made. The same code serves handBack (each difference
|
|
657
|
+
// not yet handed back becomes an entry) and the proof (each one is a reason to refuse).
|
|
658
|
+
|
|
659
|
+
/** Where the rest checks report: the proof tallies, handBack collects entries. */
|
|
660
|
+
class Sink {
|
|
661
|
+
constructor(back, tally, entries = null) {
|
|
662
|
+
this.back = back;
|
|
663
|
+
this.tally = tally;
|
|
664
|
+
this.entries = entries;
|
|
665
|
+
}
|
|
666
|
+
covered(refs, kind, box = null) {
|
|
667
|
+
if (this.back.kind(refs, kind, box)) return true;
|
|
668
|
+
// handBack: the planner already said it for the whole slide or sheet ("You changed formatting on the sheet ...") but kept no copy:
|
|
669
|
+
// the copy is what is missing, not a second line saying the same thing (a browser test found the bold of one cell said twice)
|
|
670
|
+
if (this.entries && this.back.boxed(box, kind)) return (this.entries.copy = true);
|
|
671
|
+
return false;
|
|
672
|
+
}
|
|
673
|
+
/** a difference of the person's neither in the result nor handed back: `words` for the proof, `entry` for handBack */
|
|
674
|
+
lost(words, entry) {
|
|
675
|
+
if (this.entries) this.entries.push({ text: "", ...entry });
|
|
676
|
+
else this.tally.add(words);
|
|
677
|
+
}
|
|
678
|
+
/** a difference in the result that neither side made: only the proof counts it (handBack cannot hand back what is not the person's) */
|
|
679
|
+
foreign(words) {
|
|
680
|
+
if (!this.entries) this.tally.add(words);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/** The four packages of a merge; the result is the agent's package when the merge wrote nothing. */
|
|
685
|
+
function packages(base, mine, theirs, merged) {
|
|
686
|
+
const three = [base, mine, theirs].map(readPackage);
|
|
687
|
+
return [...three, merged === theirs ? three[2] : readPackage(merged)];
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
const NONE = new Map();
|
|
691
|
+
/** A reading kept in the merge's context, so handBack and the proof read each part of each version once. */
|
|
692
|
+
const memo = (ctx, key, read) => {
|
|
693
|
+
ctx.memo ||= new Map();
|
|
694
|
+
if (!ctx.memo.has(key)) ctx.memo.set(key, read());
|
|
695
|
+
return ctx.memo.get(key);
|
|
696
|
+
};
|
|
697
|
+
const capital = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
698
|
+
const AFTER = "That change could not be merged; it is still in your copy.";
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Every part outside the main ones (`main(name)`), unit by unit (src/merge-package.js keyedUnits; `skip(name)` names the children a
|
|
702
|
+
* main check models, such as the slide list): a part the person changed is in the result as theirs or handed back, one entry per kind
|
|
703
|
+
* of part; any other difference between the agent's part and the result's is one nobody made.
|
|
704
|
+
*/
|
|
705
|
+
function partsCheck(pk, main, skip, sink, strip = () => null) {
|
|
706
|
+
const [B, M, T, R] = pk;
|
|
707
|
+
const lostBy = new Map(); // label -> refs
|
|
708
|
+
for (const name of new Set([...B.names, ...M.names, ...T.names, ...R.names])) {
|
|
709
|
+
if (main(name)) continue;
|
|
710
|
+
const same = (P, Q) => P === Q || (!P.has(name) && !Q.has(name)) || sameStored(P, Q, name);
|
|
711
|
+
if (same(B, M) && same(T, R)) continue;
|
|
712
|
+
const sk = skip(name);
|
|
713
|
+
const [ub, um, ut, ur] = pk.map((P) => P.units(name, sk, strip(name)) ?? NONE);
|
|
714
|
+
let lost = false;
|
|
715
|
+
let odd = false;
|
|
716
|
+
for (const k of new Set([...ub.keys(), ...um.keys(), ...ut.keys(), ...ur.keys()])) {
|
|
717
|
+
const [b, m, t, r] = [ub, um, ut, ur].map((u) => u.get(k));
|
|
718
|
+
// a member of a list modeled elsewhere ("~"): which members there are is the slide or sheet checks'; what each one holds is here
|
|
719
|
+
if (k.startsWith("~") && (!b || !m || !t || !r)) continue;
|
|
720
|
+
// the rule of VOLATILE (src/merge-package.js sameVolatile), on the part's units
|
|
721
|
+
const changed = !unitSame(b, m);
|
|
722
|
+
const carried = unitCarried(b, m, r);
|
|
723
|
+
if (changed && !carried) lost = true;
|
|
724
|
+
if (!unitLooseSame(t, r) && !(changed && unitLooseSame(t, b) && carried)) odd = true;
|
|
725
|
+
}
|
|
726
|
+
const ref = `p:${name}`;
|
|
727
|
+
if (lost && !sink.covered([ref], "part")) (lostBy.get(partLabel(name)) || lostBy.set(partLabel(name), []).get(partLabel(name))).push(ref);
|
|
728
|
+
if (odd) sink.foreign("parts of the file in the result that neither side changed");
|
|
729
|
+
}
|
|
730
|
+
for (const [label, refs] of lostBy) sink.lost("changes to parts of the file neither carried nor handed back", { label: capital(label), why: `You changed ${label}. ${AFTER}`, of: refs, as: "part" });
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
const inverse = (map, n) => {
|
|
734
|
+
const out = new Int32Array(n).fill(-1);
|
|
735
|
+
map.forEach((j, i) => j >= 0 && (out[j] = i));
|
|
736
|
+
return out;
|
|
737
|
+
};
|
|
738
|
+
const firstWords = (t) => {
|
|
739
|
+
const w = String(t).replace(/\s+/g, " ").trim();
|
|
740
|
+
return w ? `"${untrusted(w.split(" ").slice(0, 8).join(" "), 80)}${w.split(" ").length > 8 ? "..." : ""}"` : "an empty paragraph";
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
const DOCX_KINDS = [
|
|
744
|
+
["fmt", "look", "paragraph formatting (spacing, indents, borders, the paragraph mark) neither carried nor handed back", `You changed this paragraph's formatting (spacing, indents, borders or the like). ${AFTER}`],
|
|
745
|
+
["obj", "object", "objects in paragraphs (pictures, fields, links, footnote and comment marks) neither carried nor handed back", `You changed a picture, link, field, footnote or comment mark, or another object in this paragraph. ${AFTER}`],
|
|
746
|
+
];
|
|
747
|
+
|
|
748
|
+
function restDocx(ctx, sink) {
|
|
749
|
+
const { pk, U, L, bm, bt, tr } = ctx;
|
|
750
|
+
partsCheck(pk, (n) => n === "word/document.xml", () => null, sink);
|
|
751
|
+
const [UB, UM, UT, UR] = U.map((u) => u.paras);
|
|
752
|
+
const [kb, km, kt, kr] = U.map((u) => u.skeleton);
|
|
753
|
+
// the document outside its paragraphs: tables (rows, columns, cell settings), sections, anything else in the body
|
|
754
|
+
const layoutMine = !unitSame(kb, km);
|
|
755
|
+
const layoutIn = unitCarried(kb, km, kr);
|
|
756
|
+
if (layoutMine && !layoutIn && !sink.covered(["layout"], "layout")) sink.lost("table or page layout changes neither carried nor handed back", { label: "Tables and page layout", why: `You changed the layout of a table or of the page (rows, columns, cell settings or sections). ${AFTER}`, of: ["layout"], as: "layout" });
|
|
757
|
+
if (!unitLooseSame(kt, kr) && !(layoutMine && unitLooseSame(kt, kb) && layoutIn)) sink.foreign("tables or sections in the result that neither side changed");
|
|
758
|
+
// a paragraph's own attributes count with its objects where it is the same paragraph on both sides; a paragraph added on each side
|
|
759
|
+
// is two new elements, whose revision stamps and text ids say nothing about what they hold (w:rsid*, w14:textId: none is shown)
|
|
760
|
+
const same = (X, Y, k, added = false) => sameResidue(X[k], X.text, Y[k], Y.text) && (k !== "obj" || added || X.own === Y.own);
|
|
761
|
+
// mine's residue in the result, by the rule of VOLATILE (base `X`, mine `Y`, the result `Z`; X null for a paragraph the person added)
|
|
762
|
+
const carried = (X, Y, Z, k, added = false) => residueCarried(X ? X[k] : [], Y[k], Y.text, Z[k], Z.text) && (k !== "obj" || added || Y.own === Z.own);
|
|
763
|
+
const agentSame = (X, Y, k, added = true) => residueLooseSame(X[k], X.text, Y[k], Y.text) && (k !== "obj" || added || X.own === Y.own);
|
|
764
|
+
// each paragraph the person kept: its residue as the person left it, in the result or handed back
|
|
765
|
+
for (let x = 0; x < UB.length; x++) {
|
|
766
|
+
const m = bm[x];
|
|
767
|
+
if (m < 0) continue;
|
|
768
|
+
const t = bt[x];
|
|
769
|
+
const r = t >= 0 ? tr[t] : -1;
|
|
770
|
+
for (const [k, kind, words, why] of DOCX_KINDS) {
|
|
771
|
+
if (same(UB[x], UM[m], k) || (r >= 0 && carried(UB[x], UM[m], UR[r], k))) continue;
|
|
772
|
+
const refs = [`m${m}`, `b${x}`];
|
|
773
|
+
if (!sink.covered(refs, kind)) sink.lost(words, { label: firstWords(L[1][m].text), why, of: refs, as: kind });
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
// each paragraph the person added: its residue (anything beyond its text) in a paragraph the merge added with the same text
|
|
777
|
+
const mb = inverse(bm, UM.length);
|
|
778
|
+
const rt = inverse(tr, UR.length);
|
|
779
|
+
const fresh = new Map();
|
|
780
|
+
UR.forEach((u, r) => rt[r] < 0 && (fresh.get(u.text) || fresh.set(u.text, []).get(u.text)).push(r));
|
|
781
|
+
for (let j = 0; j < UM.length; j++) {
|
|
782
|
+
if (mb[j] >= 0) continue;
|
|
783
|
+
for (const [k, kind, words, why] of DOCX_KINDS) {
|
|
784
|
+
if (!UM[j][k].length || (fresh.get(UM[j].text) || []).some((r) => carried(null, UM[j], UR[r], k, true))) continue;
|
|
785
|
+
if (!sink.covered([`m${j}`], kind)) sink.lost(words, { label: firstWords(L[1][j].text), why, of: [`m${j}`], as: kind });
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
// the agent's direction: each paragraph of the agent's in the result keeps its residue, but where the person's change landed
|
|
789
|
+
const tb = inverse(bt, UT.length);
|
|
790
|
+
for (let t = 0; t < UT.length; t++) {
|
|
791
|
+
const r = tr[t];
|
|
792
|
+
if (r < 0) continue;
|
|
793
|
+
const x = tb[t];
|
|
794
|
+
const m = x >= 0 ? bm[x] : -1;
|
|
795
|
+
// (a paragraph's own attributes are not compared here: which of several paragraphs with the same text and no id is which is not
|
|
796
|
+
// certain, and their revision stamps are not what they hold)
|
|
797
|
+
for (const [k] of DOCX_KINDS) {
|
|
798
|
+
if (agentSame(UT[t], UR[r], k)) continue;
|
|
799
|
+
if (m >= 0 && !same(UB[x], UM[m], k, true) && agentSame(UT[t], UB[x], k) && carried(UB[x], UM[m], UR[r], k, true)) continue;
|
|
800
|
+
sink.foreign("paragraph contents in the result that neither side changed");
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const nameOfShape = (sl, sh) => untrusted(`${sh.isTitle ? "the title" : sh.name ? `"${sh.name}"` : `shape ${sh.id}`} on slide ${sl.index + 1}`, 100);
|
|
806
|
+
|
|
807
|
+
function restPptx(ctx, sink) {
|
|
808
|
+
const { decks, pk, I } = ctx;
|
|
809
|
+
const [B, M, T, R] = decks;
|
|
810
|
+
const slideParts = new Set(decks.flatMap((d) => d.map((s) => s.part).filter(Boolean)));
|
|
811
|
+
// the slide list: which slides and in which order are the slide checks'; each p:sldId's attributes (its relationship) are the part's
|
|
812
|
+
partsCheck(pk, (n) => slideParts.has(n), (n) => (n === "ppt/presentation.xml" ? (l) => (l === "sldIdLst" ? "each" : false) : null), sink);
|
|
813
|
+
const by = (L) => new Map(L.map((s) => [sid(s), s]));
|
|
814
|
+
const [inM, inT, inR] = [M, T, R].map(by);
|
|
815
|
+
const same = (P, Q, s, z) => P === Q || (s?.part && z?.part && s.part === z.part && sameStored(P, Q, s.part));
|
|
816
|
+
for (const b of B) {
|
|
817
|
+
const m = inM.get(sid(b));
|
|
818
|
+
if (!m) continue;
|
|
819
|
+
const t = inT.get(sid(b));
|
|
820
|
+
const r = t ? inR.get(sid(b)) : null;
|
|
821
|
+
// a slide nobody changed in any way (its part as stored, the same in each pair) needs nothing
|
|
822
|
+
if (same(pk[0], pk[1], b, m) && t && r && same(pk[2], pk[3], t, r)) continue;
|
|
823
|
+
const read = (n, s) => (!s ? null : memo(ctx, `${n === 3 && pk[3] === pk[2] ? 2 : n} ${s.part}`, () => slideUnits(s.part ? pk[n].text(s.part) ?? "" : "", I)));
|
|
824
|
+
const [ub, um, ut, ur] = [read(0, b), read(1, m), read(2, t), read(3, r)];
|
|
825
|
+
const fits = (u, s) => !u || (u.shapes.length === s.shapes.length && u.shapes.every((x, k) => x.id === s.shapes[k].id && x.text === s.shapes[k].text));
|
|
826
|
+
if (!fits(ub, b) || !fits(um, m) || !fits(ut, t) || !fits(ur, r)) {
|
|
827
|
+
sink.foreign("slides the proof could not read in full");
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
const box = `s${sid(b)}`;
|
|
831
|
+
const n = m.index + 1;
|
|
832
|
+
const skelMine = !unitSame(ub.skeleton, um.skeleton);
|
|
833
|
+
const skelIn = !!ur && unitCarried(ub.skeleton, um.skeleton, ur.skeleton);
|
|
834
|
+
if (skelMine && !skelIn && !sink.covered([box], "slide", box)) sink.lost("slide backgrounds, layouts or settings neither carried nor handed back", { label: `Slide ${n}`, why: `You changed the background, layout or settings of slide ${n}. ${AFTER}`, of: [box], as: "slide" });
|
|
835
|
+
if (ut && ur && !unitLooseSame(ut.skeleton, ur.skeleton) && !(skelMine && unitLooseSame(ut.skeleton, ub.skeleton) && skelIn)) sink.foreign("slide settings in the result that neither side changed");
|
|
836
|
+
const bmP = pairByKey(b.shapes, m.shapes, shapeKey);
|
|
837
|
+
const btP = t ? pairByKey(b.shapes, t.shapes, shapeKey) : null;
|
|
838
|
+
const trP = t && r ? pairByKey(t.shapes, r.shapes, shapeKey) : null;
|
|
839
|
+
const res = (X, Y, k) => sameResidue(X[k], X.text, Y[k], Y.text);
|
|
840
|
+
const look = (X, Y) => sameLookKept(X.look, X.text, Y.look, Y.text); // equal on the characters both keep, VOLATILE included
|
|
841
|
+
// the rule of VOLATILE: mine `Y` (base `X`) in the result `Z`; the agent's `Y0` and the result `Z` equal but for VOLATILE
|
|
842
|
+
const resIn = (X, Y, Z, k) => residueCarried(X[k], Y[k], Y.text, Z[k], Z.text);
|
|
843
|
+
const lookIn = (X, Y, Z) => lookCarried(I, X, Y, Z);
|
|
844
|
+
const resAgent = (Y0, Z, k) => residueLooseSame(Y0[k], Y0.text, Z[k], Z.text);
|
|
845
|
+
// what the person changed in a shape: its place, size and kind of shape (obj), its text's formatting (fmt, and the looks)
|
|
846
|
+
const changes = (X, Y) => ({ obj: !res(X, Y, "obj"), fmt: !res(X, Y, "fmt") || X.text !== Y.text || !look(X, Y) });
|
|
847
|
+
for (let x = 0; x < b.shapes.length; x++) {
|
|
848
|
+
const j = bmP[x];
|
|
849
|
+
if (j < 0) continue;
|
|
850
|
+
const tt = btP ? btP[x] : -1;
|
|
851
|
+
const rr = tt >= 0 && trP ? trP[tt] : -1;
|
|
852
|
+
const [X, Y, Z] = [ub.shapes[x], um.shapes[j], rr >= 0 ? ur.shapes[rr] : null];
|
|
853
|
+
const c = changes(X, Y);
|
|
854
|
+
const refs = [`h${sid(b)}/${shapeKey(m.shapes[j])}`];
|
|
855
|
+
const name = nameOfShape(m, m.shapes[j]);
|
|
856
|
+
if (c.obj && !(Z && resIn(X, Y, Z, "obj")) && !sink.covered(refs, "shape", box)) sink.lost("shapes moved, resized or restyled, neither carried nor handed back", { label: `Slide ${n}`, why: `You moved, resized or restyled ${name}. ${AFTER}`, of: refs, as: "shape" });
|
|
857
|
+
if (c.fmt && !(Z && resIn(X, Y, Z, "fmt") && lookIn(X, Y, Z)) && !sink.covered(refs, "look", box)) sink.lost("text formatting in shapes neither carried nor handed back", { label: `Slide ${n}`, why: `You changed the formatting of the text in ${name}. ${AFTER}`, of: refs, as: "look" });
|
|
858
|
+
}
|
|
859
|
+
// the agent's direction
|
|
860
|
+
if (!trP) continue;
|
|
861
|
+
const tb = btP ? inverse(btP, t.shapes.length) : null;
|
|
862
|
+
for (let tt = 0; tt < t.shapes.length; tt++) {
|
|
863
|
+
const rr = trP[tt];
|
|
864
|
+
if (rr < 0) continue;
|
|
865
|
+
const [Y0, Z] = [ut.shapes[tt], ur.shapes[rr]];
|
|
866
|
+
const x = tb ? tb[tt] : -1;
|
|
867
|
+
const j = x >= 0 ? bmP[x] : -1;
|
|
868
|
+
const X = x >= 0 ? ub.shapes[x] : null;
|
|
869
|
+
const Y = j >= 0 ? um.shapes[j] : null;
|
|
870
|
+
const c = X && Y ? changes(X, Y) : { obj: false, fmt: false };
|
|
871
|
+
if (!resAgent(Y0, Z, "obj") && !(c.obj && resAgent(Y0, X, "obj") && resIn(X, Y, Z, "obj"))) sink.foreign("shapes in the result that neither side changed");
|
|
872
|
+
if (Z.text === Y0.text && (!resAgent(Y0, Z, "fmt") || !lookLooseSame(I, Y0, Z)) && !(c.fmt && resIn(X, Y, Z, "fmt") && lookIn(X, Y, Z))) sink.foreign("text formatting in the result that neither side set");
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
const STYLE_LISTS = new Set(["numFmts", "fonts", "fills", "borders", "cellStyleXfs", "cellXfs"]); // read through the cells that use them
|
|
878
|
+
const a1 = (k) => {
|
|
879
|
+
let c = k % 16384;
|
|
880
|
+
let col = "";
|
|
881
|
+
for (c += 1; c > 0; c = Math.floor((c - 1) / 26)) col = String.fromCharCode(65 + ((c - 1) % 26)) + col;
|
|
882
|
+
return `${col}${Math.floor(k / 16384) + 1}`;
|
|
883
|
+
};
|
|
884
|
+
const named = (s) => `"${untrusted(s.name, 60)}"`;
|
|
885
|
+
|
|
886
|
+
function restXlsx(ctx, sink) {
|
|
887
|
+
const { books, pk } = ctx;
|
|
888
|
+
const [B, M, T, R] = books;
|
|
889
|
+
const sheetParts = new Set(books.flatMap((wb) => wb.sheets.map((s) => s.part).filter(Boolean)));
|
|
890
|
+
sheetParts.add("xl/sharedStrings.xml"); // read through the cells that use them
|
|
891
|
+
// the sheet list: which sheets, their names, ids and whether they are hidden are the sheet checks'; anything else a <sheet> holds is
|
|
892
|
+
// the part's. The style lists are read through every place that uses them (cells, rows, columns: sheetUnits) and checked below for
|
|
893
|
+
// a definition changed in place.
|
|
894
|
+
const SHEET_MODELED = / (name|sheetId|state)="[^"]*"/g;
|
|
895
|
+
partsCheck(pk, (n) => sheetParts.has(n), (n) => (n === "xl/styles.xml" ? (l) => STYLE_LISTS.has(l) : n === "xl/workbook.xml" ? (l) => (l === "sheets" ? "each" : false) : null), sink, (n) => (n === "xl/workbook.xml" ? (tag) => tag.replace(SHEET_MODELED, "") : null));
|
|
896
|
+
// each version's styles and shared strings, read when a sheet needs them; a style or rich text is an interned key, the same in every version
|
|
897
|
+
const I = (ctx.I ||= new Interner());
|
|
898
|
+
const look = (n) => {
|
|
899
|
+
const styles = (ctx.styles ||= pk.map((P, i) => {
|
|
900
|
+
if (i === 3 && P === pk[2]) return null;
|
|
901
|
+
const style = cellStyles(P.text("xl/styles.xml"));
|
|
902
|
+
const rich = sharedRich(P.text("xl/sharedStrings.xml"), style.font);
|
|
903
|
+
const keys = new Map();
|
|
904
|
+
return { style: (s) => keys.get(s) ?? keys.set(s, I.id(style.xf(s))).get(s), rich: (k) => I.id(rich[k] ?? `#missing ${k}`), lists: style.lists };
|
|
905
|
+
}));
|
|
906
|
+
return styles[n] ?? styles[2];
|
|
907
|
+
};
|
|
908
|
+
// A definition (a font, a fill, a border, a number format, a cell style entry) the person changed in place, or removed: the engine
|
|
909
|
+
// only appends to these lists (src/merge-package.js cellStyles), so it is in the result only if the result holds it as mine does
|
|
910
|
+
// there; else it is handed back, whatever references it (an outside review: a font made bold that only a column's style used was
|
|
911
|
+
// kept plain, and nothing was said).
|
|
912
|
+
if (!sameStored(pk[0], pk[1], "xl/styles.xml") && (pk[0].has("xl/styles.xml") || pk[1].has("xl/styles.xml"))) {
|
|
913
|
+
const [lb, lm, lr] = [look(0).lists, look(1).lists, look(3).lists];
|
|
914
|
+
let lost = false;
|
|
915
|
+
for (const name of STYLE_LISTS) {
|
|
916
|
+
const [xb, xm, xr] = [lb[name], lm[name], lr[name]];
|
|
917
|
+
const get = (x, i) => (x instanceof Map ? x.get(i) : x[i]);
|
|
918
|
+
for (const i of xb instanceof Map ? xb.keys() : xb.keys()) if (get(xm, i) !== get(xb, i) && !sameVolatile(get(xb, i), get(xm, i), get(xr, i))) lost = true;
|
|
919
|
+
}
|
|
920
|
+
if (lost && !sink.covered(["p:xl/styles.xml"], "part")) sink.lost("cell style definitions changed in place, neither carried nor handed back", { label: "Cell styles", why: `You changed cell styles (a font, fill, border or number format that cells, rows or columns use). ${AFTER}`, of: ["p:xl/styles.xml"], as: "part" });
|
|
921
|
+
}
|
|
922
|
+
const by = (wb) => new Map(wb.sheets.map((s) => [sheetKey(s), s]));
|
|
923
|
+
const [inM, inT, inR] = [M, T, R].map(by);
|
|
924
|
+
const same = (P, Q, s, z) => P === Q || (s?.part && z?.part && s.part === z.part && sameStored(P, Q, s.part));
|
|
925
|
+
for (const b of B.sheets) {
|
|
926
|
+
const k = sheetKey(b);
|
|
927
|
+
const m = inM.get(k);
|
|
928
|
+
if (!m) continue;
|
|
929
|
+
const t = inT.get(k);
|
|
930
|
+
const r = t ? inR.get(k) : null;
|
|
931
|
+
// a sheet nobody changed in any way, its styles and strings included, needs nothing
|
|
932
|
+
const quiet = (P, Q, s, z) => same(P, Q, s, z) && ["xl/styles.xml", "xl/sharedStrings.xml"].every((n) => P === Q || (!P.has(n) && !Q.has(n)) || sameStored(P, Q, n));
|
|
933
|
+
if (quiet(pk[0], pk[1], b, m) && t && r && quiet(pk[2], pk[3], t, r)) continue;
|
|
934
|
+
const read = (n, s) => (!s ? null : memo(ctx, `${n === 3 && pk[3] === pk[2] ? 2 : n} ${s.part}`, () => sheetUnits(s.part ? pk[n].text(s.part) ?? "" : "", look(n).style, look(n).rich)));
|
|
935
|
+
const [ub, um, ut, ur] = [read(0, b), read(1, m), read(2, t), read(3, r)];
|
|
936
|
+
const box = `S${k}`;
|
|
937
|
+
const rowsDiffer = (X, Y) => X.rows.size !== Y.rows.size || [...X.rows].some(([row, v]) => Y.rows.get(row) !== v);
|
|
938
|
+
const layout = (X, Y) => !unitSame(X.skeleton, Y.skeleton) || rowsDiffer(X, Y);
|
|
939
|
+
const mineLayout = layout(ub, um);
|
|
940
|
+
// the rule of VOLATILE on the sheet's layout: mine in the result, the agent's equal but for VOLATILE
|
|
941
|
+
const layoutIn = !!ur && unitCarried(ub.skeleton, um.skeleton, ur.skeleton) && !rowsDiffer(um, ur);
|
|
942
|
+
const layoutAgent = (X, Y) => !unitLooseSame(X.skeleton, Y.skeleton) || rowsDiffer(X, Y);
|
|
943
|
+
if (mineLayout && !layoutIn && !sink.covered([box], "layout", box)) sink.lost("sheet layout changes (row heights, column widths, merged cells, views) neither carried nor handed back", { label: `Sheet ${named(m)}`, why: `You changed row heights, column widths, merged cells or other settings of the sheet ${named(m)}. ${AFTER}`, of: [box], as: "layout" });
|
|
944
|
+
if (ut && ur && layoutAgent(ut, ur) && !(mineLayout && !layoutAgent(ut, ub) && layoutIn)) sink.foreign("sheet layout in the result that neither side changed");
|
|
945
|
+
const get = (u, key) => u.at(key);
|
|
946
|
+
const lost = [];
|
|
947
|
+
// every address either side has, without a list of them all (a sheet holds up to a million cells)
|
|
948
|
+
const each = (X, Y, fn) => {
|
|
949
|
+
for (const key of X.cells.keys()) fn(key);
|
|
950
|
+
for (const key of Y.cells.keys()) if (!X.cells.has(key)) fn(key);
|
|
951
|
+
};
|
|
952
|
+
each(ub, um, (key) => {
|
|
953
|
+
const vm = get(um, key);
|
|
954
|
+
const vb = get(ub, key);
|
|
955
|
+
if (vb === vm || (ur && sameVolatile(vb, vm, get(ur, key)))) return;
|
|
956
|
+
if (!sink.covered([`c${k}!${key}`], "look", box)) lost.push(key);
|
|
957
|
+
});
|
|
958
|
+
if (lost.length) {
|
|
959
|
+
lost.sort((x, y) => x - y);
|
|
960
|
+
const cells = `${lost.slice(0, 5).map(a1).join(", ")}${lost.length > 5 ? ` and ${lost.length - 5} more cells` : ""}`;
|
|
961
|
+
sink.lost("cell formatting neither carried nor handed back", { label: `Sheet ${named(m)}`, why: `You changed the formatting of ${cells} on the sheet ${named(m)} (bold, fill, number format, alignment, borders or the like). ${AFTER}`, of: lost.map((key) => `c${k}!${key}`), as: "look" });
|
|
962
|
+
}
|
|
963
|
+
if (!ut || !ur || ut === ur) continue;
|
|
964
|
+
each(ut, ur, (key) => {
|
|
965
|
+
const vt = get(ut, key);
|
|
966
|
+
const vr = get(ur, key);
|
|
967
|
+
if (looseSame(vt, vr)) return;
|
|
968
|
+
const vb = get(ub, key);
|
|
969
|
+
const vm = get(um, key);
|
|
970
|
+
if (vb !== vm && looseSame(vt, vb) && sameVolatile(vb, vm, vr)) return;
|
|
971
|
+
sink.foreign("cell formatting in the result that neither side set");
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
const PROVERS = { docx: proveDocx, pptx: provePptx, xlsx: proveXlsx };
|
|
977
|
+
const PREPARE = { docx: prepareDocx, pptx: preparePptx, xlsx: prepareXlsx };
|
|
978
|
+
const REST = { docx: restDocx, pptx: restPptx, xlsx: restXlsx };
|
|
979
|
+
|
|
980
|
+
/**
|
|
981
|
+
* The four versions read once for handBack and proveMerge of the same merge ({base, mine, theirs, merged, read}): pass the result
|
|
982
|
+
* as `ctx` to both.
|
|
983
|
+
*/
|
|
984
|
+
export function prepareMerge(format, args) {
|
|
985
|
+
return byFormat(PREPARE, format, "prepareMerge")(args);
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* The proof of one merge: null when every difference balances, else a short reason (counts and kinds, no document text). `merged` is
|
|
990
|
+
* the result's bytes (theirs when the merge wrote nothing); `unplaced` and `copy` are what the merge answers.
|
|
991
|
+
*/
|
|
992
|
+
export function proveMerge(format, args) {
|
|
993
|
+
return byFormat(PROVERS, format, "proveMerge")(args);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* The person's differences of the kinds no planner carries (parts, layout, the residue of units: src/merge-package.js) that are neither
|
|
998
|
+
* in the result nor handed back yet, as plain unplaced entries ({label, text: "", why, of, as}), grouped. The merge worker adds them
|
|
999
|
+
* (and keeps a copy of the person's file) before the proof runs: so the person is told of every such change, and the proof checks the
|
|
1000
|
+
* merge with them. A difference this cannot name is left to the proof, which refuses the merge. `copy` on the list: one an entry of
|
|
1001
|
+
* the merge's own already names for its whole slide or sheet, which holds once the copy is kept.
|
|
1002
|
+
*/
|
|
1003
|
+
export function handBack(format, args) {
|
|
1004
|
+
const ctx = args.ctx ?? byFormat(PREPARE, format, "handBack")(args);
|
|
1005
|
+
if (ctx.whole === false) return [];
|
|
1006
|
+
const entries = [];
|
|
1007
|
+
byFormat(REST, format, "handBack")(ctx, new Sink(handedBack(args.unplaced, args.copy), null, entries));
|
|
1008
|
+
const copy = !!entries.copy;
|
|
1009
|
+
// one entry per thing said: the same words for several units are one line naming them all
|
|
1010
|
+
const byWhy = new Map();
|
|
1011
|
+
for (const e of entries) {
|
|
1012
|
+
const key = `${e.as}\0${e.label}\0${e.why}`;
|
|
1013
|
+
if (byWhy.has(key)) byWhy.get(key).of.push(...e.of);
|
|
1014
|
+
else byWhy.set(key, { ...e, of: [...e.of] });
|
|
1015
|
+
}
|
|
1016
|
+
const out = [...byWhy.values()];
|
|
1017
|
+
if (copy) out.copy = true; // a difference an entry already names, which needs the person's file kept
|
|
1018
|
+
return out;
|
|
1019
|
+
}
|