mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 06:05:43 +01:00
Pin the remaining HTML refusal messages, and drop guards a mutant showed redundant
This commit is contained in:
@@ -5,17 +5,19 @@ import { dataAttributes, readDataAttributes } from './data-attributes.ts'
|
||||
import { nodeAttrs, nodeMarks } from '../adf/document.ts'
|
||||
|
||||
// The `text-align` value each alignment names.
|
||||
const alignments: ReadonlyMap<string, string> = new Map([
|
||||
['center', 'center'],
|
||||
['end', 'right'],
|
||||
])
|
||||
function alignments(): ReadonlyMap<string, string> {
|
||||
return new Map([
|
||||
['center', 'center'],
|
||||
['end', 'right'],
|
||||
])
|
||||
}
|
||||
|
||||
// A paragraph's or heading's attributes: its alignment mark as a `style` attribute, then the rest as `dataAttributes` writes them; `undefined` where it holds a mark besides one alignment the style spells.
|
||||
export function textBlockAttributes(node: AdfNode, vocabulary: AttributeVocabulary, written: readonly string[]): string | undefined {
|
||||
const [mark, ...others] = nodeMarks(node)
|
||||
if (mark === undefined) return dataAttributes(node, vocabulary, written)
|
||||
const attrs = nodeAttrs(mark)
|
||||
const textAlign = others.length === 0 && mark.type === 'alignment' && Object.keys(attrs).length === 1 ? alignments.get(String(attrs['align'])) : undefined
|
||||
const textAlign = others.length === 0 && mark.type === 'alignment' && Object.keys(attrs).length === 1 ? alignments().get(String(attrs['align'])) : undefined
|
||||
const bare: AdfNode = { ...node }
|
||||
delete bare.marks
|
||||
const data = textAlign === undefined ? undefined : dataAttributes(bare, vocabulary, written)
|
||||
@@ -25,7 +27,7 @@ export function textBlockAttributes(node: AdfNode, vocabulary: AttributeVocabula
|
||||
// The attributes and marks a paragraph's or heading's start tag holds, as `textBlockAttributes` writes them.
|
||||
export function readTextBlockAttributes(pairs: readonly (readonly [string, string])[], element: string, vocabulary: AttributeVocabulary, written: readonly string[]): Read<{ attrs: AdfAttributes; marks: AdfMark[] }> {
|
||||
const [first] = pairs
|
||||
const align = first?.[0] === 'style' ? [...alignments].find(([, textAlign]) => first[1] === `text-align:${textAlign}`)?.[0] : undefined
|
||||
const align = first?.[0] === 'style' ? [...alignments()].find(([, textAlign]) => first[1] === `text-align:${textAlign}`)?.[0] : undefined
|
||||
if (first?.[0] === 'style' && align === undefined) return { fault: { code: 'unmappable-html', message: `style on <${element}> holds text-align:center or text-align:right` } }
|
||||
const data = readDataAttributes(align === undefined ? pairs : pairs.slice(1), element, vocabulary, written)
|
||||
if (data.fault !== undefined) return data
|
||||
|
||||
@@ -10,6 +10,7 @@ export function colourDeclaration(property: string, value: JsonValue | undefined
|
||||
|
||||
// The colour a `property:colour` declaration holds, `undefined` where it holds another property or no such colour.
|
||||
export function readColour(property: string, declaration: string): string | undefined {
|
||||
const value = declaration.slice(property.length + 1)
|
||||
return declaration.startsWith(`${property}:`) && colour.test(value) ? value : undefined
|
||||
const prefix = `${property}:`
|
||||
const value = declaration.slice(prefix.length)
|
||||
return declaration.startsWith(prefix) && colour.test(value) ? value : undefined
|
||||
}
|
||||
|
||||
@@ -689,6 +689,13 @@ test('writes panels, expands and nested expands nested 500 levels, and refuses t
|
||||
}
|
||||
})
|
||||
|
||||
test('writes a caption as a figcaption, refuses an inline carry nested past 500 in it, and carries what is no caption', () => {
|
||||
const media: AdfNode = { attrs: { type: 'external', url: 'https://example.com/moon.jpg' }, type: 'media' }
|
||||
const deep = nested('unknownInline', largestNesting, text('x'))
|
||||
assert.equal(refusal(adfToHtml(document({ content: [media, { content: [deep], type: 'caption' }], type: 'mediaSingle' }))), 'unsupported-nesting-depth at /content/0/content/1/content/0')
|
||||
assert.equal(carriedBlock({ content: [media, paragraph(text('a'))], type: 'mediaSingle' }), true)
|
||||
})
|
||||
|
||||
test('carries a media or a paragraph whose marks only look like the ones HTML spells', () => {
|
||||
const external: AdfAttributes = { type: 'external', url: 'https://example.com/moon.jpg' }
|
||||
const single = (media: AdfNode): AdfNode => ({ content: [media], type: 'mediaSingle' })
|
||||
|
||||
@@ -61,7 +61,7 @@ export function readMediaImage(pairs: readonly (readonly [string, string])[]): R
|
||||
// The attributes of the `<img>` a media is, `undefined` where a value has no spelling there.
|
||||
function imageAttributes(media: AdfNode): string | undefined {
|
||||
const { alt, height, type, url, width } = nodeAttrs(media)
|
||||
const src = url === undefined ? '' : typeof url === 'string' ? srcAttribute(url) : undefined
|
||||
const src = typeof url === 'string' ? srcAttribute(url) : undefined
|
||||
const own = [url === undefined ? ` class="${imageClass}"` : src, altAttribute(alt), sizeAttribute('height', height), sizeAttribute('width', width)]
|
||||
// A url without a type reads back as external, so only an external media may leave its type out.
|
||||
const written = url !== undefined && type === 'external' ? [...imageWritten, 'type'] : imageWritten
|
||||
@@ -87,7 +87,6 @@ function sizeAttribute(name: string, value: JsonValue | undefined): string | und
|
||||
|
||||
function markedLink(mark: { attrs?: AdfAttributes; type: string }): { href: string; title?: string } | undefined {
|
||||
const attrs = nodeAttrs(mark)
|
||||
const keys = Object.keys(attrs)
|
||||
if (mark.type !== 'link' || keys.length === 0 || keys.some((key) => key !== 'href' && key !== 'title')) return undefined
|
||||
if (mark.type !== 'link' || Object.keys(attrs).some((key) => key !== 'href' && key !== 'title')) return undefined
|
||||
return linkAttrs(attrs)
|
||||
}
|
||||
|
||||
@@ -716,6 +716,12 @@ const pinnedRefusals: readonly (readonly [string, string])[] = [
|
||||
['<p style="color:red">a</p>\n', 'unmappable-html at /content/0: style on <p> holds text-align:center or text-align:right'],
|
||||
['<h1 style="text-align:left">a</h1>\n', 'unmappable-html at /content/0: style on <h1> holds text-align:center or text-align:right'],
|
||||
['<p>a <span title="color:red">b</span></p>\n', 'unmappable-html at /content/0/content/1: a <span> of the dialect is a carry element, holding class="adf-carry" and then data-adf alone, or opens with class="adf-emoji", "adf-mention" or "adf-status": found "<span title=\\"color:red\\">"…'],
|
||||
['<blockquote><p>a</p></blockquote>\n', 'unmappable-html at /content/0: <blockquote> holds </blockquote> straight after it, or a line feed and its first block: found "<p>a</p></blockquote>\\n"'],
|
||||
['<ul><li>a</li></ul>\n', 'unmappable-html at /content/0: <ul> holds </ul> straight after it, or a line feed and its first item: found "<li>a</li></ul>\\n"'],
|
||||
['<details class="adf-expand">\n<summary>a\n</details>\n', 'unmappable-html at /content/0: <summary> ends at </summary>: found "a\\n</details>\\n"'],
|
||||
['<p>a < b</p>\n', 'unmappable-html at /content/0: </p> ends the paragraph, and its text spells < as <: found "< b</p>\\n"'],
|
||||
['<ul class="adf-task-list">\n<b>x</b>\n</ul>\n', 'unmappable-html at /content/0/content/0: <b> as written here is no list item of the dialect: found "<b>x</b>\\n</ul>\\n"'],
|
||||
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox" disabled=""> a<b>x</b></li>\n</ul>\n', 'unmappable-html at /content/0/content/0: <b> as written here is no inline element of the dialect: found "<b>x</b></li>\\n</ul>\\n"'],
|
||||
]
|
||||
|
||||
for (const [html, expected] of pinnedRefusals) {
|
||||
|
||||
@@ -56,6 +56,6 @@ function readItemAttributes(list: TaskListType, pairs: readonly (readonly [strin
|
||||
function checkbox(html: string, index: number, done: boolean): number | undefined {
|
||||
const input = readStartTag(html, index)
|
||||
const expected = [['type', 'checkbox'], ...(done ? [['checked', '']] : []), ['disabled', '']]
|
||||
const same = input?.name === 'input' && input.attributes.length === expected.length && input.attributes.every(([name, value], at) => name === expected[at]?.[0] && value === expected[at]?.[1])
|
||||
const same = input?.name === 'input' && JSON.stringify(input.attributes) === JSON.stringify(expected)
|
||||
return same ? input.end : undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user