Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/ce/ve.ce.TextState.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable TextState tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.ce.TextState' );
8
9 /* Tests */
10
11 QUnit.test( 'getChangeTransaction', ( assert ) => {
12 const underlineHash = ve.dm.example.underlineHash,
13 boldHash = ve.dm.example.boldHash,
14 annHash = ve.dm.example.annHash;
15
16 const cases = [
17 {
18 msg: 'Clear bold',
19 oldRawHtml: '<p>foo <b>bar</b> baz</p>',
20 oldInnerHtml: 'foo <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bar</b> baz',
21 newInnerHtml: 'foo bar baz',
22 operations: [
23 { type: 'retain', length: 5 },
24 {
25 type: 'replace',
26 remove: [ [ 'b', [ annHash( 'b' ) ] ], [ 'a', [ annHash( 'b' ) ] ], [ 'r', [ annHash( 'b' ) ] ] ],
27 insert: [ ...'bar' ]
28 },
29 { type: 'retain', length: 7 }
30 ]
31 },
32 {
33 msg: 'Extend bold',
34 oldRawHtml: '<p>foo <b>ba</b> baz</p>',
35 oldInnerHtml: 'foo <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">ba</b> baz',
36 newInnerHtml: 'foo <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bar</b> baz',
37 operations: [
38 { type: 'retain', length: 7 },
39 {
40 type: 'replace',
41 remove: [],
42 insert: [ [ 'r', [ annHash( 'b' ) ] ] ]
43 },
44 { type: 'retain', length: 7 }
45 ]
46 },
47 {
48 msg: 'Set bold',
49 oldRawHtml: '<p>foo bar baz</p>',
50 oldInnerHtml: 'foo bar baz',
51 newInnerHtml: 'foo <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bar</b> baz',
52 operations: [
53 { type: 'retain', length: 5 },
54 {
55 type: 'replace',
56 remove: [ ...'bar' ],
57 insert: [ [ 'b', [ boldHash ] ], [ 'a', [ boldHash ] ], [ 'r', [ boldHash ] ] ]
58 },
59 { type: 'retain', length: 7 }
60 ]
61 },
62 {
63 msg: 'Insert at start of bold',
64 oldRawHtml: 'wx<b>y</b>',
65 oldInnerHtml: 'wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">y</b>',
66 newInnerHtml: 'wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">zy</b>',
67 operations: [
68 { type: 'retain', length: 3 },
69 {
70 type: 'replace',
71 remove: [],
72 insert: [ [ 'z', [ annHash( 'b' ) ] ] ]
73 },
74 { type: 'retain', length: 4 }
75 ]
76 },
77 {
78 msg: 'Insert before start of bold',
79 oldRawHtml: 'wx<b>y</b>',
80 oldInnerHtml: 'wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">y</b>',
81 newInnerHtml: 'wxz<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">y</b>',
82 operations: [
83 { type: 'retain', length: 3 },
84 {
85 type: 'replace',
86 remove: [],
87 insert: [ 'z' ]
88 },
89 { type: 'retain', length: 4 }
90 ]
91 },
92 {
93 msg: 'Insert into insertion annotation',
94 willFail: true,
95 oldRawHtml: 'wx<b></b>z',
96 oldInnerHtml: 'wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation"></b>z',
97 newInnerHtml: 'wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">y</b>z',
98 operations: [
99 { type: 'retain', length: 3 },
100 {
101 type: 'replace',
102 remove: [],
103 insert: [ [ 'y', [ 'anything' ] ] ]
104 },
105 { type: 'retain', length: 4 }
106 ]
107 },
108 {
109 msg: 'Set bold underline before underline',
110 oldRawHtml: 'foo bar<u>baz</u>',
111 oldInnerHtml: 'foo bar<u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation">baz</u>',
112 newInnerHtml: 'foo <u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation"><b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bar</b>baz</u>',
113 operations: [
114 { type: 'retain', length: 5 },
115 {
116 type: 'replace',
117 remove: [ ...'bar' ],
118 insert: [
119 [ 'b', [ annHash( 'u' ), boldHash ] ],
120 [ 'a', [ annHash( 'u' ), boldHash ] ],
121 [ 'r', [ annHash( 'u' ), boldHash ] ]
122 ]
123 },
124 { type: 'retain', length: 6 }
125 ]
126 },
127 {
128 msg: 'Clear bold in italic',
129 oldRawHtml: '<p><i>foo <b>bar</b> baz</i></p>',
130 oldInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">foo <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bar</b> baz</i>',
131 newInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">foo bar baz</i>',
132 operations: [
133 { type: 'retain', length: 5 },
134 {
135 type: 'replace',
136 remove: [
137 [ 'b', [ annHash( 'i' ), annHash( 'b' ) ] ],
138 [ 'a', [ annHash( 'i' ), annHash( 'b' ) ] ],
139 [ 'r', [ annHash( 'i' ), annHash( 'b' ) ] ]
140 ],
141 insert: [
142 [ 'b', [ annHash( 'i' ) ] ],
143 [ 'a', [ annHash( 'i' ) ] ],
144 [ 'r', [ annHash( 'i' ) ] ]
145 ]
146 },
147 { type: 'retain', length: 7 }
148 ]
149 },
150 {
151 msg: 'Extend bold in italic',
152 oldRawHtml: '<p><i>foo <b>ba</b> baz</i></p>',
153 oldInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">foo <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">ba</b> baz</i>',
154 newInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">foo <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bar</b> baz</i>',
155 operations: [
156 { type: 'retain', length: 7 },
157 {
158 type: 'replace',
159 remove: [],
160 insert: [ [ 'r', [ annHash( 'i' ), annHash( 'b' ) ] ] ]
161 },
162 { type: 'retain', length: 7 }
163 ]
164 },
165 {
166 msg: 'Set bold in italic',
167 oldRawHtml: '<p><i>foo bar baz</i></p>',
168 oldInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">foo bar baz</i>',
169 newInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">foo <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bar</b> baz</i>',
170 operations: [
171 { type: 'retain', length: 5 },
172 {
173 type: 'replace',
174 remove: [
175 [ 'b', [ annHash( 'i' ) ] ],
176 [ 'a', [ annHash( 'i' ) ] ],
177 [ 'r', [ annHash( 'i' ) ] ]
178 ],
179 insert: [
180 [ 'b', [ annHash( 'i' ), boldHash ] ],
181 [ 'a', [ annHash( 'i' ), boldHash ] ],
182 [ 'r', [ annHash( 'i' ), boldHash ] ]
183 ]
184 },
185 { type: 'retain', length: 7 }
186 ]
187 },
188 {
189 msg: 'Insert at start of bold in italic',
190 oldRawHtml: '<i>wx<b>y</b></i>',
191 oldInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">y</b></i>',
192 newInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">zy</b></i>',
193 operations: [
194 { type: 'retain', length: 3 },
195 {
196 type: 'replace',
197 remove: [],
198 insert: [ [ 'z', [ annHash( 'i' ), annHash( 'b' ) ] ] ]
199 },
200 { type: 'retain', length: 4 }
201 ]
202 },
203 {
204 msg: 'Insert before start of bold in italic',
205 oldRawHtml: '<i>wx<b>y</b></i>',
206 oldInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">y</b></i>',
207 newInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">wxz<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">y</b></i>',
208 operations: [
209 { type: 'retain', length: 3 },
210 {
211 type: 'replace',
212 remove: [],
213 insert: [ [ 'z', [ annHash( 'i' ) ] ] ]
214 },
215 { type: 'retain', length: 4 }
216 ]
217 },
218 {
219 msg: 'Insert into insertion annotation in italic',
220 willFail: true,
221 oldRawHtml: '<i>wx<b></b>z</i>',
222 oldInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation"><img class="ve-ce-unicorn ve-ce-pre-unicorn"><img class="ve-ce-unicorn ve-ce-post-unicorn"></b>z</i>',
223 newInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">wx<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation"><img class="ve-ce-unicorn ve-ce-pre-unicorn">y<img class="ve-ce-unicorn ve-ce-post-unicorn"></b>z</i>',
224 operations: [
225 { type: 'retain', length: 3 },
226 {
227 type: 'replace',
228 remove: [],
229 insert: [ [ 'y', [ 'anything1', 'anything2' ] ] ]
230 },
231 { type: 'retain', length: 4 }
232 ]
233 },
234 {
235 msg: 'Set bold underline before underline in italic',
236 oldRawHtml: '<i>foo bar<u>baz</u></i>',
237 oldInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">foo bar<u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation">baz</u></i>',
238 newInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">foo <u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation"><b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bar</b>baz</u></i>',
239 operations: [
240 { type: 'retain', length: 5 },
241 {
242 type: 'replace',
243 remove: [
244 [ 'b', [ annHash( 'i' ) ] ],
245 [ 'a', [ annHash( 'i' ) ] ],
246 [ 'r', [ annHash( 'i' ) ] ]
247 ],
248 insert: [
249 [ 'b', [ annHash( 'i' ), annHash( 'u' ), boldHash ] ],
250 [ 'a', [ annHash( 'i' ), annHash( 'u' ), boldHash ] ],
251 [ 'r', [ annHash( 'i' ), annHash( 'u' ), boldHash ] ]
252 ]
253 },
254 { type: 'retain', length: 6 }
255 ]
256 },
257 {
258 msg: 'Set bold with distant existing bold tag',
259 oldRawHtml: '<b>foo</b> <i>bar</i> baz',
260 oldInnerHtml: '<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">foo</b> <i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">bar</i> baz',
261 newInnerHtml: '<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">foo</b> <i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">bar</i> <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">baz</b>',
262 operations: [
263 { type: 'retain', length: 9 },
264 {
265 type: 'replace',
266 remove: [ ...'baz' ],
267 // TODO: Reuse bold instead of creating a new bold?
268 // (Some annotation types may need specific rules as to
269 // when this can be done)
270 insert: [ [ 'b', [ boldHash ] ], [ 'a', [ boldHash ] ], [ 'z', [ boldHash ] ] ]
271 },
272 { type: 'retain', length: 3 }
273 ]
274 },
275 {
276 msg: 'Set underline across bold close',
277 oldRawHtml: '<i>a<b>bc</b>de</i>',
278 oldInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">a<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">bc</b>de</i>',
279 newInnerHtml: '<i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">a<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">b<u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation">c</u></b><u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation">d</u>e</i>',
280 operations: [
281 { type: 'retain', length: 3 },
282 {
283 type: 'replace',
284 remove: [
285 [ 'c', [ annHash( 'i' ), annHash( 'b' ) ] ],
286 [ 'd', [ annHash( 'i' ) ] ]
287 ],
288 insert: [
289 [ 'c', [ annHash( 'i' ), annHash( 'b' ), underlineHash ] ],
290 [ 'd', [ annHash( 'i' ), underlineHash ] ]
291 ]
292 },
293 { type: 'retain', length: 4 }
294 ]
295 },
296 {
297 msg: 'Set bold in one place and normal in another',
298 oldRawHtml: '<b>foo</b> <i>bar</i> baz',
299 oldInnerHtml: '<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">foo</b> <i class="' + ve.dm.example.textStyleClasses + ' ve-ce-italicAnnotation">bar</i> baz',
300 newInnerHtml: '<b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">foo</b> bar <b class="' + ve.dm.example.textStyleClasses + ' ve-ce-boldAnnotation">baz</b>',
301 operations: [
302 { type: 'retain', length: 5 },
303 {
304 type: 'replace',
305 // This weird-looking removal is the correct output for
306 // a diff algorithm that matches common start/end items
307 // then replaces the entire interior. In real life usage
308 // there won't usually be two separate changed regions.
309 remove: [
310 [ 'b', [ annHash( 'i' ) ] ],
311 [ 'a', [ annHash( 'i' ) ] ],
312 [ 'r', [ annHash( 'i' ) ] ],
313 ...' baz'
314 ],
315 // The first insertion get
316 insert: [
317 ...'bar ',
318 [ 'b', [ annHash( 'b' ) ] ],
319 [ 'a', [ annHash( 'b' ) ] ],
320 [ 'z', [ annHash( 'b' ) ] ]
321 ]
322 },
323 { type: 'retain', length: 3 }
324 ]
325 },
326 {
327 msg: 'Insert new chunk whose annotations match end chunk\'s',
328 oldRawHtml: '<u>x</u>yz',
329 oldInnerHtml: '<u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation">x</u>yz',
330 newInnerHtml: '<u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation">x</u>y<u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation">w</u>yz',
331 operations: [
332 { type: 'retain', length: 2 },
333 {
334 type: 'replace',
335 remove: [],
336 insert: [ 'y', [ 'w', [ annHash( 'u' ) ] ] ]
337 },
338 { type: 'retain', length: 5 }
339 ]
340 },
341 {
342 msg: 'Ambiguous insert with start and end both identical to original',
343 oldRawHtml: 'ab',
344 oldInnerHtml: 'ab',
345 newInnerHtml: 'ab<u class="' + ve.dm.example.textStyleClasses + ' ve-ce-underlineAnnotation">x</u>ab',
346 operations: [
347 { type: 'retain', length: 3 },
348 {
349 type: 'replace',
350 remove: [],
351 insert: [ [ 'x', [ underlineHash ] ], 'a', 'b' ]
352 },
353 { type: 'retain', length: 3 }
354 ]
355 }
356 ];
357
358 cases.forEach( ( caseItem ) => {
359 const view = ve.test.utils.createSurfaceViewFromHtml( caseItem.oldRawHtml );
360 const documentView = view.getDocument();
361 const documentNode = documentView.getDocumentNode();
362 const contentNode = documentNode.children[ 0 ];
363 ( caseItem.willFail ? assert.notEqualDomElement : assert.equalDomElement ).call(
364 assert,
365 $( '<div>' ).append( contentNode.$element.clone().contents() )[ 0 ],
366 $( '<div>' ).html( caseItem.oldInnerHtml )[ 0 ],
367 caseItem.msg + ' (oldInnerHtml)'
368 );
369 view.model.setSelection( new ve.dm.LinearSelection( new ve.Range( 1 ) ) );
370 const oldState = new ve.ce.RangeState( null, documentNode, false );
371 contentNode.$element.html( caseItem.newInnerHtml );
372 view.model.setSelection( new ve.dm.LinearSelection( new ve.Range( 1 ) ) );
373 const newState = new ve.ce.RangeState( oldState, documentNode, false );
374 const change = newState.textState.getChangeTransaction(
375 oldState.textState,
376 view.model.getDocument(),
377 newState.node.getOffset()
378 );
379 ( caseItem.willFail ? assert.notDeepEqual : assert.deepEqual ).call(
380 assert,
381 change.operations,
382 caseItem.operations,
383 caseItem.msg + ' (operations)'
384 );
385 view.destroy();
386 } );
387 } );