Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

type: bento.loadDoc's refusal message carries no file text (#768)

nyblnetfced84b1615e
diff --git a/scripts/test-type-cantopen-browser.mjs b/scripts/test-type-cantopen-browser.mjs
index ccdac937..3b2d3f07 100644
--- a/scripts/test-type-cantopen-browser.mjs
+++ b/scripts/test-type-cantopen-browser.mjs
@@ -17,17 +17,20 @@
// - the message is shown, with the file's detail as literal text;
// - no element from the file reaches the page (no img, no b), and the
// handler's sentinel never fires;
-//
// - no text from the file appears anywhere outside that message; type's
// boot error is a fixed sentence, since the detail is already on screen;
// - type publishes window.bento = { format, refused: true } (the shell
// loader's contract for "painted its own refusal"), so the page shows ONE
// message: the app does not run again on a later loader path, and no
-// "This file could not start" card is painted over it.
+// "This file could not start" card is painted over it;
+// - bento.loadDoc refuses such a document with a fixed message too, the
+// detail riding on its own `detail` property, and the open document
+// untouched.
//
// Mutations (run, judged by exit code): set the detail line with innerHTML
// instead of textContent → red; put the detail back into the thrown error
-// → red; drop the published refusal → red.
+// → red; drop the published refusal → red; put the detail back into
+// bento.loadDoc's message → red.
import { readFileSync, writeFileSync, mkdtempSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -85,6 +88,25 @@ try {
ok(r.fromFile === 0 && !r.fired, `${c.name}: nothing from the file became an element (${r.fromFile}) and its handler never ran (${r.fired})`)
await p.close()
}
+ // window.bento.loadDoc, the scripted twin of Replace from JSON: a document it
+ // refuses throws a FIXED message (a caller may render it), with the detail,
+ // which can quote the file, on its own property as data.
+ {
+ const p = await browser.newPage()
+ await p.route(/^https?:/, (r) => r.abort())
+ await p.goto(new URL('../type/dist-single/Bento_Type.bento.html', import.meta.url).href)
+ await p.waitForFunction(() => window.bento?.doc)
+ const r = await p.evaluate((markup) => {
+ const before = JSON.stringify(window.bento.doc)
+ try { window.bento.loadDoc(JSON.stringify({ format: markup, body: [] })); return { threw: false } }
+ catch (e) { return { threw: true, message: String(e.message), detail: String(e.detail ?? ''), unchanged: JSON.stringify(window.bento.doc) === before } }
+ }, MARKUP)
+ ok(r.threw && r.message === 'bento/type: the document could not be read (format)' && !/onerror|<img|bold/.test(r.message),
+ `bento.loadDoc refuses with a fixed message carrying no file text (${JSON.stringify(r.message)})`)
+ ok(r.detail.includes('<img src=x onerror='), 'and the detail is still there for a scripting caller, on its own property')
+ ok(r.unchanged, 'and the open document is untouched')
+ await p.close()
+ }
} finally {
await browser.close()
}
diff --git a/type/src/main.ts b/type/src/main.ts
index c8991cef..996289f6 100644
--- a/type/src/main.ts
+++ b/type/src/main.ts
@@ -1096,7 +1096,15 @@ dirty = false; paintTitle();
serialize: () => JSON.stringify(store.doc),
loadDoc: (json: string) => {
const parsed = parseDoc(json);
- if (!parsed.ok) throw new Error(`bento/type: ${parsed.err === 'empty' ? 'empty document' : parsed.detail}`);
+ if (!parsed.ok) {
+ // The MESSAGE is a fixed sentence: whoever catches this may render it,
+ // and `detail` can quote the file (an unknown format's own name, a JSON
+ // parser's message about the bytes). The detail rides on its own
+ // property for a scripting caller that wants it, as data.
+ const err = new Error(`bento/type: the document could not be read (${parsed.err})`);
+ if (parsed.err !== 'empty') Object.assign(err, { detail: parsed.detail });
+ throw err;
+ }
store.replace(keepLiveHistory(store.doc, parsed.doc)); // the same rule as Replace from JSON
editor.render();
schedule();