// domhtml.mjs — the editor's DOM, as HTML text, without a browser. Gate // L4 compares it with the server's HTML byte for byte. // // ProseMirror's own DOMSerializer builds the DOM from the schema's toDOM, // into a small recording document. Then two things the view does are // added, as prosemirror-view does them: the trailing break in a // textblock that is empty or ends in a non-text node or a newline // (ViewTreeUpdater.addTextblockHacks), which gives the line its height; // and the class alone that a node decoration puts on a lone image. // The text is written escaped as Go's html.EscapeString escapes it, so // equal DOMs are equal strings. import { DOMSerializer } from 'prosemirror-model' import { escapeHTML } from './markup.mjs' import { isAlone } from './imagedom.mjs' class Element { constructor (name) { this.nodeType = 1 this.localName = name this.attributes = [] this.childNodes = [] } setAttribute (name, value) { const found = this.attributes.find(attribute => attribute.name === name) if (found) found.value = String(value) else this.attributes.push({ name, value: String(value) }) } setAttributeNS (namespace, name, value) { this.setAttribute(name, value) } appendChild (child) { if (child.nodeType === 11) this.childNodes.push(...child.childNodes) else this.childNodes.push(child) return child } } class Fragment extends Element { constructor () { super('#fragment') this.nodeType = 11 } } export const document = { createElement: name => new Element(name), createElementNS: (namespace, name) => new Element(name), createTextNode: text => ({ nodeType: 3, data: text }), createDocumentFragment: () => new Fragment() } const voids = new Set(['br', 'hr', 'img', 'input']) // ViewSerializer is DOMSerializer with the view's trailing break. class ViewSerializer extends DOMSerializer { serializeNodeInner (node, options) { if (node.isText) return super.serializeNodeInner(node, options) const { dom, contentDOM } = DOMSerializer.renderSpec(options.document, this.nodes[node.type.name](node), null, node.attrs) if (contentDOM) { this.serializeFragment(node.content, options, contentDOM) // The decoration images.mjs puts on a lone image (images §2.1). if (isAlone(node)) contentDOM.childNodes[0].setAttribute('class', 'alone') if (node.isTextblock && needsTrailingBreak(node)) { const hack = options.document.createElement('br') hack.setAttribute('class', 'ProseMirror-trailingBreak') contentDOM.appendChild(hack) } } return dom } } function needsTrailingBreak (textblock) { const last = textblock.lastChild return !last || !last.isText || last.text.endsWith('\n') } export function toDOM (schema, doc) { const serializer = new ViewSerializer( DOMSerializer.nodesFromSchema(schema), DOMSerializer.marksFromSchema(schema)) return serializer.serializeFragment(doc.content, { document }) } export function toHTML (schema, doc) { return html(toDOM(schema, doc)) } function html (node) { if (node.nodeType === 3) return escapeHTML(node.data) const inner = node.childNodes.map(html).join('') if (node.nodeType === 11) return inner const attributes = node.attributes.map(({ name, value }) => value === '' ? ` ${name}` : ` ${name}="${escapeHTML(value)}"`).join('') if (voids.has(node.localName)) return `<${node.localName}${attributes}>` return `<${node.localName}${attributes}>${inner}` }