mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 14:45:44 +01:00
Write no style attribute in adfToHtml, and name every spelling of <div>, <span> and <mark> in their refusals
This commit is contained in:
@@ -9,6 +9,7 @@ const expandTypes: readonly ExpandType[] = ['expand', 'nestedExpand']
|
||||
const listTypes: readonly TaskListType[] = ['decisionList', 'taskList']
|
||||
|
||||
export const expandClasses: Readonly<Record<ExpandType, string>> = { expand: 'adf-expand', nestedExpand: 'adf-nested-expand' }
|
||||
export const mediaGroupClass = 'adf-media-group'
|
||||
export const panelClass = 'adf-panel'
|
||||
// A panel's type is no attribute of the vocabularies, since a directive spells it as its argument; here it is a data-* attribute like the rest.
|
||||
export const panelAttributes: AttributeVocabulary = { ...blockNodes.panel.attributes, panelType: 'string' }
|
||||
|
||||
+17
-15
@@ -2,14 +2,16 @@ import type { AdfAttributes, AdfNode } from '../adf/document.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import type { Read } from '../result.ts'
|
||||
import { blockNodes } from '../adf/block-nodes.ts'
|
||||
import { colourDeclaration, readColour } from './colour-style.ts'
|
||||
import { dataAttributes, dataValue, readDataAttributes } from './data-attributes.ts'
|
||||
import { isNumberList } from '../adf/attribute-vocabulary.ts'
|
||||
import { nodeAttrs } from '../adf/document.ts'
|
||||
import { optionalAttribute } from './escaping.ts'
|
||||
import { serializeCanonicalJson } from '../canonical-json.ts'
|
||||
|
||||
const elementWritten = ['background', 'colspan', 'colwidth', 'rowspan']
|
||||
const leading = ['colspan', 'rowspan', 'style', 'data-colwidth']
|
||||
const leading = ['colspan', 'rowspan', 'data-background-color', 'data-colwidth']
|
||||
// `0.3.1` wrote a cell's background as this `style` in `data-background-color`'s place, and stored HTML still reads.
|
||||
const releasedBackground = /^background-color:(#?[0-9a-zA-Z]+)$/
|
||||
const span = /^[1-9][0-9]*$/
|
||||
|
||||
export function isHeaderRow(row: { content?: AdfNode[] }): boolean {
|
||||
@@ -20,7 +22,7 @@ export function isHeaderRow(row: { content?: AdfNode[] }): boolean {
|
||||
// A cell's attributes in the order `readCellAttributes` takes them; `undefined` where a value has no spelling on the element, so the whole table rides the carry.
|
||||
export function cellAttributes(cell: AdfNode): string | undefined {
|
||||
const { background, colspan, colwidth, rowspan } = nodeAttrs(cell)
|
||||
const own = [spanAttribute('colspan', colspan), spanAttribute('rowspan', rowspan), styleAttribute(background), widthsAttribute(colwidth), dataAttributes(cell, blockNodes.tableCell.attributes, elementWritten)]
|
||||
const own = [spanAttribute('colspan', colspan), spanAttribute('rowspan', rowspan), backgroundAttribute(background), widthsAttribute(colwidth), dataAttributes(cell, blockNodes.tableCell.attributes, elementWritten)]
|
||||
return own.every((attribute) => attribute !== undefined) ? own.join('') : undefined
|
||||
}
|
||||
|
||||
@@ -29,41 +31,41 @@ export function readCellAttributes(pairs: readonly (readonly [string, string])[]
|
||||
let rest = pairs
|
||||
for (const name of leading) {
|
||||
const [first] = rest
|
||||
if (first?.[0] !== name) continue
|
||||
const value = ownValue(name, first[1])
|
||||
if (value === undefined) return { fault: { code: name === 'data-colwidth' ? 'unsupported-node-shape' : 'unmappable-html', message: `${name} on <${element}> spells no value adfToHtml writes there` } }
|
||||
const released = name === 'data-background-color' && first?.[0] === 'style'
|
||||
if (first?.[0] !== name && !released) continue
|
||||
const value = released ? releasedBackground.exec(first[1])?.[1] : ownValue(name, first[1])
|
||||
if (value === undefined) return { fault: { code: name === 'data-colwidth' ? 'unsupported-node-shape' : 'unmappable-html', message: `${first[0]} on <${element}> spells no value adfToHtml writes there` } }
|
||||
attrs[ownKey(name)] = value
|
||||
rest = rest.slice(1)
|
||||
}
|
||||
const misplaced = rest.find(([name]) => leading.includes(name))
|
||||
if (misplaced !== undefined) return { fault: { code: 'unmappable-html', message: `<${element}> takes colspan, rowspan, style and data-colwidth first, each once and in that order: found ${misplaced[0]} out of place` } }
|
||||
if (misplaced !== undefined) return { fault: { code: 'unmappable-html', message: `<${element}> takes colspan, rowspan, data-background-color and data-colwidth first, each once and in that order: found ${misplaced[0]} out of place` } }
|
||||
const data = readDataAttributes(rest, element, blockNodes.tableCell.attributes, elementWritten)
|
||||
return data.fault === undefined ? { value: { ...attrs, ...data.value } } : data
|
||||
}
|
||||
|
||||
function backgroundAttribute(background: JsonValue | undefined): string | undefined {
|
||||
if (background === undefined) return ''
|
||||
return typeof background === 'string' ? optionalAttribute('data-background-color', background) : undefined
|
||||
}
|
||||
|
||||
function spanAttribute(name: string, value: JsonValue | undefined): string | undefined {
|
||||
if (value === undefined) return ''
|
||||
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0 ? ` ${name}="${value}"` : undefined
|
||||
}
|
||||
|
||||
function styleAttribute(background: JsonValue | undefined): string | undefined {
|
||||
if (background === undefined) return ''
|
||||
const declaration = colourDeclaration('background-color', background)
|
||||
return declaration === undefined ? undefined : ` style="${declaration}"`
|
||||
}
|
||||
|
||||
function widthsAttribute(colwidth: JsonValue | undefined): string | undefined {
|
||||
if (colwidth === undefined) return ''
|
||||
return isNumberList(colwidth) ? ` data-colwidth="${serializeCanonicalJson(colwidth, 'compact').slice(1, -1)}"` : undefined
|
||||
}
|
||||
|
||||
function ownKey(name: string): string {
|
||||
if (name === 'style') return 'background'
|
||||
if (name === 'data-background-color') return 'background'
|
||||
return name === 'data-colwidth' ? 'colwidth' : name
|
||||
}
|
||||
|
||||
function ownValue(name: string, spelled: string): JsonValue | undefined {
|
||||
if (name === 'style') return readColour('background-color', spelled)
|
||||
if (name === 'data-background-color') return spelled
|
||||
if (name === 'data-colwidth') return dataValue(`[${spelled}]`, 'numbers')
|
||||
const value = Number(spelled)
|
||||
return span.test(spelled) && Number.isSafeInteger(value) ? value : undefined
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
|
||||
// Letters and digits alone, after an optional `#`, so no value can end the declaration or call `url()`.
|
||||
const colour = /^#?[0-9a-zA-Z]+$/
|
||||
|
||||
// `property:colour`, `undefined` where the value is no colour the declaration can hold.
|
||||
export function colourDeclaration(property: string, value: JsonValue | undefined): string | undefined {
|
||||
return typeof value === 'string' && colour.test(value) ? `${property}:${value}` : 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 prefix = `${property}:`
|
||||
const value = declaration.slice(prefix.length)
|
||||
return declaration.startsWith(prefix) && colour.test(value) ? value : undefined
|
||||
}
|
||||
@@ -191,17 +191,18 @@ test('carries a media node HTML cannot spell: a width that is no whole number, a
|
||||
assert.equal(carriedBlock({ content: [{ content: [text('a')], type: 'paragraph' }], type: 'mediaGroup' }), true)
|
||||
})
|
||||
|
||||
test('writes underline, subscript, superscript and the two colours as elements, a colour in a style attribute', () => {
|
||||
test('writes underline, subscript, superscript and the two colours as elements, a colour in a data-* attribute', () => {
|
||||
const marked = (...marks: AdfMark[]): string => html(adfToHtml(document(paragraph(text('x', ...marks)))))
|
||||
assert.equal(marked({ type: 'underline' }), '<p><u>x</u></p>\n')
|
||||
assert.equal(marked({ attrs: { type: 'sub' }, type: 'subsup' }), '<p><sub>x</sub></p>\n')
|
||||
assert.equal(marked({ attrs: { type: 'sup' }, type: 'subsup' }), '<p><sup>x</sup></p>\n')
|
||||
assert.equal(marked({ attrs: { color: '#ff5630' }, type: 'textColor' }), '<p><span style="color:#ff5630">x</span></p>\n')
|
||||
assert.equal(marked({ attrs: { color: '#fffae6' }, type: 'backgroundColor' }), '<p><mark style="background-color:#fffae6">x</mark></p>\n')
|
||||
assert.equal(marked({ type: 'strong' }, { type: 'underline' }, { attrs: { color: 'red' }, type: 'textColor' }), '<p><strong><u><span style="color:red">x</span></u></strong></p>\n')
|
||||
assert.equal(marked({ attrs: { color: '#ff5630' }, type: 'textColor' }), '<p><span data-text-color="#ff5630">x</span></p>\n')
|
||||
assert.equal(marked({ attrs: { color: '#fffae6' }, type: 'backgroundColor' }), '<p><mark data-background-color="#fffae6">x</mark></p>\n')
|
||||
assert.equal(marked({ type: 'strong' }, { type: 'underline' }, { attrs: { color: 'red' }, type: 'textColor' }), '<p><strong><u><span data-text-color="red">x</span></u></strong></p>\n')
|
||||
assert.equal(marked({ attrs: { color: 'rgb(1, 2, 3) "x"' }, type: 'backgroundColor' }), '<p><mark data-background-color="rgb(1, 2, 3) "x"">x</mark></p>\n')
|
||||
})
|
||||
|
||||
test('carries text holding a mark HTML cannot spell: attributes besides those the element takes, a colour that is no plain value, or a type that is neither sub nor sup', () => {
|
||||
test('carries text holding a mark HTML cannot spell: attributes besides those the element takes, a colour that is no string, or a type that is neither sub nor sup', () => {
|
||||
const carriedText = (mark: AdfMark): boolean => html(adfToHtml(document(paragraph(text('x', mark))))).includes('adf-carry')
|
||||
for (const mark of [
|
||||
{ attrs: {}, type: 'underline' },
|
||||
@@ -212,21 +213,17 @@ test('carries text holding a mark HTML cannot spell: attributes besides those th
|
||||
{ type: 'textColor' },
|
||||
{ attrs: { color: 'red', x: 1 }, type: 'textColor' },
|
||||
{ attrs: { color: 7 }, type: 'textColor' },
|
||||
{ attrs: { color: '' }, type: 'textColor' },
|
||||
{ attrs: { color: 'red;x:y' }, type: 'textColor' },
|
||||
{ attrs: { color: 'rgb(1,2,3)' }, type: 'backgroundColor' },
|
||||
{ attrs: { color: '#fff\n' }, type: 'backgroundColor' },
|
||||
{ type: 'backgroundColor' },
|
||||
]) assert.equal(carriedText(mark), true, JSON.stringify(mark))
|
||||
})
|
||||
|
||||
test('writes a paragraph or heading marked with center or end alignment as a style attribute, end as right', () => {
|
||||
test('writes a paragraph or heading marked with center or end alignment as a data-align attribute', () => {
|
||||
const aligned = (align: string, node: AdfNode = paragraph(text('x'))): string => html(adfToHtml(document({ ...node, marks: [{ attrs: { align }, type: 'alignment' }] })))
|
||||
assert.equal(aligned('center'), '<p style="text-align:center">x</p>\n')
|
||||
assert.equal(aligned('end'), '<p style="text-align:right">x</p>\n')
|
||||
assert.equal(aligned('center', { attrs: { level: 2 }, content: [text('x')], type: 'heading' }), '<h2 style="text-align:center">x</h2>\n')
|
||||
assert.equal(aligned('end', { attrs: { localId: ids.paragraph }, type: 'paragraph' }), `<p style="text-align:right" data-local-id="${ids.paragraph}"></p>\n`)
|
||||
assert.equal(html(adfToHtml(document({ content: [item({ content: [text('x')], marks: [{ attrs: { align: 'center' }, type: 'alignment' }], type: 'paragraph' })], type: 'bulletList' }))), '<ul>\n<li><p style="text-align:center">x</p></li>\n</ul>\n')
|
||||
assert.equal(aligned('center'), '<p data-align="center">x</p>\n')
|
||||
assert.equal(aligned('end'), '<p data-align="end">x</p>\n')
|
||||
assert.equal(aligned('center', { attrs: { level: 2 }, content: [text('x')], type: 'heading' }), '<h2 data-align="center">x</h2>\n')
|
||||
assert.equal(aligned('end', { attrs: { localId: ids.paragraph }, type: 'paragraph' }), `<p data-align="end" data-local-id="${ids.paragraph}"></p>\n`)
|
||||
assert.equal(html(adfToHtml(document({ content: [item({ content: [text('x')], marks: [{ attrs: { align: 'center' }, type: 'alignment' }], type: 'paragraph' })], type: 'bulletList' }))), '<ul>\n<li><p data-align="center">x</p></li>\n</ul>\n')
|
||||
})
|
||||
|
||||
test('carries a paragraph or heading holding an alignment HTML cannot spell, or a mark besides it', () => {
|
||||
@@ -445,12 +442,13 @@ test('writes a table\'s attributes as the writer panel picked them', () => {
|
||||
const table: AdfNode = { attrs: { isNumberColumnEnabled: true, layout: 'wide', width: 760 }, content: [{ attrs: { localId: 'r1' }, content: [{ attrs, content: [paragraph(text('Sold as a pair'))], type: 'tableCell' }], type: 'tableRow' }], type: 'table' }
|
||||
assert.equal(
|
||||
html(adfToHtml(document(table))),
|
||||
'<table data-is-number-column-enabled="true" data-layout="wide" data-width="760">\n<tbody>\n<tr data-local-id="r1"><td colspan="2" rowspan="3" style="background-color:#fffae6" data-colwidth="120,80" data-local-id="c1" data-valign="top">Sold as a pair</td></tr>\n</tbody>\n</table>\n',
|
||||
'<table data-is-number-column-enabled="true" data-layout="wide" data-width="760">\n<tbody>\n<tr data-local-id="r1"><td colspan="2" rowspan="3" data-background-color="#fffae6" data-colwidth="120,80" data-local-id="c1" data-valign="top">Sold as a pair</td></tr>\n</tbody>\n</table>\n',
|
||||
)
|
||||
assert.equal(html(adfToHtml(document({ content: [{ content: [{ attrs: { background: 'red', colwidth: [0.5] }, type: 'tableHeader' }], type: 'tableRow' }], type: 'table' }))), '<table>\n<thead>\n<tr><th style="background-color:red" data-colwidth="0.5"></th></tr>\n</thead>\n</table>\n')
|
||||
assert.equal(html(adfToHtml(document({ content: [{ content: [{ attrs: { background: 'red', colwidth: [0.5] }, type: 'tableHeader' }], type: 'tableRow' }], type: 'table' }))), '<table>\n<thead>\n<tr><th data-background-color="red" data-colwidth="0.5"></th></tr>\n</thead>\n</table>\n')
|
||||
assert.equal(html(adfToHtml(document({ content: [{ content: [{ attrs: { background: 'url("x")' }, type: 'tableCell' }], type: 'tableRow' }], type: 'table' }))), '<table>\n<tbody>\n<tr><td data-background-color="url("x")"></td></tr>\n</tbody>\n</table>\n')
|
||||
})
|
||||
|
||||
const carriedCellAttrs: readonly AdfAttributes[] = [{ background: 'url(x)' }, { background: 'red;color:blue' }, { background: '' }, { background: 7 }, { colspan: 0 }, { colspan: 1.5 }, { colspan: '2' }, { rowspan: -1 }, { colwidth: [] }, { colwidth: ['1'] }, { colwidth: 120 }, { valign: 7 }, { size: 'big' }]
|
||||
const carriedCellAttrs: readonly AdfAttributes[] = [{ background: 7 }, { colspan: 0 }, { colspan: 1.5 }, { colspan: '2' }, { rowspan: -1 }, { colwidth: [] }, { colwidth: ['1'] }, { colwidth: 120 }, { valign: 7 }, { size: 'big' }]
|
||||
|
||||
for (const attrs of carriedCellAttrs) {
|
||||
test(`carries the whole table where a cell holds ${JSON.stringify(attrs)}`, () => {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { carryContent } from '../carry-content.ts'
|
||||
import { cellAttributes, isHeaderRow } from '../cell-element.ts'
|
||||
import { containerElements, headingElement, listStart } from '../block-elements.ts'
|
||||
import { dataAttributes } from '../data-attributes.ts'
|
||||
import { expandClasses, panelAttributes, panelClass, taskLists } from '../block-classes.ts'
|
||||
import { expandClasses, mediaGroupClass, panelAttributes, panelClass, taskLists } from '../block-classes.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { firstParagraphForm } from '../first-paragraph.ts'
|
||||
import { imageElement } from '../image-element.ts'
|
||||
@@ -250,7 +250,7 @@ function tryMediaGroup(node: AdfNode): string | undefined {
|
||||
if (attributes === undefined) return undefined
|
||||
files += `<span class="adf-file"${attributes}></span>`
|
||||
}
|
||||
return `<div class="adf-media-group">${files}</div>`
|
||||
return `<div class="${mediaGroupClass}">${files}</div>`
|
||||
}
|
||||
|
||||
// `undefined` where `attributes` is undefined, so the node rides the carry.
|
||||
|
||||
+16
-14
@@ -1,7 +1,7 @@
|
||||
import type { AdfMark } from '../adf/document.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { colourDeclaration, readColour } from './colour-style.ts'
|
||||
import { nodeAttrs } from '../adf/document.ts'
|
||||
import { optionalAttribute } from './escaping.ts'
|
||||
|
||||
type MarkTag = { closer: string; opener: string }
|
||||
|
||||
@@ -13,10 +13,10 @@ const markElements: ReadonlyMap<string, string> = new Map([
|
||||
['strong', 'strong'],
|
||||
['underline', 'u'],
|
||||
])
|
||||
// The element and CSS property each colour mark writes around text, its colour in a `style` attribute.
|
||||
const colourElements: ReadonlyMap<string, { element: string; property: string }> = new Map([
|
||||
['backgroundColor', { element: 'mark', property: 'background-color' }],
|
||||
['textColor', { element: 'span', property: 'color' }],
|
||||
// The element each colour mark writes around text, its colour in the attribute `colourAttributeOn` names.
|
||||
const colourElements: ReadonlyMap<string, 'mark' | 'span'> = new Map<string, 'mark' | 'span'>([
|
||||
['backgroundColor', 'mark'],
|
||||
['textColor', 'span'],
|
||||
])
|
||||
const scripts: ReadonlyMap<JsonValue | undefined, 'sub' | 'sup'> = new Map<JsonValue | undefined, 'sub' | 'sup'>([
|
||||
['sub', 'sub'],
|
||||
@@ -34,22 +34,24 @@ export function markTag(mark: AdfMark): MarkTag | undefined {
|
||||
}
|
||||
const colour = colourElements.get(mark.type)
|
||||
if (colour === undefined) return undefined
|
||||
const declaration = Object.keys(attrs).length === 1 ? colourDeclaration(colour.property, attrs['color']) : undefined
|
||||
return declaration === undefined ? undefined : tagOf(colour.element, ` style="${declaration}"`)
|
||||
const { color } = attrs
|
||||
const attribute = Object.keys(attrs).length === 1 && typeof color === 'string' ? optionalAttribute(colourAttributeOn(colour), color) : undefined
|
||||
return attribute === undefined ? undefined : tagOf(colour, attribute)
|
||||
}
|
||||
|
||||
// The mark a start tag but an `<a>` opens, as `markTag` writes it; `undefined` where it opens none.
|
||||
export function markOfTag(name: string, attributes: readonly (readonly [string, string])[]): AdfMark | undefined {
|
||||
const [style, ...others] = attributes
|
||||
if (style === undefined) return markOfElement(name)
|
||||
if (style[0] !== 'style' || others.length > 0) return undefined
|
||||
for (const [type, { element, property }] of colourElements) {
|
||||
const color = element === name ? readColour(property, style[1]) : undefined
|
||||
if (color !== undefined) return { attrs: { color }, type }
|
||||
}
|
||||
const [colour, ...others] = attributes
|
||||
if (colour === undefined) return markOfElement(name)
|
||||
if (others.length > 0) return undefined
|
||||
for (const [type, element] of colourElements) if (element === name && colourAttributeOn(element) === colour[0]) return { attrs: { color: colour[1] }, type }
|
||||
return undefined
|
||||
}
|
||||
|
||||
export function colourAttributeOn(element: 'mark' | 'span'): string {
|
||||
return element === 'mark' ? 'data-background-color' : 'data-text-color'
|
||||
}
|
||||
|
||||
function markOfElement(name: string): AdfMark | undefined {
|
||||
for (const [type, element] of markElements) if (element === name) return { type }
|
||||
return scripts.has(name) ? { attrs: { type: name }, type: 'subsup' } : undefined
|
||||
|
||||
@@ -4,8 +4,10 @@ import { carriedNesting, carriedNode, carriedValue } from '../../adf/carried-jso
|
||||
import { carryContent } from '../carry-content.ts'
|
||||
import { carryJsonOffset, readCarryAttributes } from '../carry-attributes.ts'
|
||||
import { classesOfElement } from '../node-element.ts'
|
||||
import { colourAttributeOn } from '../mark-elements.ts'
|
||||
import { foundAt, refused, type Place, type ReadNode, type StartTag } from './source.ts'
|
||||
import { holdableJson, readsAsShown } from '../escaping.ts'
|
||||
import { mediaGroupClass, panelClass } from '../block-classes.ts'
|
||||
import { spellsCanonicalJson } from '../../canonical-json.ts'
|
||||
import { success } from '../../result.ts'
|
||||
|
||||
@@ -15,10 +17,7 @@ const holder = "a carry element's data-adf"
|
||||
export function readCarry(html: string, tag: StartTag, element: 'div' | 'span', levels: number, place: Place): ReadNode {
|
||||
const json = readCarryAttributes(tag.attributes)
|
||||
if (json === undefined) {
|
||||
const others = classesOfElement(tag.name).map((name) => `"${name}"`)
|
||||
const last = others.pop()
|
||||
const alternative = last === undefined ? '' : `, or opens with class=${others.length === 0 ? last : `${others.join(', ')} or ${last}`}`
|
||||
return refused('unmappable-html', `a <${tag.name}> of the dialect is a carry element, holding class="adf-carry" and then data-adf alone${alternative}: ${foundAt(html, tag.start)}`, html, tag.start, place)
|
||||
return refused('unmappable-html', `a <${element}> of the dialect ${spellings(element)}: ${foundAt(html, tag.start)}`, html, tag.start, place)
|
||||
}
|
||||
const jsonAt = tag.start + carryJsonOffset(tag.name)
|
||||
const node = heldNode(json, levels, html, jsonAt)
|
||||
@@ -29,6 +28,16 @@ export function readCarry(html: string, tag: StartTag, element: 'div' | 'span',
|
||||
return refused('unmappable-html', `a carry element shows what adfToHtml writes for its node, as it or a browser spells it, then </${element}>; to change the node, edit its ADF and write the HTML again: ${foundAt(html, read.at)}`, html, read.at, place)
|
||||
}
|
||||
|
||||
// Every way the dialect spells the element, so a refusal names the one meant.
|
||||
function spellings(element: 'div' | 'span'): string {
|
||||
const classes = [...classesOfElement(element), ...(element === 'div' ? [mediaGroupClass, panelClass] : [])].sort().map((name) => `"${name}"`)
|
||||
const last = classes.pop()
|
||||
const opens = last === undefined ? [] : [`opens with class=${classes.length === 0 ? last : `${classes.join(', ')} or ${last}`}`]
|
||||
const others = [...opens, ...(element === 'span' ? [`holds ${colourAttributeOn(element)} alone`] : [])]
|
||||
const final = others.pop()
|
||||
return `is a carry element, holding class="adf-carry" and then data-adf alone${others.map((other) => `, ${other}`).join('')}${final === undefined ? '' : `, or ${final}`}`
|
||||
}
|
||||
|
||||
function heldNode(json: string, levels: number, html: string, jsonAt: number): Read<AdfNode> {
|
||||
const held = carriedValue(json, holder, { code: 'unmappable-html', message: `${holder} holds invalid JSON` })
|
||||
if (held.fault !== undefined) return held
|
||||
|
||||
@@ -241,14 +241,14 @@ test('reads underline, subscript, superscript and the two colours from their ele
|
||||
assert.deepEqual(marked('<u>x</u>'), document(paragraph(text('x', { type: 'underline' }))))
|
||||
assert.deepEqual(marked('<sub>x</sub>'), document(paragraph(text('x', { attrs: { type: 'sub' }, type: 'subsup' }))))
|
||||
assert.deepEqual(marked('<sup>x</sup>'), document(paragraph(text('x', { attrs: { type: 'sup' }, type: 'subsup' }))))
|
||||
assert.deepEqual(marked('<span style="color:#ff5630">x</span>'), document(paragraph(text('x', { attrs: { color: '#ff5630' }, type: 'textColor' }))))
|
||||
assert.deepEqual(marked('<mark style="background-color:#fffae6">x</mark>'), document(paragraph(text('x', { attrs: { color: '#fffae6' }, type: 'backgroundColor' }))))
|
||||
assert.deepEqual(marked('<strong><u><span style="color:red">x</span></u></strong>'), document(paragraph(text('x', { type: 'strong' }, { type: 'underline' }, { attrs: { color: 'red' }, type: 'textColor' }))))
|
||||
assert.deepEqual(marked('a<span style="color:red">b</span>'), document(paragraph(text('a'), text('b', { attrs: { color: 'red' }, type: 'textColor' }))))
|
||||
assert.deepEqual(marked('<span data-text-color="#ff5630">x</span>'), document(paragraph(text('x', { attrs: { color: '#ff5630' }, type: 'textColor' }))))
|
||||
assert.deepEqual(marked('<mark data-background-color="#fffae6">x</mark>'), document(paragraph(text('x', { attrs: { color: '#fffae6' }, type: 'backgroundColor' }))))
|
||||
assert.deepEqual(marked('<strong><u><span data-text-color="red">x</span></u></strong>'), document(paragraph(text('x', { type: 'strong' }, { type: 'underline' }, { attrs: { color: 'red' }, type: 'textColor' }))))
|
||||
assert.deepEqual(marked('a<span data-text-color="red">b</span>'), document(paragraph(text('a'), text('b', { attrs: { color: 'red' }, type: 'textColor' }))))
|
||||
})
|
||||
|
||||
test('refuses a mark element the writer does not write', () => {
|
||||
for (const inner of ['<span style="color:red;x:y">x</span>', '<span style="background-color:red">x</span>', '<mark style="color:red">x</mark>', '<mark>x</mark>', '<span style="color:">x</span>', '<span style="color:red" class="c">x</span>', '<u class="c">x</u>', '<sub style="color:red">x</sub>', '<span style="color:red"><span style="color:blue">x</span></span>']) {
|
||||
for (const inner of ['<span style="color:red">x</span>', '<mark style="background-color:red">x</mark>', '<span data-background-color="red">x</span>', '<mark data-text-color="red">x</mark>', '<mark>x</mark>', '<span data-text-color="red" class="c">x</span>', '<span data-text-color="red" data-background-color="red">x</span>', '<u class="c">x</u>', '<sub data-text-color="red">x</sub>', '<span data-text-color="red"><span data-text-color="blue">x</span></span>']) {
|
||||
assert.notEqual(refusal(htmlToAdf(`<p>${inner}</p>\n`)), 'read', inner)
|
||||
}
|
||||
})
|
||||
@@ -261,13 +261,13 @@ test('reads a paragraph or heading with a data-indent-level attribute as indente
|
||||
assert.deepEqual(read(htmlToAdf('<ul>\n<li><p data-indent-level="3">x</p></li>\n</ul>\n')), document({ content: [item({ content: [text('x')], marks: [indentation(3)], type: 'paragraph' })], type: 'bulletList' }))
|
||||
})
|
||||
|
||||
test('reads a paragraph or heading with a style attribute as aligned, right as end', () => {
|
||||
test('reads a paragraph or heading with a data-align attribute as aligned', () => {
|
||||
const alignment = (align: string): AdfMark => ({ attrs: { align }, type: 'alignment' })
|
||||
assert.deepEqual(read(htmlToAdf('<p style="text-align:center">x</p>\n')), document({ content: [text('x')], marks: [alignment('center')], type: 'paragraph' }))
|
||||
assert.deepEqual(read(htmlToAdf('<h2 style="text-align:right">x</h2>\n')), document({ attrs: { level: 2 }, content: [text('x')], marks: [alignment('end')], type: 'heading' }))
|
||||
assert.deepEqual(read(htmlToAdf(`<p style="text-align:right" data-local-id="${ids.paragraph}"></p>\n`)), document({ attrs: { localId: ids.paragraph }, marks: [alignment('end')], type: 'paragraph' }))
|
||||
assert.deepEqual(read(htmlToAdf('<ul>\n<li><p style="text-align:center">x</p></li>\n</ul>\n')), document({ content: [item({ content: [text('x')], marks: [alignment('center')], type: 'paragraph' })], type: 'bulletList' }))
|
||||
for (const html of ['<p style="text-align:left">x</p>\n', '<p style="text-align:center;color:red">x</p>\n', `<p data-local-id="${ids.paragraph}" style="text-align:center">x</p>\n`, '<figure class="adf-media-single" style="text-align:center"><img src="a"></figure>\n', '<blockquote style="text-align:center">\n<p>x</p>\n</blockquote>\n']) {
|
||||
assert.deepEqual(read(htmlToAdf('<p data-align="center">x</p>\n')), document({ content: [text('x')], marks: [alignment('center')], type: 'paragraph' }))
|
||||
assert.deepEqual(read(htmlToAdf('<h2 data-align="end">x</h2>\n')), document({ attrs: { level: 2 }, content: [text('x')], marks: [alignment('end')], type: 'heading' }))
|
||||
assert.deepEqual(read(htmlToAdf(`<p data-align="end" data-local-id="${ids.paragraph}"></p>\n`)), document({ attrs: { localId: ids.paragraph }, marks: [alignment('end')], type: 'paragraph' }))
|
||||
assert.deepEqual(read(htmlToAdf('<ul>\n<li><p data-align="center">x</p></li>\n</ul>\n')), document({ content: [item({ content: [text('x')], marks: [alignment('center')], type: 'paragraph' })], type: 'bulletList' }))
|
||||
for (const html of ['<p style="text-align:center">x</p>\n', '<p data-align="left">x</p>\n', '<p data-align="right">x</p>\n', '<p data-align="">x</p>\n', `<p data-local-id="${ids.paragraph}" data-align="center">x</p>\n`, '<p data-align="center" data-align="end">x</p>\n', '<figure class="adf-media-single" data-align="center"><img src="a"></figure>\n', '<blockquote data-align="center">\n<p>x</p>\n</blockquote>\n']) {
|
||||
assert.notEqual(refusal(htmlToAdf(html)), 'read', html)
|
||||
}
|
||||
})
|
||||
@@ -305,9 +305,10 @@ test('reads a table back, its rows, cells and attributes as adfToHtml writes the
|
||||
assert.deepEqual(read(htmlToAdf('<table>\n<tbody>\n<tr><td></td><th></th></tr>\n</tbody>\n</table>\n')), document({ content: [row({ type: 'tableCell' }, { type: 'tableHeader' })], type: 'table' }))
|
||||
assert.deepEqual(read(htmlToAdf('<table>\n<tbody>\n<tr><td>a\n<p>b</p>\n</td><td>\n<hr>\n</td></tr>\n</tbody>\n</table>\n')), document({ content: [row({ content: [paragraph(text('a')), paragraph(text('b'))], type: 'tableCell' }, { content: [{ type: 'rule' }], type: 'tableCell' })], type: 'table' }))
|
||||
assert.deepEqual(
|
||||
read(htmlToAdf('<table data-layout="wide" data-width="760">\n<tbody>\n<tr data-local-id="r1"><td colspan="2" rowspan="3" style="background-color:#fffae6" data-colwidth="120,80" data-local-id="c1" data-valign="top">x</td></tr>\n</tbody>\n</table>\n')),
|
||||
read(htmlToAdf('<table data-layout="wide" data-width="760">\n<tbody>\n<tr data-local-id="r1"><td colspan="2" rowspan="3" data-background-color="#fffae6" data-colwidth="120,80" data-local-id="c1" data-valign="top">x</td></tr>\n</tbody>\n</table>\n')),
|
||||
document({ attrs: { layout: 'wide', width: 760 }, content: [{ attrs: { localId: 'r1' }, content: [{ attrs: { background: '#fffae6', colspan: 2, colwidth: [120, 80], localId: 'c1', rowspan: 3, valign: 'top' }, content: [paragraph(text('x'))], type: 'tableCell' }], type: 'tableRow' }], type: 'table' }),
|
||||
)
|
||||
assert.deepEqual(read(htmlToAdf('<table>\n<tbody>\n<tr><td colspan="2" style="background-color:#fffae6" data-colwidth="120">x</td></tr>\n</tbody>\n</table>\n')), document({ content: [row({ attrs: { background: '#fffae6', colspan: 2, colwidth: [120] }, content: [paragraph(text('x'))], type: 'tableCell' })], type: 'table' }))
|
||||
assert.deepEqual(read(htmlToAdf('<ul>\n<li>\n<table></table>\n</li>\n</ul>\n')), document({ content: [item({ type: 'table' })], type: 'bulletList' }))
|
||||
})
|
||||
|
||||
@@ -334,6 +335,8 @@ const tableRefusals: readonly (readonly [string, string])[] = [
|
||||
['<table>\n<tbody>\n<tr><td rowspan="x">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td style="color:red">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td style="background-color:url(x)">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td style="background-color:red" data-background-color="red">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td data-colwidth="1" data-background-color="red">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td data-colwidth="[1]">a</td></tr>\n</tbody>\n</table>\n', 'unsupported-node-shape'],
|
||||
['<table>\n<tbody>\n<tr><td data-colwidth="01">a</td></tr>\n</tbody>\n</table>\n', 'unsupported-node-shape'],
|
||||
['<table data-size="big"></table>\n', 'unsupported-node-shape'],
|
||||
@@ -342,7 +345,7 @@ const tableRefusals: readonly (readonly [string, string])[] = [
|
||||
['<table>\n<tfoot>\n<tr></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<thead>\n<tr><th>a</th></tr>\n</tfoot>\n<tbody>\n<tr></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td colspan="2x">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td style="color:red;background-color:red">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td data-background-color="red" data-background-color="red">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td>a<hr>\n</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
]
|
||||
|
||||
@@ -701,7 +704,7 @@ const pinnedRefusals: readonly (readonly [string, string])[] = [
|
||||
['<div class="adf-panel" data-panel-type="info" data-local-id="p">\n<p>a</p>\n</div>\n', 'unmappable-html at /content/0: the data-* attributes stand once each, sorted by the attribute they name: found data-local-id after data-panel-type'],
|
||||
['<div class="adf-panel" data-panel-type="info">\n</div>\n', 'unmappable-html at /content/0/content/0: a block element of the dialect stands here: found "</div>\\n"'],
|
||||
['<div class="adf-panel" data-panel-type="info"><p>a</p></div>\n', 'unmappable-html at /content/0: <div> holds </div> straight after it, or a line feed and its first block: found "<p>a</p></div>\\n"'],
|
||||
['<div id="x" class="adf-panel">\n<p>a</p>\n</div>\n', 'unmappable-html at /content/0: a <div> of the dialect is a carry element, holding class="adf-carry" and then data-adf alone: found "<div id=\\"x\\" class=\\"adf-p"…'],
|
||||
['<div id="x" class="adf-panel">\n<p>a</p>\n</div>\n', 'unmappable-html at /content/0: a <div> of the dialect is a carry element, holding class="adf-carry" and then data-adf alone, or opens with class="adf-media-group" or "adf-panel": found "<div id=\\"x\\" class=\\"adf-p"…'],
|
||||
['<details class="adf-expand" open="x">\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: <details> takes open="" once, after its other attributes'],
|
||||
['<details class="adf-expand" data-title="a">\n<p>a</p>\n</details>\n', 'unsupported-node-shape at /content/0: <details> takes no data-title attribute'],
|
||||
['<details>\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: <details> starts with class="adf-expand" or class="adf-nested-expand": found "<details>\\n<p>a</p>\\n</det"…'],
|
||||
@@ -734,20 +737,24 @@ const pinnedRefusals: readonly (readonly [string, string])[] = [
|
||||
['<div class="adf-media-group"><span class="adf-file" data-id="a" data-type="file"></span><span class="adf-file" data-bogus="1"></span></div>\n', 'unsupported-node-shape at /content/0/content/1: <span> takes no data-bogus attribute'],
|
||||
['<div class="adf-media-group"><span class="adf-file" data-id="a" data-type="file">x</span></div>\n', 'unmappable-html at /content/0/content/0: <span class="adf-file"> holds nothing, and ends at </span>: found "x</span></div>\\n"'],
|
||||
['<div class="adf-media-group"><span class="adf-file" data-id="a" data-type="file"></span>x</div>\n', 'unmappable-html at /content/0: <div> holds its <span class="adf-file"> files, then </div>: found "x</div>\\n"'],
|
||||
['<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 data-align="left">a</p>\n', 'unmappable-html at /content/0: data-align on <p> holds center or end'],
|
||||
['<p style="text-align:center">a</p>\n', 'unmappable-html at /content/0: <p> takes no style attribute'],
|
||||
['<h1 data-align="right">a</h1>\n', 'unmappable-html at /content/0: data-align on <h1> holds center or end'],
|
||||
['<p data-indent-level="7">a</p>\n', 'unmappable-html at /content/0: data-indent-level on <p> holds a whole number from 1 to 6'],
|
||||
['<p data-indent-level="0">a</p>\n', 'unmappable-html at /content/0: data-indent-level on <p> holds a whole number from 1 to 6'],
|
||||
['<p data-indent-level="02">a</p>\n', 'unmappable-html at /content/0: data-indent-level on <p> holds a whole number from 1 to 6'],
|
||||
['<p data-indent-level="1.5">a</p>\n', 'unmappable-html at /content/0: data-indent-level on <p> holds a whole number from 1 to 6'],
|
||||
['<p data-indent-level="2" style="text-align:center">a</p>\n', 'unmappable-html at /content/0: <p> takes no style attribute'],
|
||||
['<p data-indent-level="2" data-align="center">a</p>\n', 'unmappable-html at /content/0: <p> takes one of data-align and data-indent-level, first, before its data-* attributes'],
|
||||
['<blockquote data-indent-level="2">\n<p>a</p>\n</blockquote>\n', 'unsupported-node-shape at /content/0: <blockquote> takes no data-indent-level attribute'],
|
||||
['<h3 data-indent-level="">a</h3>\n', 'unmappable-html at /content/0: data-indent-level on <h3> holds a whole number from 1 to 6'],
|
||||
['<p data-local-id="p1" data-indent-level="2">a</p>\n', 'unmappable-html at /content/0: <p> takes data-indent-level once, first, before its data-* attributes'],
|
||||
['<p data-indent-level="2" data-indent-level="2">a</p>\n', 'unmappable-html at /content/0: <p> takes data-indent-level once, first, before its data-* attributes'],
|
||||
['<p style="text-align:center" data-indent-level="2">a</p>\n', 'unmappable-html at /content/0: <p> takes data-indent-level once, first, before its data-* attributes'],
|
||||
['<p data-local-id="p1" data-indent-level="2">a</p>\n', 'unmappable-html at /content/0: <p> takes one of data-align and data-indent-level, first, before its data-* attributes'],
|
||||
['<p data-indent-level="2" data-indent-level="2">a</p>\n', 'unmappable-html at /content/0: <p> takes one of data-align and data-indent-level, first, before its data-* attributes'],
|
||||
['<p data-align="center" data-indent-level="2">a</p>\n', 'unmappable-html at /content/0: <p> takes one of data-align and data-indent-level, first, before its data-* attributes'],
|
||||
['<details class="adf-expand" open="" open="">\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: <details> takes open="" once, after its other attributes'],
|
||||
['<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\\">"…'],
|
||||
['<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, opens with class="adf-emoji", "adf-mention" or "adf-status", or holds data-text-color alone: found "<span title=\\"color:red\\">"…'],
|
||||
['<p>a <span style="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, opens with class="adf-emoji", "adf-mention" or "adf-status", or holds data-text-color alone: found "<span style=\\"color:red\\">"…'],
|
||||
['<p>a <mark style="background-color:red">b</mark></p>\n', 'unmappable-html at /content/0/content/1: a <mark> of the dialect holds data-background-color alone: found "<mark style=\\"background-"…'],
|
||||
['<p>a <mark>b</mark></p>\n', 'unmappable-html at /content/0/content/1: a <mark> of the dialect holds data-background-color alone: found "<mark>b</mark></p>\\n"'],
|
||||
['<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"'],
|
||||
|
||||
@@ -6,7 +6,7 @@ import { built, readInlineBlock } from './inline-block.ts'
|
||||
import { carryContent } from '../carry-content.ts'
|
||||
import { centeredImage, externalMedia } from '../../adf/plain-image.ts'
|
||||
import { containerElements, headingElements, headingLevel, readListStart, type ContainerType } from '../block-elements.ts'
|
||||
import { expandClasses, expandOfClass, panelAttributes, panelClass, taskListOfClass, taskListOfType } from '../block-classes.ts'
|
||||
import { expandClasses, expandOfClass, mediaGroupClass, panelAttributes, panelClass, taskListOfClass, taskListOfType } from '../block-classes.ts'
|
||||
import { firstParagraphForm } from '../first-paragraph.ts'
|
||||
import { foundAt, misread, misspelledText, positionIn, readStartTag, refused, startTagName, type Place, type ReadNode, type StartTag } from './source.ts'
|
||||
import { isHeaderRow, readCellAttributes } from '../cell-element.ts'
|
||||
@@ -228,7 +228,7 @@ function readDiv(html: string, tag: StartTag, place: Place, open: Open): Result<
|
||||
const [first] = tag.attributes
|
||||
const className = first?.[0] === 'class' ? first[1] : undefined
|
||||
if (className === panelClass) return readPanel(html, tag, place, open)
|
||||
return className === 'adf-media-group' ? asStep(readMediaGroup(html, tag, place)) : readBlockCarry(html, tag, place, open)
|
||||
return className === mediaGroupClass ? asStep(readMediaGroup(html, tag, place)) : readBlockCarry(html, tag, place, open)
|
||||
}
|
||||
|
||||
function readPanel(html: string, tag: StartTag, place: Place, open: Open): Result<BlockStep, ParseError> {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import type { ParseError, Result } from '../../result.ts'
|
||||
import { colourAttributeOn, markOfTag } from '../mark-elements.ts'
|
||||
import { foundAt, misread, misspelledText, readStartTag, refused, type Place, type ReadNode, type StartTag } from './source.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { linkMarkFrom } from '../../adf/mark-attributes.ts'
|
||||
import { markOfTag } from '../mark-elements.ts'
|
||||
import { misspelledAt, unescapedValue } from '../escaping.ts'
|
||||
import { readAnchorAttributes } from '../anchor-element.ts'
|
||||
import { readCarry } from './carry-element.ts'
|
||||
@@ -32,6 +32,7 @@ function readTagged(html: string, index: number, place: Place): ReadNode | undef
|
||||
const element = tag === undefined ? undefined : readNodeElement(html, tag, place)
|
||||
if (element !== undefined) return element
|
||||
if (tag !== undefined && markOf(tag) !== undefined) return readMarked(html, index, place)
|
||||
if (tag?.name === 'mark') return refused('unmappable-html', `a <mark> of the dialect holds ${colourAttributeOn('mark')} alone: ${foundAt(html, index)}`, html, index, place)
|
||||
return tag?.name === 'span' ? readCarry(html, tag, 'span', largestNesting, place) : undefined
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { blockNodes } from '../../adf/block-nodes.ts'
|
||||
import { built, readInlineBlock } from './inline-block.ts'
|
||||
import { foundAt, readStartTag, refused, type Place, type ReadNode, type StartTag } from './source.ts'
|
||||
import { linkMarkFrom } from '../../adf/mark-attributes.ts'
|
||||
import { mediaGroupClass } from '../block-classes.ts'
|
||||
import { readAnchorAttributes } from '../anchor-element.ts'
|
||||
import { readDataAttributes } from '../data-attributes.ts'
|
||||
import { readMediaImage } from '../media-element.ts'
|
||||
@@ -10,7 +11,6 @@ import { success } from '../../result.ts'
|
||||
|
||||
const figureClass = 'adf-media-single'
|
||||
const fileClass = 'adf-file'
|
||||
const groupClass = 'adf-media-group'
|
||||
|
||||
// A `mediaSingle` as `adfToHtml` writes it: its media's image, then its caption, on one line.
|
||||
export function readFigure(html: string, tag: StartTag, place: Place): ReadNode {
|
||||
@@ -34,7 +34,7 @@ export function readFigure(html: string, tag: StartTag, place: Place): ReadNode
|
||||
|
||||
// A `mediaGroup` as `adfToHtml` writes it: a span for each media, none between.
|
||||
export function readMediaGroup(html: string, tag: StartTag, place: Place): ReadNode {
|
||||
if (tag.attributes.length !== 1) return refused('unmappable-html', `<div class="${groupClass}"> takes no other attribute: ${foundAt(html, tag.start)}`, html, tag.start, place)
|
||||
if (tag.attributes.length !== 1) return refused('unmappable-html', `<div class="${mediaGroupClass}"> takes no other attribute: ${foundAt(html, tag.start)}`, html, tag.start, place)
|
||||
const content: AdfNode[] = []
|
||||
let at = tag.end
|
||||
for (let span = readStartTag(html, at); span?.name === 'span'; span = readStartTag(html, at)) {
|
||||
|
||||
@@ -4,14 +4,8 @@ import type { Read } from '../result.ts'
|
||||
import { dataAttributes, readDataAttributes } from './data-attributes.ts'
|
||||
import { nodeAttrs, nodeMarks } from '../adf/document.ts'
|
||||
|
||||
// The `text-align` value each alignment names.
|
||||
function alignments(): ReadonlyMap<string, string> {
|
||||
return new Map([
|
||||
['center', 'center'],
|
||||
['end', 'right'],
|
||||
])
|
||||
}
|
||||
|
||||
const alignAttribute = 'data-align'
|
||||
const alignments = ['center', 'end']
|
||||
const indentAttribute = 'data-indent-level'
|
||||
const indentLevels = ['1', '2', '3', '4', '5', '6']
|
||||
|
||||
@@ -30,8 +24,7 @@ function markAttribute(mark: AdfMark): string | undefined {
|
||||
const attrs = nodeAttrs(mark)
|
||||
if (Object.keys(attrs).length !== 1) return undefined
|
||||
const { align, level } = attrs
|
||||
const textAlign = [...alignments()].find(([name]) => name === align)?.[1]
|
||||
if (mark.type === 'alignment') return textAlign === undefined ? undefined : ` style="text-align:${textAlign}"`
|
||||
if (mark.type === 'alignment') return typeof align === 'string' && alignments.includes(align) ? ` ${alignAttribute}="${align}"` : undefined
|
||||
return mark.type === 'indentation' && typeof level === 'number' && indentLevels.includes(String(level)) ? ` ${indentAttribute}="${level}"` : undefined
|
||||
}
|
||||
|
||||
@@ -41,7 +34,7 @@ export function readTextBlockAttributes(pairs: readonly (readonly [string, strin
|
||||
const mark = first === undefined ? undefined : readMark(first, element)
|
||||
if (mark?.fault !== undefined) return mark
|
||||
const rest = mark === undefined ? pairs : pairs.slice(1)
|
||||
if (rest.some(([name]) => name === indentAttribute)) return { fault: { code: 'unmappable-html', message: `<${element}> takes ${indentAttribute} once, first, before its data-* attributes` } }
|
||||
if (rest.some(([name]) => name === alignAttribute || name === indentAttribute)) return { fault: { code: 'unmappable-html', message: `<${element}> takes one of ${alignAttribute} and ${indentAttribute}, first, before its data-* attributes` } }
|
||||
const data = readDataAttributes(rest, element, vocabulary, written)
|
||||
if (data.fault !== undefined) return data
|
||||
return { value: { attrs: data.value, marks: mark === undefined ? [] : [mark.value] } }
|
||||
@@ -51,7 +44,6 @@ function readMark([name, spelled]: readonly [string, string], element: string):
|
||||
if (name === indentAttribute) {
|
||||
return indentLevels.includes(spelled) ? { value: { attrs: { level: Number(spelled) }, type: 'indentation' } } : { fault: { code: 'unmappable-html', message: `${indentAttribute} on <${element}> holds a whole number from 1 to 6` } }
|
||||
}
|
||||
if (name !== 'style') return undefined
|
||||
const align = [...alignments()].find(([, textAlign]) => spelled === `text-align:${textAlign}`)?.[0]
|
||||
return align === undefined ? { fault: { code: 'unmappable-html', message: `style on <${element}> holds text-align:center or text-align:right` } } : { value: { attrs: { align }, type: 'alignment' } }
|
||||
if (name !== alignAttribute) return undefined
|
||||
return alignments.includes(spelled) ? { value: { attrs: { align: spelled }, type: 'alignment' } } : { fault: { code: 'unmappable-html', message: `${alignAttribute} on <${element}> holds center or end` } }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user