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,1110 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { walkXml } from "./xml.js";
|
|
3
|
+
import { openZip } from "./zip.js";
|
|
4
|
+
|
|
5
|
+
// The whole package as the proof of a merge reads it (src/merge-proof.js): every part of the OOXML zip, compared between versions in a
|
|
6
|
+
// canonical form, and the main parts cut into units (DOCX paragraphs, PPTX shapes, XLSX cells and rows) whose canonical XML is split
|
|
7
|
+
// into what the proof models one by one (text, run properties by character, the paragraph's style, alignment and list, cell values)
|
|
8
|
+
// and a residue of everything else. Two units, or two parts, are the same only when their canonical forms are equal: nothing is
|
|
9
|
+
// compared by a list of chosen fields, so a difference of a kind no one thought of is still a difference.
|
|
10
|
+
//
|
|
11
|
+
// Canonical XML: element and attribute names by namespace URI (prefixes do not count: XML Namespaces), attributes sorted (their order
|
|
12
|
+
// is not content in XML), entities decoded, comments and processing instructions dropped (no Office program reads them inside a
|
|
13
|
+
// package part), and white space between elements dropped where an element holds other elements (OOXML has no mixed content: text lives
|
|
14
|
+
// in leaf elements such as w:t, a:t and v, whose white space is kept as written). Custom data parts (customXml/) are not OOXML and may
|
|
15
|
+
// be mixed content: their white space is kept wherever it stands.
|
|
16
|
+
//
|
|
17
|
+
// VOLATILE: content the engine rewrites on its own when it writes a unit (measured: every committed fixture opened and saved unedited
|
|
18
|
+
// through the engine path the page and the merge use came back byte for byte; one edit through the same path changed nothing outside
|
|
19
|
+
// the edited unit except these, inside it). Each is KEPT in the canonical form, marked (VOL before the attribute's name), and judged by
|
|
20
|
+
// one rule (sameVolatile below, used by every comparison of src/merge-proof.js):
|
|
21
|
+
// - a difference in it between base and mine is the person's (their own bytes: a proofing language set from en-US to fr-FR, a
|
|
22
|
+
// space kept or not, a workbook asked to compute on open), and must be in the result as mine has it, or be handed back;
|
|
23
|
+
// - the engine's own rewrite is allowed only where the person left it as base had it: between mine and the result on a unit the
|
|
24
|
+
// merge wrote for them, and between the agent's version and the result.
|
|
25
|
+
// The attributes:
|
|
26
|
+
// - xml:space on any element. The engine adds xml:space="preserve" to the text elements it writes (w:t, an inline string's t).
|
|
27
|
+
// - lang and dirty on a DrawingML run's a:rPr. The engine writes lang="en-US" dirty="0" on every run it writes in a shape: the
|
|
28
|
+
// spell-check language and the "text changed since the last spell check" flag.
|
|
29
|
+
// - fullCalcOnLoad on a workbook's calcPr. The engine writes <calcPr fullCalcOnLoad="1"/> when it writes a formula, which it does not
|
|
30
|
+
// compute (measured on test/merge-xlsx-property.test.js: every case where the merge carried a formula).
|
|
31
|
+
// Read as absent, with the reason they cannot hide a person's change:
|
|
32
|
+
// - an empty w:rPr or a:rPr, and a calcPr left empty (no attribute, no child): an empty list of properties sets nothing, the same as
|
|
33
|
+
// none, and a calcPr without attributes holds every calculation setting at its default, as no calcPr does. The engine drops
|
|
34
|
+
// <w:rPr/> from a run it edits. (An a:rPr that holds only lang or dirty is not empty: it is compared by the rule above.)
|
|
35
|
+
// - w:proofErr, the mark where Word last found a spelling or grammar error: no text, no formatting, never printed or shown but as
|
|
36
|
+
// the spell checker's squiggle, and Word checks again on open and writes them anew. The engine moves these marks when it edits the
|
|
37
|
+
// text next to them, differently for two edits that give the same text (measured on the committed corpus files delins.docx and
|
|
38
|
+
// 52449.docx: a word typed after "Tika" moved the marks around the next space), so the rule above would hand back ordinary typing.
|
|
39
|
+
// Everything else counts, rsid attributes included (the engine never changes them, so they never differ between the versions of a
|
|
40
|
+
// merge unless a person's other program wrote them).
|
|
41
|
+
//
|
|
42
|
+
// Cost: one pass over each part read (linear), each unit's canonical string built once; binary parts compared by their stored bytes
|
|
43
|
+
// first (no inflation when they are equal), then inflated. Ceiling: a version with more than MAX_TOTAL_EXPANSION bytes of parts that
|
|
44
|
+
// differ from another version is not read (src/zip.js): the merge stops with an error and writes nothing.
|
|
45
|
+
|
|
46
|
+
const XMLNS = "http://www.w3.org/XML/1998/namespace";
|
|
47
|
+
const A = "http://schemas.openxmlformats.org/drawingml/2006/main";
|
|
48
|
+
const WML = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
|
|
49
|
+
const SPACE = `{${XMLNS}}space`;
|
|
50
|
+
const ARPR = `{${A}}rPr`;
|
|
51
|
+
const SML = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
|
|
52
|
+
const CALC = `{${SML}}calcPr`;
|
|
53
|
+
const EMPTY_DROPPED = new Set([`{${WML}}rPr`, ARPR, CALC]);
|
|
54
|
+
const volatileAttr = (el, at) => at === SPACE || (el === ARPR && (at === "lang" || at === "dirty")) || (el === CALC && at === "fullCalcOnLoad");
|
|
55
|
+
/** marks a VOLATILE attribute in a canonical tag: ` \u0001name="value"` */
|
|
56
|
+
export const VOL = "\u0001";
|
|
57
|
+
const VOL_ATTR = / \u0001[^=]*="[^"]*"/g;
|
|
58
|
+
const EMPTIED = new RegExp(`<(${[...EMPTY_DROPPED].map((q) => q.replace(/[{}./:]/g, "\\$&")).join("|")})></>`, "g");
|
|
59
|
+
/** A canonical string without its VOLATILE attributes (an element they leave empty is then read as absent, as above). */
|
|
60
|
+
export const loose = (s) => (typeof s === "string" && s.includes(VOL) ? s.replace(VOL_ATTR, "").replace(EMPTIED, "") : s);
|
|
61
|
+
/** The VOLATILE attributes of a canonical string alone, in order, each with the element it stands on. */
|
|
62
|
+
export const volOf = (s) => {
|
|
63
|
+
if (typeof s !== "string" || !s.includes(VOL)) return "";
|
|
64
|
+
const out = [];
|
|
65
|
+
for (const m of s.matchAll(/<([^ >]*)([^>]*)>/g)) if (m[2].includes(VOL)) out.push(`${m[1]}${m[2].match(VOL_ATTR).join("")}`);
|
|
66
|
+
return out.join("");
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* THE RULE for VOLATILE content, written once. `b`, `m`, `r`: the same unit (a part's child, a character's run properties, a
|
|
70
|
+
* paragraph's or a shape's residue) in base, mine and the result, as canonical strings (or as anything `key` reads one from). Is mine's
|
|
71
|
+
* unit in the result? Equal; or equal but for VOLATILE, where the person left VOLATILE as base had it (the engine's rewrite of a unit
|
|
72
|
+
* it wrote: its xml:space, its lang and dirty, its fullCalcOnLoad). A difference between base and mine in VOLATILE is the person's,
|
|
73
|
+
* so then only equal counts. (Between the agent's version and the result, VOLATILE alone is the engine's: looseSame.)
|
|
74
|
+
*/
|
|
75
|
+
export function sameVolatile(b, m, r) {
|
|
76
|
+
if (r === m) return true;
|
|
77
|
+
if (r == null || m == null) return false;
|
|
78
|
+
return loose(r) === loose(m) && volOf(b ?? "") === volOf(m);
|
|
79
|
+
}
|
|
80
|
+
/** Equal but for VOLATILE: the agent's unit and the result's, where the engine may have written it. */
|
|
81
|
+
export const looseSame = (t, r) => t === r || (t != null && r != null && loose(t) === loose(r));
|
|
82
|
+
const DROPPED = new Set([`{${WML}}proofErr`]); // elements read as absent, whatever they hold
|
|
83
|
+
// On/off properties, read the one way every comparison sees them (round 8: "typo" made bold as <w:b w:val="true"/> was compared as a
|
|
84
|
+
// different property from the <w:b/> the merge writes, and the proof refused an ordinary merge). WordprocessingML's CT_OnOff elements
|
|
85
|
+
// of run and paragraph properties (ST_OnOff: on is no w:val, "true", "1" or "on"; off is "false", "0" or "off") are written as no
|
|
86
|
+
// w:val for on and w:val="0" for off. Off stays an element: written off is not the same as absent where a style turns it on.
|
|
87
|
+
// DrawingML's run properties say bold and italic with xsd:boolean attributes ("true" or "1", "false" or "0"), written "1" and "0";
|
|
88
|
+
// SpreadsheetML's font properties as below.
|
|
89
|
+
const ON_OFF = new Set(["b", "bCs", "i", "iCs", "caps", "smallCaps", "strike", "dstrike", "outline", "shadow", "emboss", "imprint", "noProof", "snapToGrid", "vanish", "webHidden", "specVanish", "rtl", "cs", "oMath", "keepNext", "keepLines", "pageBreakBefore", "widowControl", "suppressLineNumbers", "suppressAutoHyphens", "kinsoku", "wordWrap", "overflowPunct", "topLinePunct", "autoSpaceDE", "autoSpaceDN", "bidi", "adjustRightInd", "mirrorIndents", "suppressOverlap", "contextualSpacing"].map((l) => `{${WML}}${l}`));
|
|
90
|
+
const W_VAL = `{${WML}}val`;
|
|
91
|
+
const ST_ON_OFF = { true: "", 1: "", on: "", false: "0", 0: "0", off: "0" };
|
|
92
|
+
const A_RUN = new Set(["rPr", "defRPr", "endParaRPr"].map((l) => `{${A}}${l}`));
|
|
93
|
+
const XSD_BOOL = { true: "1", 1: "1", false: "0", 0: "0" };
|
|
94
|
+
// SpreadsheetML's font properties (CT_BooleanProperty: val is an xsd:boolean, true when absent), written as no val for on and val="0"
|
|
95
|
+
// for off, in a font of styles.xml and in a shared string's run properties alike
|
|
96
|
+
const S_BOOL = new Set(["b", "i", "strike", "condense", "extend", "outline", "shadow"].map((l) => `{${SML}}${l}`));
|
|
97
|
+
/** an attribute's value as compared: on/off spellings in one form (above); undefined drops the attribute */
|
|
98
|
+
const valueOf = (el, at, v) => {
|
|
99
|
+
if (at === W_VAL && ON_OFF.has(el) && Object.hasOwn(ST_ON_OFF, v)) return ST_ON_OFF[v] || undefined;
|
|
100
|
+
if ((at === "b" || at === "i") && A_RUN.has(el) && Object.hasOwn(XSD_BOOL, v)) return XSD_BOOL[v];
|
|
101
|
+
if (at === "val" && S_BOOL.has(el) && Object.hasOwn(XSD_BOOL, v)) return XSD_BOOL[v] === "1" ? undefined : "0";
|
|
102
|
+
return v;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const ESC = /[&<>"]/;
|
|
106
|
+
const esc = (s) => (ESC.test(s) ? s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """) : s);
|
|
107
|
+
const WS = /^[ \t\r\n]*$/;
|
|
108
|
+
const hash = (s) => createHash("sha1").update(s).digest("base64");
|
|
109
|
+
|
|
110
|
+
// resolved names, per namespace map (a map is shared by every element until one declares a namespace): one lookup per name
|
|
111
|
+
const RESOLVED = new WeakMap();
|
|
112
|
+
function qname(map, name, element) {
|
|
113
|
+
let memo = RESOLVED.get(map);
|
|
114
|
+
if (!memo) RESOLVED.set(map, (memo = new Map()));
|
|
115
|
+
const key = element ? name : `@${name}`;
|
|
116
|
+
let q = memo.get(key);
|
|
117
|
+
if (q !== undefined) return q;
|
|
118
|
+
const i = name.indexOf(":");
|
|
119
|
+
if (i < 0) q = element && map.get("") ? `{${map.get("")}}${name}` : name;
|
|
120
|
+
else q = `{${map.get(name.slice(0, i)) ?? name.slice(0, i)}}${name.slice(i + 1)}`;
|
|
121
|
+
memo.set(key, q);
|
|
122
|
+
return q;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Walks `xml` in canonical form: ev.open(rawName, attrs, tag, prefixes), ev.text(text), ev.close(rawName). `tag` is the canonical open tag
|
|
127
|
+
* (`<{uri}local a="v">`, attributes sorted, VOLATILE ones dropped), `prefixes` the namespace prefixes in scope; text is decoded. Linear.
|
|
128
|
+
*/
|
|
129
|
+
export function walkCanonical(xml, ev, budget = null, { lazyTags = false, keepSpace = false } = {}) {
|
|
130
|
+
// `lazyTags`: ev.open gets a function that builds the tag, for a reader that needs few of them (a sheet's million cells)
|
|
131
|
+
const ns = [new Map([["xml", XMLNS]])];
|
|
132
|
+
const kids = [];
|
|
133
|
+
let pend = "";
|
|
134
|
+
let lazy = null; // the open tag of an empty-candidate property element, sent once something is inside it
|
|
135
|
+
const flush = () => {
|
|
136
|
+
if (lazy) ev.open(lazy.name, lazy.a, lazy.tag, lazy.m);
|
|
137
|
+
lazy = null;
|
|
138
|
+
};
|
|
139
|
+
let gone = 0; // > 0 inside a DROPPED element
|
|
140
|
+
walkXml(xml, {
|
|
141
|
+
onOpen(name, a) {
|
|
142
|
+
if (gone) return void gone++;
|
|
143
|
+
let m = ns[ns.length - 1];
|
|
144
|
+
for (const k in a) if (k === "xmlns" || k.startsWith("xmlns:")) (m === ns[ns.length - 1] && (m = new Map(m)), m.set(k === "xmlns" ? "" : k.slice(6), a[k]));
|
|
145
|
+
const had0 = kids.length ? kids[kids.length - 1] : false;
|
|
146
|
+
if (kids.length) kids[kids.length - 1] = true;
|
|
147
|
+
pend = "";
|
|
148
|
+
flush();
|
|
149
|
+
ns.push(m);
|
|
150
|
+
const q = qname(m, name, true);
|
|
151
|
+
if (DROPPED.has(q)) {
|
|
152
|
+
ns.pop();
|
|
153
|
+
if (kids.length) kids[kids.length - 1] = had0;
|
|
154
|
+
return void (gone = 1);
|
|
155
|
+
}
|
|
156
|
+
const build = () => {
|
|
157
|
+
const at = [];
|
|
158
|
+
for (const k in a) {
|
|
159
|
+
if (k === "xmlns" || k.startsWith("xmlns:")) continue;
|
|
160
|
+
const qa = qname(m, k, false);
|
|
161
|
+
const v = valueOf(q, qa, a[k]);
|
|
162
|
+
if (v !== undefined) at.push(`${volatileAttr(q, qa) ? VOL : ""}${qa}="${esc(v)}"`);
|
|
163
|
+
}
|
|
164
|
+
if (at.length > 1) at.sort();
|
|
165
|
+
return `<${q}${at.length ? ` ${at.join(" ")}` : ""}>`;
|
|
166
|
+
};
|
|
167
|
+
kids.push(false);
|
|
168
|
+
const empty = EMPTY_DROPPED.has(q);
|
|
169
|
+
const tag = empty || !lazyTags ? build() : null;
|
|
170
|
+
const bare = empty && tag === `<${q}>`;
|
|
171
|
+
const give = lazyTags ? (tag === null ? build : () => tag) : tag;
|
|
172
|
+
if (bare) lazy = { name, a, tag: give, m };
|
|
173
|
+
else ev.open(name, a, give, m);
|
|
174
|
+
},
|
|
175
|
+
onText(t) {
|
|
176
|
+
if (gone) return;
|
|
177
|
+
if (!keepSpace && WS.test(t)) return void (pend += t);
|
|
178
|
+
flush();
|
|
179
|
+
if (pend) ev.text(pend);
|
|
180
|
+
pend = "";
|
|
181
|
+
ev.text(t);
|
|
182
|
+
},
|
|
183
|
+
onClose(name) {
|
|
184
|
+
if (gone) return void gone--;
|
|
185
|
+
const had = kids.pop();
|
|
186
|
+
ns.pop();
|
|
187
|
+
if (lazy) {
|
|
188
|
+
// an empty run property list: the same as none
|
|
189
|
+
lazy = null;
|
|
190
|
+
pend = "";
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (!had && pend) ev.text(pend);
|
|
194
|
+
pend = "";
|
|
195
|
+
ev.close(name);
|
|
196
|
+
},
|
|
197
|
+
}, budget);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The canonical form of a whole XML part, as one string. */
|
|
201
|
+
export function canonical(xml, opts = {}) {
|
|
202
|
+
const out = [];
|
|
203
|
+
walkCanonical(xml, { open: (_n, _a, tag) => out.push(tag), text: (t) => out.push(esc(t)), close: () => out.push("</>") }, null, opts);
|
|
204
|
+
return out.join("");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ---- parts -----------------------------------------------------------------------------------------------------------------------
|
|
208
|
+
|
|
209
|
+
const isXml = (name) => /\.(xml|rels|vml)$/i.test(name) || name === "[Content_Types].xml";
|
|
210
|
+
// children of a part's root element, keyed by their identity where they carry one (so one style, one numbering definition, one
|
|
211
|
+
// comment, one relationship is one unit), else by name and order
|
|
212
|
+
const KEY_ATTRS = ["styleId", "abstractNumId", "numId", "id", "Id", "Extension", "PartName", "name"];
|
|
213
|
+
const keyOf = (name, a) => {
|
|
214
|
+
for (const k of KEY_ATTRS) for (const ak in a) if (ak === k || ak.endsWith(`:${k}`)) return `${name.slice(name.indexOf(":") + 1)}[${k}=${a[ak]}]`;
|
|
215
|
+
return null;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/** A unit as the proof compares it: hashes of its canonical string `s`, of the string without VOLATILE `l`, and of VOLATILE alone `v`. */
|
|
219
|
+
const unitOf = (str) => {
|
|
220
|
+
const l = loose(str);
|
|
221
|
+
return { s: hash(str), l: l === str ? null : hash(l), v: str.includes(VOL) ? hash(volOf(str)) : "" };
|
|
222
|
+
};
|
|
223
|
+
/** sameVolatile (above) on units read by unitOf: the rule, on hashes */
|
|
224
|
+
export const unitCarried = (b, m, r) => r === m || (r != null && m != null && (r.s === m.s || ((r.l ?? r.s) === (m.l ?? m.s) && (b?.v ?? "") === m.v)));
|
|
225
|
+
/** looseSame on units */
|
|
226
|
+
export const unitLooseSame = (t, r) => t === r || (t != null && r != null && (t.s === r.s || (t.l ?? t.s) === (r.l ?? r.s)));
|
|
227
|
+
export const unitSame = (x, y) => x === y || (x != null && y != null && x.s === y.s);
|
|
228
|
+
|
|
229
|
+
// The collections whose order means nothing, named one by one: a child of one of these parts with one of these names is found by its
|
|
230
|
+
// identity wherever it stands, and moving it is no change. Every other child keeps its place in "@order", identity or not: in custom data
|
|
231
|
+
// (customXml/) <item id="a"> and <item id="b"> swapped is the data's own order changed (round 8: it was merged as no change).
|
|
232
|
+
const UNORDERED = [
|
|
233
|
+
// OPC (ECMA-376 Part 2): a relationship is found by its Id, a content type by its extension or part name; neither list has an order
|
|
234
|
+
[/(^|\/)_rels\/[^/]*\.rels$/i, ["Relationship"]],
|
|
235
|
+
[/^\[Content_Types\]\.xml$/, ["Default", "Override"]],
|
|
236
|
+
// WordprocessingML: the body names these by id (w:pStyle, w:numId and the abstractNumId it points at, w:commentReference,
|
|
237
|
+
// w:footnoteReference, w:endnoteReference) and a run's font by name; where each definition stands in its part is not read
|
|
238
|
+
[/^word\/styles\.xml$/i, ["style"]],
|
|
239
|
+
[/^word\/numbering\.xml$/i, ["abstractNum", "num"]],
|
|
240
|
+
[/^word\/comments\.xml$/i, ["comment"]],
|
|
241
|
+
[/^word\/footnotes\.xml$/i, ["footnote"]],
|
|
242
|
+
[/^word\/endnotes\.xml$/i, ["endnote"]],
|
|
243
|
+
[/^word\/fontTable\.xml$/i, ["font"]],
|
|
244
|
+
];
|
|
245
|
+
// Within an unordered collection, the members that compete keep their order: two styles of one type both marked w:default (Word takes
|
|
246
|
+
// the first: the eighth review reversed a Plain and a Bold default and it was merged as no change). Their order is kept in "@order"
|
|
247
|
+
// among those of the same type only (Normal, the default table style and No List are each the default of their own type; their order
|
|
248
|
+
// against each other means nothing). A member with a duplicate key is a unit of its own by its place already (keyedUnits put).
|
|
249
|
+
// The attributes are read by namespace (any prefix bound to WordprocessingML's, never another namespace's `type`; one without a prefix
|
|
250
|
+
// still counts, as it did, w:type first), and a style without w:type is a paragraph style, as Word and the engine read it (the ninth
|
|
251
|
+
// review: Plain without w:type and Bold with "paragraph" were grouped apart, and the person's reversal of them was merged as no change).
|
|
252
|
+
const ON = new Set(["1", "true", "on"]);
|
|
253
|
+
const W_DEFAULT = `{${WML}}default`;
|
|
254
|
+
const W_TYPE = `{${WML}}type`;
|
|
255
|
+
/** the group a member competes in (its w:type, "paragraph" without one), or null when it is not marked w:default; `m`: the prefixes in scope */
|
|
256
|
+
const competes = (a, m) => {
|
|
257
|
+
let on = false;
|
|
258
|
+
let wType = null;
|
|
259
|
+
let bare = null;
|
|
260
|
+
for (const k in a) {
|
|
261
|
+
const q = qname(m, k, false);
|
|
262
|
+
if (q === W_DEFAULT || q === "default") on ||= ON.has(a[k]);
|
|
263
|
+
else if (q === W_TYPE) wType = a[k];
|
|
264
|
+
else if (q === "type") bare = a[k];
|
|
265
|
+
}
|
|
266
|
+
return on ? (wType ?? bare ?? "paragraph") : null;
|
|
267
|
+
};
|
|
268
|
+
/** the local names of the children of part `name` whose order means nothing (UNORDERED), or null */
|
|
269
|
+
export const unorderedIn = (name) => {
|
|
270
|
+
const hit = UNORDERED.find(([re]) => re.test(name));
|
|
271
|
+
return hit ? new Set(hit[1]) : null;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The units of one part, Map key -> unitOf: "@root" (the root element's own tag), each child of the root by its identity (keyOf) or
|
|
276
|
+
* `name#n`, the root's own text by where it stands ("@text#n": after n children), and "@order", the order of the children, by key, but
|
|
277
|
+
* for the collections `unordered` names (a set of local names, UNORDERED above): a style, a list definition, a comment, a relationship
|
|
278
|
+
* is found by its identity wherever it stands, and its order is not what it means. `skip(localName)`: true for children modeled elsewhere;
|
|
279
|
+
* "each" for a list whose members are modeled elsewhere (which slides, which sheets, in which order) but whose attributes are not: each
|
|
280
|
+
* member is then a unit "~<list>/<key>" of what is left once `strip` takes out the modeled attributes.
|
|
281
|
+
*/
|
|
282
|
+
function keyedUnits(xml, skip = null, { keepSpace = false, strip = null, unordered = null } = {}) {
|
|
283
|
+
const units = new Map();
|
|
284
|
+
let depth = 0;
|
|
285
|
+
let cur = null;
|
|
286
|
+
let key = null;
|
|
287
|
+
let each = null; // the list open whose members are units
|
|
288
|
+
let kids = 0;
|
|
289
|
+
const seen = new Map();
|
|
290
|
+
const order = [];
|
|
291
|
+
const rivals = new Map(); // competes() group -> keys in document order
|
|
292
|
+
const put = (k0, str) => {
|
|
293
|
+
// a key seen twice keeps both, in order
|
|
294
|
+
let k = k0;
|
|
295
|
+
for (let n = 2; units.has(k); n++) k = `${k0}~${n}`;
|
|
296
|
+
units.set(k, unitOf(str));
|
|
297
|
+
};
|
|
298
|
+
walkCanonical(xml, {
|
|
299
|
+
open(name, a, tag, prefixes) {
|
|
300
|
+
depth++;
|
|
301
|
+
if (depth === 1) return void units.set("@root", unitOf(tag));
|
|
302
|
+
if (depth === 2) {
|
|
303
|
+
kids++;
|
|
304
|
+
const local = name.slice(name.indexOf(":") + 1);
|
|
305
|
+
const how = skip?.(local);
|
|
306
|
+
cur = null;
|
|
307
|
+
key = null;
|
|
308
|
+
if (how === "each") each = local;
|
|
309
|
+
if (how) return;
|
|
310
|
+
key = keyOf(name, a);
|
|
311
|
+
if (!key) {
|
|
312
|
+
const n = seen.get(local) || 0;
|
|
313
|
+
seen.set(local, n + 1);
|
|
314
|
+
key = `${local}#${n}`;
|
|
315
|
+
order.push(key);
|
|
316
|
+
} else if (!unordered?.has(local)) order.push(key);
|
|
317
|
+
else {
|
|
318
|
+
const g = competes(a, prefixes);
|
|
319
|
+
if (g !== null) (rivals.get(g) || rivals.set(g, []).get(g)).push(key);
|
|
320
|
+
}
|
|
321
|
+
cur = [];
|
|
322
|
+
} else if (depth === 3 && each) {
|
|
323
|
+
key = `~${each}/${keyOf(name, a) ?? name}`;
|
|
324
|
+
cur = [strip ? strip(tag) : tag];
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
cur?.push(tag);
|
|
328
|
+
},
|
|
329
|
+
text(t) {
|
|
330
|
+
if (depth === 1) put(`@text#${kids}`, esc(t));
|
|
331
|
+
else cur?.push(esc(t));
|
|
332
|
+
},
|
|
333
|
+
close() {
|
|
334
|
+
if (depth >= 2) cur?.push("</>");
|
|
335
|
+
if ((depth === 2 && cur) || (depth === 3 && each && cur)) {
|
|
336
|
+
put(key, cur.join(""));
|
|
337
|
+
cur = null;
|
|
338
|
+
}
|
|
339
|
+
if (depth === 2) each = null;
|
|
340
|
+
depth--;
|
|
341
|
+
},
|
|
342
|
+
}, null, { keepSpace });
|
|
343
|
+
for (const g of [...rivals.keys()].sort()) if (rivals.get(g).length > 1) order.push(`|default ${g}:`, ...rivals.get(g));
|
|
344
|
+
units.set("@order", unitOf(order.join(" ")));
|
|
345
|
+
return units;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** A package: its part names and a lazy, cached reading of each part's units (see partUnits). */
|
|
349
|
+
export function readPackage(bytes) {
|
|
350
|
+
const zip = openZip(bytes);
|
|
351
|
+
const cache = new Map();
|
|
352
|
+
return {
|
|
353
|
+
zip,
|
|
354
|
+
names: zip.names,
|
|
355
|
+
has: (name) => zip.entries.has(name),
|
|
356
|
+
raw: (name) => zip.raw(name),
|
|
357
|
+
method: (name) => zip.entries.get(name)?.method,
|
|
358
|
+
text: (name) => zip.readText(name),
|
|
359
|
+
/** Map key -> unit (unitOf); for a binary part, one key "@bytes"; null when the part is absent */
|
|
360
|
+
units(name, skip = null, strip = null) {
|
|
361
|
+
if (!zip.entries.has(name)) return null;
|
|
362
|
+
const id = `${name}\0${skip ? "skip" : ""}`;
|
|
363
|
+
if (cache.has(id)) return cache.get(id);
|
|
364
|
+
let u;
|
|
365
|
+
const bytesOf = () => new Map([["@bytes", { s: hash(zip.read(name)), l: null, v: "" }]]);
|
|
366
|
+
if (!isXml(name)) u = bytesOf();
|
|
367
|
+
else {
|
|
368
|
+
try {
|
|
369
|
+
u = keyedUnits(zip.readText(name), skip, { keepSpace: /^customxml\//i.test(name), strip, unordered: unorderedIn(name) });
|
|
370
|
+
} catch {
|
|
371
|
+
u = bytesOf(); // not well-formed XML: its bytes are what can be compared
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
cache.set(id, u);
|
|
375
|
+
return u;
|
|
376
|
+
},
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Are a part's stored bytes the same in two packages (same method, same compressed bytes)? Then nothing needs inflating. */
|
|
381
|
+
export const sameStored = (P, Q, name) => P.has(name) && Q.has(name) && P.method(name) === Q.method(name) && P.raw(name).equals(Q.raw(name));
|
|
382
|
+
|
|
383
|
+
/** What a person reads for a part they changed: a few words, by where the part sits in the package. */
|
|
384
|
+
export function partLabel(name) {
|
|
385
|
+
const n = name.toLowerCase();
|
|
386
|
+
const rules = [
|
|
387
|
+
[/header\d*\.xml$/, "the header"],
|
|
388
|
+
[/footer\d*\.xml$/, "the footer"],
|
|
389
|
+
[/footnotes\.xml$/, "footnotes"],
|
|
390
|
+
[/endnotes\.xml$/, "endnotes"],
|
|
391
|
+
[/comments[a-z]*\d*\.xml$|people\.xml$/, "comments"],
|
|
392
|
+
[/notesslides\//, "speaker notes"],
|
|
393
|
+
[/notesmaster|handoutmaster/, "notes or handout masters"],
|
|
394
|
+
[/slidemasters\/|slidelayouts\//, "slide masters or layouts"],
|
|
395
|
+
[/\/media\//, "pictures or media"],
|
|
396
|
+
[/embeddings\/|activex\//, "embedded objects"],
|
|
397
|
+
[/charts?\//, "charts"],
|
|
398
|
+
[/diagrams\//, "diagrams"],
|
|
399
|
+
[/drawings\//, "drawings"],
|
|
400
|
+
[/theme\//, "the theme"],
|
|
401
|
+
[/word\/styles\.xml$|styleswitheffects\.xml$/, "styles"],
|
|
402
|
+
[/numbering\.xml$/, "lists"],
|
|
403
|
+
[/settings\.xml$|presprops\.xml$|viewprops\.xml$|websettings\.xml$/, "document settings"],
|
|
404
|
+
[/fonttable\.xml$|\/fonts\//, "fonts"],
|
|
405
|
+
[/^docprops\//, "document properties (title, author and the like)"],
|
|
406
|
+
[/^customxml\//, "custom data"],
|
|
407
|
+
[/xl\/styles\.xml$/, "cell styles"],
|
|
408
|
+
[/xl\/workbook\.xml$/, "workbook settings or defined names"],
|
|
409
|
+
[/ppt\/presentation\.xml$/, "presentation settings"],
|
|
410
|
+
[/tables\/|pivot|querytables|connections\.xml$|externallinks\//, "tables, pivot tables or data connections"],
|
|
411
|
+
[/\.rels$|\[content_types\]\.xml$/, "the links between the file's parts"],
|
|
412
|
+
];
|
|
413
|
+
for (const [re, words] of rules) if (re.test(n)) return words;
|
|
414
|
+
return "another part of the file";
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// ---- DOCX: word/document.xml ---------------------------------------------------------------------------------------------------
|
|
418
|
+
|
|
419
|
+
const OBJ = ""; // an inline object counts as one character, as in src/docx.js
|
|
420
|
+
const PPR_MODELED = new Set(["w:pStyle", "w:jc", "w:numPr"]); // compared on their own by the proof (style, align, list): `mod`
|
|
421
|
+
|
|
422
|
+
/** Interns canonical strings as small numbers, shared by the readings of one merge so the same look has the same number everywhere. */
|
|
423
|
+
export class Interner {
|
|
424
|
+
constructor() {
|
|
425
|
+
this.ids = new Map([["", 0]]);
|
|
426
|
+
this.strs = [""];
|
|
427
|
+
this.looseIds = new Map(); // id -> the id of its string without VOLATILE
|
|
428
|
+
}
|
|
429
|
+
id(s) {
|
|
430
|
+
let n = this.ids.get(s);
|
|
431
|
+
if (n === undefined) (this.ids.set(s, (n = this.ids.size)), this.strs.push(s));
|
|
432
|
+
return n;
|
|
433
|
+
}
|
|
434
|
+
/** the string interned as `n` (-1: none, as a line break between paragraphs of a shape) */
|
|
435
|
+
str(n) {
|
|
436
|
+
return n < 0 ? null : this.strs[n];
|
|
437
|
+
}
|
|
438
|
+
/** the id of the string interned as `n` without VOLATILE */
|
|
439
|
+
loose(n) {
|
|
440
|
+
if (n < 0) return n;
|
|
441
|
+
let v = this.looseIds.get(n);
|
|
442
|
+
if (v === undefined) this.looseIds.set(n, (v = this.id(loose(this.strs[n]))));
|
|
443
|
+
return v;
|
|
444
|
+
}
|
|
445
|
+
/** the id of VOLATILE alone in the string interned as `n` */
|
|
446
|
+
vol(n) {
|
|
447
|
+
return n < 0 ? 0 : this.id(volOf(this.strs[n]));
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* word/document.xml as units. Paragraphs are listed exactly as src/docx.js lists them (every w:p in w:body not inside another w:p,
|
|
453
|
+
* in document order), each with: `text` (the reader's rules: w:t outside w:del, w:tab, a line break, an object as one character),
|
|
454
|
+
* `look` (Int32Array: the canonical run properties of each character, interned), `box` (the table, row and cell it sits in, at every
|
|
455
|
+
* depth; "" in the body), `own` (the w:p tag without its w14:paraId), `mod` (the canonical w:pStyle, w:jc and w:numPr of its own
|
|
456
|
+
* properties, by name), and two residues: `fmt` (its paragraph properties other than style, alignment and list, the paragraph mark's
|
|
457
|
+
* run properties included) and `obj` (every other element in it: pictures, fields, links, bookmarks, comment marks, footnote marks,
|
|
458
|
+
* tracked changes, and a tab or line break counted as text that says more than "a tab" or "a line": a w:br that clears floating
|
|
459
|
+
* pictures, say), each token with the character offset where it stands. A w:t that does not keep its spaces (no xml:space="preserve")
|
|
460
|
+
* and whose text starts or ends with one is a VOLATILE token in `obj` (a reader may drop those spaces: the rule of VOLATILE above). The run boundaries themselves (w:r) do not count: two
|
|
461
|
+
* runs with the same properties next to each other read the same as one. `skeleton` is the canonical document with each run of
|
|
462
|
+
* listed paragraphs replaced by one mark: tables (rows, cells and their properties), sections and anything else outside paragraphs.
|
|
463
|
+
*/
|
|
464
|
+
export function docxUnits(xml, intern) {
|
|
465
|
+
const paras = [];
|
|
466
|
+
const skel = [];
|
|
467
|
+
const stack = [];
|
|
468
|
+
let inBody = false;
|
|
469
|
+
let cur = null; // the listed paragraph open: {text, look, fmt, obj, box}
|
|
470
|
+
let pDepth = 0;
|
|
471
|
+
let delDepth = 0;
|
|
472
|
+
let inT = false;
|
|
473
|
+
const runs = []; // open w:r: {rpr: [] | null, id}
|
|
474
|
+
let rprDepth = 0; // > 0 while inside a run's own w:rPr (collected into the run's look)
|
|
475
|
+
let pprDepth = 0; // > 0 while inside the listed paragraph's own w:pPr
|
|
476
|
+
let skipDepth = 0; // > 0 while inside a modeled w:pPr child (pStyle, jc, numPr), written into cur.mod
|
|
477
|
+
let mod = null; // [name, tokens] of the modeled child open
|
|
478
|
+
let tTrim = false; // the w:t open does not keep its spaces
|
|
479
|
+
let tables = 0;
|
|
480
|
+
const cells = []; // open tables: {t, row, col}
|
|
481
|
+
const chars = []; // look ids of the open paragraph, one per character
|
|
482
|
+
const quiet = []; // per element open inside the paragraph: true when it wrote no token (a run, a text element, a counted tab or break)
|
|
483
|
+
const lookNow = () => (runs.length ? runs[runs.length - 1].id : 0);
|
|
484
|
+
const add = (s) => {
|
|
485
|
+
cur.text += s;
|
|
486
|
+
for (let k = 0; k < s.length; k++) chars.push(lookNow());
|
|
487
|
+
};
|
|
488
|
+
const token = (into, s) => into.push(`${cur.text.length}|${s}`);
|
|
489
|
+
walkCanonical(xml, {
|
|
490
|
+
open(name, a, tag) {
|
|
491
|
+
stack.push(name);
|
|
492
|
+
if (name === "w:body") inBody = true;
|
|
493
|
+
if (name === "w:tbl") cells.push({ t: tables++, row: -1, col: -1 });
|
|
494
|
+
else if (name === "w:tr" && cells.length) (cells[cells.length - 1].row++, (cells[cells.length - 1].col = -1));
|
|
495
|
+
else if (name === "w:tc" && cells.length) cells[cells.length - 1].col++;
|
|
496
|
+
if (name === "w:p") pDepth++;
|
|
497
|
+
if (name === "w:p" && inBody && pDepth === 1) {
|
|
498
|
+
// `own`: the paragraph's own attributes (revision stamps, its text id) but its id, which is how the proof pairs paragraphs
|
|
499
|
+
cur = { text: "", look: null, fmt: [], obj: [], mod: {}, own: tag.replace(/ \{[^}]*\}paraId="[^"]*"/, ""), box: cells.map((c) => `${c.t}.${c.row}.${c.col}`).join("/") };
|
|
500
|
+
chars.length = 0;
|
|
501
|
+
if (skel[skel.length - 1] !== "¶") skel.push("¶");
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (!cur) return void skel.push(tag);
|
|
505
|
+
// inside a listed paragraph
|
|
506
|
+
if (rprDepth) {
|
|
507
|
+
rprDepth++;
|
|
508
|
+
runs[runs.length - 1].rpr.push(tag);
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (name === "w:rPr" && stack[stack.length - 2] === "w:r") {
|
|
512
|
+
rprDepth = 1;
|
|
513
|
+
runs[runs.length - 1].rpr = [tag];
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
if (name === "w:del") delDepth++;
|
|
517
|
+
if (name === "w:r") {
|
|
518
|
+
runs.push({ rpr: null, id: 0 });
|
|
519
|
+
return void quiet.push(true);
|
|
520
|
+
}
|
|
521
|
+
// text, by the reader's rules (src/docx.js parseDocxParagraphs), wherever it stands in the paragraph
|
|
522
|
+
let counted = false;
|
|
523
|
+
if (name === "w:t" && delDepth === 0) (inT = true), (counted = true), (tTrim = !/ \u0001\{[^}]*\}space="preserve"/.test(tag));
|
|
524
|
+
else if (name === "w:tab" && delDepth === 0) add("\t"), (counted = true);
|
|
525
|
+
else if ((name === "w:br" || name === "w:cr") && delDepth === 0 && a["w:type"] !== "page" && a["w:type"] !== "column") add("\n"), (counted = true);
|
|
526
|
+
// a counted tab or break with attributes of its own (a w:br w:clear="all"): its tag, where it stands
|
|
527
|
+
if (counted && name !== "w:t" && tag.includes(" ") && !pprDepth) token(cur.obj, `${runs.length ? `r${lookNow()}` : ""}${tag}</>`);
|
|
528
|
+
else if ((name === "w:drawing" || name === "w:pict") && delDepth === 0) add(OBJ); // one character, and an object too
|
|
529
|
+
if (pprDepth) {
|
|
530
|
+
// the paragraph's own properties (a tab stop inside them is counted as text by the reader as well, and kept here as written)
|
|
531
|
+
pprDepth++;
|
|
532
|
+
if (skipDepth) skipDepth++, mod[1].push(tag);
|
|
533
|
+
else if (pprDepth === 2 && PPR_MODELED.has(name)) (skipDepth = 1), (mod = [name, [tag]]);
|
|
534
|
+
else token(cur.fmt, tag);
|
|
535
|
+
return void quiet.push(false);
|
|
536
|
+
}
|
|
537
|
+
if (name === "w:pPr" && stack[stack.length - 2] === "w:p" && pDepth === 1) {
|
|
538
|
+
pprDepth = 1;
|
|
539
|
+
token(cur.fmt, tag);
|
|
540
|
+
return void quiet.push(false);
|
|
541
|
+
}
|
|
542
|
+
quiet.push(counted);
|
|
543
|
+
// any other element is an object of the paragraph, with the run properties it sits under
|
|
544
|
+
if (!counted) token(cur.obj, `${runs.length ? `r${lookNow()}` : ""}${tag}`);
|
|
545
|
+
},
|
|
546
|
+
text(t) {
|
|
547
|
+
if (!cur) return void skel.push(esc(t));
|
|
548
|
+
if (rprDepth) return void runs[runs.length - 1].rpr.push(esc(t));
|
|
549
|
+
if (inT) {
|
|
550
|
+
if (tTrim && /^[ \t\r\n]|[ \t\r\n]$/.test(t)) token(cur.obj, `${VOL}unkept spaces`);
|
|
551
|
+
return void add(t);
|
|
552
|
+
}
|
|
553
|
+
if (pprDepth) return void (skipDepth ? mod[1].push(esc(t)) : token(cur.fmt, esc(t)));
|
|
554
|
+
token(cur.obj, esc(t));
|
|
555
|
+
},
|
|
556
|
+
close(name) {
|
|
557
|
+
stack.pop();
|
|
558
|
+
if (name === "w:tbl") cells.pop();
|
|
559
|
+
if (name === "w:body") inBody = false;
|
|
560
|
+
if (name === "w:p") pDepth--;
|
|
561
|
+
if (cur && name === "w:p" && pDepth === 0) {
|
|
562
|
+
cur.look = Int32Array.from(chars);
|
|
563
|
+
paras.push(cur);
|
|
564
|
+
cur = null;
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
if (!cur) return void skel.push("</>");
|
|
568
|
+
if (rprDepth) {
|
|
569
|
+
runs[runs.length - 1].rpr.push("</>");
|
|
570
|
+
if (--rprDepth === 0) {
|
|
571
|
+
const r = runs[runs.length - 1];
|
|
572
|
+
r.id = intern.id(r.rpr.join(""));
|
|
573
|
+
}
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
if (name === "w:del") delDepth = Math.max(0, delDepth - 1);
|
|
577
|
+
if (name === "w:r" && runs.length) runs.pop();
|
|
578
|
+
if (name === "w:t") inT = false;
|
|
579
|
+
const silent = quiet.pop();
|
|
580
|
+
if (pprDepth) {
|
|
581
|
+
pprDepth--;
|
|
582
|
+
if (skipDepth) {
|
|
583
|
+
mod[1].push("</>");
|
|
584
|
+
if (--skipDepth === 0) cur.mod[mod[0]] = mod[1].join("");
|
|
585
|
+
} else token(cur.fmt, "</>");
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
if (!silent) token(cur.obj, "</>");
|
|
589
|
+
},
|
|
590
|
+
});
|
|
591
|
+
return { paras, skeleton: unitOf(skel.join("")) };
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// ---- PPTX: one slide part ------------------------------------------------------------------------------------------------------
|
|
595
|
+
|
|
596
|
+
const SHAPE_TAGS = new Set(["sp", "pic", "graphicFrame", "grpSp", "cxnSp"]);
|
|
597
|
+
const localOf = (n) => n.slice(n.indexOf(":") + 1);
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* One slide as units, listed as src/pptx.js lists shapes (each shape element, nested ones included, when it closes): `id`, `text`
|
|
601
|
+
* (its paragraphs joined by a line break, a:br as one), `look` (each character's canonical a:rPr; a line break between paragraphs has
|
|
602
|
+
* none), and the residues `fmt` (inside the text body: paragraph and body properties) and `obj` (outside it: position, size, geometry,
|
|
603
|
+
* fill, line, name, placeholder, effects, a picture's image, a table, a chart...). A nested shape stands in its parent as one mark per
|
|
604
|
+
* shape, with its id (a shape moved from one group into another is a change of both groups).
|
|
605
|
+
* `skeleton`: the slide without its shapes (background, timing, transitions, color map), each run of shapes as one mark.
|
|
606
|
+
*/
|
|
607
|
+
export function slideUnits(xml, intern) {
|
|
608
|
+
const shapes = [];
|
|
609
|
+
const skel = [];
|
|
610
|
+
const open = []; // open shapes
|
|
611
|
+
let depth = 0;
|
|
612
|
+
let para = null; // the a:p open (its shape is the innermost open one)
|
|
613
|
+
let inT = false;
|
|
614
|
+
let run = null; // the a:r open: {rpr: [] | null, id, depth}
|
|
615
|
+
let rprDepth = 0;
|
|
616
|
+
let body = 0; // depth of the open p:txBody (0: none)
|
|
617
|
+
walkCanonical(xml, {
|
|
618
|
+
open(name, a, tag) {
|
|
619
|
+
depth++;
|
|
620
|
+
const l = localOf(name);
|
|
621
|
+
if (SHAPE_TAGS.has(l)) {
|
|
622
|
+
const parent = open[open.length - 1];
|
|
623
|
+
const sh = { id: null, text: "", chars: [], paras: 0, fmt: [], obj: [`0|${tag}`], depth, mark: null };
|
|
624
|
+
if (parent) {
|
|
625
|
+
// the mark is written once the child's id is known (its cNvPr comes after its own tag)
|
|
626
|
+
const into = body ? parent.fmt : parent.obj;
|
|
627
|
+
sh.mark = [into, into.length, `${parent.text.length}|▣`];
|
|
628
|
+
into.push(sh.mark[2]);
|
|
629
|
+
} else if (skel[skel.length - 1] !== "▣") skel.push("▣"); // top-level shapes are units of their own, listed in order
|
|
630
|
+
open.push(sh);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const sh = open[open.length - 1];
|
|
634
|
+
if (!sh) return void skel.push(tag);
|
|
635
|
+
if (rprDepth) return void (rprDepth++, run.rpr.push(tag));
|
|
636
|
+
if (l === "rPr" && run && depth === run.depth + 1) return void ((rprDepth = 1), (run.rpr = [tag]));
|
|
637
|
+
if (l === "cNvPr" && sh.id === null) sh.id = a.id != null ? Number(a.id) : -1;
|
|
638
|
+
if (l === "txBody") body = depth;
|
|
639
|
+
if (l === "r") return void (run = { rpr: null, id: 0, depth });
|
|
640
|
+
if (l === "p" && body) {
|
|
641
|
+
if (sh.paras++ > 0) (sh.text += "\n"), sh.chars.push(-1);
|
|
642
|
+
para = sh;
|
|
643
|
+
}
|
|
644
|
+
if (l === "t" && para) return void (inT = true);
|
|
645
|
+
if (l === "br" && para) (sh.text += "\n"), sh.chars.push(run ? run.id : -1);
|
|
646
|
+
(body ? sh.fmt : sh.obj).push(`${sh.text.length}|${run && l !== "br" ? `r${run.id}` : ""}${tag}`);
|
|
647
|
+
},
|
|
648
|
+
text(t) {
|
|
649
|
+
const sh = open[open.length - 1];
|
|
650
|
+
if (!sh) return void skel.push(esc(t));
|
|
651
|
+
if (rprDepth) return void run.rpr.push(esc(t));
|
|
652
|
+
if (inT && para) {
|
|
653
|
+
sh.text += t;
|
|
654
|
+
for (let k = 0; k < t.length; k++) sh.chars.push(run ? run.id : 0);
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
(body ? sh.fmt : sh.obj).push(`${sh.text.length}|${esc(t)}`);
|
|
658
|
+
},
|
|
659
|
+
close(name) {
|
|
660
|
+
const l = localOf(name);
|
|
661
|
+
depth--;
|
|
662
|
+
const sh = open[open.length - 1];
|
|
663
|
+
if (!sh) return void skel.push("</>");
|
|
664
|
+
if (SHAPE_TAGS.has(l) && depth + 1 === sh.depth) {
|
|
665
|
+
open.pop();
|
|
666
|
+
if (sh.mark) sh.mark[0][sh.mark[1]] = `${sh.mark[2]}${sh.id ?? -1}`;
|
|
667
|
+
shapes.push({ id: sh.id ?? -1, text: sh.text, look: Int32Array.from(sh.chars), fmt: sh.fmt, obj: sh.obj });
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
if (rprDepth) {
|
|
671
|
+
run.rpr.push("</>");
|
|
672
|
+
if (--rprDepth === 0) run.id = intern.id(run.rpr.join(""));
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
if (l === "r" && run && depth + 1 === run.depth) return void (run = null);
|
|
676
|
+
if (l === "t" && inT) return void (inT = false);
|
|
677
|
+
if (l === "p" && para === sh && body) para = null;
|
|
678
|
+
(body ? sh.fmt : sh.obj).push(`${sh.text.length}|</>`);
|
|
679
|
+
if (l === "txBody" && depth + 1 === body) body = 0;
|
|
680
|
+
},
|
|
681
|
+
});
|
|
682
|
+
return { shapes, skeleton: unitOf(skel.join("")) };
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// ---- XLSX: styles, shared strings, one sheet ------------------------------------------------------------------------------------
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* xl/styles.xml as the definitions the places that use them resolve to: `xf(s)`, a cell, row or column style index -> the canonical
|
|
689
|
+
* form of everything that style sets (the cellXfs entry with its font, fill, border, number format and cell style entry written out in
|
|
690
|
+
* place of their ids); `font(i)`, a font (a shared string's phonetic text names one); and `lists`, each definition list as written
|
|
691
|
+
* (numFmts by id, the others by index), for the check of a definition changed in place (src/merge-proof.js restXlsx: the engine only
|
|
692
|
+
* appends to these lists, measured on the committed samples for bold, italic, fill and a number format, so a definition the person
|
|
693
|
+
* changed in place is one the merge never carries, whatever references it).
|
|
694
|
+
*/
|
|
695
|
+
export function cellStyles(xml) {
|
|
696
|
+
const lists = { numFmts: new Map(), fonts: [], fills: [], borders: [], cellStyleXfs: [], cellXfs: [] };
|
|
697
|
+
if (xml != null) {
|
|
698
|
+
let depth = 0;
|
|
699
|
+
let list = null;
|
|
700
|
+
let cur = null;
|
|
701
|
+
let fmtId = null;
|
|
702
|
+
walkCanonical(xml, {
|
|
703
|
+
open(name, a, tag) {
|
|
704
|
+
depth++;
|
|
705
|
+
const l = localOf(name);
|
|
706
|
+
if (depth === 2) list = l in lists ? l : null;
|
|
707
|
+
else if (depth === 3 && list) {
|
|
708
|
+
cur = [];
|
|
709
|
+
fmtId = list === "numFmts" ? a.numFmtId : null;
|
|
710
|
+
}
|
|
711
|
+
if (depth >= 3 && cur) cur.push(tag);
|
|
712
|
+
},
|
|
713
|
+
text(t) {
|
|
714
|
+
if (depth >= 3 && cur) cur.push(esc(t));
|
|
715
|
+
},
|
|
716
|
+
close() {
|
|
717
|
+
if (depth >= 3 && cur) cur.push("</>");
|
|
718
|
+
if (depth === 3 && cur) {
|
|
719
|
+
if (list === "numFmts") lists.numFmts.set(String(fmtId), cur.join(""));
|
|
720
|
+
else lists[list].push(cur.join(""));
|
|
721
|
+
cur = null;
|
|
722
|
+
}
|
|
723
|
+
depth--;
|
|
724
|
+
},
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
const memo = new Map();
|
|
728
|
+
const ref = /(fontId|fillId|borderId|numFmtId|xfId)="([^"]*)"/g;
|
|
729
|
+
const out = (from, i) =>
|
|
730
|
+
(from[i] ?? "").replace(ref, (_m, k, v) => {
|
|
731
|
+
if (k === "fontId") return `font=${lists.fonts[Number(v)] ?? `#${v}`}`;
|
|
732
|
+
if (k === "fillId") return `fill=${lists.fills[Number(v)] ?? `#${v}`}`;
|
|
733
|
+
if (k === "borderId") return `border=${lists.borders[Number(v)] ?? `#${v}`}`;
|
|
734
|
+
if (k === "numFmtId") return `numFmt=${lists.numFmts.get(v) ?? `builtin ${v}`}`;
|
|
735
|
+
return `xf=${from === lists.cellXfs ? out(lists.cellStyleXfs, Number(v)) : v}`;
|
|
736
|
+
});
|
|
737
|
+
const xf = (s) => {
|
|
738
|
+
let v = memo.get(s);
|
|
739
|
+
if (v === undefined) memo.set(s, (v = out(lists.cellXfs, s)));
|
|
740
|
+
return v;
|
|
741
|
+
};
|
|
742
|
+
return { xf, font: (i) => lists.fonts[Number(i)] ?? `#${i}`, lists };
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* xl/sharedStrings.xml: index -> the canonical rich text of the string beyond the text the value check reads (src/xlsx.js: the text of
|
|
747
|
+
* the string's t elements outside phonetic runs): its runs' properties, where each run's text ends, and its phonetic runs and
|
|
748
|
+
* properties as written, their text included (an outside review: the reading of a name changed from hiragana to katakana was a length
|
|
749
|
+
* here, and the merge kept the old one). `font(i)`: a phonetic properties' font, written out in place of its id (cellStyles).
|
|
750
|
+
*/
|
|
751
|
+
export function sharedRich(xml, font = (i) => `#${i}`) {
|
|
752
|
+
const out = [];
|
|
753
|
+
if (xml == null) return out;
|
|
754
|
+
let depth = 0;
|
|
755
|
+
let cur = null;
|
|
756
|
+
let inT = 0; // depth of the modeled t element open
|
|
757
|
+
let phon = 0; // > 0 inside a phonetic run
|
|
758
|
+
walkCanonical(xml, {
|
|
759
|
+
open(name, a, tag) {
|
|
760
|
+
depth++;
|
|
761
|
+
const l = localOf(name);
|
|
762
|
+
if (depth === 2) cur = [];
|
|
763
|
+
else if (cur) {
|
|
764
|
+
if (l === "rPh") phon++;
|
|
765
|
+
if (l === "t" && !inT && !phon) inT = depth;
|
|
766
|
+
else cur.push(l === "phoneticPr" && a.fontId != null ? tag.replace(/ fontId="[^"]*"/, ` font=${font(a.fontId)}`) : tag);
|
|
767
|
+
}
|
|
768
|
+
},
|
|
769
|
+
text(t) {
|
|
770
|
+
if (cur && !inT) cur.push(esc(t));
|
|
771
|
+
else if (cur) cur.push(`#${t.length}`); // where a run's text ends, not the text itself (the value check's)
|
|
772
|
+
},
|
|
773
|
+
close(name) {
|
|
774
|
+
if (depth === inT) inT = 0;
|
|
775
|
+
else if (depth > 2 && cur) {
|
|
776
|
+
cur.push("</>");
|
|
777
|
+
if (localOf(name) === "rPh") phon--;
|
|
778
|
+
}
|
|
779
|
+
if (depth === 2 && cur) (out.push(cur.join("")), (cur = null));
|
|
780
|
+
depth--;
|
|
781
|
+
},
|
|
782
|
+
});
|
|
783
|
+
return out;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
const parseRef = (r) => {
|
|
787
|
+
const m = /^([A-Z]{1,3})(\d+)$/.exec(r || "");
|
|
788
|
+
return m ? (Number(m[2]) - 1) * 16384 + [...m[1]].reduce((x, ch) => x * 26 + ch.charCodeAt(0) - 64, 0) - 1 : null;
|
|
789
|
+
};
|
|
790
|
+
|
|
791
|
+
/**
|
|
792
|
+
* One worksheet as units: `cells` Map key (row * 16384 + col, as src/xlsx.js keys them) -> the canonical residue of the cell: its
|
|
793
|
+
* resolved style (cellStyles, by key), a rich text's run properties (by key), a formula's attributes, any other attribute or child. Not in it, as
|
|
794
|
+
* modeled elsewhere: the value or formula text (the proof's value check), the storage of a text (shared or inline: `t`), and the
|
|
795
|
+
* cached result of a formula (`v` beside an `f`: recomputed by every spreadsheet program when it opens the file, never typed).
|
|
796
|
+
* A cell absent is the same as one with nothing but the style it shows (`at`): its row's (when the row says customFormat), else its
|
|
797
|
+
* column's, else style 0. `rows` Map row -> its attributes other than r, its style resolved. `skeleton`: the sheet without its cells and
|
|
798
|
+
* rows (columns, their styles resolved, merged ranges, views, panes, conditional formats, page setup...). A cell written twice at one
|
|
799
|
+
* address keeps both (a reader takes one or the other: both count).
|
|
800
|
+
*/
|
|
801
|
+
const S_ATTR = / s="[^"]*"/;
|
|
802
|
+
const STYLE_ATTR = / style="[^"]*"/;
|
|
803
|
+
export function sheetUnits(xml, style, rich) {
|
|
804
|
+
// `style(s)` and `rich(i)`: short keys of a style index and a shared string (interned by the caller, so a million cells hold small
|
|
805
|
+
// strings, and the same style or rich text has the same key in every version)
|
|
806
|
+
const cells = new Map();
|
|
807
|
+
const rows = new Map();
|
|
808
|
+
const skel = [];
|
|
809
|
+
let depth = 0;
|
|
810
|
+
let inData = 0;
|
|
811
|
+
let row = null; // the open row's number, or null when its cells go to the skeleton (a row without r)
|
|
812
|
+
let cur = null; // the open cell: {key, s, t, parts, v, f, path}
|
|
813
|
+
const rowStyle = new Map(); // row -> style index its absent cells show
|
|
814
|
+
const colStyle = []; // [first, last, style index] (0-based columns)
|
|
815
|
+
const styled = (t, attr, v) => t.replace(attr === "s" ? S_ATTR : STYLE_ATTR, ` ${attr}=#${style(Number(v) || 0)}`);
|
|
816
|
+
walkCanonical(xml, {
|
|
817
|
+
open(name, a, tag) {
|
|
818
|
+
depth++;
|
|
819
|
+
const l = localOf(name);
|
|
820
|
+
if (!inData) {
|
|
821
|
+
if (l === "sheetData") inData = depth;
|
|
822
|
+
if (l === "col" && a.style != null) {
|
|
823
|
+
colStyle.push([Number(a.min) - 1, Number(a.max) - 1, Number(a.style) || 0]);
|
|
824
|
+
return void skel.push(styled(tag(), "style", a.style));
|
|
825
|
+
}
|
|
826
|
+
return void skel.push(tag());
|
|
827
|
+
}
|
|
828
|
+
if (depth === inData + 1 && l === "row" && a.r != null) {
|
|
829
|
+
row = Number(a.r);
|
|
830
|
+
let own = tag().replace(/ r="[^"]*"/, "");
|
|
831
|
+
if (a.s != null) own = styled(own, "s", a.s);
|
|
832
|
+
if ((a.customFormat === "1" || a.customFormat === "true") && a.s != null) rowStyle.set(row, Number(a.s) || 0);
|
|
833
|
+
if (!/^<(\{[^}]*\})?row>$/.test(own)) rows.set(row, own);
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
const key = depth === inData + 2 && l === "c" && row !== null ? parseRef(a.r) : null;
|
|
837
|
+
if (key !== null) {
|
|
838
|
+
let more = false; // attributes other than r, s and t
|
|
839
|
+
for (const k in a) if (k !== "r" && k !== "s" && k !== "t") more = true;
|
|
840
|
+
cur = { key, s: Number(a.s) || 0, t: a.t || "n", parts: more ? [tag().replace(/ (r|s|t)="[^"]*"/g, "")] : [], v: null, f: false, path: [] };
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
if (!cur) return void skel.push(tag());
|
|
844
|
+
cur.path.push(l);
|
|
845
|
+
if (cur.path.length === 1 && l === "v") return void (cur.v = "");
|
|
846
|
+
if (cur.path.length === 1 && l === "f") cur.f = true;
|
|
847
|
+
// an inline string's text is the value check's: where each run's text ends is kept, not the text
|
|
848
|
+
cur.parts.push(l === "t" && cur.path[0] === "is" && !cur.path.includes("rPh") ? "<t>" : tag());
|
|
849
|
+
},
|
|
850
|
+
text(t) {
|
|
851
|
+
if (!cur) return void skel.push(esc(t));
|
|
852
|
+
const l = cur.path[cur.path.length - 1];
|
|
853
|
+
if (cur.path.length === 1 && l === "v") return void (cur.t === "s" && (cur.v += t));
|
|
854
|
+
if (cur.path.length === 1 && l === "f") return; // the formula text: the value check's
|
|
855
|
+
cur.parts.push(l === "t" && cur.path[0] === "is" && !cur.path.includes("rPh") ? `#${t.length}` : esc(t));
|
|
856
|
+
},
|
|
857
|
+
close(name) {
|
|
858
|
+
const l = localOf(name);
|
|
859
|
+
const at = depth--;
|
|
860
|
+
if (!inData) return void skel.push("</>");
|
|
861
|
+
if (at === inData) return void ((inData = 0), skel.push("</>"));
|
|
862
|
+
if (cur && at === inData + 2) {
|
|
863
|
+
const shared = cur.t === "s" && !cur.f && cur.v !== null ? rich(Number(cur.v)) : "";
|
|
864
|
+
const v = !shared && !cur.parts.length ? style(cur.s) : `${style(cur.s)}|${shared}|${cur.parts.join("")}`;
|
|
865
|
+
cells.set(cur.key, cells.has(cur.key) ? `${cells.get(cur.key)}\u0003${v}` : v);
|
|
866
|
+
cur = null;
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
if (cur) {
|
|
870
|
+
const top = cur.path.pop();
|
|
871
|
+
if (!(cur.path.length === 0 && top === "v")) cur.parts.push("</>");
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
if (at === inData + 1 && l === "row" && row !== null) return void (row = null);
|
|
875
|
+
skel.push("</>");
|
|
876
|
+
},
|
|
877
|
+
}, null, { lazyTags: true });
|
|
878
|
+
const skeleton = skel.join("");
|
|
879
|
+
/** the style a cell absent at `key` shows (row * 16384 + col) */
|
|
880
|
+
const shown = (key) => {
|
|
881
|
+
const r = rowStyle.get(Math.floor(key / 16384) + 1);
|
|
882
|
+
if (r !== undefined) return style(r);
|
|
883
|
+
const c = key % 16384;
|
|
884
|
+
let v = byCol.get(c); // one scan of the columns per column (at most 16,384), not per cell
|
|
885
|
+
if (v === undefined) {
|
|
886
|
+
v = style(0);
|
|
887
|
+
for (const [lo, hi, st] of colStyle) if (c >= lo && c <= hi) v = style(st);
|
|
888
|
+
byCol.set(c, v);
|
|
889
|
+
}
|
|
890
|
+
return v;
|
|
891
|
+
};
|
|
892
|
+
const byCol = new Map();
|
|
893
|
+
const plain = !rowStyle.size && !colStyle.length ? style(0) : null;
|
|
894
|
+
return { cells, rows, skeleton: unitOf(skeleton), at: (key) => cells.get(key) ?? plain ?? shown(key) };
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// ---- comparing residues ----------------------------------------------------------------------------------------------------------
|
|
898
|
+
|
|
899
|
+
/** The length of the common start `p` and end `s` of two strings (not overlapping). Linear. */
|
|
900
|
+
export function ends(a, b) {
|
|
901
|
+
let p = 0;
|
|
902
|
+
const max = Math.min(a.length, b.length);
|
|
903
|
+
while (p < max && a[p] === b[p]) p++;
|
|
904
|
+
let s = 0;
|
|
905
|
+
while (s < max - p && a[a.length - 1 - s] === b[b.length - 1 - s]) s++;
|
|
906
|
+
return { p, s };
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/**
|
|
910
|
+
* Are two residues (lists of "offset|token" read from units with texts `x` and `y`) the same once the text change between them is
|
|
911
|
+
* taken into account? The tokens must be equal in order, and each must stand in the same place relative to the text: in the common
|
|
912
|
+
* start at the same offset, in the common end at the same distance from the end, or in the changed middle on both sides. The common
|
|
913
|
+
* start and the common end are each taken whole (they may overlap where the change is ambiguous: "Fiktiv A" -> "Fiktiv typed A" is
|
|
914
|
+
* " typed" typed after "Fiktiv" or "typed " typed before "A", and a mark right after "Fiktiv" may follow either). Linear.
|
|
915
|
+
*/
|
|
916
|
+
export function sameResidue(X, x, Y, y) {
|
|
917
|
+
if (X.length !== Y.length) return false;
|
|
918
|
+
if (!X.length) return true;
|
|
919
|
+
const { p, s } = ends(x, y);
|
|
920
|
+
let s2 = 0; // the whole common end, not cut by the common start
|
|
921
|
+
const max = Math.min(x.length, y.length);
|
|
922
|
+
while (s2 < max && x[x.length - 1 - s2] === y[y.length - 1 - s2]) s2++;
|
|
923
|
+
const d = y.length - x.length;
|
|
924
|
+
for (let k = 0; k < X.length; k++) {
|
|
925
|
+
const i = X[k].indexOf("|");
|
|
926
|
+
const j = Y[k].indexOf("|");
|
|
927
|
+
if (X[k].slice(i) !== Y[k].slice(j)) return false;
|
|
928
|
+
const ax = Number(X[k].slice(0, i));
|
|
929
|
+
const ay = Number(Y[k].slice(0, j));
|
|
930
|
+
if (ax <= p && ay === ax) continue;
|
|
931
|
+
if (ax >= x.length - s2 && ay === ax + d) continue;
|
|
932
|
+
if (ax >= p && ax <= x.length - s && ay >= p && ay <= y.length - s) continue;
|
|
933
|
+
return false;
|
|
934
|
+
}
|
|
935
|
+
return true;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// VOLATILE in a residue (lists of "offset|token"): tokens that hold a marked attribute, and the marks that stand alone ("|\u0001...")
|
|
939
|
+
const looseResidue = (X) =>
|
|
940
|
+
X.some((t) => t.includes(VOL))
|
|
941
|
+
? X.flatMap((t) => {
|
|
942
|
+
const i = t.indexOf("|");
|
|
943
|
+
const v = t.slice(i + 1);
|
|
944
|
+
return v.startsWith(VOL) ? [] : [`${t.slice(0, i + 1)}${loose(v)}`];
|
|
945
|
+
})
|
|
946
|
+
: X;
|
|
947
|
+
const volResidue = (X) => (X.some((t) => t.includes(VOL)) ? X.filter((t) => t.includes(VOL)).map((t) => ((v) => (v.startsWith(VOL) ? v : volOf(v)))(t.slice(t.indexOf("|") + 1))).join("") : "");
|
|
948
|
+
/** The rule (sameVolatile) on residues: is mine's residue `M` (text `m`) in the result's `R` (text `r`), `B` (text `b`) the base's? */
|
|
949
|
+
export const residueCarried = (B, M, m, R, r) => sameResidue(M, m, R, r) || (volResidue(B) === volResidue(M) && sameResidue(looseResidue(M), m, looseResidue(R), r));
|
|
950
|
+
/** looseSame on residues: the agent's `T` and the result's `R`, equal but for VOLATILE */
|
|
951
|
+
export const residueLooseSame = (T, t, R, r) => sameResidue(T, t, R, r) || sameResidue(looseResidue(T), t, looseResidue(R), r);
|
|
952
|
+
|
|
953
|
+
/** Per-character looks (Int32Array) of two units with texts `x` and `y`: the same on the characters both keep (common start and end)? */
|
|
954
|
+
export function sameLookKept(lx, x, ly, y) {
|
|
955
|
+
const { p, s } = ends(x, y);
|
|
956
|
+
for (let k = 0; k < p; k++) if (lx[k] !== ly[k]) return false;
|
|
957
|
+
for (let k = 1; k <= s; k++) if (lx[lx.length - k] !== ly[ly.length - k]) return false;
|
|
958
|
+
return true;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
/**
|
|
962
|
+
* The rule (sameVolatile) on per-character looks: is mine's look `Y` in the result's `Z` on the characters both keep, `X` the base's?
|
|
963
|
+
* A character of mine is in the result with the same run properties; or the same but for VOLATILE where the person left that
|
|
964
|
+
* character's VOLATILE as base had it. A character the person typed takes the run properties of the text it was typed into: its
|
|
965
|
+
* VOLATILE is left as base had it when it is that of the base character just before or just after the typing (else only the same
|
|
966
|
+
* counts). `I`: the Interner the looks were read with.
|
|
967
|
+
*/
|
|
968
|
+
export function lookCarried(I, X, Y, Z) {
|
|
969
|
+
const { p, s } = ends(Y.text, Z.text);
|
|
970
|
+
const { p: pb, s: sb } = X ? ends(X.text, Y.text) : { p: 0, s: 0 };
|
|
971
|
+
const n = Y.text.length;
|
|
972
|
+
const at = (k) => (k < pb ? k : k >= n - sb ? X.text.length - (n - k) : -1); // mine's character k in base, or -1: typed
|
|
973
|
+
const ok = (k, kz) => {
|
|
974
|
+
const y = Y.look[k];
|
|
975
|
+
const z = Z.look[kz];
|
|
976
|
+
if (y === z) return true;
|
|
977
|
+
if (I.loose(y) !== I.loose(z)) return false;
|
|
978
|
+
if (!X) return false;
|
|
979
|
+
const kb = at(k);
|
|
980
|
+
if (kb >= 0) return I.vol(X.look[kb]) === I.vol(y);
|
|
981
|
+
const v = I.vol(y); // typed: the base characters around the typing
|
|
982
|
+
return (pb > 0 && I.vol(X.look[pb - 1]) === v) || (pb < X.text.length && I.vol(X.look[pb]) === v);
|
|
983
|
+
};
|
|
984
|
+
for (let k = 0; k < p; k++) if (!ok(k, k)) return false;
|
|
985
|
+
for (let k = 1; k <= s; k++) if (!ok(n - k, Z.text.length - k)) return false;
|
|
986
|
+
return true;
|
|
987
|
+
}
|
|
988
|
+
/** looseSame on per-character looks: the agent's `Y` and the result's `Z`, on the characters both keep, equal but for VOLATILE */
|
|
989
|
+
export function lookLooseSame(I, Y, Z) {
|
|
990
|
+
const { p, s } = ends(Y.text, Z.text);
|
|
991
|
+
for (let k = 0; k < p; k++) if (I.loose(Y.look[k]) !== I.loose(Z.look[k])) return false;
|
|
992
|
+
for (let k = 1; k <= s; k++) if (I.loose(Y.look[Y.look.length - k]) !== I.loose(Z.look[Z.look.length - k])) return false;
|
|
993
|
+
return true;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// ---- the engine's paired run properties (DOCX) ------------------------------------------------------------------------------------
|
|
997
|
+
// The engine writes a run property with its complex-script or East Asian twin (vendor/engine-node/engine-node.mjs applyRunSet, the
|
|
998
|
+
// writer of the page and of the merge alike): bold as w:b and w:bCs, italic as w:i and w:iCs, a size as w:sz and w:szCs (the same
|
|
999
|
+
// value), a font as w:rFonts with ascii, hAnsi, eastAsia and cs. A person's version written by another program may carry the first
|
|
1000
|
+
// alone (an outside review: "typo" made bold with w:b only; the merge carried it as w:b w:bCs and the proof refused an ordinary merge).
|
|
1001
|
+
// The twin acts only on characters of its own script: w:bCs, w:iCs and w:szCs (and rFonts' cs) on complex-script text (Hebrew,
|
|
1002
|
+
// Arabic, Thai, the Indic scripts...), rFonts' eastAsia on East Asian text, and on a run that says w:rtl or w:cs or hints eastAsia.
|
|
1003
|
+
// So the two read the same, character by character, when: mine's run has the first property and not the twin, the result's run is
|
|
1004
|
+
// mine's with the twin added at the first's value (and nothing else), the run says none of w:rtl, w:cs or an eastAsia hint, and the
|
|
1005
|
+
// character is below U+0590 (Latin, Greek, Cyrillic, Armenian, the punctuation and symbols of those blocks: no complex-script or East
|
|
1006
|
+
// Asian character is). Anything else is not the same.
|
|
1007
|
+
const W_URI = `{${WML}}`;
|
|
1008
|
+
const TWINS = { b: "bCs", i: "iCs", sz: "szCs" };
|
|
1009
|
+
/** the children of a canonical w:rPr, as {local: attributes} (null when it is not a flat list of empty elements) */
|
|
1010
|
+
function rprKids(str) {
|
|
1011
|
+
if (str === "") return {};
|
|
1012
|
+
const head = `<${W_URI}rPr>`;
|
|
1013
|
+
if (!str.startsWith(head) || !str.endsWith("</>")) return null;
|
|
1014
|
+
const body = str.slice(head.length, -3);
|
|
1015
|
+
const out = {};
|
|
1016
|
+
const re = /<\{[^}]*\}([A-Za-z]+)([^>]*)><\/>/gy;
|
|
1017
|
+
let m;
|
|
1018
|
+
let at = 0;
|
|
1019
|
+
while ((m = re.exec(body))) {
|
|
1020
|
+
if (m[0].indexOf(W_URI) !== 1 || m[1] in out) return null;
|
|
1021
|
+
out[m[1]] = m[2];
|
|
1022
|
+
at = re.lastIndex;
|
|
1023
|
+
}
|
|
1024
|
+
return at === body.length ? out : null;
|
|
1025
|
+
}
|
|
1026
|
+
/** Does the run property `got` (an interned id of the result) read as `want` (mine's) on the character `ch`? See above. */
|
|
1027
|
+
export function twinSame(I, want, got, ch) {
|
|
1028
|
+
if (want === got) return true;
|
|
1029
|
+
if (want < 0 || got < 0 || ch.codePointAt(0) >= 0x0590) return false;
|
|
1030
|
+
const w = rprKids(I.str(want));
|
|
1031
|
+
const g = rprKids(I.str(got));
|
|
1032
|
+
if (!w || !g || "rtl" in w || "cs" in w) return false;
|
|
1033
|
+
if (/hint="eastAsia"/.test(w.rFonts ?? "") || /hint="eastAsia"/.test(g.rFonts ?? "")) return false;
|
|
1034
|
+
for (const k in w) if (!(k in g)) return false;
|
|
1035
|
+
for (const k in g) {
|
|
1036
|
+
if (k in w && k !== "rFonts") {
|
|
1037
|
+
if (g[k] !== w[k]) return false;
|
|
1038
|
+
continue;
|
|
1039
|
+
}
|
|
1040
|
+
const first = Object.keys(TWINS).find((f) => TWINS[f] === k);
|
|
1041
|
+
if (first) {
|
|
1042
|
+
if (!(first in w) || k in w || g[k] !== w[first]) return false;
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
if (k !== "rFonts") return false;
|
|
1046
|
+
// w:rFonts: mine's attributes, and eastAsia and cs added at the value of ascii
|
|
1047
|
+
const attrs = (x) => Object.fromEntries([...x.matchAll(/ \{[^}]*\}([A-Za-z]+)="([^"]*)"/g)].map((m) => [m[1], m[2]]));
|
|
1048
|
+
const [wa, ga] = [attrs(w.rFonts ?? ""), attrs(g.rFonts)];
|
|
1049
|
+
if (!("rFonts" in w)) return false;
|
|
1050
|
+
for (const a in wa) if (ga[a] !== wa[a]) return false;
|
|
1051
|
+
for (const a in ga) if (!(a in wa) && !((a === "eastAsia" || a === "cs") && ga[a] === wa.ascii)) return false;
|
|
1052
|
+
}
|
|
1053
|
+
return true;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// ---- DOCX: numbering --------------------------------------------------------------------------------------------------------------
|
|
1057
|
+
|
|
1058
|
+
/**
|
|
1059
|
+
* word/numbering.xml as what a paragraph's w:numId resolves to: numId -> the canonical w:num without its own id, with the abstract
|
|
1060
|
+
* definition it names written out in place of the abstractNumId (without its id, its w:nsid and its w:tmpl: the numbers that tie a
|
|
1061
|
+
* definition to its copies in other files and to a template; they change nothing on the page and stay compared where they are
|
|
1062
|
+
* written, in numbering.xml). "0" -> "off" (numId 0 takes a style's list off).
|
|
1063
|
+
*/
|
|
1064
|
+
const attrLocal = (a, l) => {
|
|
1065
|
+
for (const k in a) if (k === l || k.endsWith(`:${l}`)) return a[k];
|
|
1066
|
+
return undefined;
|
|
1067
|
+
};
|
|
1068
|
+
export function numberingOf(xml) {
|
|
1069
|
+
const abs = new Map();
|
|
1070
|
+
const nums = new Map();
|
|
1071
|
+
if (xml != null) {
|
|
1072
|
+
let depth = 0;
|
|
1073
|
+
let cur = null;
|
|
1074
|
+
let what = null;
|
|
1075
|
+
let id = null;
|
|
1076
|
+
let skip = 0;
|
|
1077
|
+
walkCanonical(xml, {
|
|
1078
|
+
open(name, a, tag) {
|
|
1079
|
+
depth++;
|
|
1080
|
+
if (depth === 2) {
|
|
1081
|
+
const l = localOf(name);
|
|
1082
|
+
what = l === "abstractNum" ? abs : l === "num" ? nums : null;
|
|
1083
|
+
id = attrLocal(a, l === "abstractNum" ? "abstractNumId" : "numId");
|
|
1084
|
+
cur = what ? [tag.replace(/ \{[^}]*\}(abstractNumId|numId)="[^"]*"/, "")] : null;
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
if (!cur) return;
|
|
1088
|
+
if (skip || (depth === 3 && what === abs && (localOf(name) === "nsid" || localOf(name) === "tmpl"))) return void skip++;
|
|
1089
|
+
if (depth === 3 && what === nums && localOf(name) === "abstractNumId") return void (skip++, cur.push(`<abs ${attrLocal(a, "val")}>`));
|
|
1090
|
+
cur.push(tag);
|
|
1091
|
+
},
|
|
1092
|
+
text(t) {
|
|
1093
|
+
if (cur && !skip) cur.push(esc(t));
|
|
1094
|
+
},
|
|
1095
|
+
close() {
|
|
1096
|
+
if (skip) skip--;
|
|
1097
|
+
else if (cur && depth >= 2) cur.push("</>");
|
|
1098
|
+
if (depth === 2 && cur) (what.set(String(id), cur.join("")), (cur = null));
|
|
1099
|
+
depth--;
|
|
1100
|
+
},
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
const memo = new Map([["0", "off"]]);
|
|
1104
|
+
return (numId) => {
|
|
1105
|
+
const k = String(numId);
|
|
1106
|
+
let v = memo.get(k);
|
|
1107
|
+
if (v === undefined) memo.set(k, (v = nums.has(k) ? nums.get(k).replace(/<abs ([^>]*)>/, (_m, a) => abs.get(a) ?? `#missing abstract ${a}`) : `#missing ${k}`));
|
|
1108
|
+
return v;
|
|
1109
|
+
};
|
|
1110
|
+
}
|