| 1 | // domhtml.mjs — the editor's DOM, as HTML text, without a browser. Gate |
| 2 | // L4 compares it with the server's HTML byte for byte. |
| 3 | // |
| 4 | // ProseMirror's own DOMSerializer builds the DOM from the schema's toDOM, |
| 5 | // into a small recording document. Then two things the view does are |
| 6 | // added, as prosemirror-view does them: the trailing break in a |
| 7 | // textblock that is empty or ends in a non-text node or a newline |
| 8 | // (ViewTreeUpdater.addTextblockHacks), which gives the line its height; |
| 9 | // and the class alone that a node decoration puts on a lone image. |
| 10 | // The text is written escaped as Go's html.EscapeString escapes it, so |
| 11 | // equal DOMs are equal strings. |
| 12 | |
| 13 | import { DOMSerializer } from 'prosemirror-model' |
| 14 | import { escapeHTML } from './markup.mjs' |
| 15 | import { isAlone } from './imagedom.mjs' |
| 16 | |
| 17 | class Element { |
| 18 | constructor (name) { |
| 19 | this.nodeType = 1 |
| 20 | this.localName = name |
| 21 | this.attributes = [] |
| 22 | this.childNodes = [] |
| 23 | } |
| 24 | |
| 25 | setAttribute (name, value) { |
| 26 | const found = this.attributes.find(attribute => attribute.name === name) |
| 27 | if (found) found.value = String(value) |
| 28 | else this.attributes.push({ name, value: String(value) }) |
| 29 | } |
| 30 | |
| 31 | setAttributeNS (namespace, name, value) { this.setAttribute(name, value) } |
| 32 | |
| 33 | appendChild (child) { |
| 34 | if (child.nodeType === 11) this.childNodes.push(...child.childNodes) |
| 35 | else this.childNodes.push(child) |
| 36 | return child |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | class Fragment extends Element { |
| 41 | constructor () { |
| 42 | super('#fragment') |
| 43 | this.nodeType = 11 |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | export const document = { |
| 48 | createElement: name => new Element(name), |
| 49 | createElementNS: (namespace, name) => new Element(name), |
| 50 | createTextNode: text => ({ nodeType: 3, data: text }), |
| 51 | createDocumentFragment: () => new Fragment() |
| 52 | } |
| 53 | |
| 54 | const voids = new Set(['br', 'hr', 'img', 'input']) |
| 55 | |
| 56 | // ViewSerializer is DOMSerializer with the view's trailing break. |
| 57 | class ViewSerializer extends DOMSerializer { |
| 58 | serializeNodeInner (node, options) { |
| 59 | if (node.isText) return super.serializeNodeInner(node, options) |
| 60 | const { dom, contentDOM } = DOMSerializer.renderSpec(options.document, |
| 61 | this.nodes[node.type.name](node), null, node.attrs) |
| 62 | if (contentDOM) { |
| 63 | this.serializeFragment(node.content, options, contentDOM) |
| 64 | // The decoration images.mjs puts on a lone image (images §2.1). |
| 65 | if (isAlone(node)) contentDOM.childNodes[0].setAttribute('class', 'alone') |
| 66 | if (node.isTextblock && needsTrailingBreak(node)) { |
| 67 | const hack = options.document.createElement('br') |
| 68 | hack.setAttribute('class', 'ProseMirror-trailingBreak') |
| 69 | contentDOM.appendChild(hack) |
| 70 | } |
| 71 | } |
| 72 | return dom |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | function needsTrailingBreak (textblock) { |
| 77 | const last = textblock.lastChild |
| 78 | return !last || !last.isText || last.text.endsWith('\n') |
| 79 | } |
| 80 | |
| 81 | export function toDOM (schema, doc) { |
| 82 | const serializer = new ViewSerializer( |
| 83 | DOMSerializer.nodesFromSchema(schema), |
| 84 | DOMSerializer.marksFromSchema(schema)) |
| 85 | return serializer.serializeFragment(doc.content, { document }) |
| 86 | } |
| 87 | |
| 88 | export function toHTML (schema, doc) { |
| 89 | return html(toDOM(schema, doc)) |
| 90 | } |
| 91 | |
| 92 | function html (node) { |
| 93 | if (node.nodeType === 3) return escapeHTML(node.data) |
| 94 | const inner = node.childNodes.map(html).join('') |
| 95 | if (node.nodeType === 11) return inner |
| 96 | const attributes = node.attributes.map(({ name, value }) => |
| 97 | value === '' ? ` ${name}` : ` ${name}="${escapeHTML(value)}"`).join('') |
| 98 | if (voids.has(node.localName)) return `<${node.localName}${attributes}>` |
| 99 | return `<${node.localName}${attributes}>${inner}</${node.localName}>` |
| 100 | } |
| 101 | |