onionskin-cli 0.0.3 → 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.
Files changed (92) hide show
  1. package/CHANGELOG.md +100 -0
  2. package/INSTALL.md +5 -1
  3. package/NOTICE +34 -0
  4. package/README.md +101 -33
  5. package/docs/contracts/batch.schema.json +27 -4
  6. package/docs/contracts/ops.schema.json +117 -5
  7. package/package.json +4 -2
  8. package/skills/onionskin/SKILL.md +9 -6
  9. package/skills/onionskin/reference/batch.md +5 -3
  10. package/skills/onionskin/reference/formats.md +5 -1
  11. package/skills/onionskin/reference/ops.md +47 -3
  12. package/skills/onionskin/reference/troubleshooting.md +31 -0
  13. package/src/agent-detect.js +28 -2
  14. package/src/agent-prompt.js +12 -1
  15. package/src/agent-runner.js +16 -5
  16. package/src/anchor-work.js +154 -0
  17. package/src/anchor-worker.js +38 -0
  18. package/src/anchors.js +61 -11
  19. package/src/apply.js +119 -47
  20. package/src/batch.js +32 -8
  21. package/src/check-worker.js +8 -5
  22. package/src/cli.js +91 -25
  23. package/src/constants.js +34 -1
  24. package/src/diff.js +3 -1
  25. package/src/docx.js +368 -58
  26. package/src/edit-hash.js +19 -0
  27. package/src/fsutil.js +1 -1
  28. package/src/history-worker.js +8 -8
  29. package/src/history.js +135 -31
  30. package/src/html-active.js +384 -0
  31. package/src/html-envelope.js +51 -0
  32. package/src/html-parser.js +8 -0
  33. package/src/html-render-worker.js +25 -0
  34. package/src/html-render.js +880 -0
  35. package/src/html-utf8.js +65 -0
  36. package/src/html-write-worker.js +14 -0
  37. package/src/html-write.js +380 -0
  38. package/src/html.js +1257 -0
  39. package/src/local.js +68 -4
  40. package/src/merge-package.js +1110 -0
  41. package/src/merge-pptx.js +98 -38
  42. package/src/merge-proof.js +1019 -0
  43. package/src/merge-worker.js +60 -0
  44. package/src/merge-xlsx.js +84 -29
  45. package/src/merge.js +286 -83
  46. package/src/nfc.js +46 -0
  47. package/src/notes.js +122 -0
  48. package/src/offload.js +400 -1
  49. package/src/open-worker.js +27 -0
  50. package/src/ops/common.js +2 -1
  51. package/src/ops/docx.js +58 -29
  52. package/src/ops/html.js +394 -0
  53. package/src/ops/pptx.js +74 -21
  54. package/src/ops/run.js +23 -3
  55. package/src/ops/runs.js +215 -13
  56. package/src/ops/schema.js +8 -1
  57. package/src/ops/xlsx.js +12 -7
  58. package/src/ops-worker.js +17 -0
  59. package/src/package.js +26 -2
  60. package/src/person.js +2 -1
  61. package/src/pptx.js +85 -21
  62. package/src/quick-plan.js +13 -6
  63. package/src/redline-worker.js +10 -14
  64. package/src/reply.js +46 -19
  65. package/src/retain.js +223 -0
  66. package/src/review.js +32 -18
  67. package/src/rewrite-check.js +2 -2
  68. package/src/sanitize.js +16 -3
  69. package/src/server.js +80 -36
  70. package/src/session.js +1138 -208
  71. package/src/stop-gate.js +34 -0
  72. package/src/suggest-reply.js +42 -7
  73. package/src/suggest.js +14 -29
  74. package/src/text.js +161 -11
  75. package/src/update.js +288 -0
  76. package/src/voice-guide.js +4 -3
  77. package/src/voice-learn.js +4 -3
  78. package/src/voice-sample-worker.js +7 -6
  79. package/src/voice-worker.js +4 -6
  80. package/src/worker-host.js +23 -0
  81. package/src/xlsx.js +355 -94
  82. package/src/xml-tree.js +6 -3
  83. package/src/xml.js +82 -34
  84. package/src/zip-write.js +2 -1
  85. package/src/zip.js +9 -5
  86. package/vendor/parse5/LICENSE.txt +32 -0
  87. package/vendor/parse5/parse5.mjs +7822 -0
  88. package/vendor/parse5.lock.json +23 -0
  89. package/web/dist/assets/index-DkNx0LSH.css +1 -0
  90. package/web/dist/assets/{index-DS-WST1E.js → index-DytKkMdi.js} +112 -108
  91. package/web/dist/index.html +2 -2
  92. package/web/dist/assets/index-BCAEKiOG.css +0 -1
@@ -0,0 +1,65 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { decodeHtml, NOT_READABLE_ENCODING, NOT_UTF8 } from "./html.js";
4
+
5
+ // The UTF-8 copy of an HTML file saved as windows-1252 or ISO-8859-1 (PLAN-html R10, R18: "Onionskin can save a UTF-8 copy and open
6
+ // that"). A CLI action, never the page's: `onionskin open` on such a file prints the sentence and the command, `onionskin utf8-copy
7
+ // <file>` writes `<name>.utf8.html` next to it (only when that name is free), the original untouched. No parser: decoding only.
8
+
9
+ /** The command that makes the copy, as `open` prints it. */
10
+ export const copyCommand = (bin, file, quote) => `${bin} utf8-copy ${quote(file)}`;
11
+
12
+ /** What `open` prints for a file Onionskin can make a UTF-8 copy of: the fixed sentence and the exact command. */
13
+ export const openRefusal = (bin, file, quote) => `${NOT_UTF8}\nMake the copy with:\n ${copyCommand(bin, file, quote)}\nThen open the file it names.`;
14
+
15
+ /** Can this file's bytes be copied to UTF-8 (a declared windows-1252 or ISO-8859-1 file)? */
16
+ export const copyable = (bytes) => {
17
+ const d = decodeHtml(bytes);
18
+ return !d.ok && d.copyable === true;
19
+ };
20
+
21
+ /**
22
+ * The UTF-8 bytes of a copyable file: decoded as windows-1252 (what a browser reads for both labels), its charset declaration in the
23
+ * first 1,024 bytes said as utf-8 (else the copy would say one thing and be another). Returns {ok: true, bytes} or {ok: false, reason}.
24
+ */
25
+ export function utf8Bytes(bytes) {
26
+ const d = decodeHtml(bytes);
27
+ if (d.ok) return { ok: false, reason: "This file is saved as UTF-8 already; open it as it is." };
28
+ if (!d.copyable) return { ok: false, reason: NOT_READABLE_ENCODING };
29
+ let text = new TextDecoder("windows-1252").decode(bytes);
30
+ // the declaration sits in the first 1,024 bytes (src/html.js declaredCharset); those are one byte a character in windows-1252
31
+ const head = text.slice(0, 1024).replace(/(<meta\b[^>]*?\bcharset\s*=\s*["']?\s*)([A-Za-z0-9_.:-]+)/gi, "$1utf-8");
32
+ text = head + text.slice(1024);
33
+ const out = Buffer.from(text, "utf8");
34
+ const check = decodeHtml(out);
35
+ if (!check.ok) return { ok: false, reason: "Onionskin could not make a UTF-8 copy of this file that it can read." };
36
+ return { ok: true, bytes: out };
37
+ }
38
+
39
+ /** The copy's path: `report.html` -> `report.utf8.html` (also for .htm), in the same folder. */
40
+ export const copyPath = (file) => path.join(path.dirname(file), `${path.basename(file, path.extname(file))}.utf8.html`);
41
+
42
+ /**
43
+ * Writes the copy of `file` next to it, only when its name is free (exclusive create), the original untouched. Returns {ok: true,
44
+ * path} or {ok: false, reason}.
45
+ */
46
+ export function writeUtf8Copy(file) {
47
+ let st;
48
+ try {
49
+ st = fs.statSync(file);
50
+ } catch {
51
+ return { ok: false, reason: `There is no file at ${file}.` };
52
+ }
53
+ if (!st.isFile()) return { ok: false, reason: `${file} is not a regular file.` };
54
+ if (!/\.html?$/i.test(file)) return { ok: false, reason: "utf8-copy makes a copy of an .html or .htm file." };
55
+ const r = utf8Bytes(fs.readFileSync(file));
56
+ if (!r.ok) return r;
57
+ const to = copyPath(file);
58
+ try {
59
+ fs.writeFileSync(to, r.bytes, { flag: "wx" });
60
+ } catch (err) {
61
+ if (err?.code === "EEXIST") return { ok: false, reason: `${to} exists already, so nothing was written. Open that file, or move it away and run this again.` };
62
+ return { ok: false, reason: `Could not write ${to}: ${err.message}` };
63
+ }
64
+ return { ok: true, path: to };
65
+ }
@@ -0,0 +1,14 @@
1
+ import "./html-parser.js";
2
+ import { checkedHtmlWrite } from "./html-write.js";
3
+
4
+ // The write check of an HTML file (src/html-write.js checkedHtmlWrite) off the server's thread (src/offload.js htmlWriteOffThread), within
5
+ // a time and memory limit: an agent's whole new version (/cli/agent-write, /cli/commit, a standalone commit) and, from Phase 2a, the
6
+ // person's own text edits. One message back: checkedHtmlWrite's answer, or {ok: false, code: "check_failed"} when it threw.
7
+ export function work({ before, after, edits }) {
8
+ const b = (x) => (x ? Buffer.from(x.buffer, x.byteOffset, x.byteLength) : null);
9
+ try {
10
+ return checkedHtmlWrite(b(before), b(after), edits ? { edits } : {});
11
+ } catch (err) {
12
+ return { ok: false, code: "check_failed", reason: `the write check failed: ${String(err?.message || err).slice(0, 200)}` };
13
+ }
14
+ }
@@ -0,0 +1,380 @@
1
+ import { editHash } from "./edit-hash.js";
2
+ import { ACTIVE_REFUSED, HTML_NS, inventory, inventoryDelta } from "./html-active.js";
3
+ import { decodeHtml, escapeText, kids, leadingLfParent, parseChecked, parseFragmentIn, proveEdits, proveSplice, readHtml, textContext, textRange } from "./html.js";
4
+
5
+ // Every write of an HTML file (PLAN-html R1, R10, R12, R22): the edit model the agent's ops and the person's text edits share (source
6
+ // splices plus what the parsed tree must then hold), and checkedHtmlWrite, the one check every path that changes an HTML file runs in a
7
+ // worker before anything is written: decoding, the limits, the lockstep proof for edits, the charset declaration on the first non-ASCII
8
+ // write, and the whole-file inventory of active content. The parser is not imported here (src/html-parser.js is, by the worker entries).
9
+
10
+ const isText = (x) => x.nodeName === "#text";
11
+ const isEl = (x) => x.tagName !== undefined;
12
+
13
+ // ---- the edit model ------------------------------------------------------------------------------------------------------------------
14
+ /**
15
+ * An edit: what one op (or one of the person's text edits) changes, as found on the reading it was bound to.
16
+ * splices: [{s, e, repl}] source ranges of the decoded text (UTF-16 offsets) and their new source text
17
+ * texts: [{node, start, end, repl}] ranges of a text node's VALUE and the value that replaces them
18
+ * attrs: [{el, name, value}] an attribute set (value a string) or removed (value null)
19
+ * children: [{parent, i, j, make, removed}] children [i, j) of `parent` replaced by make()'s nodes
20
+ * touched: nodes this edit reads or changes; removed: subtrees it takes out (two edits conflict when one touches what the other removes)
21
+ */
22
+ export const emptyEdit = () => ({ splices: [], texts: [], attrs: [], children: [], touched: [], removed: [] });
23
+
24
+ /**
25
+ * Where each offset of a text node's value is in the source: an Int32Array of value.length + 1 entries, -1 where an offset falls inside
26
+ * one character reference (`&fjlig;` is two characters of value) or a CRLF the parser read as one LF. The source of the node is cut at each
27
+ * `&`; a piece without one is literal text (CR and CRLF read as LF), and a piece that starts with one is parsed alone in the node's
28
+ * context, which gives the reference's value; the literal text after the reference is what the rest of the piece reads as. Null when
29
+ * the pieces do not add up to the value (NUL characters the parser drops, or anything else this cannot place exactly).
30
+ */
31
+ export function sourceMap(read, node) {
32
+ const [s, e] = textRange(node, read.text);
33
+ const src = read.text.slice(s, e);
34
+ const V = node.value;
35
+ const at = new Int32Array(V.length + 1).fill(-1);
36
+ let v = 0;
37
+ const literal = (from, to) => {
38
+ for (let k = from; k < to; ) {
39
+ at[v] = s + k;
40
+ if (src[k] === "\r") {
41
+ if (V[v] !== "\n") return false;
42
+ k += src[k + 1] === "\n" ? 2 : 1;
43
+ } else {
44
+ if (V[v] !== src[k]) return false;
45
+ k++;
46
+ }
47
+ v++;
48
+ }
49
+ return true;
50
+ };
51
+ const cr = (t) => t.replace(/\r\n?/g, "\n");
52
+ let k = src.indexOf("&");
53
+ if (!literal(0, k < 0 ? src.length : k)) return null;
54
+ while (k >= 0) {
55
+ const next = src.indexOf("&", k + 1);
56
+ const piece = src.slice(k, next < 0 ? src.length : next);
57
+ const nodes = parseFragmentIn(node.__ctx, piece).nodes;
58
+ if (nodes.length > 1 || (nodes.length === 1 && !isText(nodes[0]))) return null;
59
+ const d = nodes.length ? nodes[0].value : "";
60
+ // the reference is the shortest head of the piece after which the rest reads as itself. Only a few heads are tried, so a long piece
61
+ // costs linear time: a named reference is at most 33 characters (&CounterClockwiseContourIntegral;); a numeric one is &# (x), its
62
+ // digits (any number of leading zeros) and an optional semicolon.
63
+ const num = /^&#[xX]?/.exec(piece);
64
+ const heads = [];
65
+ if (num) {
66
+ let q = num[0].length;
67
+ const hex = num[0].length === 3;
68
+ while (q < piece.length && (hex ? /[0-9a-fA-F]/ : /[0-9]/).test(piece[q])) q++;
69
+ if (q > num[0].length) heads.push(q, ...(piece[q] === ";" ? [q + 1] : []));
70
+ heads.unshift(1);
71
+ } else for (let L = 1; L <= Math.min(piece.length, 40); L++) heads.push(L);
72
+ let len = -1;
73
+ for (const L of heads) {
74
+ const tail = cr(piece.slice(L));
75
+ if (d.length > tail.length && d.endsWith(tail)) {
76
+ len = L;
77
+ break;
78
+ }
79
+ }
80
+ if (len < 0) return null;
81
+ const ref = d.slice(0, d.length - cr(piece.slice(len)).length);
82
+ if (V.slice(v, v + ref.length) !== ref) return null;
83
+ at[v] = s + k;
84
+ v += ref.length;
85
+ if (!literal(k + len, k + piece.length)) return null;
86
+ k = next;
87
+ }
88
+ if (v !== V.length) return null;
89
+ at[V.length] = e;
90
+ // an offset between the two halves of a surrogate pair is no place to cut
91
+ for (let i = 1; i < V.length; i++) if (/[\udc00-\udfff]/.test(V[i]) && /[\ud800-\udbff]/.test(V[i - 1])) at[i] = -1;
92
+ return at;
93
+ }
94
+
95
+ /**
96
+ * The edit of replacing value ranges of text nodes (`items`: [{node, start, end, repl}], value offsets). Each range becomes a splice of
97
+ * exactly its own source, escaped for its context (src/html.js escapeText), so character references and line endings elsewhere in the
98
+ * node stay as they are. Returns {ok: true, edit} or {ok: false, node, code: "not_addressable" | "nul" | "lone_surrogate", why}.
99
+ */
100
+ export function textEdit(read, items) {
101
+ const edit = emptyEdit();
102
+ const maps = new Map();
103
+ for (const it of items) {
104
+ const { node } = it;
105
+ if (!maps.has(node)) maps.set(node, sourceMap(read, node));
106
+ const map = maps.get(node);
107
+ const s = map?.[it.start] ?? -1;
108
+ const e = map?.[it.end] ?? -1;
109
+ if (!map || s < 0 || e < 0) return { ok: false, node, code: "not_addressable", why: "the text there is written with a character reference or line ending that the change would cut in half" };
110
+ const pst = node.__ctx?.sourceCodeLocation?.startTag;
111
+ const esc = escapeText(it.repl, textContext(node), { atStartOfLeadingLf: leadingLfParent(node) && !!pst && s === pst.endOffset });
112
+ if (!esc.ok) return { ok: false, node, code: esc.code, why: esc.code === "nul" ? "the new text holds a NUL character" : "the new text holds a lone surrogate" };
113
+ edit.splices.push({ s, e, repl: esc.out });
114
+ edit.texts.push({ node, start: it.start, end: it.end, repl: it.repl });
115
+ if (!edit.touched.includes(node)) edit.touched.push(node);
116
+ }
117
+ return { ok: true, edit };
118
+ }
119
+
120
+ const cmpSplice = (a, b) => a.s - b.s || (a.e === a.s ? 0 : 1) - (b.e === b.s ? 0 : 1) || a.e - b.e || a.k - b.k;
121
+ /** Do two splices share a place? Two insertions at one point do not (op order decides); an insertion inside a range does. */
122
+ function clash(x, y) {
123
+ const ex = x.s === x.e;
124
+ const ey = y.s === y.e;
125
+ if (ex && ey) return false;
126
+ if (ex) return y.s < x.s && x.s < y.e;
127
+ if (ey) return x.s < y.s && y.s < x.e;
128
+ return Math.max(x.s, y.s) < Math.min(x.e, y.e);
129
+ }
130
+ const inside = (n, set) => {
131
+ for (let z = n; z; z = z.__par) if (set.has(z)) return true;
132
+ return false;
133
+ };
134
+ /**
135
+ * Does `edit` change a place one of `accepted` (edits of earlier ops of the same file) changes? Sources overlap, the same attribute of one
136
+ * element, or one touches what the other removes. Sorted sweep over the splices: one pair of neighbors per splice, so a document-wide
137
+ * replace of many matches against another costs n log n, not n times m.
138
+ */
139
+ export function conflicts(accepted, edit) {
140
+ if (!accepted.length) return false;
141
+ const all = [];
142
+ accepted.forEach((a) => a.splices.forEach((sp) => all.push({ ...sp, set: 0 })));
143
+ edit.splices.forEach((sp) => all.push({ ...sp, set: 1 }));
144
+ all.sort((a, b) => a.s - b.s || (a.e === a.s ? 0 : 1) - (b.e === b.s ? 0 : 1) || a.e - b.e);
145
+ const last = [null, null];
146
+ for (const x of all) {
147
+ const o = last[1 - x.set];
148
+ if (o && clash(x, o)) return true;
149
+ // the other set's splice that can still reach past x: the latest start, which (they never overlap each other) ends last
150
+ last[x.set] = x;
151
+ }
152
+ for (const a of accepted) for (const x of a.attrs) for (const y of edit.attrs) if (x.el === y.el && x.name === y.name) return true;
153
+ const gone = new Set(accepted.flatMap((a) => a.removed));
154
+ if (gone.size && edit.touched.concat(edit.removed).some((t) => inside(t, gone))) return true;
155
+ const mine = new Set(edit.removed);
156
+ if (mine.size && accepted.some((a) => a.touched.concat(a.removed).some((t) => inside(t, mine)))) return true;
157
+ return false;
158
+ }
159
+
160
+ /**
161
+ * The bytes and the proof plan of `edits` (in op order) on `read`: every splice applied to the source at once (an insertion before a range
162
+ * that starts at the same point, insertions at one point in op order), and for proveEdits the new value of every edited text node, the new
163
+ * attribute list of every edited element and the child replacements of every edited parent. The edits must not conflict.
164
+ */
165
+ export function assemble(read, edits) {
166
+ const all = [];
167
+ edits.forEach((ed, k) => ed.splices.forEach((sp) => all.push({ ...sp, k })));
168
+ all.sort(cmpSplice);
169
+ let out = "";
170
+ let at = 0;
171
+ for (const sp of all) {
172
+ out += read.text.slice(at, sp.s) + sp.repl;
173
+ at = Math.max(at, sp.e);
174
+ }
175
+ out += read.text.slice(at);
176
+ const bytes = Buffer.concat([read.bytes.subarray(0, read.bomLen), Buffer.from(out, "utf8")]);
177
+ const texts = new Map();
178
+ const ranges = new Map();
179
+ edits.forEach((ed) => ed.texts.forEach((t) => (ranges.get(t.node) || ranges.set(t.node, []).get(t.node)).push(t)));
180
+ for (const [node, rs] of ranges) {
181
+ rs.sort((a, b) => a.start - b.start);
182
+ let v = "";
183
+ let p = 0;
184
+ for (const r of rs) {
185
+ v += node.value.slice(p, r.start) + r.repl;
186
+ p = r.end;
187
+ }
188
+ texts.set(node, v + node.value.slice(p));
189
+ }
190
+ const attrs = new Map();
191
+ edits.forEach((ed) =>
192
+ ed.attrs.forEach(({ el, name, value }) => {
193
+ const list = attrs.get(el) || el.attrs.map((a) => ({ ...a }));
194
+ const i = list.findIndex((a) => a.name === name && !a.namespace);
195
+ if (value === null) {
196
+ if (i >= 0) list.splice(i, 1);
197
+ } else if (i >= 0) list[i] = { ...list[i], value };
198
+ else list.push({ name, value });
199
+ attrs.set(el, list);
200
+ }),
201
+ );
202
+ const children = new Map();
203
+ edits.forEach((ed, k) => ed.children.forEach((c) => (children.get(c.parent) || children.set(c.parent, []).get(c.parent)).push({ ...c, k })));
204
+ for (const list of children.values()) list.sort((a, b) => a.i - b.i || (a.i === a.j ? 0 : 1) - (b.i === b.j ? 0 : 1) || a.k - b.k);
205
+ return { bytes, plan: { texts, attrs, children } };
206
+ }
207
+
208
+ // ---- active content of one edit (to name the op that adds it) ---------------------------------------------------------------------
209
+ const cloneOf = (el, attrs, texts) => {
210
+ const c = { nodeName: el.nodeName, tagName: el.tagName, namespaceURI: el.namespaceURI, attrs, childNodes: [] };
211
+ c.childNodes = texts.map((value) => ({ nodeName: "#text", value, parentNode: c }));
212
+ return c;
213
+ };
214
+ const textsOf = (list) => list.filter(isText).map((x) => x.value);
215
+ /**
216
+ * inventoryDelta of what one edit takes out against what it puts in (R1), without a parse: the subtrees it removes against the nodes it
217
+ * makes, and each element it changes (attributes, or its own text, which is a disallowed element's body) before and after. Inventories
218
+ * are counted multisets, so for edits that do not conflict this names exactly the entries the whole-file inventory finds added.
219
+ * `scripted`: whether the file loads a script (the template rule).
220
+ */
221
+ export function editDelta(edit, scripted) {
222
+ const before = [];
223
+ const after = [];
224
+ const byParent = new Map();
225
+ for (const t of edit.texts) (byParent.get(t.node.__par) || byParent.set(t.node.__par, []).get(t.node.__par)).push(t);
226
+ for (const [p, ts] of byParent) {
227
+ if (!p || !isEl(p)) continue;
228
+ const vals = kids(p).filter(isText);
229
+ const now = vals.map((x) => {
230
+ const mine = ts.filter((t) => t.node === x).sort((a, b) => a.start - b.start);
231
+ let v = "";
232
+ let q = 0;
233
+ for (const r of mine) (v += x.value.slice(q, r.start) + r.repl), (q = r.end);
234
+ return v + x.value.slice(q);
235
+ });
236
+ before.push(cloneOf(p, p.attrs, vals.map((x) => x.value)));
237
+ after.push(cloneOf(p, p.attrs, now));
238
+ }
239
+ const els = new Map();
240
+ for (const a of edit.attrs) (els.get(a.el) || els.set(a.el, []).get(a.el)).push(a);
241
+ for (const [el, list] of els) {
242
+ let attrs = el.attrs.map((a) => ({ ...a }));
243
+ for (const { name, value } of list) {
244
+ const i = attrs.findIndex((a) => a.name === name && !a.namespace);
245
+ if (value === null) attrs = attrs.filter((_, k) => k !== i);
246
+ else if (i >= 0) attrs[i] = { ...attrs[i], value };
247
+ else attrs.push({ name, value });
248
+ }
249
+ before.push(cloneOf(el, el.attrs, []));
250
+ after.push(cloneOf(el, attrs, []));
251
+ }
252
+ for (const c of edit.children) {
253
+ const made = c.make();
254
+ before.push(...c.removed);
255
+ after.push(...made);
256
+ if (isEl(c.parent)) {
257
+ const old = kids(c.parent);
258
+ before.push(cloneOf(c.parent, c.parent.attrs, textsOf(old)));
259
+ after.push(cloneOf(c.parent, c.parent.attrs, textsOf([...old.slice(0, c.i), ...made, ...old.slice(c.j)])));
260
+ }
261
+ }
262
+ const inv = (nodes) => inventory({ nodeName: "#document-fragment", childNodes: nodes });
263
+ const a = inv(before);
264
+ const b = inv(after);
265
+ a.scripted = b.scripted = scripted || b.scripted;
266
+ return inventoryDelta(a, b);
267
+ }
268
+
269
+ // ---- the charset declaration (R10) -----------------------------------------------------------------------------------------------
270
+ export const CHARSET_META = '<meta charset="utf-8">';
271
+ const META_NODE = { nodeName: "meta", tagName: "meta", namespaceURI: HTML_NS, attrs: [{ name: "charset", value: "utf-8" }], childNodes: [] };
272
+ /** Said to the agent when a first non-ASCII write cannot also declare UTF-8 (code charset_needs_head). */
273
+ export const CHARSET_NEEDS_HEAD =
274
+ "This page does not say it is saved as UTF-8, and Onionskin found no place near its top to say so, so text with characters outside plain ASCII was not written. Write those characters as character references (for example &eacute; for é), or first add a <head> element at the top of the page.";
275
+ const hasHigh = (b, from = 0) => {
276
+ for (let i = from; i < b.length; i++) if (b[i] >= 0x80) return true;
277
+ return false;
278
+ };
279
+
280
+ /**
281
+ * R10: the first write that adds a non-ASCII character to a file that declares no charset (no BOM, no meta charset in its first 1,024
282
+ * bytes, and no non-ASCII byte before) also inserts <meta charset="utf-8"> as the first child of head, at the first place where that
283
+ * insertion proves (only that element added, nothing else moved) and ends within the first 1,024 bytes (where a browser looks): right
284
+ * after the head start tag, else after the html start tag, else after the doctype, else at the very start. No such place: refused.
285
+ * `after` is a parsed version ({bytes, text, bomLen, declared, doc, limits}). Returns {ok: true, bytes, doc, added} or a refusal.
286
+ */
287
+ export function withCharset(beforeInfo, after) {
288
+ const need = !beforeInfo.declared && beforeInfo.ascii && !after.bomLen && !after.declared && hasHigh(after.bytes);
289
+ if (!need) return { ok: true, bytes: after.bytes, doc: after.doc, added: false };
290
+ const root = after.doc.childNodes.find((x) => isEl(x) && x.tagName === "html");
291
+ const head = root && kids(root).find((x) => isEl(x) && x.tagName === "head" && x.namespaceURI === HTML_NS);
292
+ const doctype = after.doc.childNodes.find((x) => x.nodeName === "#documentType");
293
+ if (head) {
294
+ const path = [after.doc.childNodes.indexOf(root), kids(root).indexOf(head)];
295
+ const at = [head.sourceCodeLocation?.startTag?.endOffset, root.sourceCodeLocation?.startTag?.endOffset, doctype?.sourceCodeLocation?.endOffset, 0];
296
+ for (const off of [...new Set(at.filter(Number.isInteger))]) {
297
+ const text = after.text.slice(0, off) + CHARSET_META + after.text.slice(off);
298
+ if (Buffer.byteLength(text.slice(0, off + CHARSET_META.length), "utf8") > 1024) continue;
299
+ const bytes = Buffer.from(text, "utf8");
300
+ const p = proveSplice({ doc: after.doc, bomLen: 0, limits: after.limits }, bytes, { parentPath: path, i: 0, j: 0, make: () => [META_NODE] });
301
+ if (p.ok) return { ok: true, bytes, doc: p.parsed.doc, added: true };
302
+ }
303
+ }
304
+ return { ok: false, code: "charset_needs_head", reason: CHARSET_NEEDS_HEAD };
305
+ }
306
+
307
+ // ---- the check every write runs --------------------------------------------------------------------------------------------------
308
+ const refusal = (code, reason, extra = {}) => ({ ok: false, code, reason, ...extra });
309
+ const unreadable = (p) => (p.code === "limits" ? refusal("too_large", p.why) : refusal("not_utf8", p.why));
310
+
311
+ /**
312
+ * The check of one write of an HTML file, run in a worker (src/html-write-worker.js; the driver runs it in the ops worker). The whole write
313
+ * is accepted or refused; nothing is written on a refusal. Inputs: the file's bytes now (`beforeBytes`) and the bytes to write
314
+ * (`afterBytes`), and in `opts` at most one of:
315
+ * plan: an edit plan (src/html-write.js assemble) on `opts.read`: the lockstep proof that only those places changed (src/html.js
316
+ * proveEdits); an op write
317
+ * parsed: `afterBytes` already proven and parsed by the caller (the driver), not parsed again
318
+ * edits: the person's own text edits, [{nodeId, text, baseTextHash?}] on a reading of `beforeBytes` (R11: each replaces one source
319
+ * text node; `baseTextHash`, src/edit-hash.js, must be the hash of that node's text now, else "edit_moved"): applied
320
+ * here, proven, and `afterBytes` (may be null) must equal the result
321
+ * none: a whole new version (a commit, an agent write): decoding, the limits and a parse, no splice proof
322
+ * then the charset rule (withCharset) and the inventory of active content before and after (R1). `opts.read` (a reading of beforeBytes)
323
+ * and `opts.beforeInv` save a parse. Holds at most two trees.
324
+ * Returns {ok: true, bytes (to write: afterBytes, or with the charset declaration added), charset_added, links ({added, removed} link and
325
+ * load targets, for History)} or {ok: false, code, reason, reasons?}: code "active_content" (reason ACTIVE_REFUSED, reasons the fixed
326
+ * codes of src/html-active.js), "too_large", "not_utf8", "before_unreadable", "proof_failed", "charset_needs_head", "not_addressable",
327
+ * "overlapping_edit", "edit_moved", "edits_mismatch", "not_well_formed" (a NUL or a lone surrogate in an edit's text).
328
+ */
329
+ export function checkedHtmlWrite(beforeBytes, afterBytes, opts = {}) {
330
+ let read = opts.read || null;
331
+ let beforeDoc = read?.doc;
332
+ let declared = read ? read.bomLen > 0 || decodeHtml(read.bytes).declared != null : false;
333
+ if (!read) {
334
+ const p = parseChecked(beforeBytes);
335
+ if (!p.ok) return refusal("before_unreadable", `the file as it is now cannot be read as HTML: ${p.why}`);
336
+ beforeDoc = p.doc;
337
+ declared = p.bomLen > 0 || p.declared != null;
338
+ }
339
+ const beforeInfo = { declared, ascii: !hasHigh(read ? read.bytes : beforeBytes) };
340
+ const beforeInv = opts.beforeInv || inventory(beforeDoc);
341
+ let after;
342
+ if (opts.edits) {
343
+ if (!read || !read.nodes) read = readHtml(beforeBytes);
344
+ const seen = new Set();
345
+ const items = [];
346
+ for (const ed of opts.edits) {
347
+ const x = read.nodes[ed?.nodeId];
348
+ if (!x || !isText(x) || x.__hidden || x.__ok !== true || !["normal", "rcdata"].includes(textContext(x))) return refusal("not_addressable", "a text edit names a place that is not one editable text of the page");
349
+ if (seen.has(x)) return refusal("overlapping_edit", "two text edits name the same text");
350
+ // the page's hash of the text it edited (src/edit-hash.js): another text at that place means the page and the file disagree
351
+ if (ed.baseTextHash != null && editHash(x.value) !== ed.baseTextHash) return refusal("edit_moved", "a text edit was made on text that is not at that place in the file now");
352
+ seen.add(x);
353
+ items.push({ node: x, start: 0, end: x.value.length, repl: String(ed.text ?? "") });
354
+ }
355
+ const t = textEdit(read, items);
356
+ if (!t.ok) return refusal(t.code === "not_addressable" ? "not_addressable" : "not_well_formed", t.why);
357
+ const { bytes, plan } = assemble(read, [t.edit]);
358
+ if (afterBytes && !Buffer.from(afterBytes).equals(bytes)) return refusal("edits_mismatch", "the bytes to write are not the text edits applied to the file");
359
+ const p = proveEdits(read, bytes, plan);
360
+ if (!p.ok) return p.code === "limits" ? refusal("too_large", p.why) : refusal("proof_failed", "the change would also change the page somewhere else, so nothing was written");
361
+ after = { ...p.parsed, declared: decodeHtml(bytes).declared };
362
+ } else if (opts.plan) {
363
+ const p = proveEdits(read, afterBytes, opts.plan);
364
+ if (!p.ok) return p.code === "limits" ? refusal("too_large", p.why) : refusal("proof_failed", "the change would also change the page somewhere else, so nothing was written");
365
+ after = { ...p.parsed, declared: decodeHtml(p.parsed.bytes).declared };
366
+ } else if (opts.parsed) {
367
+ after = { ...opts.parsed, declared: decodeHtml(opts.parsed.bytes).declared };
368
+ } else {
369
+ beforeDoc = null; // the old tree is not needed past its inventory: at most two trees
370
+ const p = parseChecked(afterBytes);
371
+ if (!p.ok) return unreadable(p);
372
+ after = p;
373
+ }
374
+ const c = withCharset(beforeInfo, after);
375
+ if (!c.ok) return c;
376
+ after = null;
377
+ const d = inventoryDelta(beforeInv, inventory(c.doc));
378
+ if (d.refused) return refusal("active_content", ACTIVE_REFUSED, { reasons: d.reasons });
379
+ return { ok: true, bytes: c.bytes, charset_added: c.added, links: d.links };
380
+ }