// The Blog Editor's preservation schema (Phase 16.2 D-03, D-04): what the
// schema models, what becomes a raw HTML card, which attributes survive an
// edit, and how the stored form of links, tables and figures looks when a
// block is serialized from the model.
import { afterEach, describe, expect, it } from 'vitest'
import { RAW_HTML_TAGS } from '../../assets/src/blogeditor/extensions'
import { serializeDocument } from '../../assets/src/blogeditor/serializer'
import { insertAtBlock, mount } from './helpers'
afterEach(() => {
document.body.replaceChildren()
})
/** types lists the top-level node types of the editor's document. */
function types(html: string): string[] {
const { editor } = mount(html)
const out: string[] = []
editor.state.doc.forEach((node) => out.push(node.type.name))
return out
}
/**
* shape describes markup for comparison: element names, attributes sorted by
* name (a style without its final semicolon) and text. An edited block may
* list attributes in another order (the model renders modelled ones after
* the captured ones) and the DOM normalizes a style declaration; names and
* values must stay the same.
*/
function shape(html: string): string {
const template = document.createElement('template')
template.innerHTML = html
const walk = (node: Node): string => {
if (node.nodeType === 3) return JSON.stringify(node.nodeValue)
if (node.nodeType !== 1) return ''
const el = node as Element
const attrs = Array.from(el.attributes)
.map((a) => `${a.name}=${JSON.stringify(a.name === 'style' ? a.value.trim().replace(/;$/, '') : a.value)}`)
.sort()
return `<${el.nodeName.toLowerCase()} ${attrs.join(' ')}>${Array.from(el.childNodes).map(walk).join('')}>`
}
return Array.from(template.content.childNodes).map(walk).join('')
}
/** model serializes the whole document from the model, without block reuse. */
function model(html: string): string {
const { editor } = mount(html)
return serializeDocument(editor, null)
}
const rawSamples: Record<(typeof RAW_HTML_TAGS)[number], string> = {
footer: '',
section: '