-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiv.test.js
More file actions
104 lines (82 loc) · 3.81 KB
/
div.test.js
File metadata and controls
104 lines (82 loc) · 3.81 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Editor } from '@tiptap/core';
import Document from '@tiptap/extension-document';
import Text from '@tiptap/extension-text';
import { DivParagraph } from './div';
const createEditor = () => new Editor({
extensions: [Document, Text, DivParagraph],
content: '',
});
const getJSON = (editor, html) => {
editor.commands.setContent(html, false);
return editor.getJSON();
};
describe('DivParagraph extension', () => {
let editor;
beforeEach(() => {
editor = createEditor();
});
afterEach(() => {
editor.destroy();
});
describe('parseHTML getAttrs', () => {
it('should parse a div with only inline children as a paragraph', () => {
const json = getJSON(editor, '<div><span>hello</span> world</div>');
const paragraphs = json.content.filter(n => n.type === 'paragraph');
expect(paragraphs).toHaveLength(1);
expect(paragraphs[0].content.some(n => n.text?.includes('hello'))).toBe(true);
});
it('should parse a div with only text as a paragraph', () => {
const json = getJSON(editor, '<div>plain text</div>');
const paragraphs = json.content.filter(n => n.type === 'paragraph');
expect(paragraphs).toHaveLength(1);
expect(paragraphs[0].content[0].text).toBe('plain text');
});
it('should reject a div that contains a block-level child (nested div)', () => {
const json = getJSON(editor, '<div><div>nested</div></div>');
// The outer div should NOT become a paragraph itself.
// ProseMirror should lift the inner div into its own paragraph.
const paragraphs = json.content.filter(n => n.type === 'paragraph');
expect(paragraphs).toHaveLength(1);
expect(paragraphs[0].content[0].text).toBe('nested');
});
it('should reject a div that contains a <p> child', () => {
const json = getJSON(editor, '<div><p>inside p</p></div>');
const paragraphs = json.content.filter(n => n.type === 'paragraph');
expect(paragraphs).toHaveLength(1);
expect(paragraphs[0].content[0].text).toBe('inside p');
});
it('should reject a div that contains a <ul> child', () => {
const json = getJSON(editor, '<div><ul><li>item</li></ul></div>');
const paragraphs = json.content.filter(n => n.type === 'paragraph');
// The outer div should be skipped (not become a paragraph) because
// it contains a block-level <ul>. The text content is still parsed.
// There should be no extra empty paragraph from the outer div.
expect(paragraphs.every(p => p.content?.length > 0)).toBe(true);
});
it('should reject a div that contains an <h1> child', () => {
const json = getJSON(editor, '<div><h1>heading</h1></div>');
// The heading text should still be parsed, but the outer div
// should not produce an extra empty paragraph wrapper.
const allText = json.content
.flatMap(n => n.content || [])
.map(n => n.text)
.filter(Boolean);
expect(allText).toContain('heading');
});
it('should not produce extra empty paragraphs for structural divs', () => {
const json = getJSON(editor, '<div><div>a</div><div>b</div></div>');
const paragraphs = json.content.filter(n => n.type === 'paragraph');
// Should get two paragraphs (from the two inner divs), not three
expect(paragraphs).toHaveLength(2);
expect(paragraphs[0].content[0].text).toBe('a');
expect(paragraphs[1].content[0].text).toBe('b');
});
it('should parse multiple sibling inline-only divs as separate paragraphs', () => {
const json = getJSON(editor, '<div>first</div><div>second</div>');
const paragraphs = json.content.filter(n => n.type === 'paragraph');
expect(paragraphs).toHaveLength(2);
expect(paragraphs[0].content[0].text).toBe('first');
expect(paragraphs[1].content[0].text).toBe('second');
});
});
});