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.
- package/CHANGELOG.md +100 -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 +16 -5
- 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 +2 -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 +80 -36
- package/src/session.js +1138 -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/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-DS-WST1E.js → index-DytKkMdi.js} +112 -108
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-BCAEKiOG.css +0 -1
package/src/history.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
import { env } from "./constants.js";
|
|
4
|
+
import { env, unknownFormat } from "./constants.js";
|
|
6
5
|
import { readUnits } from "./anchors.js";
|
|
7
6
|
import { diffText } from "./diff.js";
|
|
8
7
|
import { exactHash, normText, similarity } from "./text.js";
|
|
@@ -10,6 +9,9 @@ import { fmtNote } from "./fmt.js";
|
|
|
10
9
|
import { sub, writeStateFile } from "./paths.js";
|
|
11
10
|
import { untrusted } from "./sanitize.js";
|
|
12
11
|
import { a1, a1Range, ch, sheetRows } from "./xlsx.js";
|
|
12
|
+
import { startWorker } from "./offload.js";
|
|
13
|
+
import { elementPath, ownHash, ownText, pathString, safeTag } from "./html.js";
|
|
14
|
+
import { inventory, inventoryDelta } from "./html-active.js";
|
|
13
15
|
|
|
14
16
|
// Change history: the last updates to the document, built from the version snapshots the session already keeps. Read-only: nothing here
|
|
15
17
|
// writes the document. One update = one round (the user's own edits saved by one Send, one agent run, one revert, one outside change
|
|
@@ -147,38 +149,81 @@ const docxLocator = (p) => (p ? { format: "docx", body_path: p.path ?? null, par
|
|
|
147
149
|
export const docxFmt = (p) => ({ props: p.fmt, style: p.style, styleName: p.styleName, align: p.align });
|
|
148
150
|
|
|
149
151
|
/**
|
|
150
|
-
*
|
|
151
|
-
* the
|
|
152
|
-
*
|
|
152
|
+
* Paragraph identity between two versions of a document, the one rule every pairing of DOCX paragraphs follows (History, both sides
|
|
153
|
+
* of the merge, redline, voice). A paragraph id (w14:paraId) that is unique in its version is the paragraph's identity: a text match
|
|
154
|
+
* may never pair it with a paragraph other than the one carrying that id on the other side, and no paragraph is paired with one that
|
|
155
|
+
* carries the id of another paragraph. Text and order decide where ids are absent or duplicated, and where an id is found in one
|
|
156
|
+
* version only (it says nothing about the other: a re-save that gave every paragraph a new id still reads as unchanged). `strict` (the
|
|
157
|
+
* agent's side of the merge): a paragraph with an id of its own that the other version, which has ids, does not have at all was
|
|
158
|
+
* removed or rebuilt, and is paired with nothing.
|
|
159
|
+
* Returns {may, keyA, keyB}: `may(i, j)` says whether A[i] and B[j] may be the same paragraph; `keyA` and `keyB` are LCS keys (the
|
|
160
|
+
* text hash, then "#" and the id when that id is unique in both versions) so that order matching links no two paragraphs that
|
|
161
|
+
* different ids tell apart.
|
|
153
162
|
*/
|
|
154
|
-
export function
|
|
155
|
-
const
|
|
163
|
+
export function paraIdentity(A, B, { strict = false } = {}) {
|
|
164
|
+
const tally = (L) => {
|
|
165
|
+
const m = new Map();
|
|
166
|
+
for (const p of L) if (p.paraId) m.set(p.paraId, (m.get(p.paraId) || 0) + 1);
|
|
167
|
+
return m;
|
|
168
|
+
};
|
|
169
|
+
const inA = tally(A);
|
|
170
|
+
const inB = tally(B);
|
|
171
|
+
const may = (i, j) => {
|
|
172
|
+
const a = A[i].paraId || null;
|
|
173
|
+
const b = B[j].paraId || null;
|
|
174
|
+
if (a === b) return true; // the same id, or neither has one
|
|
175
|
+
const ownA = a && inA.get(a) === 1;
|
|
176
|
+
const ownB = b && inB.get(b) === 1;
|
|
177
|
+
if (ownA && (inB.has(a) || (strict && inB.size))) return false; // A[i] is the paragraph with its id there; strict: or it is gone
|
|
178
|
+
if (ownB && inA.has(b)) return false; // the same from B's side
|
|
179
|
+
if ((b && inA.get(b) === 1) || (a && inB.get(a) === 1)) return false; // it carries the id of another paragraph
|
|
180
|
+
return true; // ids absent, duplicated, or found on one side only (a re-save that renumbered every id): text and order decide
|
|
181
|
+
};
|
|
182
|
+
const sure = (id, own, other) => (id && own.get(id) === 1 && other.get(id) === 1 ? id : "");
|
|
183
|
+
return {
|
|
184
|
+
may,
|
|
185
|
+
keyA: A.map((p) => `${p.hash}#${sure(p.paraId, inA, inB)}`),
|
|
186
|
+
keyB: B.map((p) => `${p.hash}#${sure(p.paraId, inB, inA)}`),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Match paragraphs the way the durable anchors resolve, under the identity rule of paraIdentity: same text in order (LCS on the text
|
|
192
|
+
* hash, ids told apart), the same unique id (moved when the text is the same), the same text moved elsewhere, then the best similar
|
|
193
|
+
* unmatched paragraph (>= 0.3 alone in its gap, >= 0.5 within a gap, >= 0.6 anywhere), all under a work budget. No step pairs two
|
|
194
|
+
* paragraphs the ids tell apart. Returns {mapA, mapB, moved}.
|
|
195
|
+
*/
|
|
196
|
+
export function matchParagraphs(A, B, { strict = false } = {}) {
|
|
197
|
+
const { may, keyA, keyB } = paraIdentity(A, B, { strict });
|
|
198
|
+
const { mapA, mapB } = lcsPairs(keyA, keyB);
|
|
199
|
+
// keys equal by text with no shared id can still be two different identities (ids unique on both sides, present on one only)
|
|
200
|
+
for (let i = 0; i < A.length; i++) if (mapA[i] >= 0 && !may(i, mapA[i])) ((mapB[mapA[i]] = -1), (mapA[i] = -1));
|
|
156
201
|
const lcsMapB = Int32Array.from(mapB);
|
|
157
202
|
const moved = new Set();
|
|
158
203
|
const link = (i, j) => ((mapA[i] = j), (mapB[j] = i));
|
|
204
|
+
// the same unique id on both sides: the same paragraph, wherever it is; out of the order matched above with the same text, moved
|
|
205
|
+
const idOf = (k) => k.slice(k.indexOf("#") + 1); // "" when the id does not decide
|
|
206
|
+
const idAt = new Map();
|
|
207
|
+
keyA.forEach((k, i) => mapA[i] < 0 && idOf(k) && idAt.set(idOf(k), i));
|
|
208
|
+
keyB.forEach((k, j) => {
|
|
209
|
+
const i = mapB[j] < 0 && idOf(k) ? idAt.get(idOf(k)) : undefined;
|
|
210
|
+
if (i === undefined) return;
|
|
211
|
+
link(i, j);
|
|
212
|
+
if (A[i].hash === B[j].hash) moved.add(j);
|
|
213
|
+
});
|
|
159
214
|
const long = (p) => normText(p.text).length >= 20; // short or empty texts repeat: a "move" of one would be a coincidence
|
|
160
215
|
const queue = new Map();
|
|
161
216
|
A.forEach((p, i) => mapA[i] < 0 && long(p) && (queue.get(p.hash) || queue.set(p.hash, []).get(p.hash)).push(i));
|
|
217
|
+
// Ceiling: candidates the ids rule out are skipped within a budget for the whole pass (many copies of one long text, each with an id
|
|
218
|
+
// of its own the other version does not have, would make the search quadratic); past it, the remaining candidates are not tried.
|
|
219
|
+
let tries = 100_000;
|
|
162
220
|
B.forEach((p, j) => {
|
|
163
221
|
const q = mapB[j] < 0 && queue.get(p.hash);
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const idCount = (L) => {
|
|
167
|
-
const m = new Map();
|
|
168
|
-
for (const p of L) if (p.paraId) m.set(p.paraId, (m.get(p.paraId) || 0) + 1);
|
|
169
|
-
return m;
|
|
170
|
-
};
|
|
171
|
-
const idsA = idCount(A);
|
|
172
|
-
const idsB = idCount(B);
|
|
173
|
-
const byId = new Map();
|
|
174
|
-
A.forEach((p, i) => mapA[i] < 0 && p.paraId && idsA.get(p.paraId) === 1 && byId.set(p.paraId, i));
|
|
175
|
-
B.forEach((p, j) => {
|
|
176
|
-
if (mapB[j] >= 0 || !p.paraId || idsB.get(p.paraId) !== 1) return;
|
|
177
|
-
const i = byId.get(p.paraId);
|
|
178
|
-
if (i != null && mapA[i] < 0) link(i, j);
|
|
222
|
+
const k = q ? q.findIndex((i) => tries-- > 0 && may(i, j)) : -1;
|
|
223
|
+
if (k >= 0) (link(q.splice(k, 1)[0], j), moved.add(j));
|
|
179
224
|
});
|
|
180
225
|
let budget = 20_000; // similarity calls in all
|
|
181
|
-
const sim = (i, j) => (budget-- > 0 ? similarity(A[i].text.slice(0, 600), B[j].text.slice(0, 600)) : 0);
|
|
226
|
+
const sim = (i, j) => (may(i, j) && budget-- > 0 ? similarity(A[i].text.slice(0, 600), B[j].text.slice(0, 600)) : 0);
|
|
182
227
|
// gap pass: unmatched paragraphs between the same two matched neighbors
|
|
183
228
|
const prevA = new Int32Array(B.length);
|
|
184
229
|
const nextA = new Int32Array(B.length);
|
|
@@ -191,6 +236,7 @@ export function matchParagraphs(A, B) {
|
|
|
191
236
|
let gapPrev = -2;
|
|
192
237
|
let floor = -1;
|
|
193
238
|
for (let j = 0; j < B.length; j++) {
|
|
239
|
+
if (budget <= 0) break; // no similarity left to link with: listing the candidates of every paragraph of a long gap was quadratic
|
|
194
240
|
if (mapB[j] >= 0) continue;
|
|
195
241
|
if (prevA[j] !== gapPrev) (gapPrev = prevA[j], (floor = prevA[j]));
|
|
196
242
|
const cand = [];
|
|
@@ -332,9 +378,11 @@ function compareSheet(sheet, rowsA, rowsB, out, wbA, wbB) {
|
|
|
332
378
|
|
|
333
379
|
function compareXlsx(wbA, wbB, out) {
|
|
334
380
|
const claimed = new Set();
|
|
381
|
+
const namesB = new Map(); // sheet name -> how many sheets of B have it, counted once (MAX_SHEETS bounds the scans by name)
|
|
382
|
+
for (const y of wbB.sheets) namesB.set(y.name, (namesB.get(y.name) || 0) + 1);
|
|
335
383
|
const sheetLabel = (s) => `Sheet "${clean(s.name, 60)}"`;
|
|
336
384
|
for (const sb of wbB.sheets) {
|
|
337
|
-
const sa = wbA.sheets.find((x) => x.name === sb.name && !claimed.has(x)) || wbA.sheets.find((x) => x.sheetId === sb.sheetId && !claimed.has(x) && !
|
|
385
|
+
const sa = wbA.sheets.find((x) => x.name === sb.name && !claimed.has(x)) || wbA.sheets.find((x) => x.sheetId === sb.sheetId && !claimed.has(x) && !(namesB.get(x.name) > (x.name === sb.name ? 1 : 0)));
|
|
338
386
|
if (!sa) {
|
|
339
387
|
out.add("added", () => place({ format: "xlsx", label: sheetLabel(sb), locator: xlsxLocator(sb, "", null), change: "added", note: "sheet added" }));
|
|
340
388
|
continue;
|
|
@@ -384,13 +432,22 @@ function compareShapes(sa, sb, out) {
|
|
|
384
432
|
if (old) (used.add(old), pair(old, sh));
|
|
385
433
|
else unmatchedB.push(sh);
|
|
386
434
|
}
|
|
387
|
-
|
|
388
|
-
const
|
|
435
|
+
// by name and kind among the shapes left unpaired, grouped once (a scan of every shape per shape was quadratic on a slide of many shapes)
|
|
436
|
+
const nk = (x) => `${x.kind}\u0000${x.name}`;
|
|
437
|
+
const groupsA = new Map();
|
|
438
|
+
for (const x of sa.shapes) if (!used.has(x)) (groupsA.get(nk(x)) || groupsA.set(nk(x), new Set()).get(nk(x))).add(x);
|
|
439
|
+
const countB = new Map();
|
|
440
|
+
for (const x of unmatchedB) countB.set(nk(x), (countB.get(nk(x)) || 0) + 1);
|
|
389
441
|
for (const sh of unmatchedB) {
|
|
390
|
-
const cand = sh.name ?
|
|
391
|
-
if (cand
|
|
392
|
-
|
|
442
|
+
const cand = sh.name ? groupsA.get(nk(sh)) : null;
|
|
443
|
+
if (cand?.size === 1 && countB.get(nk(sh)) === 1) {
|
|
444
|
+
const [old] = cand;
|
|
445
|
+
cand.delete(old);
|
|
446
|
+
used.add(old);
|
|
447
|
+
pair(old, sh);
|
|
448
|
+
} else out.add("added", () => place({ format: "pptx", label: shapeLabel(sb, sh), locator: shapeLocator(sb, sh), change: "added", text: sh.text ? clean(sh.text, TEXT_CAP) : null, note: sh.text ? null : `${KIND_NOTE[sh.kind] || "an object"} was added` }));
|
|
393
449
|
}
|
|
450
|
+
const leftA = sa.shapes.filter((x) => !used.has(x));
|
|
394
451
|
for (const sh of leftA) out.add("removed", () => place({ format: "pptx", label: shapeLabel(sa, sh), locator: null, change: "removed", text: sh.text ? clean(sh.text, TEXT_CAP) : null, note: sh.text ? null : `${KIND_NOTE[sh.kind] || "an object"} was removed` }));
|
|
395
452
|
function pair(old, sh) {
|
|
396
453
|
if (old.hash === sh.hash) {
|
|
@@ -402,6 +459,51 @@ function compareShapes(sa, sb, out) {
|
|
|
402
459
|
}
|
|
403
460
|
}
|
|
404
461
|
|
|
462
|
+
// ---- HTML -------------------------------------------------------------------------------------------------------------
|
|
463
|
+
// A text-level diff by element (PLAN-html 2a): the units of each version (elements that hold text directly, and images and other leaves,
|
|
464
|
+
// src/html.js) matched by tag and own text in order; a pair left between two matches with the same tag is a change of its text, the rest
|
|
465
|
+
// added or removed. Link and image targets that appeared or went away are listed too, as plain text (R1): an agent may change a link.
|
|
466
|
+
|
|
467
|
+
const htmlUnitText = (u) => (u.kind === "leaf" ? `${u.tag} ${u.node.attrs.find((a) => a.name === "alt")?.value ?? ""}`.trim() : ownText(u.node));
|
|
468
|
+
const htmlLabel = (u) => untrusted(`${safeTag(u.tag)}${u.htmlId ? ` #${u.htmlId}` : ""}`, 100);
|
|
469
|
+
const htmlLocator = (read, u) => ({ format: "html", node_id: u.nodeId, path: pathString(elementPath(u.node)), element_id: u.htmlId ?? null, text_hash: ownHash(u.node) });
|
|
470
|
+
|
|
471
|
+
function compareHtml(RA, RB, out) {
|
|
472
|
+
const key = (u) => `${u.tag}\u0000${exactHash(htmlUnitText(u))}`;
|
|
473
|
+
const A = RA.units;
|
|
474
|
+
const B = RB.units;
|
|
475
|
+
const { mapA, mapB } = lcsPairs(A.map(key), B.map(key));
|
|
476
|
+
// walk the gaps between matched pairs: same-tag units across a gap pair up in order (a changed text), the rest are added or removed
|
|
477
|
+
let i = 0;
|
|
478
|
+
let j = 0;
|
|
479
|
+
const flushGap = (iEnd, jEnd) => {
|
|
480
|
+
const gapA = [];
|
|
481
|
+
for (let x = i; x < iEnd; x++) if (mapA[x] < 0) gapA.push(A[x]);
|
|
482
|
+
const used = new Set();
|
|
483
|
+
for (let y = j; y < jEnd; y++) {
|
|
484
|
+
if (mapB[y] >= 0) continue;
|
|
485
|
+
const ub = B[y];
|
|
486
|
+
const ua = gapA.find((u) => !used.has(u) && u.tag === ub.tag);
|
|
487
|
+
if (ua) {
|
|
488
|
+
used.add(ua);
|
|
489
|
+
out.add("changed", () => place({ format: "html", label: htmlLabel(ub), locator: htmlLocator(RB, ub), ...textChange(htmlUnitText(ua), htmlUnitText(ub)) }));
|
|
490
|
+
} else out.add("added", () => place({ format: "html", label: htmlLabel(ub), locator: htmlLocator(RB, ub), change: "added", text: clean(normText(htmlUnitText(ub)), TEXT_CAP) || null, note: ub.kind === "leaf" ? `${safeTag(ub.tag)} added` : null }));
|
|
491
|
+
}
|
|
492
|
+
for (const ua of gapA) if (!used.has(ua)) out.add("removed", () => place({ format: "html", label: htmlLabel(ua), locator: null, change: "removed", text: clean(normText(htmlUnitText(ua)), TEXT_CAP) || null, note: ua.kind === "leaf" ? `${safeTag(ua.tag)} removed` : null }));
|
|
493
|
+
};
|
|
494
|
+
for (let y = 0; y <= B.length; y++) {
|
|
495
|
+
if (y < B.length && mapB[y] < 0) continue;
|
|
496
|
+
const x = y < B.length ? mapB[y] : A.length;
|
|
497
|
+
flushGap(x, y);
|
|
498
|
+
i = x + 1;
|
|
499
|
+
j = y + 1;
|
|
500
|
+
}
|
|
501
|
+
const d = inventoryDelta(inventory(RA.doc), inventory(RB.doc)).links;
|
|
502
|
+
const what = (e) => (e.kind === "link" ? "Link" : "Image or style");
|
|
503
|
+
for (const e of d.added) out.add("changed", () => place({ format: "html", label: untrusted(`${what(e)} target`, 60), locator: null, change: "link", note: clean(`now points to ${e.url}`, 300) }));
|
|
504
|
+
for (const e of d.removed) out.add("changed", () => place({ format: "html", label: untrusted(`${what(e)} target`, 60), locator: null, change: "link", note: clean(`no longer points to ${e.url}`, 300) }));
|
|
505
|
+
}
|
|
506
|
+
|
|
405
507
|
/** Compare two versions of one format: {counts: {changed, added, removed}, places: [...] | null, more}. Pure; runs in the worker. */
|
|
406
508
|
export function compareBytes(format, before, after, { places = false } = {}) {
|
|
407
509
|
const out = new Out(places);
|
|
@@ -409,7 +511,9 @@ export function compareBytes(format, before, after, { places = false } = {}) {
|
|
|
409
511
|
const B = parse(format, after);
|
|
410
512
|
if (format === "docx") compareDocx(A.paragraphs, B.paragraphs, out);
|
|
411
513
|
else if (format === "xlsx") compareXlsx(A.wb, B.wb, out);
|
|
412
|
-
else compareDeck(A.deck, B.deck, out);
|
|
514
|
+
else if (format === "pptx") compareDeck(A.deck, B.deck, out);
|
|
515
|
+
else if (format === "html") compareHtml(A.read, B.read, out);
|
|
516
|
+
else unknownFormat(format, "compareBytes");
|
|
413
517
|
return { counts: out.counts, places: out.places, more: out.places ? out.total - out.places.length : 0 };
|
|
414
518
|
}
|
|
415
519
|
|
|
@@ -450,7 +554,7 @@ function runInWorker(jobs) {
|
|
|
450
554
|
const failAll = (why) => finish(jobs.map(() => ({ ok: false, error: why })));
|
|
451
555
|
const timer = setTimeout(() => failAll("timed out"), WORKER_TIMEOUT_MS());
|
|
452
556
|
try {
|
|
453
|
-
worker =
|
|
557
|
+
worker = startWorker("history-worker.js", { workerData: { jobs } });
|
|
454
558
|
} catch (err) {
|
|
455
559
|
return failAll(String(err?.message || err));
|
|
456
560
|
}
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
|
|
3
|
+
// What is active in an HTML file (PLAN-html R1): the one table the render copy (src/html-render.js, later), the agent's ops and the check
|
|
4
|
+
// on every write use. It is an allowlist, not a list of attacks: an element or attribute is allowed only when it is named here, a URL only
|
|
5
|
+
// when its kind is named for its attribute, CSS only in the plain form below. Everything else is "active", whatever it is: present and
|
|
6
|
+
// unchanged it is left alone (the file had it), added or changed by a write it refuses the whole write (inventoryDelta).
|
|
7
|
+
// Pure: no parser here. The functions read a parse5 tree by its shape (nodeName, tagName, namespaceURI, attrs, childNodes, the content of
|
|
8
|
+
// a template, the value of a text node), so the same table serves every module that holds a tree.
|
|
9
|
+
|
|
10
|
+
export const HTML_NS = "http://www.w3.org/1999/xhtml";
|
|
11
|
+
|
|
12
|
+
/** The fixed sentence for a refused write (R1). */
|
|
13
|
+
export const ACTIVE_REFUSED = "The agent tried to add code that runs in a browser, so nothing was written.";
|
|
14
|
+
|
|
15
|
+
// Elements a write may add or change: document structure, text and its formatting, lists, tables, images, links, and the legacy
|
|
16
|
+
// formatting Office and email exports use (`font`, `center`, Word's `o:p`). Not here, so refused when added: script, noscript, template,
|
|
17
|
+
// style sheets by link, frames, objects, forms and their controls, media, canvas, SVG and MathML (another namespace), base, and every
|
|
18
|
+
// element this table does not name. `style` and `meta` are here with their own rules (CSS below; meta: no http-equiv).
|
|
19
|
+
const ELEMENTS = new Set(
|
|
20
|
+
[
|
|
21
|
+
"html", "head", "body", "title", "meta", "style",
|
|
22
|
+
"address", "article", "aside", "footer", "header", "h1", "h2", "h3", "h4", "h5", "h6", "hgroup", "main", "nav", "section", "search",
|
|
23
|
+
"blockquote", "dd", "div", "dl", "dt", "figcaption", "figure", "hr", "li", "menu", "ol", "p", "pre", "ul",
|
|
24
|
+
"a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn", "em", "i", "kbd", "mark", "q", "rp", "rt", "ruby", "s", "samp",
|
|
25
|
+
"small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr", "del", "ins",
|
|
26
|
+
"img", "picture",
|
|
27
|
+
"caption", "col", "colgroup", "table", "tbody", "td", "tfoot", "th", "thead", "tr",
|
|
28
|
+
"details", "summary",
|
|
29
|
+
"center", "font", "big", "tt", "strike", "nobr", "o:p",
|
|
30
|
+
],
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
// Attributes allowed on every allowed element.
|
|
34
|
+
const GLOBAL_ATTRS = new Set(["class", "id", "title", "lang", "dir", "style", "hidden", "translate", "align", "valign", "width", "height", "bgcolor", "nowrap", "border"]);
|
|
35
|
+
// Attributes allowed on one element only (URL-bearing ones are listed in URL_ATTRS too).
|
|
36
|
+
const ELEMENT_ATTRS = {
|
|
37
|
+
a: ["href"],
|
|
38
|
+
img: ["src", "alt"],
|
|
39
|
+
td: ["colspan", "rowspan", "headers", "scope", "abbr"],
|
|
40
|
+
th: ["colspan", "rowspan", "headers", "scope", "abbr"],
|
|
41
|
+
col: ["span"],
|
|
42
|
+
colgroup: ["span"],
|
|
43
|
+
table: ["cellpadding", "cellspacing", "summary"],
|
|
44
|
+
ol: ["start", "reversed"],
|
|
45
|
+
li: ["value"],
|
|
46
|
+
time: ["datetime"],
|
|
47
|
+
del: ["datetime"],
|
|
48
|
+
ins: ["datetime"],
|
|
49
|
+
data: ["value"],
|
|
50
|
+
meta: ["charset", "name", "content"],
|
|
51
|
+
font: ["color", "face", "size"],
|
|
52
|
+
br: ["clear"],
|
|
53
|
+
details: ["open"],
|
|
54
|
+
html: ["xmlns"],
|
|
55
|
+
};
|
|
56
|
+
const PER_ELEMENT = new Map(Object.entries(ELEMENT_ATTRS).map(([k, v]) => [k, new Set(v)]));
|
|
57
|
+
|
|
58
|
+
/** The URL-bearing allowed attributes: "link" (followed when clicked) or "load" (fetched when shown), and the kinds each allows. */
|
|
59
|
+
const URL_ATTRS = { "a href": "link", "img src": "load" };
|
|
60
|
+
export const URL_KINDS = {
|
|
61
|
+
link: new Set(["fragment", "relative", "http", "https", "mailto", "tel"]),
|
|
62
|
+
load: new Set(["relative", "data-image"]),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** Template delimiters (R1): refused in added text or attribute values when the file loads any script. */
|
|
66
|
+
const TEMPLATE = /\{\{|\{%|<%|\$\{/;
|
|
67
|
+
|
|
68
|
+
/** Is an element of this name and namespace on the allowlist? Only HTML-namespace elements are. */
|
|
69
|
+
export function elementAllowed(name, namespace = HTML_NS) {
|
|
70
|
+
return namespace === HTML_NS && ELEMENTS.has(String(name));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Is attribute `name` allowed on allowed element `element`? Names compare as the parser reports them (lowercase). */
|
|
74
|
+
export function attributeAllowed(element, name) {
|
|
75
|
+
name = String(name);
|
|
76
|
+
return GLOBAL_ATTRS.has(name) || PER_ELEMENT.get(String(element))?.has(name) === true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// a raster image as a data: URL (no SVG in v1, R5): the media type, optional parameters, then the comma
|
|
80
|
+
const DATA_IMAGE = /^data:image\/(?:png|jpeg|jpg|gif|webp|avif|bmp|x-icon|vnd\.microsoft\.icon)(?:;[a-z0-9=._+-]*)*,/i;
|
|
81
|
+
const SCHEME = /^([a-zA-Z][a-zA-Z0-9+.-]*):/;
|
|
82
|
+
/**
|
|
83
|
+
* The kind of a URL as an attribute value holds it (character references already decoded by the parser): "fragment" ("" or "#..."),
|
|
84
|
+
* "relative" (a path with no scheme and no host), "data-image", "http", "https", "mailto", "tel", or "other". A value is "other" when a
|
|
85
|
+
* browser could read it as something else than it looks: any control character (browsers drop tabs and newlines anywhere in a URL), a
|
|
86
|
+
* backslash (read as a slash), a leading "//" (another host), or a scheme not named here.
|
|
87
|
+
*/
|
|
88
|
+
export function urlKind(value) {
|
|
89
|
+
// trimmed of HTML whitespace by index: a regex for the trailing run tried every start inside a run (quadratic in its length)
|
|
90
|
+
const raw = String(value ?? "");
|
|
91
|
+
const htmlSpace = (c) => c === 32 || c === 9 || c === 10 || c === 12 || c === 13;
|
|
92
|
+
let a = 0;
|
|
93
|
+
let b = raw.length;
|
|
94
|
+
while (a < b && htmlSpace(raw.charCodeAt(a))) a++;
|
|
95
|
+
while (b > a && htmlSpace(raw.charCodeAt(b - 1))) b--;
|
|
96
|
+
const v = raw.slice(a, b);
|
|
97
|
+
if (/[\u0000-\u001f\u007f\\]/.test(v)) return "other";
|
|
98
|
+
if (v === "" || v.startsWith("#")) return "fragment";
|
|
99
|
+
if (v.startsWith("//")) return "other";
|
|
100
|
+
const m = SCHEME.exec(v);
|
|
101
|
+
if (!m) return "relative";
|
|
102
|
+
const s = m[1].toLowerCase();
|
|
103
|
+
if (s === "http" || s === "https" || s === "mailto" || s === "tel") return s;
|
|
104
|
+
if (s === "data") return DATA_IMAGE.test(v) ? "data-image" : "other";
|
|
105
|
+
return "other";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The CSS rule (R1): CSS in a style attribute or a style element is plain when it has no backslash (escapes could spell any name), no
|
|
110
|
+
* @import, no image-set() or src() (they load without url()), no expression(), behavior or -moz-binding, and every url() is relative or a
|
|
111
|
+
* data: image. Returns {ok, loads: [url]}.
|
|
112
|
+
*/
|
|
113
|
+
export function cssPlain(css) {
|
|
114
|
+
const s = String(css ?? "");
|
|
115
|
+
const loads = [];
|
|
116
|
+
if (/\\|@import|image-set\s*\(|\bsrc\s*\(|expression\s*\(|behavior\s*:|-moz-binding/i.test(s)) return { ok: false, loads };
|
|
117
|
+
// url( ... ) read by index, as the regex /url\(\s*(?:"([^"]*)"|'([^']*)'|([^)"'\s]*))\s*\)/gi read it (that regex backtracked over every
|
|
118
|
+
// split of a whitespace run between its two \s*: quadratic in the run). What is left must hold no url( this cannot read: not plain.
|
|
119
|
+
// JavaScript's \s, as the regex read it: ASCII by code, anything else by the regex (rare, so cheap)
|
|
120
|
+
const space = (c) => {
|
|
121
|
+
const k = c.charCodeAt(0);
|
|
122
|
+
return k < 128 ? k === 32 || (k >= 9 && k <= 13) : /\s/.test(c);
|
|
123
|
+
};
|
|
124
|
+
const isUrl = (i) => (s.charCodeAt(i) | 32) === 117 && (s.charCodeAt(i + 1) | 32) === 114 && (s.charCodeAt(i + 2) | 32) === 108; // u r l, any case
|
|
125
|
+
// where each position's next stop is (an unquoted value ends at ), a quote or whitespace; a quoted one at its quote), found once
|
|
126
|
+
// backwards, so a run of unclosed url( is not scanned again from each one
|
|
127
|
+
let stop = null;
|
|
128
|
+
const stops = () => {
|
|
129
|
+
if (stop) return stop;
|
|
130
|
+
const n = s.length;
|
|
131
|
+
stop = { any: new Int32Array(n + 1), dq: new Int32Array(n + 1), sq: new Int32Array(n + 1) };
|
|
132
|
+
stop.any[n] = stop.dq[n] = stop.sq[n] = n;
|
|
133
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
134
|
+
const c = s[i];
|
|
135
|
+
stop.any[i] = c === ")" || c === '"' || c === "'" || space(c) ? i : stop.any[i + 1];
|
|
136
|
+
stop.dq[i] = c === '"' ? i : stop.dq[i + 1];
|
|
137
|
+
stop.sq[i] = c === "'" ? i : stop.sq[i + 1];
|
|
138
|
+
}
|
|
139
|
+
return stop;
|
|
140
|
+
};
|
|
141
|
+
let rest = "";
|
|
142
|
+
let from = 0;
|
|
143
|
+
for (let i = 0; i + 3 < s.length; ) {
|
|
144
|
+
if (!(isUrl(i) && s[i + 3] === "(")) {
|
|
145
|
+
i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
let j = i + 4;
|
|
149
|
+
while (j < s.length && space(s[j])) j++;
|
|
150
|
+
let val = null;
|
|
151
|
+
let k = j;
|
|
152
|
+
if (s[j] === '"' || s[j] === "'") {
|
|
153
|
+
const close = j + 1 <= s.length ? (s[j] === '"' ? stops().dq : stops().sq)[j + 1] : s.length;
|
|
154
|
+
if (close < s.length) [val, k] = [s.slice(j + 1, close), close + 1];
|
|
155
|
+
} else {
|
|
156
|
+
k = stops().any[j];
|
|
157
|
+
val = s.slice(j, k);
|
|
158
|
+
}
|
|
159
|
+
let m = k;
|
|
160
|
+
while (val !== null && m < s.length && space(s[m])) m++;
|
|
161
|
+
if (val === null || s[m] !== ")") {
|
|
162
|
+
i++;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
loads.push(val);
|
|
166
|
+
rest += s.slice(from, i);
|
|
167
|
+
from = i = m + 1;
|
|
168
|
+
}
|
|
169
|
+
rest += s.slice(from);
|
|
170
|
+
for (let i = 0; i + 3 < rest.length; i++) {
|
|
171
|
+
if (!((rest.charCodeAt(i) | 32) === 117 && (rest.charCodeAt(i + 1) | 32) === 114 && (rest.charCodeAt(i + 2) | 32) === 108)) continue;
|
|
172
|
+
let j = i + 3;
|
|
173
|
+
while (j < rest.length && space(rest[j])) j++;
|
|
174
|
+
if (rest[j] === "(") return { ok: false, loads }; // a url( this cannot read: not plain
|
|
175
|
+
}
|
|
176
|
+
return { ok: loads.every((u) => URL_KINDS.load.has(urlKind(u))), loads };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ---- the display set (the render copy, src/html-render.js) --------------------------------------------------------------------------
|
|
180
|
+
// A second set, for SHOWING a page, not for writing one. Why it differs: the render copy is shown in a frame that runs no script, sends
|
|
181
|
+
// nothing and navigates nowhere, so it may show controls a write may never add (a page with a sign-up form should look like itself; the
|
|
182
|
+
// controls are shown disabled and never inside a `form`), and it shows less than a write may keep: no `meta` (the copy's one meta is the
|
|
183
|
+
// CSP the server adds first), no link target (the renderer keeps it as `data-osk-href` for display only) and no `src` (the renderer inlines
|
|
184
|
+
// a local or `data:` image itself, or shows a placeholder). The renderer, not this table, handles `href`, `src` and `style`.
|
|
185
|
+
const DISPLAY_EXTRA = ["input", "button", "select", "option", "optgroup", "textarea", "label", "fieldset", "legend", "output", "meter", "progress"];
|
|
186
|
+
const DISPLAY_ELEMENTS = new Set([...ELEMENTS, ...DISPLAY_EXTRA].filter((t) => t !== "meta"));
|
|
187
|
+
const DISPLAY_ATTRS = {
|
|
188
|
+
input: ["type", "value", "placeholder", "checked", "size", "readonly", "multiple", "min", "max", "step"],
|
|
189
|
+
button: ["value"],
|
|
190
|
+
select: ["multiple", "size"],
|
|
191
|
+
option: ["value", "selected", "label"],
|
|
192
|
+
optgroup: ["label"],
|
|
193
|
+
textarea: ["rows", "cols", "placeholder", "readonly", "wrap"],
|
|
194
|
+
meter: ["value", "min", "max", "low", "high", "optimum"],
|
|
195
|
+
progress: ["value", "max"],
|
|
196
|
+
style: ["media"],
|
|
197
|
+
};
|
|
198
|
+
const DISPLAY_PER_ELEMENT = new Map(Object.entries(DISPLAY_ATTRS).map(([k, v]) => [k, new Set(v)]));
|
|
199
|
+
/** The `type` values an input keeps in the render copy; any other (`image` loads its `src`) is not kept. */
|
|
200
|
+
export const DISPLAY_INPUT_TYPES = new Set(["text", "search", "email", "url", "tel", "number", "password", "date", "time", "datetime-local", "month", "week", "color", "range", "checkbox", "radio", "submit", "reset", "button", "file", "hidden"]);
|
|
201
|
+
/** Form controls: the render copy shows each one `disabled`. */
|
|
202
|
+
export const DISPLAY_CONTROLS = new Set(["input", "button", "select", "textarea", "fieldset"]);
|
|
203
|
+
|
|
204
|
+
/** May the render copy show an element of this name and namespace? HTML namespace only, as for writes. */
|
|
205
|
+
export function displayElementAllowed(name, namespace = HTML_NS) {
|
|
206
|
+
return namespace === HTML_NS && DISPLAY_ELEMENTS.has(String(name));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** May the render copy keep attribute `name` on element `element`? Never `href`, `src`, `style` or `name` (the renderer's own rules). */
|
|
210
|
+
export function displayAttributeAllowed(element, name) {
|
|
211
|
+
name = String(name);
|
|
212
|
+
if (name === "href" || name === "src" || name === "style" || name === "name" || element === "meta") return false;
|
|
213
|
+
return attributeAllowed(element, name) || DISPLAY_PER_ELEMENT.get(String(element))?.has(name) === true;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ---- the inventory --------------------------------------------------------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
const kidsOf = (n) => (n.nodeName === "template" && n.content ? n.content.childNodes : n.childNodes || []);
|
|
219
|
+
const KEEP = 2048; // a value longer than this is kept by its hash (an inventory never holds a whole script or a megabyte attribute)
|
|
220
|
+
const sha = (s) => crypto.createHash("sha256").update(String(s)).digest("hex").slice(0, 32);
|
|
221
|
+
const keep = (s) => (s.length <= KEEP ? s : `#${sha(s)}`);
|
|
222
|
+
// an entry's key: its fields as a JSON array (one string per entry, never split: a field may hold any character)
|
|
223
|
+
const keyOf = (...fields) => JSON.stringify(fields);
|
|
224
|
+
const FRAMES = new Set(["iframe", "frame", "frameset", "object", "embed", "applet", "portal", "fencedframe"]);
|
|
225
|
+
const FORMS = new Set(["form", "input", "button", "select", "textarea", "option", "optgroup", "datalist", "fieldset", "legend", "label", "output", "isindex", "keygen"]);
|
|
226
|
+
const RAW = new Set(["script", "style", "xmp", "iframe", "noembed", "noframes", "plaintext", "noscript"]);
|
|
227
|
+
|
|
228
|
+
/** The reason code of a disallowed element. */
|
|
229
|
+
const elementCode = (tag, ns) => (tag === "script" ? "script" : ns !== HTML_NS ? "element" : FRAMES.has(tag) ? "frame" : FORMS.has(tag) ? "form" : "element");
|
|
230
|
+
|
|
231
|
+
// The place of an element for the positional part of the inventory: a hash of the tag names from the inventory's root down to it (no
|
|
232
|
+
// indexes, so elements inserted or deleted before it do not move it), two 32-bit lanes.
|
|
233
|
+
const placeOf = (parent, tag) => {
|
|
234
|
+
let a = parent[0] ^ 0x811c9dc5;
|
|
235
|
+
let b = parent[1] ^ 0x01000193;
|
|
236
|
+
for (let i = 0; i < tag.length; i++) {
|
|
237
|
+
a = Math.imul(a ^ tag.charCodeAt(i), 0x01000193);
|
|
238
|
+
b = Math.imul(b ^ tag.charCodeAt(i), 0x5bd1e995);
|
|
239
|
+
}
|
|
240
|
+
a = Math.imul(a ^ (a >>> 15), 0x2c1b3c6d) ^ 0x3e;
|
|
241
|
+
b = Math.imul(b ^ (b >>> 13), 0x297a2d39) ^ 0x3e;
|
|
242
|
+
return [a | 0, b | 0];
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* A summary of what is active in a parsed document (a parse5 tree), in two parts:
|
|
247
|
+
* - entries: canonical and order-independent: every element outside the allowlist (with all its attributes and, for script and other
|
|
248
|
+
* raw-text elements, its body), every attribute outside the allowlist, every link target and every load target (a URL attribute or a
|
|
249
|
+
* CSS url()), CSS that is not plain, and (when the file holds any script) every text or attribute value with a template delimiter.
|
|
250
|
+
* [{key, kind, code?, url?, urlKind?, n}] sorted by key: what was added or removed, and the links History lists.
|
|
251
|
+
* - seq: positional: every one of those entries that is not allowed (a `code`, templates aside) in document order, each with the
|
|
252
|
+
* element that owns it: the place of the owner (the tag names from the root down to it, placeOf), its tag and its `id`. Two
|
|
253
|
+
* inventories whose seq differ other than by items removed moved, swapped, copied or changed something active (outside review
|
|
254
|
+
* 029e51b, html-active.js:282: two `onclick` values swapped between elements kept the same entries).
|
|
255
|
+
* Returns {scripted, entries, seq}. Iterative: any depth. Values over 2,048 characters are kept by hash.
|
|
256
|
+
*/
|
|
257
|
+
export function inventory(tree) {
|
|
258
|
+
const counts = new Map(); // key -> {kind, code, url, urlKind, n}
|
|
259
|
+
const seq = [];
|
|
260
|
+
let own = null; // the active items of the element being read, sorted into seq after it
|
|
261
|
+
const add = (key, rec) => {
|
|
262
|
+
const e = counts.get(key);
|
|
263
|
+
if (e) e.n++;
|
|
264
|
+
else counts.set(key, { key, ...rec, n: 1 });
|
|
265
|
+
if (rec.code && rec.kind !== "template" && own) own.push(key);
|
|
266
|
+
};
|
|
267
|
+
const tpl = []; // [where, value] of values with a template delimiter, kept when the file turns out to hold a script
|
|
268
|
+
let scripted = false;
|
|
269
|
+
const ROOT = [0, 0];
|
|
270
|
+
const stack = [tree];
|
|
271
|
+
const places = [ROOT];
|
|
272
|
+
while (stack.length) {
|
|
273
|
+
const n = stack.pop();
|
|
274
|
+
const up = places.pop();
|
|
275
|
+
const ks = kidsOf(n);
|
|
276
|
+
const here = n.tagName === undefined ? up : placeOf(up, `${n.namespaceURI === HTML_NS ? "" : `${n.namespaceURI}|`}${n.tagName}`);
|
|
277
|
+
for (let i = ks.length - 1; i >= 0; i--) (stack.push(ks[i]), places.push(here));
|
|
278
|
+
if (n.nodeName === "#text") {
|
|
279
|
+
const p = n.parentNode;
|
|
280
|
+
const raw = p && ((p.namespaceURI === HTML_NS && RAW.has(p.tagName)) || p.tagName === "script" || p.tagName === "style");
|
|
281
|
+
if (!raw && TEMPLATE.test(n.value)) tpl.push(["text", n.value]);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (n.tagName === undefined) continue;
|
|
285
|
+
const tag = n.tagName;
|
|
286
|
+
const ns = n.namespaceURI;
|
|
287
|
+
const attrs = n.attrs || [];
|
|
288
|
+
own = [];
|
|
289
|
+
// owner and key as separate fields (an id can hold any character, so no joined string is split later: outside review of 4d2b2fc)
|
|
290
|
+
const owner = keyOf(`${here[0] >>> 0}.${here[1] >>> 0}`, tag, attrs.find((a) => a.name === "id" && !a.prefix)?.value ?? "");
|
|
291
|
+
const flush = () => {
|
|
292
|
+
for (const key of own.sort()) seq.push({ owner, key });
|
|
293
|
+
own = null;
|
|
294
|
+
};
|
|
295
|
+
if (tag === "script") scripted = true; // an SVG script runs too
|
|
296
|
+
if (!elementAllowed(tag, ns)) {
|
|
297
|
+
// its own text (a script or style body, in HTML or SVG): a changed body is a changed entry
|
|
298
|
+
const body = kidsOf(n).filter((c) => c.nodeName === "#text").map((c) => c.value).join("");
|
|
299
|
+
const sig = attrs.map((a) => [`${a.prefix ? `${a.prefix}:` : ""}${a.name}`, keep(a.value)]).sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : x[1] < y[1] ? -1 : x[1] > y[1] ? 1 : 0));
|
|
300
|
+
add(keyOf("el", ns === HTML_NS ? "" : ns, tag, sig, body ? sha(body) : ""), { kind: "element", code: elementCode(tag, ns) });
|
|
301
|
+
for (const a of attrs) if (TEMPLATE.test(a.value)) tpl.push(["attr", a.value]);
|
|
302
|
+
flush();
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
for (const a of attrs) {
|
|
306
|
+
const name = a.name; // an allowed element is an HTML one: its attributes have no namespace prefix
|
|
307
|
+
if (TEMPLATE.test(a.value)) tpl.push(["attr", a.value]);
|
|
308
|
+
const urlRole = URL_ATTRS[`${tag} ${name}`];
|
|
309
|
+
if (urlRole) {
|
|
310
|
+
const k = urlKind(a.value);
|
|
311
|
+
add(keyOf("url", urlRole, tag, name, keep(a.value)), { kind: urlRole, url: keep(a.value), urlKind: k, code: URL_KINDS[urlRole].has(k) ? undefined : `${urlRole}_url` });
|
|
312
|
+
} else if (name === "style") {
|
|
313
|
+
const c = cssPlain(a.value);
|
|
314
|
+
if (!c.ok) add(keyOf("css", "attr", sha(a.value)), { kind: "css", code: "css" });
|
|
315
|
+
for (const u of c.loads) add(keyOf("url", "load", "css", "url", keep(u)), { kind: "load", url: keep(u), urlKind: urlKind(u), code: URL_KINDS.load.has(urlKind(u)) ? undefined : "load_url" });
|
|
316
|
+
} else if (!attributeAllowed(tag, name)) {
|
|
317
|
+
add(keyOf("attr", tag, name, keep(a.value)), { kind: "attribute", code: /^on/i.test(name) ? "event_handler" : "attribute" });
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (tag === "style" && ns === HTML_NS) {
|
|
321
|
+
const body = kidsOf(n).map((c) => c.value ?? "").join("");
|
|
322
|
+
const c = cssPlain(body);
|
|
323
|
+
if (!c.ok) add(keyOf("css", "style", sha(body)), { kind: "css", code: "css" });
|
|
324
|
+
for (const u of c.loads) add(keyOf("url", "load", "css", "url", keep(u)), { kind: "load", url: keep(u), urlKind: urlKind(u), code: URL_KINDS.load.has(urlKind(u)) ? undefined : "load_url" });
|
|
325
|
+
}
|
|
326
|
+
flush();
|
|
327
|
+
}
|
|
328
|
+
for (const [where, v] of tpl) add(keyOf("tpl", where, sha(v)), { kind: "template", code: "template" });
|
|
329
|
+
const entries = [...counts.values()].sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)).map((e) => Object.fromEntries(Object.entries(e).filter(([, v]) => v !== undefined)));
|
|
330
|
+
return { scripted, entries, seq };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Reason codes, in the order a refusal names the first one found. Neutral words only: never any of the file's content. */
|
|
334
|
+
export const REASONS = ["script", "event_handler", "frame", "form", "element", "attribute", "link_url", "load_url", "css", "template"];
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* What a write added or changed, from the inventories before and after: `added` and `removed` entries (a count that grew or shrank is
|
|
338
|
+
* that many), `links` ({added, removed} link and load targets, for History), and `refused` with `reason` (one REASONS code) and `reasons`
|
|
339
|
+
* when anything added is active, or when the active items after are not the ones before in the same order on the same owners less some
|
|
340
|
+
* removed (seq: a move, a swap or a copy between elements). A changed target is a removed entry and an added one: refused only when the
|
|
341
|
+
* new one's kind is not allowed for its attribute (R1), listed in `links` either way. Text with a template delimiter counts only when
|
|
342
|
+
* either version holds a script.
|
|
343
|
+
*/
|
|
344
|
+
export function inventoryDelta(before, after) {
|
|
345
|
+
const count = (inv) => new Map(inv.entries.map((e) => [e.key, e]));
|
|
346
|
+
const A = count(before);
|
|
347
|
+
const B = count(after);
|
|
348
|
+
const added = [];
|
|
349
|
+
const removed = [];
|
|
350
|
+
for (const [k, e] of B) {
|
|
351
|
+
const d = e.n - (A.get(k)?.n || 0);
|
|
352
|
+
if (d > 0) added.push({ ...e, n: d });
|
|
353
|
+
}
|
|
354
|
+
for (const [k, e] of A) {
|
|
355
|
+
const d = e.n - (B.get(k)?.n || 0);
|
|
356
|
+
if (d > 0) removed.push({ ...e, n: d });
|
|
357
|
+
}
|
|
358
|
+
const scripted = before.scripted || after.scripted;
|
|
359
|
+
const codes = new Set(added.filter((e) => e.code && (e.kind !== "template" || scripted)).map((e) => e.code));
|
|
360
|
+
// positional: the active items after must be the ones before, in order, less some removed (a subsequence); each one not found names
|
|
361
|
+
// its entry's code. Ceiling: an owner is told by its place, tag and id, so an item moved to another element of the same tag, place and
|
|
362
|
+
// id with no other active item between is not seen (the same code runs, on a neighbor). Wrapping an owner in a new element is a move.
|
|
363
|
+
const codeOf = new Map([...A, ...B].map(([k, e]) => [k, e.code]));
|
|
364
|
+
const bs = after.seq || [];
|
|
365
|
+
const as = before.seq || [];
|
|
366
|
+
for (let i = 0, j = 0; j < bs.length; j++) {
|
|
367
|
+
while (i < as.length && (as[i].owner !== bs[j].owner || as[i].key !== bs[j].key)) i++;
|
|
368
|
+
if (i < as.length) i++;
|
|
369
|
+
else {
|
|
370
|
+
const c = codeOf.get(bs[j].key);
|
|
371
|
+
if (c) codes.add(c);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
const reasons = REASONS.filter((r) => codes.has(r));
|
|
375
|
+
const isUrl = (e) => e.kind === "link" || e.kind === "load";
|
|
376
|
+
return {
|
|
377
|
+
refused: reasons.length > 0,
|
|
378
|
+
reason: reasons[0] ?? null,
|
|
379
|
+
reasons,
|
|
380
|
+
added,
|
|
381
|
+
removed,
|
|
382
|
+
links: { added: added.filter(isUrl).map((e) => ({ kind: e.kind, url: e.url, urlKind: e.urlKind })), removed: removed.filter(isUrl).map((e) => ({ kind: e.kind, url: e.url, urlKind: e.urlKind })) },
|
|
383
|
+
};
|
|
384
|
+
}
|