onionskin-cli 0.0.3 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/INSTALL.md +5 -1
  3. package/NOTICE +34 -0
  4. package/README.md +102 -34
  5. package/docs/contracts/batch.schema.json +27 -4
  6. package/docs/contracts/ops.schema.json +117 -5
  7. package/package.json +4 -2
  8. package/scripts/checker/check.py +24 -5
  9. package/scripts/checker/requirements.txt +482 -5
  10. package/scripts/requirements.txt +482 -5
  11. package/scripts/rewriter/apple/main.swift +1 -1
  12. package/scripts/rewriter/apple/onionskin-fm +0 -0
  13. package/scripts/rewriter/apple/onionskin-fm.json +1 -1
  14. package/skills/onionskin/SKILL.md +12 -9
  15. package/skills/onionskin/reference/batch.md +5 -3
  16. package/skills/onionskin/reference/formats.md +5 -1
  17. package/skills/onionskin/reference/ops.md +48 -4
  18. package/skills/onionskin/reference/troubleshooting.md +31 -0
  19. package/src/agent-detect.js +28 -2
  20. package/src/agent-prompt.js +12 -1
  21. package/src/agent-runner.js +110 -20
  22. package/src/anchor-work.js +154 -0
  23. package/src/anchor-worker.js +38 -0
  24. package/src/anchors.js +61 -11
  25. package/src/apply.js +119 -47
  26. package/src/batch.js +32 -8
  27. package/src/check-worker.js +8 -5
  28. package/src/checker.js +238 -33
  29. package/src/cli.js +248 -42
  30. package/src/constants.js +34 -1
  31. package/src/diff.js +3 -1
  32. package/src/doctor.js +12 -3
  33. package/src/docx.js +464 -87
  34. package/src/edit-hash.js +19 -0
  35. package/src/fsutil.js +14 -6
  36. package/src/history-worker.js +8 -8
  37. package/src/history.js +144 -31
  38. package/src/html-active.js +384 -0
  39. package/src/html-envelope.js +51 -0
  40. package/src/html-parser.js +8 -0
  41. package/src/html-render-worker.js +25 -0
  42. package/src/html-render.js +939 -0
  43. package/src/html-utf8.js +65 -0
  44. package/src/html-write-worker.js +14 -0
  45. package/src/html-write.js +380 -0
  46. package/src/html.js +1259 -0
  47. package/src/local.js +68 -4
  48. package/src/merge-package.js +1166 -0
  49. package/src/merge-pptx.js +98 -38
  50. package/src/merge-proof.js +1019 -0
  51. package/src/merge-worker.js +60 -0
  52. package/src/merge-xlsx.js +84 -29
  53. package/src/merge.js +286 -83
  54. package/src/nfc.js +46 -0
  55. package/src/notes.js +122 -0
  56. package/src/offload.js +419 -1
  57. package/src/open-worker.js +27 -0
  58. package/src/ops/common.js +2 -1
  59. package/src/ops/docx-table.js +5 -3
  60. package/src/ops/docx.js +58 -29
  61. package/src/ops/html.js +394 -0
  62. package/src/ops/pptx.js +82 -23
  63. package/src/ops/run.js +23 -3
  64. package/src/ops/runs.js +215 -13
  65. package/src/ops/schema.js +8 -1
  66. package/src/ops/xlsx.js +121 -10
  67. package/src/ops-worker.js +17 -0
  68. package/src/package.js +43 -4
  69. package/src/person.js +2 -1
  70. package/src/pptx.js +323 -23
  71. package/src/question.js +10 -3
  72. package/src/quick-plan.js +13 -6
  73. package/src/quick.js +1 -1
  74. package/src/redline-check.js +12 -2
  75. package/src/redline-worker.js +10 -14
  76. package/src/reply.js +46 -19
  77. package/src/retain.js +223 -0
  78. package/src/review.js +32 -18
  79. package/src/rewrite-check.js +2 -2
  80. package/src/rewriter.js +38 -8
  81. package/src/sanitize.js +16 -3
  82. package/src/server-entry.js +4 -3
  83. package/src/server.js +175 -62
  84. package/src/session.js +1151 -210
  85. package/src/setup.js +20 -2
  86. package/src/stop-gate.js +34 -0
  87. package/src/suggest-reply.js +42 -7
  88. package/src/suggest.js +14 -29
  89. package/src/text.js +161 -11
  90. package/src/update.js +517 -0
  91. package/src/voice-guide.js +15 -9
  92. package/src/voice-learn.js +15 -4
  93. package/src/voice-sample-worker.js +7 -6
  94. package/src/voice-store.js +11 -4
  95. package/src/voice-worker.js +4 -6
  96. package/src/voice.js +2 -1
  97. package/src/worker-host.js +23 -0
  98. package/src/xlsx.js +378 -97
  99. package/src/xml-tree.js +6 -3
  100. package/src/xml.js +82 -34
  101. package/src/zip-write.js +10 -5
  102. package/src/zip.js +73 -18
  103. package/vendor/parse5/LICENSE.txt +32 -0
  104. package/vendor/parse5/parse5.mjs +7822 -0
  105. package/vendor/parse5.lock.json +23 -0
  106. package/web/dist/assets/{index-DS-WST1E.js → index-CjsTf5gj.js} +58 -52
  107. package/web/dist/assets/index-DnbLtHhG.css +1 -0
  108. package/web/dist/index.html +2 -2
  109. package/web/dist/assets/index-BCAEKiOG.css +0 -1
@@ -0,0 +1,939 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { cssPlain, DISPLAY_CONTROLS, DISPLAY_INPUT_TYPES, displayAttributeAllowed, displayElementAllowed, HTML_NS, URL_KINDS, urlKind } from "./html-active.js";
5
+ import { editHosts, escapeAttr, escapeText, htmlParser, kids } from "./html.js";
6
+
7
+ // The render copy (PLAN-html 3.2 as changed by R2 to R5, R12, R16, R20 to R22): one version of a person's HTML file turned into a page the
8
+ // Onionskin page shows in `<iframe sandbox="allow-same-origin" srcdoc>`. It is written by the serializer below from the parsed tree, never
9
+ // by passing the file's bytes through: an element or attribute is written only when the display set (src/html-active.js) names it, every
10
+ // text and attribute value is escaped here, and everything else is left out and counted in `stats`. The copy holds no address a browser
11
+ // could load or follow: no `href` (a link keeps its target as `data-osk-href`, for display), no `src` but a `data:` image this module made
12
+ // from a local image or a `data:` image it checked, no `url(` in CSS but such an image, and no `link`, `meta` (but the CSP meta it puts
13
+ // first in `head`), `base`, `form`, frame, `object`, `embed`, `script`, `noscript` or `template` element. Then the copy is parsed again and
14
+ // must give the tree it was written from (R2); if not, the caller shows the plain text view (plainCopy).
15
+ // Runs in a worker only (src/html-render-worker.js): the parser is there. Every walker is iterative.
16
+
17
+ /** The frame's own policy (3.2): the first child of `head` in every copy. */
18
+ export const RENDER_CSP = "default-src 'none'; img-src data:; style-src 'unsafe-inline'; font-src data:; media-src data:";
19
+
20
+ /**
21
+ * Caps (3.2, R5, R21). Images: one at most 10 MB, at most 500 shown and 30 MB in all as the copy holds them (base64; each place an
22
+ * image is shown counts, since each holds its own copy of the bytes); a local image at most 3 folders below the file, at most 16,000 px a side and 64 million pixels.
23
+ * Stylesheets: a local one at most 2 MB, `@import` at most 5 levels deep, and the page's styles in all at most 2 MB, 20,000 rules and
24
+ * 200 `:has(`, else every style of the page is left out.
25
+ */
26
+ export const RENDER_LIMITS = { asset: 10 * 1024 * 1024, assets: 500, total: 30 * 1024 * 1024, folderDepth: 3, side: 16_000, pixels: 64_000_000, sheet: 2 * 1024 * 1024, importDepth: 5, cssBytes: 2 * 1024 * 1024, cssRules: 20_000, cssHas: 200 };
27
+
28
+ /** Why an image or a stylesheet was not shown: fixed codes, never any of the file's content. */
29
+ export const ASSET_CODES = ["remote", "folder", "absolute", "outside", "too_deep", "hidden", "library", "bad_url", "not_found", "symlink", "not_regular", "empty", "too_large", "changed", "unreadable", "type", "dimensions", "over_count", "over_total", "repeat", "not_text", "css_cap"];
30
+
31
+ const VOID = new Set(["area", "base", "basefont", "bgsound", "br", "col", "embed", "frame", "hr", "image", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]);
32
+ const FRAMES = new Set(["iframe", "frame", "frameset", "object", "embed", "applet", "portal", "fencedframe"]);
33
+ const MEDIA = new Set(["video", "audio", "canvas"]);
34
+ /** Left out with everything in them, counted as "dropped". */
35
+ const DROP = new Set(["meta", "base", "noembed", "noframes", "param", "source", "track", "area", "keygen", "basefont", "bgsound", "slot", "image"]);
36
+ /** Shown under another, parse-equivalent name: raw-text and legacy elements as `pre`, `dir` as `ul`. */
37
+ const RENAME = { xmp: "pre", plaintext: "pre", listing: "pre", dir: "ul" };
38
+ /** Where a `div` standing for a `form` would be moved out by the parser (a `form` there is empty: the parser closes it at once). */
39
+ const TABLE_CONTEXT = new Set(["table", "tbody", "thead", "tfoot", "tr"]);
40
+ const LEADING_LF = new Set(["pre", "textarea", "listing"]);
41
+ const RCDATA = new Set(["title", "textarea"]);
42
+ const PLACEHOLDER_STYLE = "display:inline-block;border:1px dashed #888;padding:1px 4px;font:12px/1.4 sans-serif;color:#666;background:#f4f4f4";
43
+ /** The fixed labels of the placeholders, by kind. */
44
+ export const PLACEHOLDER_LABELS = { image: "[image not shown]", media: "[media not shown]", graphic: "[graphic not shown]" };
45
+
46
+ // ---- the serializer -----------------------------------------------------------------------------------------------------------------
47
+ // One set of functions writes the copy and, from the tree of the copy parsed again, the same text again; the two must be equal (R2).
48
+ // Adjacent text nodes are kept apart by an empty comment, so text on either side of something left out stays two nodes, as in the file
49
+ // (the page maps the n-th text node of the frame to `texts[n]`).
50
+
51
+ class Writer {
52
+ constructor() {
53
+ this.out = ["<!DOCTYPE html>"];
54
+ this.stack = [{ tag: "#document", lastText: false, first: true }];
55
+ this.texts = [];
56
+ this.failed = null;
57
+ this.bodyText = 0;
58
+ this.inBody = 0;
59
+ }
60
+ open(tag, attrs) {
61
+ const top = this.stack[this.stack.length - 1];
62
+ top.lastText = false;
63
+ top.first = false;
64
+ let s = `<${tag}`;
65
+ for (const [name, value] of attrs) s += ` ${name}=${attrValue(value)}`;
66
+ this.out.push(`${s}>`);
67
+ if (VOID.has(tag)) return false;
68
+ this.stack.push({ tag, lastText: false, first: true });
69
+ if (tag === "body") this.inBody++;
70
+ return true;
71
+ }
72
+ close() {
73
+ const f = this.stack.pop();
74
+ if (f.tag === "body") this.inBody--;
75
+ this.out.push(`</${f.tag}>`);
76
+ }
77
+ text(value, id, visible = true) {
78
+ if (!value) return;
79
+ const top = this.stack[this.stack.length - 1];
80
+ if (top.lastText) this.out.push("<!---->");
81
+ let s;
82
+ if (top.tag === "style") {
83
+ if (/<\/style/i.test(value)) return (this.failed ??= "style_end");
84
+ s = value;
85
+ } else {
86
+ const e = escapeText(value, RCDATA.has(top.tag) ? "rcdata" : "normal", { atStartOfLeadingLf: top.first && LEADING_LF.has(top.tag) });
87
+ if (!e.ok) return (this.failed ??= e.code);
88
+ s = e.out;
89
+ }
90
+ this.out.push(s);
91
+ top.lastText = true;
92
+ top.first = false;
93
+ this.texts.push(id ?? -1);
94
+ if (visible && this.inBody && top.tag !== "style") this.bodyText += value.replace(/\s+/g, "").length;
95
+ }
96
+ comment(data = "") {
97
+ const top = this.stack[this.stack.length - 1];
98
+ top.lastText = false;
99
+ top.first = false;
100
+ this.out.push(`<!--${data}-->`);
101
+ }
102
+ }
103
+
104
+ function attrValue(v) {
105
+ const e = escapeAttr(String(v), '"');
106
+ return e.ok ? e.out : '""';
107
+ }
108
+
109
+ /** The text of a parsed copy, written by the same Writer: equal to the copy when the parse gave the tree the copy was written from. */
110
+ function serializeTree(doc) {
111
+ const w = new Writer();
112
+ const stack = [];
113
+ const push = (list) => {
114
+ for (let i = list.length - 1; i >= 0; i--) stack.push(list[i]);
115
+ };
116
+ push(kids(doc).filter((n) => n.nodeName !== "#documentType"));
117
+ for (const n of kids(doc)) if (n.nodeName === "#documentType" && n.name !== "html") return null;
118
+ while (stack.length) {
119
+ const n = stack.pop();
120
+ if (n === CLOSE) {
121
+ w.close();
122
+ continue;
123
+ }
124
+ if (n.nodeName === "#text") w.text(n.value);
125
+ else if (n.nodeName === "#comment") w.comment(n.data);
126
+ else if (n.tagName !== undefined) {
127
+ if (n.namespaceURI !== HTML_NS) return null; // the copy holds no foreign element
128
+ if (n.attrs.some((a) => a.namespace || a.prefix)) return null;
129
+ if (w.open(n.tagName, n.attrs.map((a) => [a.name, a.value]))) {
130
+ stack.push(CLOSE);
131
+ push(kids(n));
132
+ }
133
+ } else return null;
134
+ }
135
+ return w.failed ? null : w.out.join("");
136
+ }
137
+ const CLOSE = Symbol("close");
138
+
139
+ // ---- whitespace runs ---------------------------------------------------------------------------------------------------------------
140
+ // urlKind and cssPlain (src/html-active.js) and the CSS patterns below backtrack over a long run of whitespace that is not followed by
141
+ // what they look for (a 500,000-space run inside a url( held one call for minutes). So a URL holding a run of 64 or more is treated as
142
+ // one this cannot read, and CSS has every such run shortened to one space before any pattern reads it (CSS treats a run as one space,
143
+ // but inside a quoted string, where 64 spaces in a row are not a thing a page needs).
144
+ const LONG_WS = /[ \t\n\f\r]{64}/;
145
+ const kindOf = (v) => (LONG_WS.test(v) ? "other" : urlKind(v));
146
+ const shortWs = (css) => (LONG_WS.test(css) ? css.replace(/[ \t\n\f\r]{64,}/g, " ") : css);
147
+ const isWs = (c) => c === 32 || c === 9 || c === 10 || c === 12 || c === 13;
148
+ function trimWs(s) {
149
+ let a = 0;
150
+ let b = s.length;
151
+ while (a < b && isWs(s.charCodeAt(a))) a++;
152
+ while (b > a && isWs(s.charCodeAt(b - 1))) b--;
153
+ return s.slice(a, b);
154
+ }
155
+
156
+ // elements inside an editable part the person never types in (web/src/html-view.js NOT_TYPED, the controls the copy shows)
157
+ const ISLANDS = new Set(["button", "select", "option", "optgroup", "textarea", "input", "datalist", "output", "progress", "meter"]);
158
+
159
+ // ---- assets (R5) ----------------------------------------------------------------------------------------------------------------------
160
+ const isHomeOrRoot = (dir) => {
161
+ const real = (p) => {
162
+ try {
163
+ return fs.realpathSync(p);
164
+ } catch {
165
+ return path.resolve(p);
166
+ }
167
+ };
168
+ const d = real(dir);
169
+ return d === real(os.homedir()) || d === path.parse(d).root || /^\/Volumes\/[^/]+$/.test(d);
170
+ };
171
+
172
+ /**
173
+ * A relative reference resolved against `base` (folder names below the file's folder) to the names of the file it means, or why not:
174
+ * no query or fragment, no absolute path, no encoded separator, no folder or file whose name starts with a dot, no `Library` folder
175
+ * (any case), never above the file's folder, at most `folderDepth` folders down. Pure: no file is touched.
176
+ */
177
+ export function resolveRef(ref, base = [], limits = RENDER_LIMITS) {
178
+ let v = trimWs(String(ref ?? ""));
179
+ if (LONG_WS.test(v)) return { ok: false, code: "bad_url" };
180
+ const cut = v.search(/[?#]/);
181
+ if (cut >= 0) v = v.slice(0, cut);
182
+ if (v.startsWith("/")) return { ok: false, code: "absolute" };
183
+ if (/%(?:2f|5c|00)/i.test(v)) return { ok: false, code: "bad_url" };
184
+ let d;
185
+ try {
186
+ d = decodeURIComponent(v);
187
+ } catch {
188
+ return { ok: false, code: "bad_url" };
189
+ }
190
+ if (/[\u0000-\u001f\u007f\\:]/.test(d)) return { ok: false, code: "bad_url" };
191
+ const segs = [...base];
192
+ for (const part of d.split("/")) {
193
+ if (part === "" || part === ".") continue;
194
+ if (part === "..") {
195
+ if (!segs.length) return { ok: false, code: "outside" };
196
+ segs.pop();
197
+ } else segs.push(part);
198
+ }
199
+ if (!segs.length || d.endsWith("/")) return { ok: false, code: "bad_url" };
200
+ if (segs.length - 1 > limits.folderDepth) return { ok: false, code: "too_deep" };
201
+ for (let i = 0; i < segs.length; i++) {
202
+ if (segs[i].startsWith(".")) return { ok: false, code: "hidden" };
203
+ if (i < segs.length - 1 && segs[i].toLowerCase() === "library") return { ok: false, code: "library" };
204
+ }
205
+ return { ok: true, segs };
206
+ }
207
+
208
+ /**
209
+ * The real reader of a local asset (R5), opened the way src/watch.js opens a file: every folder on the way is a real folder (lstat, not a
210
+ * symlink), every name is spelled as its folder lists it (a case variant is not the file on a case-sensitive disk and must not be here
211
+ * either), the file is a regular file opened non-blocking without following a symlink, its descriptor is checked with fstat against the
212
+ * lstat and read. `readAsset(segs, {fileDir, max})` -> {ok: true, bytes, key ("dev:ino")} | {ok: false, code}. Keeps folder listings
213
+ * for the one render it serves.
214
+ * A folder swapped for a symlink between those checks and the open (Node has no openat, so the open follows a symlinked folder) is
215
+ * caught after it: once the file is open, and again once it is read, its path is resolved again (realpath) and must be the expected one
216
+ * under the file's real folder, the path's lstat must be the open descriptor's device and inode, and every folder on the way must still
217
+ * be the same real folder (device and inode, not a symlink). Any difference refuses the asset ("changed"); nothing read is used.
218
+ * Ceiling: a swap made and undone entirely between the open and the first of those checks, twice over, is not seen; the bytes would
219
+ * then come from a file the person's own account can read anyway. Upgrade path: a native openat walk.
220
+ * `hooks` (tests only): {beforeFile(segs), afterOpen(segs)}: run after the folders are checked (before the file's lstat and open) and
221
+ * right after the open.
222
+ */
223
+ export function makeAssetReader({ hooks = null } = {}) {
224
+ const listings = new Map();
225
+ const listed = (dir, name) => {
226
+ let set = listings.get(dir);
227
+ if (!set) {
228
+ try {
229
+ set = new Set(fs.readdirSync(dir).map((n) => n.normalize("NFC")));
230
+ } catch {
231
+ set = new Set();
232
+ }
233
+ listings.set(dir, set);
234
+ }
235
+ return set.has(name.normalize("NFC"));
236
+ };
237
+ const reals = new Map();
238
+ const realOf = (p) => {
239
+ try {
240
+ return fs.realpathSync(p).normalize("NFC");
241
+ } catch {
242
+ return null;
243
+ }
244
+ };
245
+ return function readAsset(segs, { fileDir, max }) {
246
+ let base = reals.get(fileDir);
247
+ if (base === undefined) reals.set(fileDir, (base = realOf(fileDir)));
248
+ if (!base) return { ok: false, code: "not_found" };
249
+ let dir = fileDir;
250
+ const folders = []; // [path, dev, ino] of every folder on the way, as checked
251
+ for (let i = 0; i < segs.length; i++) {
252
+ const name = segs[i];
253
+ if (!listed(dir, name)) return { ok: false, code: "not_found" };
254
+ const p = path.join(dir, name);
255
+ if (i === segs.length - 1) hooks?.beforeFile?.(segs);
256
+ let st;
257
+ try {
258
+ st = fs.lstatSync(p);
259
+ } catch {
260
+ return { ok: false, code: "not_found" };
261
+ }
262
+ if (st.isSymbolicLink()) return { ok: false, code: "symlink" };
263
+ if (i < segs.length - 1) {
264
+ if (!st.isDirectory()) return { ok: false, code: "not_found" };
265
+ folders.push([p, st.dev, st.ino]);
266
+ dir = p;
267
+ continue;
268
+ }
269
+ if (!st.isFile()) return { ok: false, code: "not_regular" };
270
+ if (st.size === 0) return { ok: false, code: "empty" };
271
+ if (st.size > max) return { ok: false, code: "too_large" };
272
+ const expected = path.join(base, ...segs.map((x) => x.normalize("NFC")));
273
+ let fd;
274
+ try {
275
+ fd = fs.openSync(p, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
276
+ } catch (err) {
277
+ return { ok: false, code: err?.code === "ELOOP" ? "symlink" : "unreadable" };
278
+ }
279
+ try {
280
+ hooks?.afterOpen?.(segs);
281
+ const f = fs.fstatSync(fd);
282
+ if (!f.isFile() || f.ino !== st.ino || f.dev !== st.dev) return { ok: false, code: "changed" };
283
+ // the path still leads to this descriptor, through the same real folders, to the expected place
284
+ const same = () => {
285
+ let now;
286
+ try {
287
+ now = fs.lstatSync(p);
288
+ } catch {
289
+ return false;
290
+ }
291
+ if (now.isSymbolicLink() || now.ino !== f.ino || now.dev !== f.dev) return false;
292
+ for (const [fp, dev, ino] of folders) {
293
+ let d;
294
+ try {
295
+ d = fs.lstatSync(fp);
296
+ } catch {
297
+ return false;
298
+ }
299
+ if (d.isSymbolicLink() || !d.isDirectory() || d.dev !== dev || d.ino !== ino) return false;
300
+ }
301
+ return realOf(p) === expected;
302
+ };
303
+ if (!same()) return { ok: false, code: "changed" };
304
+ if (f.size === 0) return { ok: false, code: "empty" };
305
+ if (f.size > max) return { ok: false, code: "too_large" };
306
+ const bytes = Buffer.alloc(f.size);
307
+ let off = 0;
308
+ while (off < bytes.length) {
309
+ const k = fs.readSync(fd, bytes, off, bytes.length - off, off);
310
+ if (k <= 0) break;
311
+ off += k;
312
+ }
313
+ if (off !== bytes.length || !same()) return { ok: false, code: "changed" };
314
+ return { ok: true, bytes, key: `${f.dev}:${f.ino}` };
315
+ } catch {
316
+ return { ok: false, code: "unreadable" };
317
+ } finally {
318
+ fs.closeSync(fd);
319
+ }
320
+ }
321
+ return { ok: false, code: "not_found" };
322
+ };
323
+ }
324
+
325
+ /**
326
+ * The type and size of an image, from its first bytes (never its name): PNG, JPEG, GIF or WebP (v1, no SVG). Returns {ok, mime, width,
327
+ * height} or {ok: false, code: "type" | "dimensions"} (a header this cannot read, or past 16,000 px a side or 64 million pixels).
328
+ */
329
+ export function sniffImage(b, limits = RENDER_LIMITS) {
330
+ const dims = (mime, width, height) => (width > 0 && height > 0 && width <= limits.side && height <= limits.side && width * height <= limits.pixels ? { ok: true, mime, width, height } : { ok: false, code: "dimensions" });
331
+ const at = (s, o) => b.length >= o + s.length && b.toString("latin1", o, o + s.length) === s;
332
+ if (b.length >= 24 && at("\x89PNG\r\n\x1a\n", 0) && at("IHDR", 12)) return dims("image/png", b.readUInt32BE(16), b.readUInt32BE(20));
333
+ if (b.length >= 10 && (at("GIF87a", 0) || at("GIF89a", 0))) return dims("image/gif", b.readUInt16LE(6), b.readUInt16LE(8));
334
+ if (b.length >= 30 && at("RIFF", 0) && at("WEBP", 8)) {
335
+ if (at("VP8 ", 12) && b[23] === 0x9d && b[24] === 0x01 && b[25] === 0x2a) return dims("image/webp", b.readUInt16LE(26) & 0x3fff, b.readUInt16LE(28) & 0x3fff);
336
+ if (at("VP8L", 12) && b[20] === 0x2f) return dims("image/webp", 1 + (((b[22] & 0x3f) << 8) | b[21]), 1 + (((b[24] & 0x0f) << 10) | (b[23] << 2) | ((b[22] & 0xc0) >> 6)));
337
+ if (at("VP8X", 12)) return dims("image/webp", 1 + b.readUIntLE(24, 3), 1 + b.readUIntLE(27, 3));
338
+ return { ok: false, code: "type" };
339
+ }
340
+ if (b.length >= 4 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) {
341
+ // walk the markers to the first frame header (SOF): its height and width
342
+ let i = 2;
343
+ while (i + 9 < b.length) {
344
+ if (b[i] !== 0xff) return { ok: false, code: "type" };
345
+ while (b[i] === 0xff && i < b.length) i++;
346
+ const m = b[i];
347
+ if (m === 0xd8 || m === 0x01 || (m >= 0xd0 && m <= 0xd7)) {
348
+ i++;
349
+ continue;
350
+ }
351
+ if (m === 0xd9 || m === 0xda) return { ok: false, code: "type" };
352
+ if (i + 8 >= b.length) break;
353
+ const len = b.readUInt16BE(i + 1);
354
+ if ((m >= 0xc0 && m <= 0xc3) || (m >= 0xc5 && m <= 0xc7) || (m >= 0xc9 && m <= 0xcb) || (m >= 0xcd && m <= 0xcf)) return dims("image/jpeg", b.readUInt16BE(i + 6), b.readUInt16BE(i + 4));
355
+ if (len < 2) return { ok: false, code: "type" };
356
+ i += 1 + len;
357
+ }
358
+ return { ok: false, code: "type" };
359
+ }
360
+ return { ok: false, code: "type" };
361
+ }
362
+
363
+ /** The bytes of a `data:` URL ({mime, bytes}), or null when it is not one this can read. */
364
+ function dataUrl(v) {
365
+ const m = /^[ \t\n\f\r]*data:([^,]*),(.*)$/is.exec(String(v));
366
+ if (!m) return null;
367
+ const params = m[1].split(";").map((s) => s.trim().toLowerCase());
368
+ const mime = params[0] || "text/plain";
369
+ const b64 = params.includes("base64");
370
+ try {
371
+ const bytes = b64 ? Buffer.from(m[2].replace(/[ \t\n\f\r]/g, ""), "base64") : Buffer.from(decodeURIComponent(m[2]), "utf8");
372
+ return { mime, bytes };
373
+ } catch {
374
+ return null;
375
+ }
376
+ }
377
+
378
+ const UTF8 = new TextDecoder("utf-8", { fatal: true });
379
+
380
+ // ---- CSS (R1's rule, R16, R21) -------------------------------------------------------------------------------------------------------
381
+ const IMPORT = /@import[ \t\n\f\r]+(?:url\([ \t\n\f\r]*(?:"([^"]*)"|'([^']*)'|([^)"' \t\n\f\r]+)?)[ \t\n\f\r]*\)|"([^"]*)"|'([^']*)')[ \t\n\f\r]*([^;{}]*)(?:;|$)/gi;
382
+ const MEDIA_LIST = /^[a-z0-9 ,()\-:._]*$/i;
383
+
384
+ // ---- the render ---------------------------------------------------------------------------------------------------------------------
385
+ /**
386
+ * The render copy of one reading (src/html.js readHtml). `fileDir` is the folder of the file's real path; `readAsset(segs, {fileDir,
387
+ * max})` reads one local asset (default: makeAssetReader(), the real one). `editable`: the parts the person may type in are marked
388
+ * `contenteditable` (src/html.js editHosts; the view route's copy always is). Returns
389
+ * {bytes (the copy, UTF-8), texts (for the n-th text node of the copy in tree order, the id of its text node in the reading, or -1),
390
+ * stats}
391
+ * or {plain: true, reason} when the copy, parsed again, is not the tree it was written from (reason "reparse") or a text could not be
392
+ * written ("nul", "lone_surrogate", "style_end"): the caller then shows plainCopy(read).
393
+ * `stats` holds numbers and fixed codes only, never any of the file's content:
394
+ * {scripts, handlers (on* attributes), noscript, templates, frames, forms (shown as inert div), controls (shown disabled), media,
395
+ * foreign (SVG and MathML left out), links (targets kept for display), attributes (left out), elements (written), dropped (elements
396
+ * left out), renamed, remote (images, stylesheets, imports and CSS url() not loaded because not local), assets: {shown, refused:
397
+ * {code: n}}, styles: {kept, left: {code: n}, heavy: null | "css_bytes" | "css_rules" | "css_has"}, folder: null | "home_or_root",
398
+ * bodyText (visible characters in the body, not counting spaces: 0 means R16's "built by scripts" message)}
399
+ */
400
+ export function renderCopy(read, { fileDir, readAsset = makeAssetReader(), limits = RENDER_LIMITS, editable = false } = {}) {
401
+ const stats = { scripts: 0, handlers: 0, noscript: 0, templates: 0, frames: 0, forms: 0, controls: 0, media: 0, foreign: 0, links: 0, attributes: 0, elements: 0, dropped: 0, renamed: 0, remote: 0, assets: { shown: 0, refused: {} }, styles: { kept: 0, left: {}, heavy: null }, folder: null, bodyText: 0 };
402
+ const bump = (o, k) => (o[k] = (o[k] || 0) + 1);
403
+ // what the file holds, wherever it is (the banner: "Scripts are off in this view")
404
+ for (const x of read.nodes) {
405
+ if (x.tagName === undefined) continue;
406
+ if (x.tagName === "script") stats.scripts++;
407
+ for (const a of x.attrs) if (/^on/i.test(a.name)) stats.handlers++;
408
+ }
409
+ const noAssets = fileDir == null || isHomeOrRoot(fileDir);
410
+ if (noAssets) stats.folder = "home_or_root";
411
+ let total = 0;
412
+ let count = 0;
413
+ const imageCache = new Map(); // joined names -> {ok, bytes, ...}
414
+ const refuse = (code) => {
415
+ if (code === "remote") stats.remote++;
416
+ else bump(stats.assets.refused, code);
417
+ return null;
418
+ };
419
+ /** A `data:` URI for an image in the copy, counted against the caps, or null (counted). */
420
+ const imageUri = (bytes) => {
421
+ const s = sniffImage(bytes, limits);
422
+ if (!s.ok) return refuse(s.code);
423
+ if (bytes.length > limits.asset) return refuse("too_large");
424
+ if (count + 1 > limits.assets) return refuse("over_count");
425
+ const size = 4 * Math.ceil(bytes.length / 3); // as it is held in the copy (base64): the copy stays within the page and 30 MB
426
+ if (total + size > limits.total) return refuse("over_total");
427
+ count++;
428
+ total += size;
429
+ stats.assets.shown++;
430
+ let b64 = base64s.get(bytes);
431
+ if (b64 === undefined) base64s.set(bytes, (b64 = bytes.toString("base64")));
432
+ return `data:${s.mime};base64,${b64}`;
433
+ };
434
+ const base64s = new WeakMap();
435
+ const readLocal = (ref, base, max) => {
436
+ if (noAssets) return { ok: false, code: "folder" };
437
+ const r = resolveRef(ref, base, limits);
438
+ if (!r.ok) return r;
439
+ const got = readAsset(r.segs, { fileDir, max });
440
+ return got.ok ? { ...got, segs: r.segs } : got;
441
+ };
442
+ /** An image reference (an img src or a CSS url()) as a data: URI, or null (counted). `base` null: relative is not resolvable. */
443
+ const image = (ref, base) => {
444
+ const kind = kindOf(ref);
445
+ if (kind === "data-image" || (kind === "other" && /^[ \t\n\f\r]*data:image\//i.test(ref))) {
446
+ const d = dataUrl(ref);
447
+ return d ? imageUri(d.bytes) : refuse("type");
448
+ }
449
+ if (kind !== "relative") return refuse("remote");
450
+ if (base === null) return refuse("bad_url"); // a relative url() in a data: stylesheet has nothing to resolve against
451
+ const r = resolveRef(ref, base, limits);
452
+ if (!r.ok) return refuse(r.code);
453
+ const key = r.segs.join("/");
454
+ let got = imageCache.get(key);
455
+ if (!got) {
456
+ // bounded before it happens: at most `assets` files read, and no more bytes read in all than `total` (read and not shown counts
457
+ // too), so a page naming thousands of large local files reads at most 30 MB of them; a file that is not an image is not kept
458
+ const room = Math.min(limits.asset, limits.total - readBytes);
459
+ if (count >= limits.assets || reads >= limits.assets) got = { ok: false, code: "over_count" };
460
+ else if (room <= 0) got = { ok: false, code: "over_total" };
461
+ else {
462
+ reads++;
463
+ got = readLocal(ref, base, room);
464
+ if (got.ok) {
465
+ readBytes += got.bytes.length;
466
+ const sn = sniffImage(got.bytes, limits);
467
+ if (!sn.ok) got = sn;
468
+ } else if (got.code === "too_large" && room < limits.asset) got = { ok: false, code: "over_total" };
469
+ }
470
+ imageCache.set(key, got);
471
+ }
472
+ if (!got.ok) return refuse(got.code);
473
+ return imageUri(got.bytes);
474
+ };
475
+ let reads = 0;
476
+ let readBytes = 0;
477
+
478
+ // ---- pass 1: the page's stylesheets, their imports read, then the caps decided before anything is written (R21) ----
479
+ const sheetsSeen = new Set(); // dev:ino of every local stylesheet already inlined: never the same file twice
480
+ const sheetText = (bytes) => {
481
+ try {
482
+ return shortWs(UTF8.decode(bytes).replace(/^\ufeff/, ""));
483
+ } catch {
484
+ return null;
485
+ }
486
+ };
487
+ const sheets = new Map(); // node -> its CSS in the copy, or null (left out)
488
+ // a local stylesheet is read once (by its path, then by its file), and reading stops where the 2 MB cap is spent (which leaves every
489
+ // style of the page out anyway), so a page naming one sheet a thousand times, or a thousand sheets, reads at most 2 MB of CSS
490
+ const sheetPaths = new Set();
491
+ let cssRead = 0;
492
+ const readSheet = (ref, base) => {
493
+ if (noAssets) return { ok: false, code: "folder" };
494
+ const r = resolveRef(ref, base, limits);
495
+ if (!r.ok) return r;
496
+ const key = r.segs.join("/");
497
+ if (sheetPaths.has(key)) return { ok: false, code: "repeat" };
498
+ sheetPaths.add(key);
499
+ const room = Math.min(limits.sheet, limits.cssBytes - cssRead);
500
+ if (room <= 0) return (stats.styles.heavy ??= "css_bytes"), { ok: false, code: "css_cap" };
501
+ const got = readLocal(ref, base, room);
502
+ if (got.ok) cssRead += got.bytes.length;
503
+ else if (got.code === "too_large" && room < limits.sheet) return (stats.styles.heavy ??= "css_bytes"), { ok: false, code: "css_cap" };
504
+ if (got.ok && sheetsSeen.has(got.key)) return { ok: false, code: "repeat" };
505
+ if (got.ok) sheetsSeen.add(got.key);
506
+ return got;
507
+ };
508
+ const readDataSheet = (ref) => {
509
+ const d = dataUrl(ref);
510
+ if (!d || d.mime !== "text/css") return refuse("not_text");
511
+ const room = Math.min(limits.sheet, limits.cssBytes - cssRead);
512
+ if (room <= 0) return (stats.styles.heavy ??= "css_bytes"), refuse("css_cap");
513
+ if (d.bytes.length > room) {
514
+ if (room < limits.sheet) return (stats.styles.heavy ??= "css_bytes"), refuse("css_cap");
515
+ return refuse("too_large");
516
+ }
517
+ cssRead += d.bytes.length;
518
+ const text = sheetText(d.bytes);
519
+ if (text === null) return refuse("not_text");
520
+ return text;
521
+ };
522
+ const sheetNodes = [];
523
+ {
524
+ const stack = [read.doc];
525
+ while (stack.length) {
526
+ const n = stack.pop();
527
+ if (n.tagName !== undefined) {
528
+ if (n.namespaceURI !== HTML_NS) continue;
529
+ const t = n.tagName;
530
+ if (t === "script" || t === "template" || t === "noscript" || FRAMES.has(t) || t === "noembed" || t === "noframes") continue;
531
+ if (t === "style" || (t === "link" && isStylesheet(n))) sheetNodes.push(n);
532
+ if (t === "style") continue;
533
+ }
534
+ const ks = kids(n);
535
+ for (let i = ks.length - 1; i >= 0; i--) stack.push(ks[i]);
536
+ }
537
+ }
538
+ let cssBytes = 0;
539
+ let cssRules = 0;
540
+ let cssHas = 0;
541
+ const raw = new Map(); // node -> its parts (expandSheet), url() still to rewrite
542
+ for (const n of sheetNodes) {
543
+ let css = null;
544
+ let base = [];
545
+ if (n.tagName === "style") {
546
+ css = shortWs(kids(n).map((c) => c.value ?? "").join(""));
547
+ cssRead += Buffer.byteLength(css);
548
+ }
549
+ else {
550
+ const href = attr(n, "href") ?? "";
551
+ if (/^[ \t\n\f\r]*data:/i.test(href)) {
552
+ css = readDataSheet(href);
553
+ base = null;
554
+ } else if (kindOf(href) === "relative") {
555
+ const got = readSheet(href, []);
556
+ if (!got.ok) refuse(got.code);
557
+ else {
558
+ css = sheetText(got.bytes);
559
+ if (css === null) refuse("not_text");
560
+ base = got.segs.slice(0, -1);
561
+ }
562
+ } else refuse("remote");
563
+ }
564
+ if (css === null) continue;
565
+ const parts = expandSheet(css, base);
566
+ for (const p of parts) {
567
+ cssBytes += Buffer.byteLength(p.text);
568
+ cssRules += (p.text.match(/\{/g) || []).length;
569
+ cssHas += (p.text.match(/:has\(/gi) || []).length;
570
+ }
571
+ raw.set(n, parts);
572
+ }
573
+ // a sheet and its imports as a list of parts in order, each with the folder its url() resolve against (null: none, a data: sheet);
574
+ // iterative: a stack of sheets still to split at their imports and parts ready to keep
575
+ function expandSheet(css, base) {
576
+ const out = [];
577
+ const st = [{ sheet: css, base, depth: 0 }];
578
+ while (st.length) {
579
+ const it = st.pop();
580
+ if (it.part !== undefined) {
581
+ if (it.part) out.push({ text: it.part, base: it.base });
582
+ continue;
583
+ }
584
+ const items = [];
585
+ let last = 0;
586
+ for (const m of it.sheet.matchAll(IMPORT)) {
587
+ items.push({ part: it.sheet.slice(last, m.index), base: it.base });
588
+ last = m.index + m[0].length;
589
+ const r = importTarget(m, it.base, it.depth);
590
+ if (!r) continue;
591
+ if (r.media) items.push({ part: `@media ${r.media}{`, base: [] });
592
+ items.push({ sheet: r.text, base: r.base, depth: it.depth + 1 });
593
+ if (r.media) items.push({ part: "}", base: [] });
594
+ }
595
+ items.push({ part: it.sheet.slice(last), base: it.base });
596
+ for (let i = items.length - 1; i >= 0; i--) st.push(items[i]);
597
+ }
598
+ return out;
599
+ }
600
+ function importTarget(m, b, depth) {
601
+ if (depth >= limits.importDepth) return (bump(stats.styles.left, "import_depth"), null);
602
+ const target = m[1] ?? m[2] ?? m[3] ?? m[4] ?? m[5] ?? "";
603
+ const media = String(m[6] || "").trim();
604
+ if (media && (!MEDIA_LIST.test(media) || /layer|supports|url/i.test(media))) return (bump(stats.styles.left, "import_media"), null);
605
+ if (/^[ \t\n\f\r]*data:/i.test(target)) {
606
+ const text = readDataSheet(target);
607
+ if (text === null) return null;
608
+ return { text, base: null, media };
609
+ }
610
+ if (kindOf(target) !== "relative" || b === null) return refuse("remote");
611
+ const got = readSheet(target, b);
612
+ if (!got.ok) return refuse(got.code);
613
+ const text = sheetText(got.bytes);
614
+ if (text === null) return refuse("not_text");
615
+ return { text, base: got.segs.slice(0, -1), media };
616
+ }
617
+
618
+ // (already "css_bytes" when reading spent the 2 MB)
619
+ if (!stats.styles.heavy && cssBytes > limits.cssBytes) stats.styles.heavy = "css_bytes";
620
+ else if (!stats.styles.heavy && cssRules > limits.cssRules) stats.styles.heavy = "css_rules";
621
+ else if (!stats.styles.heavy && cssHas > limits.cssHas) stats.styles.heavy = "css_has";
622
+ const stylesOn = !stats.styles.heavy;
623
+
624
+ /** CSS text with every url() made a checked data: image or `none`; null when the result is not plain (R1's rule) or not writable. */
625
+ const resolveUrls = (css, base) => {
626
+ const s = css;
627
+ if (s.length < 4 || !/url\(/i.test(s)) return s;
628
+ const space = (c) => {
629
+ const k = c.charCodeAt(0);
630
+ return k < 128 ? k === 32 || (k >= 9 && k <= 13) : /\s/.test(c);
631
+ };
632
+ const isUrl = (i) => (s.charCodeAt(i) | 32) === 117 && (s.charCodeAt(i + 1) | 32) === 114 && (s.charCodeAt(i + 2) | 32) === 108;
633
+ let stop = null;
634
+ const stops = () => {
635
+ if (stop) return stop;
636
+ const n = s.length;
637
+ stop = { any: new Int32Array(n + 1), dq: new Int32Array(n + 1), sq: new Int32Array(n + 1), ns: new Int32Array(n + 1) };
638
+ stop.any[n] = stop.dq[n] = stop.sq[n] = stop.ns[n] = n;
639
+ for (let i = n - 1; i >= 0; i--) {
640
+ const c = s[i];
641
+ const ws = space(c);
642
+ stop.any[i] = c === ")" || c === '"' || c === "'" || ws ? i : stop.any[i + 1];
643
+ stop.dq[i] = c === '"' ? i : stop.dq[i + 1];
644
+ stop.sq[i] = c === "'" ? i : stop.sq[i + 1];
645
+ stop.ns[i] = ws ? stop.ns[i + 1] : i;
646
+ }
647
+ return stop;
648
+ };
649
+ const out = [];
650
+ let from = 0;
651
+ for (let i = 0; i + 3 < s.length; ) {
652
+ if (!(isUrl(i) && s[i + 3] === "(")) {
653
+ i++;
654
+ continue;
655
+ }
656
+ const st = stops();
657
+ const j = st.ns[i + 4];
658
+ let val = null;
659
+ let k = j;
660
+ if (j < s.length && (s[j] === '"' || s[j] === "'")) {
661
+ const close = j + 1 <= s.length ? (s[j] === '"' ? st.dq : st.sq)[j + 1] : s.length;
662
+ if (close < s.length) [val, k] = [s.slice(j + 1, close), close + 1];
663
+ } else {
664
+ k = st.any[j];
665
+ val = s.slice(j, k);
666
+ }
667
+ const m = val !== null ? st.ns[k] : s.length;
668
+ if (val === null || m >= s.length || s[m] !== ")") {
669
+ i++;
670
+ continue;
671
+ }
672
+ const uri = image(val, base);
673
+ out.push(s.slice(from, i), uri ? `url("${uri}")` : "none");
674
+ from = i = m + 1;
675
+ }
676
+ if (!from) return s;
677
+ out.push(s.slice(from));
678
+ return out.join("");
679
+ };
680
+ const finalCss = (parts, inElement) => {
681
+ const text = parts.map((p) => resolveUrls(p.text, p.base)).join("");
682
+ const c = cssPlain(text);
683
+ if (!c.ok || !c.loads.every((u) => kindOf(u) === "data-image")) return (bump(stats.styles.left, "not_plain"), null);
684
+ if (inElement && /<\/style/i.test(text)) return (bump(stats.styles.left, "not_plain"), null);
685
+ return text;
686
+ };
687
+ if (stylesOn) for (const [n, parts] of raw) sheets.set(n, finalCss(parts, true));
688
+
689
+ // ---- pass 2: the copy ----
690
+ const attrCss = new Map(); // a style attribute's value -> its CSS in the copy, or null (left out)
691
+ const w = new Writer();
692
+ // the parts the person may type in (src/html.js editHosts): editable as the frame parses the copy; inside one, a link, a placeholder
693
+ // or a control is an island the caret skips (`contenteditable="false"`)
694
+ const hosts = editable ? editHosts(read) : new Set();
695
+ const inHost = (n) => {
696
+ for (let a = n.__par; a; a = a.__par) if (hosts.has(a)) return true;
697
+ return false;
698
+ };
699
+ const editAttr = (n, tag, at) => {
700
+ if (hosts.has(n)) at.push(["contenteditable", "plaintext-only"]);
701
+ else if ((tag === "a" || ISLANDS.has(tag)) && inHost(n)) at.push(["contenteditable", "false"]);
702
+ };
703
+ const ph = (kind, n) => {
704
+ const at = [];
705
+ if (n.__id !== undefined) at.push(["data-osk-id", String(n.__id)]);
706
+ at.push(["data-osk-placeholder", kind], ["style", PLACEHOLDER_STYLE]);
707
+ if (inHost(n)) at.push(["contenteditable", "false"]);
708
+ w.open("span", at);
709
+ w.text(PLACEHOLDER_LABELS[kind], -1, false); // a label of ours: not the page's text (R16 counts the page's own)
710
+ w.close();
711
+ };
712
+ /** The attributes the copy keeps on `n`, written as `tag`: the display set, the CSS rule for style, data-osk-id first. */
713
+ const attrsOf = (n, tag) => {
714
+ const at = [];
715
+ if (n.__id !== undefined) at.push(["data-osk-id", String(n.__id)]);
716
+ for (const a of n.attrs) {
717
+ if (a.namespace || a.prefix) {
718
+ stats.attributes++;
719
+ continue;
720
+ }
721
+ const name = a.name;
722
+ if (/^on/i.test(name)) continue; // counted above
723
+ if (name === "style") {
724
+ if (!stylesOn) continue;
725
+ // the same declarations on many elements are checked once (one with a url() counts its image at each place it is shown)
726
+ let css = attrCss.get(a.value);
727
+ if (css === undefined) {
728
+ css = finalCss([{ text: shortWs(a.value), base: [] }], false);
729
+ if (!/url\s*\(/i.test(a.value)) attrCss.set(a.value, css);
730
+ } else if (css === null) bump(stats.styles.left, "not_plain");
731
+ if (css !== null) (at.push(["style", css]), stats.styles.kept++);
732
+ continue;
733
+ }
734
+ if (name === "href" && tag === "a") {
735
+ const k = kindOf(a.value);
736
+ if (URL_KINDS.link.has(k) && a.value.length <= 2048) (at.push(["data-osk-href", a.value]), stats.links++);
737
+ else stats.attributes++;
738
+ continue;
739
+ }
740
+ if (name === "type" && tag === "input") {
741
+ const t = a.value.trim().toLowerCase();
742
+ if (DISPLAY_INPUT_TYPES.has(t)) at.push(["type", t]);
743
+ continue;
744
+ }
745
+ if (tag === "style" && name === "media") {
746
+ if (MEDIA_LIST.test(a.value) && !/url/i.test(a.value)) at.push(["media", a.value]);
747
+ continue;
748
+ }
749
+ if (displayAttributeAllowed(tag, name)) at.push([name, a.value]);
750
+ else stats.attributes++;
751
+ }
752
+ if (DISPLAY_CONTROLS.has(tag)) {
753
+ if (!at.some(([k]) => k === "disabled")) at.push(["disabled", ""]);
754
+ stats.controls++;
755
+ }
756
+ editAttr(n, tag, at);
757
+ return at;
758
+ };
759
+
760
+ const stack = [];
761
+ const pushKids = (n) => {
762
+ const ks = kids(n);
763
+ for (let i = ks.length - 1; i >= 0; i--) stack.push(ks[i]);
764
+ };
765
+ const html = kids(read.doc).find((x) => x.tagName === "html");
766
+ let bodyDone = false;
767
+ if (html) {
768
+ w.open("html", attrsOf(html, "html"));
769
+ stack.push(CLOSE);
770
+ pushKids(html);
771
+ } else {
772
+ w.open("html", []);
773
+ stack.push(CLOSE);
774
+ }
775
+ while (stack.length) {
776
+ const n = stack.pop();
777
+ if (n === CLOSE) {
778
+ if (w.stack.length === 2 && !bodyDone) {
779
+ // the html element closes: a page with no body (a frameset) gets an empty one
780
+ w.open("body", []);
781
+ w.close();
782
+ bodyDone = true;
783
+ }
784
+ w.close();
785
+ continue;
786
+ }
787
+ if (n === CSP) {
788
+ w.open("meta", [["http-equiv", "Content-Security-Policy"], ["content", RENDER_CSP]]);
789
+ continue;
790
+ }
791
+ if (n.nodeName === "#text") {
792
+ w.text(n.value, n.__id);
793
+ continue;
794
+ }
795
+ if (n.tagName === undefined) continue; // comments, the doctype: not in the copy
796
+ const t = n.tagName;
797
+ if (n.namespaceURI !== HTML_NS) {
798
+ stats.foreign++;
799
+ ph("graphic", n);
800
+ continue;
801
+ }
802
+ const parentTag = w.stack[w.stack.length - 1].tag;
803
+ if (t === "head") {
804
+ w.open("head", attrsOf(n, "head"));
805
+ stack.push(CLOSE);
806
+ pushKids(n);
807
+ stack.push(CSP);
808
+ stats.elements++;
809
+ continue;
810
+ }
811
+ if (t === "body") bodyDone = true;
812
+ if (t === "script") continue; // counted above
813
+ if (t === "noscript") stats.noscript++;
814
+ else if (t === "template") stats.templates++;
815
+ else if (FRAMES.has(t)) stats.frames++;
816
+ if (t === "noscript" || t === "template" || FRAMES.has(t)) continue;
817
+ if (DROP.has(t) || (VOID.has(t) && !displayElementAllowed(t) && t !== "link")) {
818
+ stats.dropped++;
819
+ continue;
820
+ }
821
+ if (t === "link") {
822
+ const css = sheets.get(n);
823
+ if (css) {
824
+ w.open("style", n.__id !== undefined ? [["data-osk-id", String(n.__id)]] : []);
825
+ w.text(css);
826
+ w.close();
827
+ stats.styles.kept++;
828
+ stats.elements++;
829
+ } else stats.dropped++;
830
+ continue;
831
+ }
832
+ if (t === "style") {
833
+ const css = stylesOn ? sheets.get(n) : null;
834
+ if (css) {
835
+ w.open("style", attrsOf(n, "style"));
836
+ w.text(css);
837
+ w.close();
838
+ stats.styles.kept++;
839
+ stats.elements++;
840
+ }
841
+ continue;
842
+ }
843
+ if (MEDIA.has(t)) {
844
+ stats.media++;
845
+ ph("media", n);
846
+ continue;
847
+ }
848
+ if (t === "img" || (t === "input" && attr(n, "type")?.trim().toLowerCase() === "image")) {
849
+ const src = attr(n, "src");
850
+ if (t === "input") {
851
+ stats.controls++;
852
+ ph("image", n);
853
+ continue;
854
+ }
855
+ if (src === null) {
856
+ w.open("img", attrsOf(n, "img"));
857
+ stats.elements++;
858
+ continue;
859
+ }
860
+ const uri = image(src, []);
861
+ if (!uri) {
862
+ ph("image", n);
863
+ continue;
864
+ }
865
+ w.open("img", [...attrsOf(n, "img"), ["src", uri]]);
866
+ stats.elements++;
867
+ continue;
868
+ }
869
+ let tag = t;
870
+ if (t === "form") {
871
+ stats.forms++;
872
+ if (TABLE_CONTEXT.has(parentTag)) continue;
873
+ tag = "div";
874
+ } else if (RENAME[t]) {
875
+ tag = RENAME[t];
876
+ stats.renamed++;
877
+ } else if (!displayElementAllowed(t)) {
878
+ tag = "span"; // an element this does not know: its content shown, as a browser shows an unknown element's
879
+ stats.renamed++;
880
+ }
881
+ stats.elements++;
882
+ if (w.open(tag, attrsOf(n, tag))) {
883
+ stack.push(CLOSE);
884
+ if (t === "xmp" || t === "plaintext") {
885
+ const s = kids(n).map((c) => c.value ?? "").join("");
886
+ const id = kids(n).find((c) => c.nodeName === "#text")?.__id;
887
+ w.text(s, id);
888
+ } else pushKids(n);
889
+ }
890
+ }
891
+ if (w.failed) return { plain: true, reason: w.failed };
892
+ const copy = w.out.join("");
893
+ // R2: the copy parsed again, as the frame parses it (scripting off: a frame that runs no script), must be the tree it was written from
894
+ const again = htmlParser().parse(copy, { scriptingEnabled: false });
895
+ if (serializeTree(again) !== copy) return { plain: true, reason: "reparse" };
896
+ stats.bodyText = w.bodyText;
897
+ return { bytes: Buffer.from(copy, "utf8"), texts: w.texts, stats };
898
+ }
899
+ const CSP = Symbol("csp");
900
+
901
+ const attr = (n, name) => n.attrs?.find((a) => a.name === name && !a.namespace)?.value ?? null;
902
+ const isStylesheet = (n) => {
903
+ const rel = (attr(n, "rel") ?? "").toLowerCase().split(/[ \t\n\f\r]+/);
904
+ return rel.includes("stylesheet") && !rel.includes("alternate");
905
+ };
906
+
907
+ /**
908
+ * The plain text view (R2's fallback): the page's text units as headings and paragraphs of escaped text, in a minimal page with the same
909
+ * CSP meta; each written from a unit gets that element's `data-osk-id`. Text nested in a unit already written is not written again.
910
+ * Returns {bytes, texts, stats: {units, bodyText}}.
911
+ */
912
+ export function plainCopy(read) {
913
+ const w = new Writer();
914
+ w.open("html", []);
915
+ w.open("head", []);
916
+ w.open("meta", [["http-equiv", "Content-Security-Policy"], ["content", RENDER_CSP]]);
917
+ w.close();
918
+ w.open("body", []);
919
+ let end = -1;
920
+ let units = 0;
921
+ for (const u of read.units) {
922
+ if (u.kind !== "text") continue;
923
+ const el = u.node;
924
+ if ((el.__fs ?? 0) < end) continue;
925
+ end = el.__fe ?? 0;
926
+ const text = read.flat.slice(el.__fs ?? 0, el.__fe ?? 0).trim().replace(/[\u0000\ud800-\udfff]/g, (c) => (c === "\u0000" ? "" : c));
927
+ if (!text) continue;
928
+ const tag = el.namespaceURI === HTML_NS && /^h[1-6]$/.test(el.tagName) ? el.tagName : "p";
929
+ w.open(tag, u.nodeId !== null ? [["data-osk-id", String(u.nodeId)]] : []);
930
+ w.text(safeText(text));
931
+ w.close();
932
+ units++;
933
+ }
934
+ w.close();
935
+ w.close();
936
+ return { bytes: Buffer.from(w.out.join(""), "utf8"), texts: w.texts, stats: { units, bodyText: w.bodyText } };
937
+ }
938
+ // lone surrogates cannot be written: each becomes U+FFFD (a paired one is kept)
939
+ const safeText = (s) => s.replace(/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g, "\ufffd");