-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathbuilders.js
More file actions
89 lines (81 loc) · 2.74 KB
/
Copy pathbuilders.js
File metadata and controls
89 lines (81 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
// eslint-disable-next-line n/no-extraneous-import
import { expect } from '@jest/globals'
import { Node } from '@tiptap/pm/model'
import { builders } from 'prosemirror-test-builder'
import { createRichEditor } from '../EditorFactory.js'
/**
* Get node builders from the default rich editor.
* @return {object}
*/
export function getBuilders() {
const editor = createRichEditor()
return builders(editor.schema, {
tr: { nodeType: 'tableRow' },
td: { nodeType: 'tableCell' },
th: { nodeType: 'tableHeader' },
em: { markType: 'italic' },
thead: { nodeType: 'tableHeadRow' },
br: { nodeType: 'hardBreak' },
p: { nodeType: 'paragraph' },
})
}
export const p = getBuilders().p
export const br = getBuilders().br
export const em = getBuilders().em
export const tr = getBuilders().tr
export const td = getBuilders().td
export const th = getBuilders().th
export const doc = getBuilders().doc
export const table = getBuilders().table
export const thead = getBuilders().thead
/**
* Create string representation of prosemirror / TipTap Node with attributes
* @param {Node} node to serialize
* @return {string}
*/
function createDocumentString(node) {
const extractAttributes = (node) => {
const attrs = node.attrs || {}
const attrString = Object.keys(attrs)
.map((key) => {
// null is the TipTap default so we ignore it (e.g. a value of `unknown` must be manually set by the application)
return (attrs[key] === null)
? undefined
: key + '=' + (typeof attrs[key] === 'string' ? `"${attrs[key]}"` : attrs[key])
})
.filter(v => !!v)
.join(',')
return attrString ? `<${attrString}>` : ''
}
const stringifyNode = (node) => {
const name = node.type.name
if (name === 'text') return '"' + node.text.replace('"', '\\"').replace('\n', '\\n') + '"'
const children = node.content.content.map(createDocumentString)
return name + extractAttributes(node) + '(' + children.join(',') + ')'
}
// First extract marks and place them like nodes in the string
const marks = node.marks.map(mark => mark.type.name + extractAttributes(mark) + '(')
return marks.join('') + stringifyNode(node) + ')'.repeat(marks.length)
}
/**
* Compare given document from editor with builders
*
* @param {Node} subject The editor document
* @param {Node} expected The expected document
* @example
* const editor = createRichEditor()
* expectDocument(editor.state.doc, table(
* tr(
* td('foo')
* )
* ))
*/
export function expectDocument(subject, expected) {
expect(typeof subject).toBe('object')
expect(typeof expected).toBe('object')
expect(createDocumentString(subject)).toBe(createDocumentString(doc(expected, p())))
}