3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 14:45:44 +01:00

Read adfToHtml's HTML in htmlToAdf as a browser serializes it too

This commit is contained in:
2026-10-31 01:48:10 +01:00
committed by Lillem4n
parent 088494511d
commit 6cc59f8574
13 changed files with 199 additions and 57 deletions
+10 -1
View File
@@ -2,14 +2,23 @@ try {
const { adfToHtml, adfToLosslessMarkdown, htmlToAdf, isAdfDocument, losslessMarkdownToAdf } = await import('/dist/index.js')
// WebDriver's JSON reads -0 back as 0, so a document crosses as JSON text with -0 tagged; run.js revives it.
const spelled = (result) => (result.ok ? { ok: true, value: JSON.stringify(result.value, (_, value) => (Object.is(value, -0) ? '\u0000-0' : value)) } : result)
// The page's own innerHTML is what a viewer/editor app posts back, so htmlToAdf reads what the browser writes.
const throughInnerHtml = (adf) => {
const html = adfToHtml(adf)
if (!html.ok) return { parsed: html }
const page = document.createElement('div')
page.innerHTML = html.value
return { parsed: spelled(htmlToAdf(page.innerHTML)) }
}
window.convertCorpus = (corpus) => ({
browserDocuments: corpus.browserDocuments.map(({ json, name }) => ({ name, ...throughInnerHtml(JSON.parse(json)) })),
errors: corpus.errors.map(({ markdown, name }) => ({ name, parsed: losslessMarkdownToAdf(markdown) })),
normalization: corpus.normalization.map(({ markdown, name }) => ({ name, parsed: spelled(losslessMarkdownToAdf(markdown)) })),
realPayloads: corpus.realPayloads.map(({ json, name }) => {
const adf = JSON.parse(json)
const emitted = adfToLosslessMarkdown(adf)
const html = adfToHtml(adf)
return { emitted, html, htmlParsed: html.ok ? spelled(htmlToAdf(html.value)) : undefined, isDocument: isAdfDocument(adf), name, parsed: emitted.ok ? spelled(losslessMarkdownToAdf(emitted.value)) : undefined }
return { emitted, html, htmlParsed: html.ok ? spelled(htmlToAdf(html.value)) : undefined, innerHtml: throughInnerHtml(adf), isDocument: isAdfDocument(adf), name, parsed: emitted.ok ? spelled(losslessMarkdownToAdf(emitted.value)) : undefined }
}),
roundTrip: corpus.roundTrip.map(({ json, markdown, name }) => {
const adf = JSON.parse(json)
+29 -1
View File
@@ -70,7 +70,25 @@ async function whenDriverAnswers() {
}
}
// Text the browser's innerHTML spells other than adfToHtml does (WHATWG HTML §13.3): a raw ", &nbsp;, and in an attribute < and > escaped or not by version.
const text = (value, ...marks) => (marks.length === 0 ? { text: value, type: 'text' } : { marks, text: value, type: 'text' })
const browserDocuments = [
{
json: JSON.stringify({
content: [
{ content: [text('Say "hi"\u00a0now & <then>'), text(' link', { attrs: { href: 'https://example.com/?a<b>c&d="e"', title: 'A "title"\u00a0<here>' }, type: 'link' })], type: 'paragraph' },
{ attrs: { language: 'js' }, content: [text('if (a < b) say("x\u00a0y")\n done()')], type: 'codeBlock' },
{ content: [{ attrs: { id: '0199f4c2-8b1e-7d03-9a6f-2c5e8d71b4a0', text: 'Say "hi"\u00a0now' }, type: 'mention' }], type: 'paragraph' },
{ attrs: { panelType: 'info' }, content: [{ content: [text('A "quote"\u00a0here', { type: 'underline' }, { attrs: { href: 'https://example.com/?a<b>', title: 'T"\u00a0' }, type: 'link' })], type: 'paragraph' }], type: 'panel' },
],
type: 'doc',
version: 1,
}),
name: 'quotes, no-break spaces and angle brackets',
},
]
const corpus = {
browserDocuments,
errors: fixtureNames('errors', '.md').map((name) => ({ markdown: fixture(name, '.md'), name })),
normalization: fixtureNames('normalization', '.md').map((name) => ({ markdown: fixture(name, '.md'), name })),
realPayloads: fixtureNames('real-payloads', '.json').map((name) => ({ json: fixture(name, '.json'), name })),
@@ -135,6 +153,16 @@ for (const [index, { json, name }] of corpus.realPayloads.entries()) {
assert.deepEqual(result.html, adfToHtml(JSON.parse(json)), 'it wrote other HTML than Node wrote')
assert.ok(result.htmlParsed.ok, `its HTML did not read back — ${refusal(result.htmlParsed)}`)
assert.deepEqual(revived(result.htmlParsed.value), JSON.parse(json))
assert.ok(result.innerHtml.parsed.ok, `its innerHTML did not read back — ${refusal(result.innerHtml.parsed)}`)
assert.deepEqual(revived(result.innerHtml.parsed.value), JSON.parse(json))
})
}
for (const [index, { json, name }] of corpus.browserDocuments.entries()) {
const result = results.browserDocuments[index]
checking(name, result, () => {
assert.ok(result.parsed.ok, `its innerHTML did not read back — ${refusal(result.parsed)}`)
assert.deepEqual(revived(result.parsed.value), JSON.parse(json))
})
}
@@ -147,5 +175,5 @@ for (const [index, { name }] of corpus.errors.entries()) {
}
console.log(
`Firefox ${session.capabilities.browserVersion} converted ${corpus.roundTrip.length} round-trip, ${corpus.normalization.length} normalization and ${corpus.errors.length} error fixtures and ${corpus.realPayloads.length} real payloads`,
`Firefox ${session.capabilities.browserVersion} converted ${corpus.roundTrip.length} round-trip, ${corpus.normalization.length} normalization and ${corpus.errors.length} error fixtures, ${corpus.realPayloads.length} real payloads and ${corpus.browserDocuments.length} documents through innerHTML`,
)