-
Notifications
You must be signed in to change notification settings - Fork 46
/
rich-text.js
256 lines (218 loc) · 9.81 KB
/
rich-text.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import * as Y from 'yjs';
import { create, toHTMLString } from '@wordpress/rich-text';
import '@wordpress/format-library';
import { applyHTMLDelta, gutenFormatsToYFormats, richTextMapToHTML } from '../rich-text';
const richTextMapFrom = ( html ) => {
const richTextMap = new Y.Doc().get( 'richTextMap', Y.Map );
richTextMap.set( 'xmlText', new Y.XmlText() );
richTextMap.set( 'multilineTag', undefined );
richTextMap.set( 'replacements', new Y.Array() );
applyHTMLDelta( '', html, richTextMap );
return richTextMap;
};
describe( 'gutenFormatsToYFormats', () => {
it( 'should convert to a range-based Yjs format', () => {
const { formats } = create( { html: '<a href="some-url">abc</a>' } );
const result = gutenFormatsToYFormats( formats );
expect( result ).toEqual( [ { format: { a: { href: 'some-url' } }, index: 0, length: 3 } ] );
} );
it( 'should handle nested tags', () => {
const { formats } = create( { html: '<em>foo<strong>bar</strong></em>' } );
const result = gutenFormatsToYFormats( formats );
expect( result ).toEqual( [
{ format: { em: true }, index: 0, length: 6 },
{ format: { strong: true }, index: 3, length: 3 },
] );
} );
} );
describe( 'applyHTMLDelta', () => {
it( 'should handle empty strings', () => {
const before = '';
const after = 'abc';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should handle full replacements', () => {
const before = 'foo';
const after = 'bar';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should add tags with attributes', () => {
const before = 'abc';
const after = '<a href="https://foo.com">abc</a>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should handle plain text before a tag', () => {
const before = '<em>a</em>';
const after = 'b<em>a</em>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should handle plain text after a tag', () => {
const before = '<em>a</em>';
const after = '<em>a</em>b';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
// TODO: Unsolved problem
it.skip( 'should handle nested tags', () => {
const before = '<a href="url">link italic</a>';
const after = '<a href="url">link <em>italic</em></a>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
const yxmlResult = richTextMapToHTML( richTextMap );
// yxmlResult is not actually what we want:
// '<a href="url">link </a><a href="url"><em>italic</em></a>'
// But since @wordpress/rich-text can convert this back to the correct HTML,
// it's ok for our purposes.
// Update 2021-12-11: This behavior was reversed in https://github.com/WordPress/gutenberg/pull/35016 😬
// Should we use normaliseFormats() in richTextMapToHTML?
// https://github.com/WordPress/gutenberg/blob/trunk/packages/rich-text/src/normalise-formats.js
const wpRichText = create( { html: yxmlResult } );
const wpRichTextStringResult = toHTMLString( { value: wpRichText } );
expect( wpRichTextStringResult ).toBe( after );
} );
// TODO: Unsolved problem
it.skip( 'should not merge adjacent tags', () => {
const before = '<code>foobar</code>';
const after = '<code>foo</code><code>bar</code>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should remove tags', () => {
const before = '<em>bold</em>';
const after = 'bold';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should handle inline images', () => {
const before = 'my pic';
const after = 'my <img src="foo" alt="bar"> pic';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
} );
describe( 'conflict merging', () => {
const updateSimultaneously = ( initial, a, b ) => {
const richTextMap1 = richTextMapFrom( initial );
const richTextMap2 = richTextMapFrom( initial );
Y.applyUpdate( richTextMap1.doc, Y.encodeStateAsUpdate( richTextMap2.doc ) );
Y.applyUpdate( richTextMap2.doc, Y.encodeStateAsUpdate( richTextMap1.doc ) );
applyHTMLDelta( richTextMapToHTML( richTextMap1 ), a, richTextMap1 );
applyHTMLDelta( richTextMapToHTML( richTextMap2 ), b, richTextMap2 );
Y.applyUpdate( richTextMap1.doc, Y.encodeStateAsUpdate( richTextMap2.doc ) );
Y.applyUpdate( richTextMap2.doc, Y.encodeStateAsUpdate( richTextMap1.doc ) );
return [ richTextMap1, richTextMap2 ].map( richTextMapToHTML );
};
it( 'should merge an append and prepend', () => {
const [ a, b ] = updateSimultaneously( 'bb', 'abb', 'bbc' );
expect( a ).toBe( 'abbc' );
expect( a ).toBe( b );
} );
it( 'should handle attribute value replacement', () => {
const htmlA = '<a href="some-url">foo</a>';
const htmlB = '<a href="another-url">foo</a>';
const [ a, b ] = updateSimultaneously( 'foo', htmlA, htmlB );
// Either value is ok, but results must match
expect( [ htmlA, htmlB ] ).toContain( a );
expect( a ).toBe( b );
} );
it( 'should apply both formats', () => {
const [ a, b ] = updateSimultaneously( 'foo', '<em>foo</em>', '<code>foo</code>' );
expect( a ).toBe( '<code><em>foo</em></code>' );
expect( a ).toBe( b );
} );
it( 'should handle inline images', () => {
const htmlA = '<li>a<img src="#"></li>';
const htmlB = '<li><img src="#">b</li>';
const [ a, b ] = updateSimultaneously( '<li><img src="#"></li>', htmlA, htmlB );
expect( a ).toBe( '<li>a<img src="#">b</li>' );
expect( a ).toBe( b );
} );
it( 'should handle lists', () => {
const htmlA = '<li>#one</li><li>two</li>';
const htmlB = '<li>one</li><li>two#</li>';
const [ a, b ] = updateSimultaneously( '<li>one</li><li>two</li>', htmlA, htmlB );
expect( a ).toBe( '<li>#one</li><li>two#</li>' );
expect( a ).toBe( b );
} );
// TODO: Unsolved problem
// We might not be able to solve this until Yjs solves the "nested tag structure fidelity" problem
// that Y.XmlText has (https://github.com/yjs/yjs/issues/337)
it.skip( 'should handle nested lists', () => {
const htmlA = '<li>#outer<ul><li>inner</li></ul></li>';
const htmlB = '<li>outer<ul><li>inner#</li></ul></li>';
const [ a, b ] = updateSimultaneously( '<li>outer<ul><li>inner</li></ul></li>', htmlA, htmlB );
expect( a ).toBe( '<li>#outer<ul><li>inner#</li></ul></li>' );
expect( a ).toBe( b );
} );
} );
describe( 'multiline', () => {
it( 'should support multiline tags', () => {
const before = '<li>foo</li><li>foo</li>';
const after = '<li>foo</li><li>bar</li>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should support multiline tags with nested tags', () => {
const before = '<li><em>foo</em></li><li>foo</li>';
const after = '<li>foo</li><li><em>bar</em></li>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should support nested lists 1', () => {
const before = '<li>outer<ol><li>inner<ul><li>innermost</li></ul></li><li>inner</li></ol></li><li>outer</li>';
const after =
'<li>outer<ol><li>inner<ul><li>innermost edit</li></ul></li><li>inner</li></ol></li><li>outer</li>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should support nested lists 2', () => {
const before = '<li>outer<ol><li>inner</li><li>inner<ul><li>innermost</li></ul></li></ol></li>';
const after = '<li>outer<ol><li>inner</li><li>inner<ul><li>innermost edit</li></ul></li></ol></li>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should support nested lists with inner formatting', () => {
const before = '<li>list item<ul><li><strong>nested</strong> item</li></ul></li>';
const after = '<li>list item<ul><li><strong>nested</strong> list item</li></ul></li>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should support indenting a list', () => {
const before = '<li>outer</li>';
const after = '<li>outer<ul><li></li></ul></li>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should support nested lists with empty item', () => {
const before = '<li>outer<ul><li></li></ul></li><li>outer</li>';
const after = '<li>#outer<ul><li></li></ul></li><li>outer</li>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
it( 'should support nested lists with inline image', () => {
const before = '<li>outer<ul><li><img src="#"></li></ul></li><li>outer</li>';
const after = '<li>#outer<ul><li><img src="#"></li></ul></li><li>outer</li>';
const richTextMap = richTextMapFrom( before );
applyHTMLDelta( before, after, richTextMap );
expect( richTextMapToHTML( richTextMap ) ).toBe( after );
} );
} );