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/html.js
ADDED
|
@@ -0,0 +1,1257 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { CapError } from "./xml.js";
|
|
3
|
+
import { hitsOf, normPaid, normText, OCCURRENCE, searchCost, spend, textHash, weigh } from "./text.js";
|
|
4
|
+
import { cutText } from "./retain.js";
|
|
5
|
+
import { editHash } from "./edit-hash.js";
|
|
6
|
+
|
|
7
|
+
// HTML files (PLAN-html 3.1, 3.4, R7, R8, R10, R22): the file's bytes are the source of truth. This module decodes them, parses them once
|
|
8
|
+
// with source locations (parse5, vendored), indexes the located nodes, tells which text nodes can be edited by a splice at their exact
|
|
9
|
+
// place, builds the units and the outline an agent reads, makes and re-resolves durable anchors, and splices text with a proof that
|
|
10
|
+
// nothing outside the edited place changed.
|
|
11
|
+
//
|
|
12
|
+
// The parser is not imported here: src/html-parser.js imports it and installs it, and only modules that run in worker threads import
|
|
13
|
+
// that one (test/html-parser-thread.test.js), so the server's own thread never parses a page. Without it, readHtml and the proof throw.
|
|
14
|
+
// Every walker here is iterative (a page may nest 512 levels, and a tree past that is refused only after it is built).
|
|
15
|
+
|
|
16
|
+
let P5 = null;
|
|
17
|
+
/** Installs the parser (src/html-parser.js does, in a worker). */
|
|
18
|
+
export function installParser(p5) {
|
|
19
|
+
P5 = p5;
|
|
20
|
+
}
|
|
21
|
+
const parser = () => {
|
|
22
|
+
if (!P5) throw new Error("the HTML parser is not loaded on this thread: only worker threads that import src/html-parser.js read HTML files");
|
|
23
|
+
return P5;
|
|
24
|
+
};
|
|
25
|
+
/** Is the parser loaded on this thread (a worker thread that imported src/html-parser.js, or a test)? */
|
|
26
|
+
export const parserLoaded = () => P5 !== null;
|
|
27
|
+
/** The installed parser (src/html-render.js parses its render copy again with it); throws on a thread that has none. */
|
|
28
|
+
export const htmlParser = parser;
|
|
29
|
+
|
|
30
|
+
export const HTML_NS = "http://www.w3.org/1999/xhtml";
|
|
31
|
+
|
|
32
|
+
// ---- limits (R22: set from spike S2's measurements; docs/GUIDE.md "Size limits") ------------------------------------------------
|
|
33
|
+
/**
|
|
34
|
+
* What one HTML file may hold. Measured in S2 (worker CPU, a busy Mac): 10 MB with 200,000 elements opened in 1.2 to 1.5 s and about
|
|
35
|
+
* 600 MB of heap, the proof another 0.8 s; 20 MB with 400,000 elements ran a 1 GB worker out of memory (about 2.5 to 3 KB per element
|
|
36
|
+
* while two trees are alive). `data:` attribute values count against the file size only (R16), not the 1 MB attribute cap.
|
|
37
|
+
*/
|
|
38
|
+
export const HTML_LIMITS = { bytes: 10 * 1024 * 1024, elements: 200_000, attributes: 400_000, text: 8_000_000, depth: 512, attrValue: 1_000_000 };
|
|
39
|
+
const n = (x) => x.toLocaleString("en-US");
|
|
40
|
+
const SPLIT = "Split it into smaller pages and open those";
|
|
41
|
+
const capBytes = (size, max) => new CapError(`this page is ${(size / 1048576).toFixed(1)} MB, more than the ${max / 1048576} MB Onionskin can read. ${SPLIT}`, "bytes");
|
|
42
|
+
const capCount = (max, noun) => new CapError(`this page has more than ${n(max)} ${noun}, more than Onionskin can read. ${SPLIT}`, noun);
|
|
43
|
+
const capDepth = (max) => new CapError(`this page nests elements more than ${max} levels deep, more than Onionskin can read (a browser shows such a page differently from its source). Open a copy with less nesting`, "nesting depth");
|
|
44
|
+
const capValue = (max) => new CapError(`an attribute value in this page is longer than ${n(max)} characters, more than Onionskin can read. ${SPLIT}`, "attribute value");
|
|
45
|
+
|
|
46
|
+
// ---- decoding (R10, R22) ----------------------------------------------------------------------------------------------------------
|
|
47
|
+
/** R18: the sentence for a file saved in windows-1252 or ISO-8859-1, which Onionskin can offer to copy as UTF-8. */
|
|
48
|
+
export const NOT_UTF8 = "This file is not saved as UTF-8; Onionskin can save a UTF-8 copy and open that.";
|
|
49
|
+
/** For any other encoding (a declared Shift_JIS or UTF-16, or bytes that are not valid UTF-8): no copy is offered. */
|
|
50
|
+
export const NOT_READABLE_ENCODING = "This file is not saved as UTF-8, and Onionskin cannot tell how it is saved, so it cannot open it.";
|
|
51
|
+
const UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
52
|
+
const UTF8_LABELS = new Set(["utf-8", "utf8", "unicode-1-1-utf-8", "unicode11utf8", "unicode20utf8", "x-unicode20utf8"]);
|
|
53
|
+
// the WHATWG labels of windows-1252 (ISO-8859-1 and US-ASCII are read as windows-1252 by browsers): the copy can be made losslessly
|
|
54
|
+
const WINDOWS_1252 = new Set(["ansi_x3.4-1968", "ascii", "cp1252", "cp819", "csisolatin1", "ibm819", "iso-8859-1", "iso-ir-100", "iso8859-1", "iso88591", "iso_8859-1", "iso_8859-1:1987", "l1", "latin1", "us-ascii", "windows-1252", "x-cp1252"]);
|
|
55
|
+
|
|
56
|
+
/** The charset a page declares in its first 1,024 bytes (a meta charset, or a meta http-equiv content-type), lowercased; null when none. */
|
|
57
|
+
function declaredCharset(bytes) {
|
|
58
|
+
const head = Buffer.from(bytes.buffer, bytes.byteOffset, Math.min(bytes.length, 1024)).toString("latin1");
|
|
59
|
+
for (const m of head.matchAll(/<meta\b[^>]*>/gi)) {
|
|
60
|
+
const cs = /\bcharset\s*=\s*["']?\s*([A-Za-z0-9_.:-]+)/i.exec(m[0]);
|
|
61
|
+
if (cs) return cs[1].toLowerCase();
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The bytes of an HTML file as text, or why not. UTF-8 only, decoded in fatal mode with the BOM set aside (left in, parse5 makes it a
|
|
68
|
+
* text node at the start of the body) and reported; decoding then encoding must give the bytes back. Returns
|
|
69
|
+
* {ok: true, encoding: "utf8", text, bomLen (0 or 3), declared (the label, or null)}
|
|
70
|
+
* {ok: false, encoding: "declared-other", label, ascii, copyable, code: "not_utf8", sentence} (a declared windows-1252 file that is
|
|
71
|
+
* plain ASCII is still refused: a later write of a non-ASCII character would not match its declaration)
|
|
72
|
+
* {ok: false, encoding: "invalid", code: "utf16" | "invalid_utf8" | "roundtrip", sentence}
|
|
73
|
+
* `sentence` is NOT_UTF8 when a UTF-8 copy can be made (windows-1252 and its aliases), else NOT_READABLE_ENCODING.
|
|
74
|
+
*/
|
|
75
|
+
export function decodeHtml(bytes) {
|
|
76
|
+
const b = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
77
|
+
if (b.length >= 2 && ((b[0] === 0xff && b[1] === 0xfe) || (b[0] === 0xfe && b[1] === 0xff))) return { ok: false, encoding: "invalid", code: "utf16", sentence: NOT_READABLE_ENCODING };
|
|
78
|
+
const bomLen = b.length >= 3 && b[0] === 0xef && b[1] === 0xbb && b[2] === 0xbf ? 3 : 0;
|
|
79
|
+
const label = declaredCharset(b);
|
|
80
|
+
// a UTF-8 BOM wins over a declaration, as in a browser
|
|
81
|
+
if (!bomLen && label && !UTF8_LABELS.has(label)) {
|
|
82
|
+
const copyable = WINDOWS_1252.has(label);
|
|
83
|
+
return { ok: false, encoding: "declared-other", label: cutText(label, 40), ascii: !b.some((x) => x >= 0x80), copyable, code: "not_utf8", sentence: copyable ? NOT_UTF8 : NOT_READABLE_ENCODING };
|
|
84
|
+
}
|
|
85
|
+
const body = b.subarray(bomLen);
|
|
86
|
+
let text;
|
|
87
|
+
try {
|
|
88
|
+
text = UTF8.decode(body);
|
|
89
|
+
} catch {
|
|
90
|
+
return { ok: false, encoding: "invalid", code: "invalid_utf8", sentence: NOT_READABLE_ENCODING };
|
|
91
|
+
}
|
|
92
|
+
if (!Buffer.from(text, "utf8").equals(body)) return { ok: false, encoding: "invalid", code: "roundtrip", sentence: NOT_READABLE_ENCODING };
|
|
93
|
+
return { ok: true, encoding: "utf8", text, bomLen, declared: label };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A refusal of the file's encoding: its message is the sentence, `code` the reason code. */
|
|
97
|
+
export class HtmlEncodingError extends Error {
|
|
98
|
+
constructor(d) {
|
|
99
|
+
super(d.sentence);
|
|
100
|
+
this.code = d.code;
|
|
101
|
+
this.encoding = d.encoding;
|
|
102
|
+
this.label = d.label ?? null;
|
|
103
|
+
this.copyable = !!d.copyable;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---- offsets ------------------------------------------------------------------------------------------------------------------------
|
|
108
|
+
/**
|
|
109
|
+
* The one function from parse5's offsets (UTF-16 indexes into the decoded text, CRLF included) to byte offsets in the file (the BOM
|
|
110
|
+
* counted). A checkpoint every 4,096 code units, so a lookup reads at most 4,096; an offset inside a surrogate pair throws.
|
|
111
|
+
*/
|
|
112
|
+
export function makeByteMap(text, bomLen = 0) {
|
|
113
|
+
const STEP = 4096;
|
|
114
|
+
const len = text.length;
|
|
115
|
+
const cps = new Float64Array(Math.floor(len / STEP) + 2);
|
|
116
|
+
const width = (c) => (c < 0x80 ? 1 : c < 0x800 ? 2 : c >= 0xd800 && c <= 0xdfff ? 2 : 3); // a surrogate pair is 4 bytes, 2 + 2
|
|
117
|
+
let bytes = 0;
|
|
118
|
+
for (let i = 0, k = 0; i <= len; i++) {
|
|
119
|
+
if (i % STEP === 0) cps[k++] = bytes;
|
|
120
|
+
if (i === len) break;
|
|
121
|
+
bytes += width(text.charCodeAt(i));
|
|
122
|
+
}
|
|
123
|
+
return function toByte(off) {
|
|
124
|
+
if (!(off >= 0 && off <= len)) throw new RangeError("offset out of range");
|
|
125
|
+
if (off > 0 && off < len) {
|
|
126
|
+
const c = text.charCodeAt(off);
|
|
127
|
+
const p = text.charCodeAt(off - 1);
|
|
128
|
+
if (c >= 0xdc00 && c <= 0xdfff && p >= 0xd800 && p <= 0xdbff) throw new RangeError("offset inside a surrogate pair");
|
|
129
|
+
}
|
|
130
|
+
const k = Math.floor(off / STEP);
|
|
131
|
+
let b = cps[k];
|
|
132
|
+
for (let i = k * STEP; i < off; i++) b += width(text.charCodeAt(i));
|
|
133
|
+
return bomLen + b;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---- parsing under the limits ---------------------------------------------------------------------------------------------------------
|
|
138
|
+
/**
|
|
139
|
+
* One parse of `text` with source locations and scripting on (R22: off can move `title` into the body), counting elements, attributes
|
|
140
|
+
* and text characters as the tree is built, so a page past a limit is refused before it fills memory (a 10 MB file of tiny elements
|
|
141
|
+
* would be millions of them). Returns {doc, dupOffsets}: the start offsets of duplicate attributes (parse5 gives a dropped duplicate
|
|
142
|
+
* no location). Throws a CapError past a limit.
|
|
143
|
+
*/
|
|
144
|
+
function parseLimited(text, limits = HTML_LIMITS) {
|
|
145
|
+
const p5 = parser();
|
|
146
|
+
const d = p5.defaultTreeAdapter;
|
|
147
|
+
const c = { elements: 0, attrs: 0, text: 0 };
|
|
148
|
+
const attrs = (list) => {
|
|
149
|
+
if ((c.attrs += list.length) > limits.attributes) throw capCount(limits.attributes, "attributes");
|
|
150
|
+
for (const a of list) if (a.value.length > limits.attrValue && !/^[ \t\n\f\r]*data:/i.test(a.value)) throw capValue(limits.attrValue);
|
|
151
|
+
};
|
|
152
|
+
const chars = (t) => {
|
|
153
|
+
if ((c.text += t.length) > limits.text) throw capCount(limits.text, "characters of text");
|
|
154
|
+
};
|
|
155
|
+
const treeAdapter = {
|
|
156
|
+
...d,
|
|
157
|
+
createElement(tag, ns, list) {
|
|
158
|
+
if (++c.elements > limits.elements) throw capCount(limits.elements, "elements");
|
|
159
|
+
attrs(list);
|
|
160
|
+
return d.createElement(tag, ns, list);
|
|
161
|
+
},
|
|
162
|
+
adoptAttributes(recipient, list) {
|
|
163
|
+
const before = recipient.attrs.length;
|
|
164
|
+
d.adoptAttributes(recipient, list);
|
|
165
|
+
attrs(recipient.attrs.slice(before));
|
|
166
|
+
},
|
|
167
|
+
insertText(parent, t) {
|
|
168
|
+
chars(t);
|
|
169
|
+
d.insertText(parent, t);
|
|
170
|
+
},
|
|
171
|
+
insertTextBefore(parent, t, ref) {
|
|
172
|
+
chars(t);
|
|
173
|
+
d.insertTextBefore(parent, t, ref);
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
const dupOffsets = [];
|
|
177
|
+
const doc = p5.parse(text, { sourceCodeLocationInfo: true, scriptingEnabled: true, treeAdapter, onParseError: (e) => e.code === "duplicate-attribute" && dupOffsets.push(e.startOffset) });
|
|
178
|
+
return { doc, dupOffsets: dupOffsets.sort((a, b) => a - b) };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ---- tree helpers ---------------------------------------------------------------------------------------------------------------------
|
|
182
|
+
/** Children of a node; a template's are those of its content. */
|
|
183
|
+
export const kids = (x) => (x.nodeName === "template" && x.content ? x.content.childNodes : x.childNodes || []);
|
|
184
|
+
const isEl = (x) => x.tagName !== undefined;
|
|
185
|
+
const isText = (x) => x.nodeName === "#text";
|
|
186
|
+
const html = (x) => x.namespaceURI === HTML_NS;
|
|
187
|
+
/** Elements whose content is raw text: never a unit, never an edit, never shown to an agent (R7). `noscript` is raw with scripting on. */
|
|
188
|
+
export const RAWTEXT = new Set(["script", "style", "xmp", "iframe", "noembed", "noframes", "plaintext", "noscript"]);
|
|
189
|
+
export const RCDATA = new Set(["title", "textarea"]);
|
|
190
|
+
/** A raw-text element, or a script or style in another namespace (an SVG script runs and an SVG style applies): its text is frozen and hidden. */
|
|
191
|
+
const frozen = (el) => (html(el) ? RAWTEXT.has(el.tagName) : el.tagName === "script" || el.tagName === "style");
|
|
192
|
+
const LEADING_LF = new Set(["pre", "listing", "textarea"]);
|
|
193
|
+
const HEADING = /^h[1-6]$/;
|
|
194
|
+
/** Leaf elements that are units with no text of their own (images and other replaced content). */
|
|
195
|
+
const LEAF = new Set(["img", "video", "audio", "canvas", "iframe", "embed", "object", "input", "hr", "svg"]);
|
|
196
|
+
// elements a browser shows on a line of their own: the visible text puts a space at their edges, so their words never run together
|
|
197
|
+
const BLOCK = new Set(["address", "article", "aside", "blockquote", "body", "br", "caption", "dd", "details", "dialog", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer", "form", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "hr", "html", "li", "main", "menu", "nav", "ol", "option", "p", "pre", "section", "summary", "table", "tbody", "td", "tfoot", "th", "thead", "title", "tr", "ul"]);
|
|
198
|
+
|
|
199
|
+
/** The path of a node from the document: child indexes (template content counts as the template's children). */
|
|
200
|
+
export function pathOf(x) {
|
|
201
|
+
const p = [];
|
|
202
|
+
while (x && x.__par) (p.push(x.__idx), (x = x.__par));
|
|
203
|
+
return p.reverse();
|
|
204
|
+
}
|
|
205
|
+
/** The node at child-index path `p` from `doc`, or null. */
|
|
206
|
+
export function atPath(doc, p) {
|
|
207
|
+
let x = doc;
|
|
208
|
+
for (const i of p) {
|
|
209
|
+
const ks = kids(x);
|
|
210
|
+
if (i >= ks.length) return null;
|
|
211
|
+
x = ks[i];
|
|
212
|
+
}
|
|
213
|
+
return x;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Deep structural equality of two subtrees (names, namespaces, attributes in order, values, template content), iterative. */
|
|
217
|
+
export function deepEqual(a, b) {
|
|
218
|
+
const st = [[a, b]];
|
|
219
|
+
while (st.length) {
|
|
220
|
+
const [x, y] = st.pop();
|
|
221
|
+
if (x.nodeName !== y.nodeName) return false;
|
|
222
|
+
if (isText(x)) {
|
|
223
|
+
if (x.value !== y.value) return false;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (x.nodeName === "#comment") {
|
|
227
|
+
if (x.data !== y.data) return false;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (x.nodeName === "#documentType") {
|
|
231
|
+
if (x.name !== y.name || x.publicId !== y.publicId || x.systemId !== y.systemId) return false;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (isEl(x)) {
|
|
235
|
+
if (x.tagName !== y.tagName || x.namespaceURI !== y.namespaceURI || x.attrs.length !== y.attrs.length) return false;
|
|
236
|
+
for (let i = 0; i < x.attrs.length; i++) {
|
|
237
|
+
const p = x.attrs[i];
|
|
238
|
+
const q = y.attrs[i];
|
|
239
|
+
if (p.name !== q.name || p.value !== q.value || (p.namespace || "") !== (q.namespace || "") || (p.prefix || "") !== (q.prefix || "")) return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const xk = kids(x);
|
|
243
|
+
const yk = kids(y);
|
|
244
|
+
if (xk.length !== yk.length) return false;
|
|
245
|
+
for (let i = 0; i < xk.length; i++) st.push([xk[i], yk[i]]);
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
/** One node's own identity (no children): what the proof compares on the path to the edited place. */
|
|
250
|
+
function shallowSame(a, b) {
|
|
251
|
+
if (a.nodeName !== b.nodeName) return false;
|
|
252
|
+
if (isText(a)) return a.value === b.value;
|
|
253
|
+
if (a.nodeName === "#comment") return a.data === b.data;
|
|
254
|
+
if (!isEl(a)) return true;
|
|
255
|
+
if (a.tagName !== b.tagName || a.namespaceURI !== b.namespaceURI || a.attrs.length !== b.attrs.length) return false;
|
|
256
|
+
return a.attrs.every((p, i) => p.name === b.attrs[i].name && p.value === b.attrs[i].value && (p.namespace || "") === (b.attrs[i].namespace || "") && (p.prefix || "") === (b.attrs[i].prefix || ""));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ---- contexts, addressability (R8) and escaping ---------------------------------------------------------------------------------------
|
|
260
|
+
/** The text context of a text node: "raw" (frozen), "rcdata" (title, textarea), "normal", or "none" (no parent element). */
|
|
261
|
+
export function textContext(x) {
|
|
262
|
+
const p = x.__ctx;
|
|
263
|
+
if (!p) return "none";
|
|
264
|
+
if (frozen(p)) return "raw";
|
|
265
|
+
if (html(p) && RCDATA.has(p.tagName)) return "rcdata";
|
|
266
|
+
return "normal";
|
|
267
|
+
}
|
|
268
|
+
/** Is the text node's parent a pre, listing or textarea (the parser drops a newline right after their start tag)? */
|
|
269
|
+
export const leadingLfParent = (x) => !!x.__ctx && html(x.__ctx) && LEADING_LF.has(x.__ctx.tagName);
|
|
270
|
+
/**
|
|
271
|
+
* The source range [start, end) of a text node. parse5 includes in the span the newline it drops right after <pre>, <listing> or
|
|
272
|
+
* <textarea> when more whitespace follows; that newline is not part of the node, so it is left outside (S2, E2).
|
|
273
|
+
*/
|
|
274
|
+
export function textRange(x, text) {
|
|
275
|
+
const loc = x.sourceCodeLocation;
|
|
276
|
+
let s = loc.startOffset;
|
|
277
|
+
const e = loc.endOffset;
|
|
278
|
+
const p = x.__ctx;
|
|
279
|
+
const pst = p?.sourceCodeLocation?.startTag;
|
|
280
|
+
if (p && leadingLfParent(x) && pst && s === pst.endOffset) {
|
|
281
|
+
if (text.startsWith("\r\n", s) && !x.value.startsWith("\n\n") && e - s > x.value.length) s += 2;
|
|
282
|
+
else if ((text[s] === "\n" || text[s] === "\r") && e - s > x.value.length) s += 1;
|
|
283
|
+
}
|
|
284
|
+
return [s, e];
|
|
285
|
+
}
|
|
286
|
+
/** R8 for a text node: located, not whitespace only, not raw text, and its source slice parsed alone in its parent's context is it. */
|
|
287
|
+
function addressableText(x, text) {
|
|
288
|
+
if (!x.sourceCodeLocation || !x.__ctx || !/\S/.test(x.value)) return false;
|
|
289
|
+
const c = textContext(x);
|
|
290
|
+
if (c !== "normal" && c !== "rcdata") return false;
|
|
291
|
+
const [s, e] = textRange(x, text);
|
|
292
|
+
const f = parser().parseFragment(x.__ctx, text.slice(s, e), { scriptingEnabled: true }).childNodes;
|
|
293
|
+
return f.length === 1 && isText(f[0]) && f[0].value === x.value;
|
|
294
|
+
}
|
|
295
|
+
/** R8 for an element, run on demand (O(its subtree)): its source slice, parsed alone in its parent's context, is exactly it. */
|
|
296
|
+
export function elementAddressable(read, nodeId) {
|
|
297
|
+
const x = read.nodes[nodeId];
|
|
298
|
+
const loc = x?.sourceCodeLocation;
|
|
299
|
+
if (!x || !isEl(x) || !loc?.startTag || !x.__ctx || (html(x) && ["html", "head", "body"].includes(x.tagName))) return false;
|
|
300
|
+
const f = parser().parseFragment(x.__ctx, read.text.slice(loc.startOffset, loc.endOffset), { scriptingEnabled: true }).childNodes;
|
|
301
|
+
return f.length === 1 && deepEqual(f[0], x);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* New text for a splice that replaces one whole text node, escaped for its context (S2): normal text escapes & < > and CR, RCDATA & <
|
|
306
|
+
* and CR; a leading LF at the very start of pre, listing or textarea gets one more literal LF for the parser to drop (a character
|
|
307
|
+
* reference would not survive). Raw text is frozen. NUL and lone surrogates are refused. Returns {ok, out} or {ok: false, code}.
|
|
308
|
+
*/
|
|
309
|
+
export function escapeText(s, context, { atStartOfLeadingLf = false } = {}) {
|
|
310
|
+
if (context !== "normal" && context !== "rcdata") return { ok: false, code: "frozen" };
|
|
311
|
+
if (s.includes("\u0000")) return { ok: false, code: "nul" };
|
|
312
|
+
if (/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/.test(s)) return { ok: false, code: "lone_surrogate" };
|
|
313
|
+
let out = s.replace(/&/g, "&").replace(/</g, "<");
|
|
314
|
+
if (context === "normal") out = out.replace(/>/g, ">");
|
|
315
|
+
out = out.replace(/\r/g, " ");
|
|
316
|
+
if (atStartOfLeadingLf && out.startsWith("\n")) out = `\n${out}`;
|
|
317
|
+
return { ok: true, out };
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* An attribute value escaped for its quoting style ('"', "'" or "" for unquoted, which becomes '"' unless the value is a plain token).
|
|
321
|
+
* Returns {ok, out (with its quotes), quote} or {ok: false, code: "nul"}.
|
|
322
|
+
*/
|
|
323
|
+
export function escapeAttr(v, quote) {
|
|
324
|
+
if (v.includes("\u0000")) return { ok: false, code: "nul" };
|
|
325
|
+
if (/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/.test(v)) return { ok: false, code: "lone_surrogate" };
|
|
326
|
+
if (quote === "" && !/^[^\s"'=<>`&]+$/.test(v)) quote = '"';
|
|
327
|
+
let out = v.replace(/&/g, "&").replace(/\r/g, " ");
|
|
328
|
+
if (quote === '"') out = out.replace(/"/g, """);
|
|
329
|
+
if (quote === "'") out = out.replace(/'/g, "'");
|
|
330
|
+
return { ok: true, out: quote + out + quote, quote };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// ---- names an agent may see (R7) ------------------------------------------------------------------------------------------------------
|
|
334
|
+
const SAFE_NAME = /^[a-z0-9-]{1,32}$/;
|
|
335
|
+
const sha = (s) => crypto.createHash("sha256").update(String(s)).digest("hex");
|
|
336
|
+
/** A tag name as an agent may see it: itself when it matches [a-z0-9-]{1,32}, else a neutral token made from its hash (stable, so a path still compares). */
|
|
337
|
+
export const safeTag = (tag) => (SAFE_NAME.test(tag) ? tag : `t-${sha(tag).slice(0, 12)}`);
|
|
338
|
+
const idOf = (x) => x.attrs?.find((a) => a.name === "id" && !a.namespace)?.value ?? null;
|
|
339
|
+
|
|
340
|
+
// ---- reading a file -------------------------------------------------------------------------------------------------------------------
|
|
341
|
+
/**
|
|
342
|
+
* Reads an HTML file (bytes) in full: size, decoding, one parse under the limits, depth, the located nodes, addressability of every text
|
|
343
|
+
* node, the units and the outline's text. Runs in a worker only (the parser is there). Throws a CapError past a limit (its message states
|
|
344
|
+
* the limit in plain words) and an HtmlEncodingError for a file that is not UTF-8. The read holds one tree.
|
|
345
|
+
*
|
|
346
|
+
* Returns a reading: {format: "html", bytes, text, bomLen, toByte, doc, nodes, units, flat, byId, dupOffsets, limits}.
|
|
347
|
+
* - nodes: the located nodes (elements, text, comments, the doctype; never an implied element) in source order (start offset, then tree
|
|
348
|
+
* order); a node's id (`__id`) is its index here. The render copy and the page use the same ids.
|
|
349
|
+
* - units: the elements that directly hold non-whitespace text, and leaf elements (images and the like), in tree order, outside raw
|
|
350
|
+
* text and template content: {node, nodeId (null for an implied element), tag, kind: "text" | "leaf", editable, heading (the index in
|
|
351
|
+
* `units` of the nearest heading at or before it, or -1), htmlId (its id when unique and [a-z0-9-]{1,32}, else null), hasId}.
|
|
352
|
+
* - flat: the page's visible text (no raw text, no template content), whitespace runs as one space and a space at block edges;
|
|
353
|
+
* every element's [__fs, __fe) is its range in it.
|
|
354
|
+
*/
|
|
355
|
+
export function readHtml(bytes, limits = HTML_LIMITS) {
|
|
356
|
+
const b = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
357
|
+
if (b.length > limits.bytes) throw capBytes(b.length, limits.bytes);
|
|
358
|
+
const d = decodeHtml(b);
|
|
359
|
+
if (!d.ok) throw new HtmlEncodingError(d);
|
|
360
|
+
const { doc, dupOffsets } = parseLimited(d.text, limits);
|
|
361
|
+
return indexRead({ bytes: b, text: d.text, bomLen: d.bomLen, doc, dupOffsets, limits });
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** A reading from an already parsed version (proveSplice's): the same as readHtml without a second parse. */
|
|
365
|
+
export function readParsed(parsed) {
|
|
366
|
+
return indexRead({ ...parsed, limits: parsed.limits || HTML_LIMITS });
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function indexRead({ bytes, text, bomLen, doc, dupOffsets, limits }) {
|
|
370
|
+
const len = text.length;
|
|
371
|
+
const located = [];
|
|
372
|
+
const units = [];
|
|
373
|
+
const byHtmlId = new Map(); // id -> element, or null when two elements share it
|
|
374
|
+
const flat = [];
|
|
375
|
+
let flatLen = 0;
|
|
376
|
+
const put = (s) => {
|
|
377
|
+
flat.push(s);
|
|
378
|
+
flatLen += s.length;
|
|
379
|
+
};
|
|
380
|
+
let lastSpace = true;
|
|
381
|
+
// a block's edge is a space, as a selection across two paragraphs reads once its whitespace is folded (src/text.js normText)
|
|
382
|
+
const lineBreak = () => {
|
|
383
|
+
if (!lastSpace) put(" ");
|
|
384
|
+
lastSpace = true;
|
|
385
|
+
};
|
|
386
|
+
let heading = -1;
|
|
387
|
+
doc.__par = null;
|
|
388
|
+
doc.__depth = 0;
|
|
389
|
+
doc.__ctx = null;
|
|
390
|
+
doc.__hidden = false; // inside raw text or template content: not shown, never a unit
|
|
391
|
+
const nth = new Map();
|
|
392
|
+
const stack = [doc];
|
|
393
|
+
while (stack.length) {
|
|
394
|
+
const x = stack.pop();
|
|
395
|
+
if (x.exit) {
|
|
396
|
+
const el = x.exit;
|
|
397
|
+
if (el.__block) lineBreak();
|
|
398
|
+
el.__fe = flatLen;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
const loc = x.sourceCodeLocation;
|
|
402
|
+
if (loc) {
|
|
403
|
+
// parse5 8.1.1 reports endOffset = length + 1 for a comment or doctype cut off by the end of the input (S2, E1)
|
|
404
|
+
if (loc.endOffset > len) loc.endOffset = len;
|
|
405
|
+
if (loc.endTag && loc.endTag.endOffset > len) loc.endTag.endOffset = len;
|
|
406
|
+
if (x !== doc) located.push(x);
|
|
407
|
+
}
|
|
408
|
+
if (isEl(x) && x.__depth > limits.depth) throw capDepth(limits.depth);
|
|
409
|
+
if (isText(x)) {
|
|
410
|
+
if (x.__hidden) continue;
|
|
411
|
+
if (x.__ctx && /\S/.test(x.value)) x.__ok = addressableText(x, text);
|
|
412
|
+
let t = x.value.replace(/[ \t\n\f\r]+/g, " ");
|
|
413
|
+
if (lastSpace && t.startsWith(" ")) t = t.slice(1);
|
|
414
|
+
if (t) {
|
|
415
|
+
x.__fs = flatLen;
|
|
416
|
+
put(t);
|
|
417
|
+
lastSpace = t.endsWith(" ");
|
|
418
|
+
}
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
const ks = kids(x);
|
|
422
|
+
if (isEl(x)) {
|
|
423
|
+
x.__block = html(x) && BLOCK.has(x.tagName);
|
|
424
|
+
if (x.__block) lineBreak();
|
|
425
|
+
x.__fs = flatLen;
|
|
426
|
+
stack.push({ exit: x });
|
|
427
|
+
const id = idOf(x);
|
|
428
|
+
if (id !== null && SAFE_NAME.test(id)) byHtmlId.set(id, byHtmlId.has(id) ? null : x);
|
|
429
|
+
if (!x.__hidden) {
|
|
430
|
+
// raw-text and template elements hold no unit text (R7); an iframe is still a leaf
|
|
431
|
+
const raw = frozen(x) || (html(x) && x.tagName === "template");
|
|
432
|
+
const direct = !raw && ks.some((c) => isText(c) && /\S/.test(c.value));
|
|
433
|
+
const leaf = LEAF.has(x.tagName);
|
|
434
|
+
if (direct || leaf) {
|
|
435
|
+
const before = heading; // a heading's own heading is the one before it; what it holds is under it
|
|
436
|
+
if (html(x) && HEADING.test(x.tagName)) heading = units.length;
|
|
437
|
+
units.push({ node: x, nodeId: null, tag: x.tagName, kind: direct ? "text" : "leaf", editable: false, heading: heading === units.length ? before : heading, htmlId: null, hasId: id !== null });
|
|
438
|
+
x.__unit = units.length - 1;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
// children: their parent, index, context element, depth, nth among same-tag element siblings, and whether they are shown
|
|
443
|
+
nth.clear();
|
|
444
|
+
const hide = x.__hidden || (isEl(x) && (frozen(x) || (html(x) && x.tagName === "template")));
|
|
445
|
+
for (let i = 0; i < ks.length; i++) {
|
|
446
|
+
const c = ks[i];
|
|
447
|
+
c.__par = x;
|
|
448
|
+
c.__idx = i;
|
|
449
|
+
c.__depth = x.__depth + 1;
|
|
450
|
+
c.__ctx = isEl(x) ? x : null;
|
|
451
|
+
c.__hidden = hide;
|
|
452
|
+
if (isEl(c)) {
|
|
453
|
+
// by tag alone (any namespace): a path segment tag:nth then names one child
|
|
454
|
+
c.__nth = nth.get(c.tagName) || 0;
|
|
455
|
+
nth.set(c.tagName, c.__nth + 1);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
for (let i = ks.length - 1; i >= 0; i--) stack.push(ks[i]);
|
|
459
|
+
}
|
|
460
|
+
// ids: located nodes in source order; tree order breaks ties (an adoption-agency clone carries its original's start location)
|
|
461
|
+
located.forEach((x, i) => (x.__tree = i));
|
|
462
|
+
located.sort((a, b) => a.sourceCodeLocation.startOffset - b.sourceCodeLocation.startOffset || a.__tree - b.__tree);
|
|
463
|
+
located.forEach((x, i) => (x.__id = i));
|
|
464
|
+
for (const u of units) {
|
|
465
|
+
u.nodeId = u.node.__id ?? null;
|
|
466
|
+
u.editable = u.kind === "text" && kids(u.node).some((c) => c.__ok === true);
|
|
467
|
+
const id = idOf(u.node);
|
|
468
|
+
u.htmlId = id !== null && byHtmlId.get(id) === u.node ? id : null;
|
|
469
|
+
}
|
|
470
|
+
const byId = new Map();
|
|
471
|
+
for (const [k, v] of byHtmlId) if (v) byId.set(k, v);
|
|
472
|
+
return { format: "html", bytes, text, bomLen, toByte: makeByteMap(text, bomLen), doc, nodes: located, units, flat: flat.join(""), byId, dupOffsets, limits };
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Text the person may type in (PLAN-html D4, R8, R11): source text nodes the write check accepts (src/html-write.js checkedHtmlWrite
|
|
476
|
+
// "edits": located, addressable, in normal or RCDATA context, not hidden) and not inside a link, a form control, `title` or `head`, where
|
|
477
|
+
// typing would change what a control or a link says rather than the page's text (the page applies the markup half again,
|
|
478
|
+
// web/src/html-view.js canType).
|
|
479
|
+
const NOT_TYPED = new Set(["a", "button", "select", "option", "optgroup", "textarea", "datalist", "output", "title", "head"]);
|
|
480
|
+
/** The ids (in `read.nodes`) of the source text nodes the person may type in: the `editable` list the view route sends the page. */
|
|
481
|
+
export function editableTextIds(read) {
|
|
482
|
+
const out = [];
|
|
483
|
+
for (const x of read.nodes) {
|
|
484
|
+
if (!isText(x) || x.__ok !== true || x.__hidden || !["normal", "rcdata"].includes(textContext(x))) continue;
|
|
485
|
+
let ok = true;
|
|
486
|
+
for (let p = x.__par; p && ok; p = p.__par) if (p.tagName && NOT_TYPED.has(p.tagName)) ok = false;
|
|
487
|
+
if (ok) out.push(x.__id);
|
|
488
|
+
}
|
|
489
|
+
return out;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// The parts of the page the person may type in, made editable in the render copy itself (src/html-render.js) so the page never has to
|
|
493
|
+
// change an attribute on a large page (measured in Chromium: making 100,000 paragraphs editable from script costs seconds of layout) and
|
|
494
|
+
// a key pressed right after a click is never lost. A part is the element right above a text node of editableTextIds, unless an element
|
|
495
|
+
// above it is a part already, or its subtree holds more than EDIT_HOST_MAX nodes (it is then made editable at the click, by the page:
|
|
496
|
+
// web/src/html-frame.js beginHost, and compared whole while typed in). The page checks every typed change again (canType).
|
|
497
|
+
export const EDIT_HOST_MAX = 400;
|
|
498
|
+
/** The elements the render copy marks `contenteditable` (see above), as a Set of the reading's elements. */
|
|
499
|
+
export function editHosts(read, ids = editableTextIds(read)) {
|
|
500
|
+
const cand = new Set();
|
|
501
|
+
for (const id of ids) {
|
|
502
|
+
const p = read.nodes[id]?.__par;
|
|
503
|
+
if (p && p.tagName !== undefined) cand.add(p);
|
|
504
|
+
}
|
|
505
|
+
// subtree sizes, capped at EDIT_HOST_MAX + 1, for the candidates: counted by walking each one (stopping at the cap), the inner ones first
|
|
506
|
+
const size = new Map();
|
|
507
|
+
const sizeOf = (el) => {
|
|
508
|
+
let n = 0;
|
|
509
|
+
const stack = [...kids(el)];
|
|
510
|
+
while (stack.length && n <= EDIT_HOST_MAX) {
|
|
511
|
+
const x = stack.pop();
|
|
512
|
+
n++;
|
|
513
|
+
for (const c of kids(x)) stack.push(c);
|
|
514
|
+
}
|
|
515
|
+
return n;
|
|
516
|
+
};
|
|
517
|
+
const small = (el) => {
|
|
518
|
+
let v = size.get(el);
|
|
519
|
+
if (v === undefined) size.set(el, (v = sizeOf(el)));
|
|
520
|
+
return v <= EDIT_HOST_MAX;
|
|
521
|
+
};
|
|
522
|
+
const hosts = new Set();
|
|
523
|
+
for (const c of cand) {
|
|
524
|
+
if (!small(c)) continue;
|
|
525
|
+
let covered = false;
|
|
526
|
+
for (let a = c.__par; a && !covered; a = a.__par) if (cand.has(a) && small(a)) covered = true;
|
|
527
|
+
if (!covered) hosts.add(c);
|
|
528
|
+
}
|
|
529
|
+
return hosts;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// The identity of a text node the person may type in (PLAN-html R9; the page re-places a pending edit after a version change by it, never
|
|
533
|
+
// by the node's numeric id, which means nothing in another version: web/src/html-view.js createEdits replace). Per node of `ids`, six
|
|
534
|
+
// int32 in a row:
|
|
535
|
+
// nk (2 ints): a 64-bit key of its neighbors: its parent's tag, its index among the parent's text children, and the text hashes
|
|
536
|
+
// (src/edit-hash.js) of the shown, non-blank text nodes just before and just after it in the document;
|
|
537
|
+
// ik (2 ints): a 64-bit key of its parent's `id` (when unique in this version), or 0, 0;
|
|
538
|
+
// tw: how many shown text nodes of this version have exactly its text (its twins, itself included);
|
|
539
|
+
// pw: how many of them are under its parent with that unique id (anywhere below it, not only its children), or 0 when it has none.
|
|
540
|
+
// The node's own text is not in a key: the page reads it from the frame and compares its hash, so the same keys also find a node whose
|
|
541
|
+
// text is already the edit's new text. The element path is not in a key on purpose: an element inserted before shifts every path after
|
|
542
|
+
// it, and "same path, same text" is exactly what put an edit on a twin paragraph inserted before its own (the outside review, 029e51b).
|
|
543
|
+
// Nor is the index in `ik`: a parent's own text moved among its children is still found by the id (when it is the only one with its text
|
|
544
|
+
// there). The page places an edit only where the counts say one node can be meant (twins are never told apart by content).
|
|
545
|
+
const HASH_SEEDS = [0x9e3779b9, 0x85ebca6b];
|
|
546
|
+
function key64(s, out, at) {
|
|
547
|
+
// two 32-bit lanes of a multiply-xor hash with different seeds (not a security hash: the write check re-checks the text at Send)
|
|
548
|
+
for (let lane = 0; lane < 2; lane++) {
|
|
549
|
+
let h = HASH_SEEDS[lane] ^ s.length;
|
|
550
|
+
for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), lane ? 0x27d4eb2f : 0x165667b1);
|
|
551
|
+
h = Math.imul(h ^ (h >>> 15), 0x2c1b3c6d);
|
|
552
|
+
h = Math.imul(h ^ (h >>> 12), 0x297a2d39);
|
|
553
|
+
out[at + lane] = (h ^ (h >>> 15)) | 0;
|
|
554
|
+
}
|
|
555
|
+
// 0, 0 means "no key": a real key is never that
|
|
556
|
+
if (out[at] === 0 && out[at + 1] === 0) out[at + 1] = 1;
|
|
557
|
+
}
|
|
558
|
+
export const IDENTITY_INTS = 6;
|
|
559
|
+
// Ceiling of the parent counts (pw): nodes visited under id'd parents, all together. Nested id'd parents each holding text cost their
|
|
560
|
+
// subtrees again (quadratic at worst); past this budget a parent gets pw 0, so its texts are placed by their neighbors only (or listed).
|
|
561
|
+
const PARENT_COUNT_BUDGET = 2_000_000;
|
|
562
|
+
/** The identities of the text nodes `ids` (editableTextIds), as one Int32Array of IDENTITY_INTS per node (see above). */
|
|
563
|
+
export function editIdentities(read, ids) {
|
|
564
|
+
const shown = [];
|
|
565
|
+
const twins = new Map();
|
|
566
|
+
for (const x of read.nodes) {
|
|
567
|
+
if (!isText(x) || x.__hidden || !/\S/.test(x.value)) continue;
|
|
568
|
+
x.__shown = shown.length;
|
|
569
|
+
shown.push(x);
|
|
570
|
+
twins.set(x.value, (twins.get(x.value) || 0) + 1);
|
|
571
|
+
}
|
|
572
|
+
const under = new Map(); // id'd parent -> Map(text -> shown text nodes with it in its subtree), or null past the budget
|
|
573
|
+
let budget = PARENT_COUNT_BUDGET;
|
|
574
|
+
const countsUnder = (p) => {
|
|
575
|
+
if (under.has(p)) return under.get(p);
|
|
576
|
+
const m = new Map();
|
|
577
|
+
const stack = [p];
|
|
578
|
+
while (stack.length && budget > 0) {
|
|
579
|
+
const n = stack.pop();
|
|
580
|
+
budget--;
|
|
581
|
+
if (isText(n)) {
|
|
582
|
+
if (n.__shown !== undefined && shown[n.__shown] === n) m.set(n.value, (m.get(n.value) || 0) + 1);
|
|
583
|
+
} else for (const c of kids(n)) stack.push(c);
|
|
584
|
+
}
|
|
585
|
+
const r = stack.length ? null : m;
|
|
586
|
+
under.set(p, r);
|
|
587
|
+
return r;
|
|
588
|
+
};
|
|
589
|
+
const out = new Int32Array(IDENTITY_INTS * ids.length);
|
|
590
|
+
ids.forEach((id, k) => {
|
|
591
|
+
const x = read.nodes[id];
|
|
592
|
+
const p = x.__par;
|
|
593
|
+
let index = 0;
|
|
594
|
+
for (const c of kids(p)) {
|
|
595
|
+
if (c === x) break;
|
|
596
|
+
if (isText(c)) index++;
|
|
597
|
+
}
|
|
598
|
+
const at = k * IDENTITY_INTS;
|
|
599
|
+
const i = x.__shown;
|
|
600
|
+
const prev = i > 0 ? editHash(shown[i - 1].value) : "";
|
|
601
|
+
const next = i < shown.length - 1 ? editHash(shown[i + 1].value) : "";
|
|
602
|
+
key64(`${p?.tagName ?? ""}\u0001${index}\u0001${prev}\u0001${next}`, out, at);
|
|
603
|
+
const hid = p && idOf(p);
|
|
604
|
+
if (hid !== null && hid !== undefined && read.byId.get(hid) === p) {
|
|
605
|
+
const m = countsUnder(p);
|
|
606
|
+
if (m) {
|
|
607
|
+
key64(`${hid}`, out, at + 2);
|
|
608
|
+
out[at + 5] = m.get(x.value) || 0;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
out[at + 4] = twins.get(x.value) || 0;
|
|
612
|
+
});
|
|
613
|
+
return out;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** Does this element's start tag hold a duplicate attribute (parse5 keeps the first; removing it would bring back the next)? */
|
|
617
|
+
export function hasDuplicateAttributes(read, nodeId) {
|
|
618
|
+
const st = read.nodes[nodeId]?.sourceCodeLocation?.startTag;
|
|
619
|
+
if (!st) return false;
|
|
620
|
+
const o = read.dupOffsets;
|
|
621
|
+
let lo = 0;
|
|
622
|
+
let hi = o.length;
|
|
623
|
+
while (lo < hi) {
|
|
624
|
+
const m = (lo + hi) >> 1;
|
|
625
|
+
if (o[m] < st.startOffset) lo = m + 1;
|
|
626
|
+
else hi = m;
|
|
627
|
+
}
|
|
628
|
+
return lo < o.length && o[lo] < st.endOffset;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// ---- units, text, the outline -------------------------------------------------------------------------------------------------------
|
|
632
|
+
/** The visible text of an element (its range of `flat`), as kept: line breaks and spaces as they are there. */
|
|
633
|
+
const rawTextOf = (read, el) => read.flat.slice(el.__fs ?? 0, el.__fe ?? 0);
|
|
634
|
+
/** The visible text of an element (never raw text such as a script or style body, never template content: R7). */
|
|
635
|
+
export const visibleText = rawTextOf;
|
|
636
|
+
/** The element's own text: its direct text children, normalized. Its hash identifies it (an edit inside a nested element leaves it). */
|
|
637
|
+
export function ownText(el) {
|
|
638
|
+
let s = "";
|
|
639
|
+
for (const c of kids(el)) if (isText(c)) s += c.value;
|
|
640
|
+
return s;
|
|
641
|
+
}
|
|
642
|
+
const OWN = new WeakMap();
|
|
643
|
+
/** textHash of an element's own text; a leaf by its tag and source (an image has no text). Kept per element of a reading. */
|
|
644
|
+
export function ownHash(el) {
|
|
645
|
+
let h = OWN.get(el);
|
|
646
|
+
if (h === undefined) {
|
|
647
|
+
h = LEAF.has(el.tagName) ? textHash(`${el.tagName} ${el.attrs.find((a) => a.name === "src")?.value ?? ""} ${el.attrs.find((a) => a.name === "alt")?.value ?? ""}`) : textHash(ownText(el));
|
|
648
|
+
OWN.set(el, h);
|
|
649
|
+
}
|
|
650
|
+
return h;
|
|
651
|
+
}
|
|
652
|
+
/** The cost of ownHash the first time (its text weighed), nothing once kept. */
|
|
653
|
+
const ownCost = (el) => (OWN.has(el) ? 0 : weigh(ownText(el)) + 1);
|
|
654
|
+
|
|
655
|
+
/** The element path an agent and a durable anchor use: [`tag:nth`] from the root, tag names made safe (R7), nth among same-tag siblings. */
|
|
656
|
+
export function elementPath(el) {
|
|
657
|
+
const p = [];
|
|
658
|
+
for (let x = el; x && x.__par; x = x.__par) if (isEl(x)) p.push(`${safeTag(x.tagName)}:${x.__nth}`);
|
|
659
|
+
return p.reverse();
|
|
660
|
+
}
|
|
661
|
+
/** A path as the agent reads it: /html[1]/body[1]/p[3] (1-based, as XPath counts). */
|
|
662
|
+
export const pathString = (p) => (Array.isArray(p) ? `/${p.map((s) => s.replace(/:(\d+)$/, (_, k) => `[${Number(k) + 1}]`)).join("/")}` : null);
|
|
663
|
+
|
|
664
|
+
const LABEL_EXCERPT = 60;
|
|
665
|
+
const TEXT_SHOWN = 2001; // more than the outline shows of a unit (src/review.js UNIT_TEXT_MAX), so a longer text reads as cut there
|
|
666
|
+
/**
|
|
667
|
+
* The units in src/review.js rawUnits shape, for the outline an agent gets (review.js outlineOf cuts and cleans them): {label, text (the
|
|
668
|
+
* unit's visible text), protected (not editable here), at: {node_id, path, element_id, text_hash}}. The label
|
|
669
|
+
* names the tag (R7), the id when it is safe, and the nearest heading before it. Script and style bodies are never in any text (R7).
|
|
670
|
+
* `label`, `text` and `at` are read when asked for: the outline reads them for the units it shows, not for every unit of a large page.
|
|
671
|
+
*/
|
|
672
|
+
export function htmlRawUnits(read) {
|
|
673
|
+
const head = new Map();
|
|
674
|
+
const headingText = (i) => {
|
|
675
|
+
if (!head.has(i)) head.set(i, normText(rawTextOf(read, read.units[i].node).slice(0, 4 * LABEL_EXCERPT)).slice(0, LABEL_EXCERPT));
|
|
676
|
+
return head.get(i);
|
|
677
|
+
};
|
|
678
|
+
return read.units.map((u, i) => ({
|
|
679
|
+
get label() {
|
|
680
|
+
const id = u.htmlId ? `#${u.htmlId}` : u.hasId ? "#(id)" : "";
|
|
681
|
+
const h = u.heading >= 0 ? headingText(u.heading) : "";
|
|
682
|
+
const level = html(u.node) && HEADING.test(u.tag) ? ` (heading ${u.tag[1]})` : "";
|
|
683
|
+
return `${safeTag(u.tag)}${id}${level}${h ? ` under "${h}"` : ""}`;
|
|
684
|
+
},
|
|
685
|
+
get text() {
|
|
686
|
+
if (u.kind === "leaf") return u.node.attrs.find((a) => a.name === "alt")?.value ?? "";
|
|
687
|
+
return read.flat.slice(u.node.__fs ?? 0, Math.min(u.node.__fe ?? 0, (u.node.__fs ?? 0) + TEXT_SHOWN)).trim();
|
|
688
|
+
},
|
|
689
|
+
protected: !u.editable,
|
|
690
|
+
get at() {
|
|
691
|
+
return { node_id: u.nodeId, path: pathString(elementPath(u.node)), element_id: u.htmlId, text_hash: ownHash(u.node) };
|
|
692
|
+
},
|
|
693
|
+
}));
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// ---- durable anchors (3.4) ------------------------------------------------------------------------------------------------------------
|
|
697
|
+
const CTX = 40;
|
|
698
|
+
const KEEP = 2000;
|
|
699
|
+
// the hash of no own text: every container (a row, a list, a wrapper div) has it, so a path that leads to one proves nothing by it alone
|
|
700
|
+
const EMPTY = textHash("");
|
|
701
|
+
/** The element's visible text, as an anchor keeps it, is `text` (paid; false when the budget cannot pay). */
|
|
702
|
+
function sameText(read, el, text) {
|
|
703
|
+
const raw = rawTextOf(read, el).slice(0, 2 * KEEP);
|
|
704
|
+
return spend(weigh(raw)) && normText(raw).slice(0, KEEP) === (text ?? "");
|
|
705
|
+
}
|
|
706
|
+
const HEAD_KEEP = 80;
|
|
707
|
+
/** The element a page anchor points at: the node itself when it is an element, else its parent; an implied one climbs to a located one. */
|
|
708
|
+
function elementOf(read, nodeId) {
|
|
709
|
+
let x = Number.isInteger(nodeId) ? read.nodes[nodeId] : null;
|
|
710
|
+
if (x && !isEl(x)) x = x.__par;
|
|
711
|
+
while (x && isEl(x) && !x.sourceCodeLocation) x = x.__par; // R8: an implied element anchors to its nearest located ancestor
|
|
712
|
+
return x && isEl(x) ? x : null;
|
|
713
|
+
}
|
|
714
|
+
/** The nearest heading at or before an element, by the units' order (`heading` of its unit, else of the last unit before it). */
|
|
715
|
+
function headingFor(read, el) {
|
|
716
|
+
let i = el.__unit;
|
|
717
|
+
if (i === undefined) {
|
|
718
|
+
// an element that is no unit: the last unit that starts at or before it in the visible text
|
|
719
|
+
let lo = 0;
|
|
720
|
+
let hi = read.units.length;
|
|
721
|
+
while (lo < hi) {
|
|
722
|
+
const m = (lo + hi) >> 1;
|
|
723
|
+
if ((read.units[m].node.__fs ?? 0) <= (el.__fs ?? 0)) lo = m + 1;
|
|
724
|
+
else hi = m;
|
|
725
|
+
}
|
|
726
|
+
i = lo - 1;
|
|
727
|
+
}
|
|
728
|
+
const h = i >= 0 ? read.units[i].heading : -1;
|
|
729
|
+
if (h < 0) return "";
|
|
730
|
+
const raw = rawTextOf(read, read.units[h].node).slice(0, 4 * HEAD_KEEP);
|
|
731
|
+
return spend(weigh(raw)) ? normText(raw).slice(0, HEAD_KEEP) : "";
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Where `q` occurs in the element's visible text, paid from the budget: at `hint` when it is there; else the occurrence whose context
|
|
735
|
+
* best matches `prefix` and `suffix` (normalized), the nearest to `hint` among equals. -1 when none (or the budget cannot pay).
|
|
736
|
+
*/
|
|
737
|
+
function placeIn(read, el, q, hint, prefix = "", suffix = "") {
|
|
738
|
+
if (!q) return -1;
|
|
739
|
+
const t = rawTextOf(read, el);
|
|
740
|
+
if (Number.isInteger(hint) && hint >= 0 && spend(q.length + 1) && t.startsWith(q, hint)) return hint;
|
|
741
|
+
if (!spend(searchCost(t.length, q))) return -1;
|
|
742
|
+
return bestOccurrence(t, q, 0, t.length, Number.isInteger(hint) ? hint : 0, prefix, suffix);
|
|
743
|
+
}
|
|
744
|
+
/** The occurrence of `q` in s[from, to) that best matches the context, then the nearest to `at`; each occurrence is paid for. */
|
|
745
|
+
function bestOccurrence(s, q, from, to, at, prefix, suffix) {
|
|
746
|
+
let best = -1;
|
|
747
|
+
let bestScore = -Infinity;
|
|
748
|
+
for (let i = s.indexOf(q, from), k = 0; i >= 0 && i + q.length <= to && k < 1000; i = s.indexOf(q, i + 1), k++) {
|
|
749
|
+
if (!spend(OCCURRENCE + 4 * CTX)) break;
|
|
750
|
+
const before = prefix ? normText(s.slice(Math.max(0, i - 2 * CTX), i)) : "";
|
|
751
|
+
const after = suffix ? normText(s.slice(i + q.length, i + q.length + 2 * CTX)) : "";
|
|
752
|
+
const score = (prefix && before.endsWith(prefix) ? 2 : 0) + (suffix && after.startsWith(suffix) ? 2 : 0) - Math.abs(i - at) / (s.length + 1);
|
|
753
|
+
if (score > bestScore) (best = i), (bestScore = score);
|
|
754
|
+
}
|
|
755
|
+
return best;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// ---- the page's offsets (web/src/html-view.js anchorFromRange) and this module's -----------------------------------------------
|
|
759
|
+
// The page counts UTF-16 code units of decoded source text as the frame holds it: in ONE source text node when `location.nodeId` is a
|
|
760
|
+
// text node, else in the joined text of the element's source text nodes the copy shows (src/html-render.js: not in template content,
|
|
761
|
+
// raw text or a non-HTML element, which the copy replaces). This module counts in `flat`: whitespace runs as one space, a leading space
|
|
762
|
+
// after a space dropped. These convert one to the other, exactly, so a selection's place survives the round trip (test/html-anchors.test.js).
|
|
763
|
+
|
|
764
|
+
/** The source text nodes under `el` the page counts (its elementText), in tree order. Iterative. */
|
|
765
|
+
function pageTexts(el) {
|
|
766
|
+
const out = [];
|
|
767
|
+
const stack = [...kids(el)].reverse();
|
|
768
|
+
while (stack.length) {
|
|
769
|
+
const x = stack.pop();
|
|
770
|
+
if (isText(x)) {
|
|
771
|
+
if (!x.__hidden) out.push(x);
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
if (!isEl(x) || !html(x) || x.tagName === "template") continue;
|
|
775
|
+
const ks = kids(x);
|
|
776
|
+
for (let i = ks.length - 1; i >= 0; i--) stack.push(ks[i]);
|
|
777
|
+
}
|
|
778
|
+
return out;
|
|
779
|
+
}
|
|
780
|
+
/** Was the first (collapsed) space of text node `x` dropped from `flat` (it followed a space)? */
|
|
781
|
+
const leadDropped = (read, x) => x.value.length > 0 && WS(x.value.charCodeAt(0)) && (x.__fs === undefined || read.flat[x.__fs] !== " ");
|
|
782
|
+
// characters of value[0, k) that `collapse` keeps: each non-space, and the first space of each run. Additive per character, so a table of
|
|
783
|
+
// counts every 4,096 code units makes one lookup cost at most 4,096 steps (a page of one 8-million-character text node, anchored 500
|
|
784
|
+
// times, test/stress-matrix.test.js). Kept per text node of a reading.
|
|
785
|
+
const WS = (c) => c === 32 || c === 9 || c === 10 || c === 12 || c === 13;
|
|
786
|
+
const STEP = 4096;
|
|
787
|
+
const KEPT = new WeakMap();
|
|
788
|
+
function keptCount(x, k) {
|
|
789
|
+
const v = x.value;
|
|
790
|
+
let table = KEPT.get(x);
|
|
791
|
+
if (!table) {
|
|
792
|
+
table = new Int32Array(Math.floor(v.length / STEP) + 1);
|
|
793
|
+
let n = 0;
|
|
794
|
+
for (let i = 0; i < v.length; i++) {
|
|
795
|
+
if (i % STEP === 0) table[i / STEP] = n;
|
|
796
|
+
const c = v.charCodeAt(i);
|
|
797
|
+
if (!WS(c) || i === 0 || !WS(v.charCodeAt(i - 1))) n++;
|
|
798
|
+
}
|
|
799
|
+
if (v.length % STEP === 0) table[v.length / STEP] = n;
|
|
800
|
+
KEPT.set(x, table);
|
|
801
|
+
}
|
|
802
|
+
const b = Math.floor(k / STEP);
|
|
803
|
+
let n = table[b];
|
|
804
|
+
for (let i = b * STEP; i < k; i++) {
|
|
805
|
+
const c = v.charCodeAt(i);
|
|
806
|
+
if (!WS(c) || i === 0 || !WS(v.charCodeAt(i - 1))) n++;
|
|
807
|
+
}
|
|
808
|
+
return n;
|
|
809
|
+
}
|
|
810
|
+
/** The `flat` offset of the page offset `k` in text node `x` (0 <= k <= value length). */
|
|
811
|
+
function flatOf(read, x, k) {
|
|
812
|
+
const base = x.__fs ?? null;
|
|
813
|
+
if (base === null) return null; // no text of it is in `flat` (whitespace only, after a space)
|
|
814
|
+
const n = keptCount(x, k) - (leadDropped(read, x) && k > 0 ? 1 : 0);
|
|
815
|
+
return base + Math.max(0, n);
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* The page offset in text node `x` of `flat` offset `f`: the first offset that maps at or past it (an end), and for a start the character
|
|
819
|
+
* that makes `f` (past the rest of a whitespace run, which makes nothing).
|
|
820
|
+
*/
|
|
821
|
+
function pageOf(read, x, f, start = false) {
|
|
822
|
+
const v = x.value;
|
|
823
|
+
let lo = 0;
|
|
824
|
+
let hi = v.length;
|
|
825
|
+
while (lo < hi) {
|
|
826
|
+
const m = (lo + hi) >> 1;
|
|
827
|
+
if (flatOf(read, x, m) < f) lo = m + 1;
|
|
828
|
+
else hi = m;
|
|
829
|
+
}
|
|
830
|
+
if (start) while (lo < v.length && flatOf(read, x, lo + 1) === flatOf(read, x, lo)) lo++;
|
|
831
|
+
return lo;
|
|
832
|
+
}
|
|
833
|
+
/**
|
|
834
|
+
* A page location -> where its first selected character sits in `rawTextOf(el)` (the hint htmlDurable gives placeIn), or null. `lead`:
|
|
835
|
+
* code units of whitespace the stored quote trimmed off its start.
|
|
836
|
+
*/
|
|
837
|
+
function hintOf(read, el, loc, lead = 0) {
|
|
838
|
+
if (!Number.isInteger(loc?.start)) return null;
|
|
839
|
+
const node = Number.isInteger(loc.nodeId) ? read.nodes[loc.nodeId] : null;
|
|
840
|
+
let x = null;
|
|
841
|
+
let k = loc.start + lead;
|
|
842
|
+
if (node && isText(node)) x = node;
|
|
843
|
+
else {
|
|
844
|
+
for (const t of pageTexts(el)) {
|
|
845
|
+
if (k <= t.value.length) {
|
|
846
|
+
x = t;
|
|
847
|
+
break;
|
|
848
|
+
}
|
|
849
|
+
k -= t.value.length;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
if (!x || k > x.value.length) return null;
|
|
853
|
+
const f = flatOf(read, x, k);
|
|
854
|
+
return f === null ? null : f - (el.__fs ?? 0);
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* The page location of [s, e) in `rawTextOf(el)`: {nodeId, start, end} in one source text node when both ends are in it, else in the
|
|
858
|
+
* element's joined text (page coordinates either way); null when the text is in no node the page counts.
|
|
859
|
+
*/
|
|
860
|
+
export function pageLocation(read, el, s, e) {
|
|
861
|
+
const fs = (el.__fs ?? 0) + s;
|
|
862
|
+
const fe = (el.__fs ?? 0) + e;
|
|
863
|
+
const list = pageTexts(el);
|
|
864
|
+
let before = 0;
|
|
865
|
+
let a = null;
|
|
866
|
+
let b = null;
|
|
867
|
+
for (const t of list) {
|
|
868
|
+
const ts = t.__fs;
|
|
869
|
+
const tl = ts === undefined ? 0 : keptCount(t, t.value.length) - (leadDropped(read, t) ? 1 : 0);
|
|
870
|
+
if (ts !== undefined && !a && fs < ts + tl && fs >= ts) a = { t, k: pageOf(read, t, fs, true), before };
|
|
871
|
+
if (ts !== undefined && fe <= ts + tl && fe > ts) {
|
|
872
|
+
b = { t, k: pageOf(read, t, fe), before };
|
|
873
|
+
break;
|
|
874
|
+
}
|
|
875
|
+
before += t.value.length;
|
|
876
|
+
}
|
|
877
|
+
if (!a || !b) return null;
|
|
878
|
+
if (a.t === b.t) return { nodeId: a.t.__id ?? null, start: a.k, end: b.k };
|
|
879
|
+
return { nodeId: el.__id ?? null, start: a.before + a.k, end: b.before + b.k };
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* The page's anchor ({location: {nodeId, start?, end?}, quote, prefix, suffix, cut?, hitKind?}) -> a durable anchor on these bytes, or
|
|
884
|
+
* null when the node is not in them. nodeId is the id of a located node in this version (`nodes`); a text node anchors to its element.
|
|
885
|
+
* The anchor: {format: "html", nodeId (a hint for this version only), path, tag, elementId, kind, textHash (of the element's own text),
|
|
886
|
+
* text (its visible text, normalized, 2,000 characters), quote, prefix, suffix (normalized, 40 characters of context), start, end (the
|
|
887
|
+
* quote in the element's visible text), heading, flatAt (where the element starts in the page's visible text, to search near)}.
|
|
888
|
+
* Everything is paid from the budget (src/text.js): when the element's text cannot be paid for, the anchor is weak (`weak: true`, the
|
|
889
|
+
* path and the page's context, no hash), and resolveHtml finds it again by its path.
|
|
890
|
+
*/
|
|
891
|
+
export function htmlDurable(read, anchor) {
|
|
892
|
+
const loc = anchor?.location;
|
|
893
|
+
const el = loc ? elementOf(read, loc.nodeId) : null;
|
|
894
|
+
if (!el) return null;
|
|
895
|
+
const path = spend(el.__depth + 1) ? elementPath(el) : null;
|
|
896
|
+
if (!path) return null;
|
|
897
|
+
const tag = safeTag(el.tagName);
|
|
898
|
+
const elementId = (() => {
|
|
899
|
+
const id = idOf(el);
|
|
900
|
+
return id !== null && read.byId.get(id) === el ? id : null;
|
|
901
|
+
})();
|
|
902
|
+
const base = { format: "html", nodeId: el.__id ?? null, path, tag, elementId, kind: el.__unit !== undefined ? read.units[el.__unit].kind : "element", flatAt: el.__fs ?? 0 };
|
|
903
|
+
const whole = anchor.cut === true || !!anchor.hitKind;
|
|
904
|
+
const excerpt = rawTextOf(read, el).slice(0, 2 * KEEP);
|
|
905
|
+
if (!spend(ownCost(el)) || !spend(weigh(excerpt))) {
|
|
906
|
+
// weak: the path and the page's context, cut, not normalized (nothing more can be paid for)
|
|
907
|
+
return { ...base, weak: true, textHash: null, text: "", quote: "", prefix: cutText(String(anchor.prefix ?? "").slice(-CTX), CTX), suffix: cutText(String(anchor.suffix ?? ""), CTX), start: null, end: null, heading: "" };
|
|
908
|
+
}
|
|
909
|
+
const q = whole ? "" : (normPaid(anchor.quote) ?? "");
|
|
910
|
+
const prefix = (normPaid(String(anchor.prefix ?? "").slice(-4 * CTX)) ?? "").slice(-CTX);
|
|
911
|
+
const suffix = (normPaid(String(anchor.suffix ?? "").slice(0, 4 * CTX)) ?? "").slice(0, CTX);
|
|
912
|
+
// the page's offsets (decoded source text) turned into this module's (whitespace folded): the exact place, not a hint that misses
|
|
913
|
+
const rawQuote = String(anchor.quote ?? "");
|
|
914
|
+
const lead = rawQuote.length - rawQuote.replace(/^[ \t\n\f\r]+/, "").length;
|
|
915
|
+
const at = placeIn(read, el, q, q ? hintOf(read, el, loc, lead) : null, prefix, suffix);
|
|
916
|
+
return {
|
|
917
|
+
...base,
|
|
918
|
+
textHash: ownHash(el),
|
|
919
|
+
text: normText(excerpt).slice(0, KEEP),
|
|
920
|
+
quote: q,
|
|
921
|
+
prefix,
|
|
922
|
+
suffix,
|
|
923
|
+
start: at >= 0 ? at : null,
|
|
924
|
+
end: at >= 0 ? at + q.length : null,
|
|
925
|
+
heading: headingFor(read, el),
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
/** The element at a durable path ([`tag:nth`], see elementPath), or null. Each step pays for the children it scans. */
|
|
930
|
+
export function byPath(read, path) {
|
|
931
|
+
let x = read.doc;
|
|
932
|
+
for (const seg of path) {
|
|
933
|
+
const m = /^(.*):(\d+)$/.exec(seg);
|
|
934
|
+
if (!m) return null;
|
|
935
|
+
const ks = kids(x);
|
|
936
|
+
if (!spend(ks.length + 1)) return null;
|
|
937
|
+
const want = Number(m[2]);
|
|
938
|
+
let next = null;
|
|
939
|
+
for (const c of ks) {
|
|
940
|
+
if (isEl(c) && c.__nth === want && safeTag(c.tagName) === m[1]) {
|
|
941
|
+
next = c;
|
|
942
|
+
break;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (!next) return null;
|
|
946
|
+
x = next;
|
|
947
|
+
}
|
|
948
|
+
return x === read.doc ? null : x;
|
|
949
|
+
}
|
|
950
|
+
/** The deepest element whose visible text holds [s, e): from the element that holds the text at `s`, up. */
|
|
951
|
+
function elementAt(read, s, e) {
|
|
952
|
+
// the text node that holds offset s: the last one starting at or before it, found by walking the units' elements is not enough (a
|
|
953
|
+
// text node of a non-unit), so the walk goes by the located nodes' visible-text starts
|
|
954
|
+
const ts = read.__textStarts ||= read.nodes.filter((x) => isText(x) && x.__fs !== undefined).sort((a, b) => a.__fs - b.__fs);
|
|
955
|
+
let lo = 0;
|
|
956
|
+
let hi = ts.length;
|
|
957
|
+
while (lo < hi) {
|
|
958
|
+
const m = (lo + hi) >> 1;
|
|
959
|
+
if (ts[m].__fs <= s) lo = m + 1;
|
|
960
|
+
else hi = m;
|
|
961
|
+
}
|
|
962
|
+
let x = lo > 0 ? ts[lo - 1].__par : null;
|
|
963
|
+
while (x && isEl(x) && !((x.__fs ?? 0) <= s && (x.__fe ?? 0) >= e)) x = x.__par;
|
|
964
|
+
while (x && isEl(x) && !x.sourceCodeLocation) x = x.__par;
|
|
965
|
+
return x && isEl(x) ? x : null;
|
|
966
|
+
}
|
|
967
|
+
const NEAR = 8192;
|
|
968
|
+
/**
|
|
969
|
+
* The occurrence of `q` in the page's visible text closest to `at` that best matches the context (prefix and suffix), searched near the
|
|
970
|
+
* old place first, then over the whole text. Paid from the budget: a search it cannot pay for finds nothing. -1 when none.
|
|
971
|
+
*/
|
|
972
|
+
function nearestQuote(read, q, at, prefix, suffix) {
|
|
973
|
+
const f = read.flat;
|
|
974
|
+
const holder = read.__hits ||= { text: f };
|
|
975
|
+
for (const [s, e] of [[Math.max(0, at - NEAR), Math.min(f.length, at + NEAR + q.length)], [0, f.length]]) {
|
|
976
|
+
const whole = s === 0 && e === f.length;
|
|
977
|
+
if (!spend(whole ? searchCost(f.length, q, hitsOf(HITS, holder, f, q)) : searchCost(e - s, q))) return -1;
|
|
978
|
+
const best = bestOccurrence(f, q, s, e, at, prefix, suffix);
|
|
979
|
+
if (best >= 0) return best;
|
|
980
|
+
if (whole) break;
|
|
981
|
+
}
|
|
982
|
+
return -1;
|
|
983
|
+
}
|
|
984
|
+
const HITS = new WeakMap();
|
|
985
|
+
/** `x` or its nearest located ancestor whose (safe) tag is `tag`, or null. */
|
|
986
|
+
function withTag(x, tag) {
|
|
987
|
+
for (let y = x; y && isEl(y); y = y.__par) if (safeTag(y.tagName) === tag && y.sourceCodeLocation) return y;
|
|
988
|
+
return null;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
/**
|
|
992
|
+
* Re-resolve a durable anchor against a (newer) reading. Order (3.4): the unique id attribute; the same path with the same own-text hash
|
|
993
|
+
* (a weak anchor: the same path and tag); the quote (or the start of the element's text) searched near its old place, then everywhere,
|
|
994
|
+
* with its context; all within the budget. Result: {method: "id" | "path" | "quote" | "none", nodeId, path, tag, elementId, start, end,
|
|
995
|
+
* changed (its own text differs), quoteLost, text (200 characters), heading}.
|
|
996
|
+
*/
|
|
997
|
+
export function resolveHtml(read, d) {
|
|
998
|
+
if (!d || d.format !== "html") return { method: "none" };
|
|
999
|
+
let el = null;
|
|
1000
|
+
let method = "none";
|
|
1001
|
+
if (d.elementId && spend(1)) {
|
|
1002
|
+
const x = read.byId.get(d.elementId) || null;
|
|
1003
|
+
if (x) [el, method] = [x, "id"];
|
|
1004
|
+
}
|
|
1005
|
+
if (!el && Array.isArray(d.path)) {
|
|
1006
|
+
const x = byPath(read, d.path);
|
|
1007
|
+
if (x && (d.weak ? safeTag(x.tagName) === d.tag : spend(ownCost(x)) && ownHash(x) === d.textHash && (d.textHash !== EMPTY || sameText(read, x, d.text)))) [el, method] = [x, "path"];
|
|
1008
|
+
}
|
|
1009
|
+
if (!el && !d.weak) {
|
|
1010
|
+
const needle = d.quote || (d.text || "").slice(0, 60);
|
|
1011
|
+
const old = (Number(d.flatAt) || 0) + (d.quote && Number.isInteger(d.start) ? d.start : 0); // where it was in the visible text
|
|
1012
|
+
const at = needle ? nearestQuote(read, needle, old, d.quote ? d.prefix : "", d.quote ? d.suffix : "") : -1;
|
|
1013
|
+
if (at >= 0) {
|
|
1014
|
+
let x = elementAt(read, at, at + needle.length);
|
|
1015
|
+
if (!d.quote) x = withTag(x, d.tag) || x; // an element anchor (no quote) lands on the element of its own tag that holds the text
|
|
1016
|
+
if (x) [el, method] = [x, "quote"];
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
while (el && isEl(el) && !el.sourceCodeLocation) el = el.__par; // an implied element at the path: its located ancestor (it has an id)
|
|
1020
|
+
if (!el || !isEl(el)) return { method: "none" };
|
|
1021
|
+
const q = d.quote || "";
|
|
1022
|
+
const at = q ? placeIn(read, el, q, Number.isInteger(d.start) ? d.start : null, d.prefix, d.suffix) : -1;
|
|
1023
|
+
const text = rawTextOf(read, el).slice(0, 800);
|
|
1024
|
+
return {
|
|
1025
|
+
method,
|
|
1026
|
+
nodeId: el.__id ?? null,
|
|
1027
|
+
// where the page draws it, in its own offsets (pageLocation): the quote's exact characters, or the whole element
|
|
1028
|
+
page: at >= 0 ? pageLocation(read, el, at, at + q.length) || { nodeId: el.__id ?? null } : { nodeId: el.__id ?? null },
|
|
1029
|
+
path: pathString(elementPath(el)),
|
|
1030
|
+
tag: safeTag(el.tagName),
|
|
1031
|
+
elementId: d.elementId && read.byId.get(d.elementId) === el ? d.elementId : null,
|
|
1032
|
+
start: at >= 0 ? at : null,
|
|
1033
|
+
end: at >= 0 ? at + q.length : null,
|
|
1034
|
+
changed: !(spend(ownCost(el)) && ownHash(el) === d.textHash),
|
|
1035
|
+
quoteLost: !!q && at < 0,
|
|
1036
|
+
text: spend(weigh(text)) ? normText(text).slice(0, 200) : "",
|
|
1037
|
+
heading: headingFor(read, el),
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
/** Locator fields for the batch (the names an op's target will use): the path an agent reads, the safe id, the text hash, the quote's place. */
|
|
1042
|
+
export const htmlLocator = (d) =>
|
|
1043
|
+
d ? { path: pathString(d.path), element_id: typeof d.elementId === "string" && SAFE_NAME.test(d.elementId) ? d.elementId : null, text_hash: d.textHash ?? null, start: d.start ?? null, end: d.end ?? null } : null;
|
|
1044
|
+
|
|
1045
|
+
// ---- splices and the proof ------------------------------------------------------------------------------------------------------------
|
|
1046
|
+
/**
|
|
1047
|
+
* The proof of a splice (S2's lockstep proof, recommended over hashing both trees): `afterBytes` decode the same way (UTF-8, the same BOM),
|
|
1048
|
+
* parse within the limits, and their tree equals the old one everywhere outside the edited place: the same nodes along the path to the
|
|
1049
|
+
* edited parent, every subtree off that path equal, and the edited parent's children equal to the expected list (the old ones before `i`,
|
|
1050
|
+
* `make()`'s nodes, the old ones from `j`; adjacent text merged, empty text dropped). `edit` = {parentPath, i, j, make} (see editText,
|
|
1051
|
+
* editAttributes, editChildren). Holds at most two trees: the old reading's and the new parse. Returns {ok: true, parsed} (pass `parsed`
|
|
1052
|
+
* to readParsed for the new reading, never a third parse) or {ok: false, code}: "decode", "bom", "limits", "ancestor_changed",
|
|
1053
|
+
* "sibling_changed", "parent_changed", "children_count", "edited_place_differs".
|
|
1054
|
+
*/
|
|
1055
|
+
export function proveSplice(before, afterBytes, edit) {
|
|
1056
|
+
const b = Buffer.isBuffer(afterBytes) ? afterBytes : Buffer.from(afterBytes.buffer, afterBytes.byteOffset, afterBytes.byteLength);
|
|
1057
|
+
if (b.length > before.limits.bytes) return { ok: false, code: "limits", why: capBytes(b.length, before.limits.bytes).message };
|
|
1058
|
+
const dec = decodeHtml(b);
|
|
1059
|
+
if (!dec.ok) return { ok: false, code: "decode" };
|
|
1060
|
+
if (dec.bomLen !== before.bomLen) return { ok: false, code: "bom" };
|
|
1061
|
+
let parsed;
|
|
1062
|
+
try {
|
|
1063
|
+
parsed = parseLimited(dec.text, before.limits);
|
|
1064
|
+
} catch (err) {
|
|
1065
|
+
if (err instanceof CapError) return { ok: false, code: "limits", why: err.message };
|
|
1066
|
+
throw err;
|
|
1067
|
+
}
|
|
1068
|
+
let a = before.doc;
|
|
1069
|
+
let y = parsed.doc;
|
|
1070
|
+
const { parentPath, i, j } = edit;
|
|
1071
|
+
for (let depth = 0; depth < parentPath.length; depth++) {
|
|
1072
|
+
if (!shallowSame(a, y)) return { ok: false, code: "ancestor_changed" };
|
|
1073
|
+
const ak = kids(a);
|
|
1074
|
+
const bk = kids(y);
|
|
1075
|
+
if (ak.length !== bk.length) return { ok: false, code: "children_count" };
|
|
1076
|
+
const at = parentPath[depth];
|
|
1077
|
+
for (let k = 0; k < ak.length; k++) if (k !== at && !deepEqual(ak[k], bk[k])) return { ok: false, code: "sibling_changed" };
|
|
1078
|
+
a = ak[at];
|
|
1079
|
+
y = bk[at];
|
|
1080
|
+
if (!a || !y) return { ok: false, code: "ancestor_changed" };
|
|
1081
|
+
}
|
|
1082
|
+
if (!shallowSame(a, y)) return { ok: false, code: "parent_changed" };
|
|
1083
|
+
const old = kids(a);
|
|
1084
|
+
const want = [];
|
|
1085
|
+
for (const x of [...old.slice(0, i), ...edit.make(), ...old.slice(j)]) {
|
|
1086
|
+
if (isText(x)) {
|
|
1087
|
+
if (x.value === "") continue;
|
|
1088
|
+
const p = want[want.length - 1];
|
|
1089
|
+
if (p && isText(p)) {
|
|
1090
|
+
want[want.length - 1] = { nodeName: "#text", value: p.value + x.value };
|
|
1091
|
+
continue;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
want.push(x);
|
|
1095
|
+
}
|
|
1096
|
+
const got = kids(y);
|
|
1097
|
+
if (got.length !== want.length) return { ok: false, code: "children_count" };
|
|
1098
|
+
for (let k = 0; k < got.length; k++) if (!deepEqual(want[k], got[k])) return { ok: false, code: "edited_place_differs" };
|
|
1099
|
+
return { ok: true, parsed: { bytes: b, text: dec.text, bomLen: dec.bomLen, doc: parsed.doc, dupOffsets: parsed.dupOffsets, limits: before.limits } };
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
/** The edit of replacing one text node's value (`newText` "" removes it). */
|
|
1103
|
+
export const editText = (node, newText) => ({ parentPath: pathOf(node.__par), i: node.__idx, j: node.__idx + 1, make: () => (newText === "" ? [] : [{ nodeName: "#text", value: newText }]) });
|
|
1104
|
+
/** The edit of giving an element a new attribute list (its children unchanged). */
|
|
1105
|
+
export const editAttributes = (el, attrs) => ({ parentPath: pathOf(el.__par), i: el.__idx, j: el.__idx + 1, make: () => [{ ...el, attrs: attrs.map((a) => ({ ...a })) }] });
|
|
1106
|
+
/** The edit of replacing children [i, j) of `parent` with the nodes `make()` returns (a fragment parsed in the parent's context). */
|
|
1107
|
+
export const editChildren = (parent, i, j, make) => ({ parentPath: pathOf(parent), i, j, make });
|
|
1108
|
+
|
|
1109
|
+
/** `bytes` with [s, e) of the decoded text (UTF-16 offsets) replaced by `repl`, through the one byte map. */
|
|
1110
|
+
export function spliceBytes(read, s, e, repl) {
|
|
1111
|
+
const b0 = read.toByte(s);
|
|
1112
|
+
const b1 = read.toByte(e);
|
|
1113
|
+
return Buffer.concat([read.bytes.subarray(0, b0), Buffer.from(repl, "utf8"), read.bytes.subarray(b1)]);
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
/**
|
|
1117
|
+
* The person's text edit (R11: the edit unit is one source text node): replace the text node `nodeId` with `newText`, escaped for its
|
|
1118
|
+
* context, then prove the result. Returns {ok: true, bytes, parsed} or {ok: false, code}: "no_such_node", "not_text", "whitespace_only",
|
|
1119
|
+
* "frozen" (raw text), "not_addressable" (R8), "nul", "lone_surrogate", "would_change_structure" (the proof refused), "limits".
|
|
1120
|
+
*/
|
|
1121
|
+
export function spliceText(read, nodeId, newText) {
|
|
1122
|
+
const x = read.nodes[nodeId];
|
|
1123
|
+
if (!x) return { ok: false, code: "no_such_node" };
|
|
1124
|
+
if (!isText(x)) return { ok: false, code: "not_text" };
|
|
1125
|
+
const context = textContext(x);
|
|
1126
|
+
if (context === "raw" || x.__hidden) return { ok: false, code: "frozen" };
|
|
1127
|
+
if (!/\S/.test(x.value)) return { ok: false, code: "whitespace_only" };
|
|
1128
|
+
if (x.__ok !== true) return { ok: false, code: "not_addressable" };
|
|
1129
|
+
newText = String(newText);
|
|
1130
|
+
const [s, e] = textRange(x, read.text);
|
|
1131
|
+
const pst = x.__ctx?.sourceCodeLocation?.startTag;
|
|
1132
|
+
const esc = escapeText(newText, context, { atStartOfLeadingLf: leadingLfParent(x) && !!pst && s === pst.endOffset });
|
|
1133
|
+
if (!esc.ok) return { ok: false, code: esc.code };
|
|
1134
|
+
const bytes = spliceBytes(read, s, e, esc.out);
|
|
1135
|
+
const p = proveSplice(read, bytes, editText(x, newText));
|
|
1136
|
+
if (!p.ok) return { ok: false, code: p.code === "limits" ? "limits" : "would_change_structure" };
|
|
1137
|
+
return { ok: true, bytes, parsed: p.parsed };
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// ---- many edits at once, and whole files (Phase 1c: src/ops/html.js, src/html-write.js) ----------------------------------------------
|
|
1141
|
+
/** The deepest element nesting of a tree, iterative. */
|
|
1142
|
+
function depthOf(doc) {
|
|
1143
|
+
let max = 0;
|
|
1144
|
+
const st = [[doc, 0]];
|
|
1145
|
+
while (st.length) {
|
|
1146
|
+
const [x, d] = st.pop();
|
|
1147
|
+
if (d > max) max = d;
|
|
1148
|
+
for (const c of kids(x)) if (isEl(c)) st.push([c, d + 1]);
|
|
1149
|
+
}
|
|
1150
|
+
return max;
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
/**
|
|
1154
|
+
* Bytes as an HTML file, without the index a reading builds: size, decoding, one parse with source locations within the limits, depth.
|
|
1155
|
+
* For a whole new version (a commit) that has no edit to prove. Returns {ok: true, bytes, text, bomLen, declared, doc, dupOffsets,
|
|
1156
|
+
* limits} or {ok: false, code, why}: code "limits" (why: the limit in plain words) or the decoding's code (utf16, invalid_utf8,
|
|
1157
|
+
* roundtrip, not_utf8).
|
|
1158
|
+
*/
|
|
1159
|
+
export function parseChecked(bytes, limits = HTML_LIMITS) {
|
|
1160
|
+
const b = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1161
|
+
if (b.length > limits.bytes) return { ok: false, code: "limits", why: capBytes(b.length, limits.bytes).message };
|
|
1162
|
+
const d = decodeHtml(b);
|
|
1163
|
+
if (!d.ok) return { ok: false, code: d.code, why: d.sentence };
|
|
1164
|
+
let parsed;
|
|
1165
|
+
try {
|
|
1166
|
+
parsed = parseLimited(d.text, limits);
|
|
1167
|
+
} catch (err) {
|
|
1168
|
+
if (err instanceof CapError) return { ok: false, code: "limits", why: err.message };
|
|
1169
|
+
throw err;
|
|
1170
|
+
}
|
|
1171
|
+
if (depthOf(parsed.doc) > limits.depth) return { ok: false, code: "limits", why: capDepth(limits.depth).message };
|
|
1172
|
+
return { ok: true, bytes: b, text: d.text, bomLen: d.bomLen, declared: d.declared, doc: parsed.doc, dupOffsets: parsed.dupOffsets, limits };
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
/** A fragment parsed in the context of element `ctx` as the page parses (scripting on): its nodes, and whether serializing them and parsing that again gives the same nodes. */
|
|
1176
|
+
export function parseFragmentIn(ctx, markup) {
|
|
1177
|
+
const p5 = parser();
|
|
1178
|
+
const f = p5.parseFragment(ctx, markup, { scriptingEnabled: true });
|
|
1179
|
+
const again = p5.parseFragment(ctx, p5.serialize(f), { scriptingEnabled: true }).childNodes;
|
|
1180
|
+
const nodes = f.childNodes;
|
|
1181
|
+
return { nodes, roundTrips: nodes.length === again.length && nodes.every((x, i) => deepEqual(x, again[i])) };
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
/**
|
|
1185
|
+
* The lockstep proof of many edits at once (the composite form of proveSplice, so a batch of ops costs one parse, not one per op: one
|
|
1186
|
+
* parse with its proof took about 1.5 s of worker CPU at the limits). `plan`:
|
|
1187
|
+
* texts: Map(text node -> its new value; "" removes it)
|
|
1188
|
+
* attrs: Map(element -> its new attribute list; its children compared as usual)
|
|
1189
|
+
* children: Map(parent -> [{i, j, make}] sorted by i, an insertion (i === j) before a range starting at the same i, then op order):
|
|
1190
|
+
* children [i, j) of the parent replaced by make()'s nodes; ranges do not overlap.
|
|
1191
|
+
* Every node not named compares equal to the old one (names, namespaces, attributes in order, values, comments, the doctype); each edited
|
|
1192
|
+
* parent's children equal the expected list (adjacent text merged, empty text dropped). Holds at most two trees. Returns {ok: true,
|
|
1193
|
+
* parsed} or {ok: false, code}: "limits", "decode", "bom", "node_changed", "children_count", "edited_place_differs".
|
|
1194
|
+
*/
|
|
1195
|
+
export function proveEdits(before, afterBytes, plan) {
|
|
1196
|
+
const b = Buffer.isBuffer(afterBytes) ? afterBytes : Buffer.from(afterBytes.buffer, afterBytes.byteOffset, afterBytes.byteLength);
|
|
1197
|
+
const p = parseChecked(b, before.limits);
|
|
1198
|
+
if (!p.ok) return p.code === "limits" ? { ok: false, code: "limits", why: p.why } : { ok: false, code: "decode" };
|
|
1199
|
+
if (p.bomLen !== before.bomLen) return { ok: false, code: "bom" };
|
|
1200
|
+
const T = plan.texts || new Map();
|
|
1201
|
+
const A = plan.attrs || new Map();
|
|
1202
|
+
const C = plan.children || new Map();
|
|
1203
|
+
const edited = new Set(C.keys());
|
|
1204
|
+
for (const t of T.keys()) edited.add(t.__par);
|
|
1205
|
+
const stack = [[before.doc, p.doc]];
|
|
1206
|
+
while (stack.length) {
|
|
1207
|
+
const [a, y] = stack.pop();
|
|
1208
|
+
if (a.nodeName !== y.nodeName) return { ok: false, code: "node_changed" };
|
|
1209
|
+
if (isEl(a)) {
|
|
1210
|
+
const want = A.get(a) || a.attrs;
|
|
1211
|
+
if (a.tagName !== y.tagName || a.namespaceURI !== y.namespaceURI || want.length !== y.attrs.length) return { ok: false, code: "node_changed" };
|
|
1212
|
+
for (let k = 0; k < want.length; k++) {
|
|
1213
|
+
const u = want[k];
|
|
1214
|
+
const v = y.attrs[k];
|
|
1215
|
+
if (u.name !== v.name || u.value !== v.value || (u.namespace || "") !== (v.namespace || "") || (u.prefix || "") !== (v.prefix || "")) return { ok: false, code: "node_changed" };
|
|
1216
|
+
}
|
|
1217
|
+
} else if (!shallowSame(a, y) || (a.nodeName === "#documentType" && (a.name !== y.name || a.publicId !== y.publicId || a.systemId !== y.systemId))) return { ok: false, code: "node_changed" };
|
|
1218
|
+
const ak = kids(a);
|
|
1219
|
+
const yk = kids(y);
|
|
1220
|
+
if (!edited.has(a)) {
|
|
1221
|
+
if (ak.length !== yk.length) return { ok: false, code: "children_count" };
|
|
1222
|
+
for (let k = 0; k < ak.length; k++) {
|
|
1223
|
+
if (isText(ak[k])) {
|
|
1224
|
+
if (!isText(yk[k]) || ak[k].value !== yk[k].value) return { ok: false, code: "node_changed" };
|
|
1225
|
+
} else stack.push([ak[k], yk[k]]);
|
|
1226
|
+
}
|
|
1227
|
+
continue;
|
|
1228
|
+
}
|
|
1229
|
+
// the expected children of an edited parent: {text} (merged, possibly edited), {orig} (compared in lockstep), {made} (compared whole)
|
|
1230
|
+
const want = [];
|
|
1231
|
+
const putText = (v) => {
|
|
1232
|
+
if (v === "") return;
|
|
1233
|
+
const last = want[want.length - 1];
|
|
1234
|
+
if (last && last.text !== undefined) last.text += v;
|
|
1235
|
+
else want.push({ text: v });
|
|
1236
|
+
};
|
|
1237
|
+
const putOld = (x) => (isText(x) ? putText(T.has(x) ? T.get(x) : x.value) : want.push({ orig: x }));
|
|
1238
|
+
const putMade = (x) => (isText(x) ? putText(x.value) : want.push({ made: x }));
|
|
1239
|
+
let k = 0;
|
|
1240
|
+
for (const e of C.get(a) || []) {
|
|
1241
|
+
while (k < e.i) putOld(ak[k++]);
|
|
1242
|
+
for (const x of e.make()) putMade(x);
|
|
1243
|
+
k = Math.max(k, e.j);
|
|
1244
|
+
}
|
|
1245
|
+
while (k < ak.length) putOld(ak[k++]);
|
|
1246
|
+
if (want.length !== yk.length) return { ok: false, code: "children_count" };
|
|
1247
|
+
for (let q = 0; q < want.length; q++) {
|
|
1248
|
+
const w = want[q];
|
|
1249
|
+
const g = yk[q];
|
|
1250
|
+
if (w.text !== undefined) {
|
|
1251
|
+
if (!isText(g) || g.value !== w.text) return { ok: false, code: "edited_place_differs" };
|
|
1252
|
+
} else if (w.orig) stack.push([w.orig, g]);
|
|
1253
|
+
else if (!deepEqual(w.made, g)) return { ok: false, code: "edited_place_differs" };
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
return { ok: true, parsed: { bytes: b, text: p.text, bomLen: p.bomLen, doc: p.doc, dupOffsets: p.dupOffsets, limits: before.limits } };
|
|
1257
|
+
}
|