@pie-lib/editable-html-tip-tap 2.1.0-next.0 → 2.1.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/lib/components/EditableHtml.js +4 -3
- package/lib/components/EditableHtml.js.map +1 -1
- package/lib/components/MenuBar.js +14 -15
- package/lib/components/MenuBar.js.map +1 -1
- package/lib/extensions/heading-paragraph.js +61 -0
- package/lib/extensions/heading-paragraph.js.map +1 -0
- package/lib/utils/helper.js +58 -2
- package/lib/utils/helper.js.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/EditableHtml.test.jsx +122 -0
- package/src/__tests__/div-to-paragraph-conversion.test.jsx +125 -0
- package/src/components/EditableHtml.jsx +4 -2
- package/src/components/MenuBar.jsx +3 -2
- package/src/extensions/heading-paragraph.js +53 -0
- package/src/utils/__tests__/helper.test.js +126 -0
- package/src/utils/helper.js +54 -2
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { normalizeInitialMarkup } from '../helper';
|
|
2
|
+
|
|
3
|
+
describe('normalizeInitialMarkup', () => {
|
|
4
|
+
describe('basic normalization', () => {
|
|
5
|
+
it('returns empty div for empty string', () => {
|
|
6
|
+
expect(normalizeInitialMarkup('')).toBe('<div></div>');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('returns empty div for null', () => {
|
|
10
|
+
expect(normalizeInitialMarkup(null)).toBe('<div></div>');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('returns empty div for undefined', () => {
|
|
14
|
+
expect(normalizeInitialMarkup(undefined)).toBe('<div></div>');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('wraps plain text in div', () => {
|
|
18
|
+
expect(normalizeInitialMarkup('Hello')).toBe('<div>Hello</div>');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('returns HTML tags as-is when detected as HTML', () => {
|
|
22
|
+
// Since '<script>' matches the HTML pattern, it's returned as-is
|
|
23
|
+
// To be escaped, it would need to not match the HTML pattern
|
|
24
|
+
expect(normalizeInitialMarkup('<script>')).toBe('<script>');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('escapes HTML entities in plain text', () => {
|
|
28
|
+
// Plain text without angle brackets gets escaped
|
|
29
|
+
expect(normalizeInitialMarkup('Hello & World')).toBe('<div>Hello & World</div>');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('returns single div as-is', () => {
|
|
33
|
+
const html = '<div>Hello</div>';
|
|
34
|
+
expect(normalizeInitialMarkup(html)).toBe(html);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('returns paragraph as-is', () => {
|
|
38
|
+
const html = '<p>Hello</p>';
|
|
39
|
+
expect(normalizeInitialMarkup(html)).toBe(html);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('consecutive divs to paragraph conversion', () => {
|
|
44
|
+
it('converts two consecutive divs to paragraph with br', () => {
|
|
45
|
+
const input = '<div>A</div><div>B</div>';
|
|
46
|
+
const expected = '<p>A<br>B</p>';
|
|
47
|
+
expect(normalizeInitialMarkup(input)).toBe(expected);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('converts three consecutive divs to paragraph with br tags', () => {
|
|
51
|
+
const input = '<div>A</div><div>B</div><div>C</div>';
|
|
52
|
+
const expected = '<p>A<br>B<br>C</p>';
|
|
53
|
+
expect(normalizeInitialMarkup(input)).toBe(expected);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('handles divs with whitespace', () => {
|
|
57
|
+
const input = '<div> A </div><div> B </div>';
|
|
58
|
+
const expected = '<p> A <br> B </p>';
|
|
59
|
+
expect(normalizeInitialMarkup(input)).toBe(expected);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('handles divs with inline elements', () => {
|
|
63
|
+
const input = '<div><strong>A</strong></div><div><em>B</em></div>';
|
|
64
|
+
const expected = '<p><strong>A</strong><br><em>B</em></p>';
|
|
65
|
+
expect(normalizeInitialMarkup(input)).toBe(expected);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('handles empty divs', () => {
|
|
69
|
+
const input = '<div></div><div>B</div>';
|
|
70
|
+
const expected = '<p><br>B</p>';
|
|
71
|
+
expect(normalizeInitialMarkup(input)).toBe(expected);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('preserves existing br tags within divs', () => {
|
|
75
|
+
const input = '<div>A<br>A2</div><div>B</div>';
|
|
76
|
+
const expected = '<p>A<br>A2<br>B</p>';
|
|
77
|
+
expect(normalizeInitialMarkup(input)).toBe(expected);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('cases that should NOT convert', () => {
|
|
82
|
+
it('does not convert single div', () => {
|
|
83
|
+
const input = '<div>Hello</div>';
|
|
84
|
+
expect(normalizeInitialMarkup(input)).toBe(input);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('does not convert divs with nested block elements', () => {
|
|
88
|
+
const input = '<div><div>Nested</div></div><div>B</div>';
|
|
89
|
+
expect(normalizeInitialMarkup(input)).toBe(input);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('does not convert divs containing tables', () => {
|
|
93
|
+
const input = '<div><table><tr><td>A</td></tr></table></div><div>B</div>';
|
|
94
|
+
expect(normalizeInitialMarkup(input)).toBe(input);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('does not convert divs containing lists', () => {
|
|
98
|
+
const input = '<div><ul><li>Item</li></ul></div><div>B</div>';
|
|
99
|
+
expect(normalizeInitialMarkup(input)).toBe(input);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('does not convert mixed element types', () => {
|
|
103
|
+
const input = '<div>A</div><p>B</p>';
|
|
104
|
+
expect(normalizeInitialMarkup(input)).toBe(input);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('does not convert paragraphs', () => {
|
|
108
|
+
const input = '<p>A</p><p>B</p>';
|
|
109
|
+
expect(normalizeInitialMarkup(input)).toBe(input);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('edge cases', () => {
|
|
114
|
+
it('handles divs with attributes', () => {
|
|
115
|
+
const input = '<div class="test">A</div><div>B</div>';
|
|
116
|
+
// Should not convert since we only want simple divs
|
|
117
|
+
expect(normalizeInitialMarkup(input)).toBe(input);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('handles complex inline formatting', () => {
|
|
121
|
+
const input = '<div><strong><em>A</em></strong></div><div><u>B</u></div>';
|
|
122
|
+
const expected = '<p><strong><em>A</em></strong><br><u>B</u></p>';
|
|
123
|
+
expect(normalizeInitialMarkup(input)).toBe(expected);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
package/src/utils/helper.js
CHANGED
|
@@ -6,12 +6,64 @@ const escapeHtml = (str) =>
|
|
|
6
6
|
.replace(/"/g, '"')
|
|
7
7
|
.replace(/'/g, ''');
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Converts consecutive div elements into a single paragraph with line breaks.
|
|
11
|
+
* Example: "<div>A</div><div>B</div>" becomes "<p>A<br>B</p>"
|
|
12
|
+
*/
|
|
13
|
+
const convertConsecutiveDivsToParagraph = (html) => {
|
|
14
|
+
// Create a temporary element to parse the HTML
|
|
15
|
+
const temp = document.createElement('div');
|
|
16
|
+
temp.innerHTML = html;
|
|
17
|
+
|
|
18
|
+
// Get all top-level children
|
|
19
|
+
const children = Array.from(temp.children);
|
|
20
|
+
|
|
21
|
+
// Only convert if there are 2 or more divs
|
|
22
|
+
if (children.length < 2) {
|
|
23
|
+
return html;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Check if all children are divs with simple content (text or inline elements)
|
|
27
|
+
const allDivs = children.every((child) => child.tagName === 'DIV');
|
|
28
|
+
|
|
29
|
+
if (!allDivs) {
|
|
30
|
+
return html;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Check if divs have no attributes (only convert plain divs)
|
|
34
|
+
const hasNoAttributes = children.every((div) => div.attributes.length === 0);
|
|
35
|
+
|
|
36
|
+
if (!hasNoAttributes) {
|
|
37
|
+
return html;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check if divs contain only simple content (no nested block elements)
|
|
41
|
+
const hasOnlySimpleContent = children.every((div) => {
|
|
42
|
+
return Array.from(div.children).every((child) => {
|
|
43
|
+
const tag = child.tagName;
|
|
44
|
+
// Allow inline elements and br tags
|
|
45
|
+
return ['SPAN', 'B', 'I', 'EM', 'STRONG', 'U', 'SUB', 'SUP', 'A', 'CODE', 'BR'].includes(tag);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
if (!hasOnlySimpleContent) {
|
|
50
|
+
return html;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Convert to paragraph with br tags
|
|
54
|
+
const contents = children.map((div) => div.innerHTML);
|
|
55
|
+
return `<p>${contents.join('<br>')}</p>`;
|
|
56
|
+
};
|
|
57
|
+
|
|
9
58
|
export const normalizeInitialMarkup = (markup) => {
|
|
10
59
|
const trimmed = String(markup ?? '').trim();
|
|
11
60
|
if (!trimmed) return '<div></div>';
|
|
12
61
|
|
|
13
62
|
const looksLikeHtml = /<[^>]+>/.test(trimmed);
|
|
14
|
-
if (looksLikeHtml)
|
|
63
|
+
if (!looksLikeHtml) {
|
|
64
|
+
return `<div>${escapeHtml(trimmed)}</div>`;
|
|
65
|
+
}
|
|
15
66
|
|
|
16
|
-
|
|
67
|
+
// Apply the div-to-paragraph transformation
|
|
68
|
+
return convertConsecutiveDivsToParagraph(trimmed);
|
|
17
69
|
};
|