Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/dm/ve.dm.TransactionProcessor.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel TransactionProcessor tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.dm.TransactionProcessor' );
8
9 /* Tests */
10
11 QUnit.test( 'commit', ( assert ) => {
12 const store = ve.dm.example.createExampleDocument().getStore(),
13 cases = {
14 'no operations': {
15 calls: [],
16 expected: function () {}
17 },
18 retaining: {
19 calls: [ [ 'pushRetain', 38 ] ],
20 expected: function () {}
21 },
22 'changing, removing and adding attributes': {
23 calls: [
24 [ 'pushReplaceElementAttribute', 'level', 1, 2 ],
25 [ 'pushRetain', 12 ],
26 [ 'pushReplaceElementAttribute', 'style', 'bullet', 'number' ],
27 [ 'pushReplaceElementAttribute', 'test', undefined, 'abcd' ],
28 [ 'pushRetain', 27 ],
29 [ 'pushReplaceElementAttribute', 'src', ve.dm.example.imgSrc, undefined ]
30 ],
31 expected: function ( data ) {
32 data[ 0 ].attributes.level = 2;
33 data[ 12 ].attributes.style = 'number';
34 data[ 12 ].attributes.test = 'abcd';
35 delete data[ 39 ].attributes.src;
36 }
37 },
38 'changing attributes on non-element data throws an exception': {
39 calls: [
40 [ 'pushRetain', 1 ],
41 [ 'pushReplaceElementAttribute', 'foo', 23, 42 ]
42 ],
43 exception: /Invalid element error, cannot set attributes on non-element data/
44 },
45 'inserting text': {
46 calls: [
47 [ 'pushRetain', 1 ],
48 [ 'pushReplacement', 1, 0, [ ...'Foo' ] ]
49 ],
50 expected: function ( data ) {
51 data.splice( 1, 0, ...'Foo' );
52 }
53 },
54 'removing text': {
55 calls: [
56 [ 'pushRetain', 1 ],
57 [ 'pushReplacement', 1, 1, [] ]
58 ],
59 expected: function ( data ) {
60 data.splice( 1, 1 );
61 }
62 },
63 'replacing text': {
64 calls: [
65 [ 'pushRetain', 1 ],
66 [ 'pushReplacement', 1, 1, [ ...'Foo' ] ]
67 ],
68 expected: function ( data ) {
69 data.splice( 1, 1, ...'Foo' );
70 }
71 },
72 'emptying text': {
73 calls: [
74 [ 'pushRetain', 10 ],
75 [ 'pushReplacement', 10, 1, [] ]
76 ],
77 expected: function ( data ) {
78 data.splice( 10, 1 );
79 }
80 },
81 'inserting mixed content': {
82 calls: [
83 [ 'pushRetain', 1 ],
84 [ 'pushReplacement', 1, 1, [ ...'Foo', { type: 'inlineImage' }, { type: '/inlineImage' }, ...'Bar' ] ]
85 ],
86 expected: function ( data ) {
87 data.splice( 1, 1, ...'Foo', { type: 'inlineImage' }, { type: '/inlineImage' }, ...'Bar' );
88 }
89 },
90 'inserting unbalanced data': {
91 calls: [
92 [ 'pushReplacement', 0, 0, [ { type: 'table' } ] ]
93 ],
94 exception: /Unbalanced set of replace operations found/
95 },
96 'inserting unclosed inline node': {
97 calls: [
98 [ 'pushRetain', 1 ],
99 [ 'pushReplacement', 1, 1, [ 'F', { type: 'inlineImage' }, ...'OO' ] ]
100 ],
101 exception: /Unbalanced set of replace operations found/
102 },
103 'inserting an inline node in a structure position': {
104 calls: [
105 [ 'pushReplacement', 0, 0, [
106 { type: 'inlineImage' },
107 { type: '/inlineNode' }
108 ] ]
109 ],
110 exception: /Cannot add content node \(inlineImage\) to a document node/
111 },
112 'wrapping a heading in an inline node': {
113 calls: [
114 [ 'pushRetain', 39 ],
115 [ 'pushReplacement', 39, 0, [ { type: 'inlineImage' } ] ],
116 [ 'pushRetain', 2 ],
117 [ 'pushReplacement', 42, 0, [ { type: '/inlineImage' } ] ]
118 ],
119 exception: /Cannot add a child to inlineImage node/
120 },
121 'converting an element': {
122 calls: [
123 [ 'pushReplacement', 0, 1, [ { type: 'paragraph' } ] ],
124 [ 'pushRetain', 3 ],
125 [ 'pushReplacement', 4, 1, [ { type: '/paragraph' } ] ]
126 ],
127 expected: function ( data ) {
128 data[ 0 ].type = 'paragraph';
129 delete data[ 0 ].attributes;
130 data[ 4 ].type = '/paragraph';
131 }
132 },
133 'conversion with wrong closing': {
134 calls: [
135 [ 'pushReplacement', 0, 1, [ { type: 'paragraph' } ] ],
136 [ 'pushRetain', 3 ],
137 [ 'pushReplacement', 4, 1, [ { type: '/paragraph' }, { type: 'paragraph' } ] ]
138 ],
139 exception: /Unbalanced set of replace operations found/
140 },
141 'splitting an element': {
142 calls: [
143 [ 'pushRetain', 2 ],
144 [
145 'pushReplacement', 2, 0,
146 [ { type: '/heading' }, { type: 'heading', attributes: { level: 1 } } ]
147 ]
148 ],
149 expected: function ( data ) {
150 data.splice(
151 2,
152 0,
153 { type: '/heading' },
154 { type: 'heading', attributes: { level: 1 } }
155 );
156 }
157 },
158 'merging an element': {
159 calls: [
160 [ 'pushRetain', 57 ],
161 [ 'pushReplacement', 57, 2, [] ]
162 ],
163 expected: function ( data ) {
164 data.splice( 57, 2 );
165 }
166 },
167 'stripping elements': {
168 calls: [
169 [ 'pushRetain', 3 ],
170 [ 'pushReplacement', 3, 1, [] ],
171 [ 'pushRetain', 6 ],
172 [ 'pushReplacement', 10, 1, [] ]
173 ],
174 expected: function ( data ) {
175 data.splice( 10, 1 );
176 data.splice( 3, 1 );
177 }
178 },
179 'wrapping elements': {
180 calls: [
181 [ 'pushRetain', 55 ],
182 [ 'pushReplacement', 55, 0, [ { type: 'list', attributes: { style: 'number' } }, { type: 'listItem' } ] ],
183 [ 'pushRetain', 3 ],
184 [ 'pushReplacement', 58, 0, [ { type: '/listItem' }, { type: 'listItem' } ] ],
185 [ 'pushRetain', 3 ],
186 [ 'pushReplacement', 61, 0, [ { type: '/listItem' }, { type: '/list' } ] ]
187 ],
188 expected: function ( data ) {
189 data.splice( 61, 0, { type: '/listItem' }, { type: '/list' } );
190 data.splice( 58, 0, { type: '/listItem' }, { type: 'listItem' } );
191 data.splice( 55, 0, { type: 'list', attributes: { style: 'number' } }, { type: 'listItem' } );
192 }
193 },
194 'unwrapping elements': {
195 calls: [
196 [ 'pushRetain', 43 ],
197 [ 'pushReplacement', 43, 2, [] ],
198 [ 'pushRetain', 3 ],
199 [ 'pushReplacement', 48, 2, [] ],
200 [ 'pushRetain', 3 ],
201 [ 'pushReplacement', 53, 2, [] ]
202 ],
203 expected: function ( data ) {
204 data.splice( 53, 2 );
205 data.splice( 48, 2 );
206 data.splice( 43, 2 );
207 }
208 },
209 'rewrapping elements': {
210 calls: [
211 [ 'pushRetain', 43 ],
212 [ 'pushReplacement', 43, 2, [ { type: 'list', attributes: { style: 'number' } }, { type: 'listItem' } ] ],
213 [ 'pushRetain', 3 ],
214 [ 'pushReplacement', 48, 2, [ { type: '/listItem' }, { type: 'listItem' } ] ],
215 [ 'pushRetain', 3 ],
216 [ 'pushReplacement', 53, 2, [ { type: '/listItem' }, { type: '/list' } ] ]
217 ],
218 expected: function ( data ) {
219 data.splice( 53, 2, { type: '/listItem' }, { type: '/list' } );
220 data.splice( 48, 2, { type: '/listItem' }, { type: 'listItem' } );
221 data.splice( 43, 2, { type: 'list', attributes: { style: 'number' } }, { type: 'listItem' } );
222 }
223 },
224 'merging a nested element': {
225 calls: [
226 [ 'pushRetain', 47 ],
227 [ 'pushReplacement', 47, 4, [] ]
228 ],
229 expected: function ( data ) {
230 data.splice( 47, 4 );
231 }
232 },
233 'merging an element that also has a content insertion': {
234 calls: [
235 [ 'pushRetain', 56 ],
236 [ 'pushReplacement', 56, 0, [ 'x' ] ],
237 [ 'pushRetain', 1 ],
238 [ 'pushReplacement', 57, 2, [] ]
239 ],
240 expected: function ( data ) {
241 data.splice( 57, 2 );
242 data.splice( 56, 0, 'x' );
243 }
244 },
245 'merging a nested element that also has a structural insertion': {
246 calls: [
247 [ 'pushRetain', 45 ],
248 [ 'pushReplacement', 45, 0, [ { type: 'paragraph' }, 'x', { type: '/paragraph' } ] ],
249 [ 'pushRetain', 2 ],
250 [ 'pushReplacement', 47, 4, [] ]
251 ],
252 expected: function ( data ) {
253 data.splice( 47, 4 );
254 data.splice( 45, 0, { type: 'paragraph' }, 'x', { type: '/paragraph' } );
255 }
256 },
257 'merging the same element from both sides at once': {
258 data: [
259 { type: 'paragraph' },
260 'a',
261 { type: '/paragraph' },
262 { type: 'paragraph' },
263 'b',
264 { type: '/paragraph' },
265 { type: 'paragraph' },
266 'c',
267 { type: '/paragraph' },
268 { type: 'paragraph' },
269 'd',
270 { type: '/paragraph' }
271 ],
272 calls: [
273 [ 'pushRetain', 2 ],
274 [ 'pushReplacement', 2, 2, [] ],
275 [ 'pushRetain', 1 ],
276 [ 'pushReplacement', 5, 2, [] ]
277 ],
278 expected: function ( data ) {
279 data.splice( 5, 2 );
280 data.splice( 2, 2 );
281 }
282 },
283 'deleting images on both sides of a text node at once': {
284 data: [
285 { type: 'paragraph' },
286 'a',
287 { type: 'inlineImage' },
288 { type: '/inlineImage' },
289 'b',
290 { type: 'inlineImage' },
291 { type: '/inlineImage' },
292 'c',
293 { type: '/paragraph' }
294 ],
295 calls: [
296 [ 'pushRetain', 2 ],
297 [ 'pushReplacement', 2, 2, [] ],
298 [ 'pushRetain', 1 ],
299 [ 'pushReplacement', 5, 2, [] ]
300 ],
301 expected: function ( data ) {
302 data.splice( 5, 2 );
303 data.splice( 2, 2 );
304 }
305 },
306 'unwrap inside of a split inside of a wrap': {
307 data: [
308 { type: 'list', attributes: { style: 'bullet' } },
309 { type: 'listItem' },
310 { type: 'div' },
311 { type: 'paragraph' },
312 'a',
313 { type: '/paragraph' },
314 { type: 'paragraph' },
315 'b',
316 { type: '/paragraph' },
317 { type: 'paragraph' },
318 'c',
319 { type: '/paragraph' },
320 { type: '/div' },
321 { type: '/listItem' },
322 { type: '/list' },
323 { type: 'paragraph' },
324 'd',
325 { type: '/paragraph' }
326 ],
327 calls: [
328 [ 'pushReplacement', 0, 0, [ { type: 'div' } ] ],
329 [ 'pushRetain', 6 ],
330 [ 'pushReplacement', 6, 0, [ { type: '/div' } ] ],
331 [ 'pushRetain', 3 ],
332 [ 'pushReplacement', 9, 0, [ { type: 'div' } ] ],
333 [ 'pushRetain', 6 ],
334 [ 'pushReplacement', 15, 0, [ { type: '/div' } ] ]
335 ],
336 expected: function ( data ) {
337 data.splice( 15, 0, { type: '/div' } );
338 data.splice( 9, 0, { type: 'div' } );
339 data.splice( 6, 0, { type: '/div' } );
340 data.splice( 0, 0, { type: 'div' } );
341 }
342 },
343 'inserting text after alien node at the end': {
344 data: [
345 { type: 'paragraph' },
346 'a',
347 { type: 'alienInline' },
348 { type: '/alienInline' },
349 { type: '/paragraph' }
350 ],
351 calls: [
352 [ 'pushRetain', 4 ],
353 [ 'pushReplacement', 4, 0, [ 'b' ] ]
354 ],
355 expected: function ( data ) {
356 data.splice( 4, 0, 'b' );
357 }
358 },
359 'structural replacement starting at an offset without metadata': {
360 data: [
361 { type: 'paragraph' },
362 'F',
363 { type: '/paragraph' },
364 {
365 type: 'alienMeta',
366 originalDomElements: $.parseHTML( '<!-- foo -->' )
367 },
368 { type: '/alienMeta' },
369 { type: 'paragraph' },
370 ...'oo',
371 { type: '/paragraph' }
372 ],
373 calls: [
374 [ 'pushReplacement', 0, 9, [ { type: 'table' }, { type: '/table' } ] ]
375 ],
376 expected: function ( data ) {
377 data.splice( 0, 3 );
378 data.splice( 2, 4, { type: 'table' }, { type: '/table' } );
379 }
380 },
381 'structural replacement starting at an offset with metadata': {
382 data: [
383 {
384 type: 'alienMeta',
385 originalDomElements: $.parseHTML( '<!-- foo -->' )
386 },
387 { type: '/alienMeta' },
388 { type: 'paragraph' },
389 'F',
390 {
391 type: 'alienMeta',
392 attributes: {
393 style: 'comment',
394 text: ' inline '
395 }
396 },
397 { type: '/alienMeta' },
398 ...'oo',
399 { type: '/paragraph' }
400 ],
401 calls: [
402 [ 'pushReplacement', 0, 9, [ { type: 'table' }, { type: '/table' } ] ]
403 ],
404 expected: function ( data ) {
405 // Metadata is merged
406 data.splice( 2, 2 );
407 data.splice( 4, 3, { type: 'table' }, { type: '/table' } );
408 }
409 },
410 'structural replacement ending at an offset with metadata': {
411 data: [
412 {
413 type: 'alienMeta',
414 originalDomElements: $.parseHTML( '<!-- foo -->' )
415 },
416 { type: '/alienMeta' },
417 { type: 'paragraph' },
418 'F',
419 {
420 type: 'alienMeta',
421 attributes: {
422 style: 'comment',
423 text: ' inline '
424 }
425 },
426 { type: '/alienMeta' },
427 ...'oo',
428 { type: '/paragraph' },
429 {
430 type: 'alienMeta',
431 originalDomElements: $.parseHTML( '<!-- bar -->' )
432 },
433 { type: '/alienMeta' },
434 { type: 'paragraph' },
435 ...'Bar',
436 { type: '/paragraph' }
437 ],
438 calls: [
439 [ 'pushReplacement', 0, 9, [ { type: 'table' }, { type: '/table' } ] ],
440 [ 'pushRetain', 5 ]
441 ],
442 expected: function ( data ) {
443 // Metadata is merged
444 data.splice( 2, 2 );
445 data.splice( 4, 3, { type: 'table' }, { type: '/table' } );
446 }
447 },
448 'structural deletion ending at an offset with metadata': {
449 data: [
450 {
451 type: 'alienMeta',
452 originalDomElements: $.parseHTML( '<!-- foo -->' )
453 },
454 { type: '/alienMeta' },
455 { type: 'paragraph' },
456 'F',
457 {
458 type: 'alienMeta',
459 attributes: {
460 style: 'comment',
461 text: ' inline '
462 }
463 },
464 { type: '/alienMeta' },
465 ...'oo',
466 { type: '/paragraph' },
467 {
468 type: 'alienMeta',
469 originalDomElements: $.parseHTML( '<!-- bar -->' )
470 },
471 { type: '/alienMeta' },
472 { type: 'paragraph' },
473 ...'Bar',
474 { type: '/paragraph' }
475 ],
476 calls: [
477 [ 'pushReplacement', 0, 11, [] ],
478 [ 'pushRetain', 5 ]
479 ],
480 expected: function ( data ) {
481 // Metadata is merged
482 data.splice( 2, 2 );
483 data.splice( 4, 3 );
484 }
485 },
486 'preserves surrounding metadata on unwrap': {
487 data: ve.dm.example.listWithMeta,
488 calls: [
489 [ 'newFromWrap', new ve.Range( 5, 33 ),
490 [ { type: 'list' } ], [],
491 [ { type: 'listItem', attributes: { styles: [ 'bullet' ] } } ], []
492 ]
493 ],
494 expected: function ( data ) {
495 data.splice( 35, 1 ); // Remove '/list'
496 data.splice( 32, 1 ); // Remove '/listItem'
497 data.splice( 20, 1 ); // Remove 'listItem'
498 data.splice( 17, 1 ); // Remove '/listItem'
499 data.splice( 5, 1 ); // Remove 'listItem'
500 data.splice( 2, 1 ); // Remove 'list'
501 }
502 },
503 'preserves interleaved metadata on unwrap': {
504 data: ve.dm.example.listWithMeta,
505 calls: [
506 [ 'newFromWrap', new ve.Range( 5, 35 ),
507 [ { type: 'list' } ], [],
508 [ { type: 'listItem', attributes: { styles: [ 'bullet' ] } } ], []
509 ]
510 ],
511 expected: function ( data ) {
512 data.splice( 35, 1 ); // Remove '/list'
513 data.splice( 32, 1 ); // Remove '/listItem'
514 data.splice( 20, 1 ); // Remove 'listItem'
515 data.splice( 17, 1 ); // Remove '/listItem'
516 data.splice( 5, 1 ); // Remove 'listItem'
517 data.splice( 2, 1 ); // Remove 'list'
518 }
519 },
520 'preserves trailing metadata': {
521 data: ve.dm.example.listWithMeta,
522 calls: [
523 [ 'newFromInsertion', 12, [ 'b' ] ]
524 ],
525 expected: function ( data ) {
526 ve.batchSplice( data, 12, 0, [ 'b' ] );
527 }
528 }
529 };
530
531 // Run tests
532 for ( const msg in cases ) {
533 const caseItem = cases[ msg ];
534 // Generate original document
535 const originalData = caseItem.data || ve.dm.example.data;
536 const originalDoc = new ve.dm.Document(
537 ve.dm.example.preprocessAnnotations( ve.copy( originalData ), store )
538 );
539 originalDoc.buildNodeTree();
540 const testDoc = new ve.dm.Document(
541 ve.dm.example.preprocessAnnotations( ve.copy( originalData ), store )
542 );
543 testDoc.buildNodeTree();
544
545 const txBuilder = new ve.dm.TransactionBuilder();
546 let tx = null;
547 for ( let i = 0; i < caseItem.calls.length; i++ ) {
548 // Some calls need the document as its first argument
549 if ( /^(pushReplacement$|new)/.test( caseItem.calls[ i ][ 0 ] ) ) {
550 caseItem.calls[ i ].splice( 1, 0, testDoc );
551 }
552 // Special case static methods of TransactionBuilder
553 if ( /^new/.test( caseItem.calls[ i ][ 0 ] ) ) {
554 tx = ve.dm.TransactionBuilder.static[ caseItem.calls[ i ][ 0 ] ].apply( null, caseItem.calls[ i ].slice( 1 ) );
555 break;
556 }
557 txBuilder[ caseItem.calls[ i ][ 0 ] ].apply( txBuilder, caseItem.calls[ i ].slice( 1 ) );
558 }
559 if ( tx === null ) {
560 tx = txBuilder.getTransaction();
561 }
562
563 if ( 'expected' in caseItem ) {
564 // Generate expected document
565 const expectedData = ve.copy( originalData );
566 caseItem.expected( expectedData );
567 const expectedDoc = new ve.dm.Document(
568 ve.dm.example.preprocessAnnotations( expectedData, store )
569 );
570 expectedDoc.buildNodeTree();
571
572 if ( 'events' in caseItem ) {
573 // Set up event handlers
574 caseItem.events.forEach( ( event ) => {
575 let node = testDoc.getDocumentNode();
576 for ( let j = 1; j < event.length; j++ ) {
577 node = node.getChildren()[ event[ j ] ];
578 }
579 node.on( event[ 0 ], ( function ( obj ) {
580 return function () {
581 obj.fired = ( obj.fired || 0 ) + 1;
582 };
583 }( event ) ) );
584 } );
585 }
586
587 // Commit
588 testDoc.commit( tx );
589 assert.isLinearDataFrozen( testDoc.data, msg + ': linear data is frozen' );
590 assert.equalLinearDataWithDom( testDoc.getStore(), testDoc.getFullData(), expectedDoc.getFullData(), 'commit (data): ' + msg );
591 assert.equalNodeTree(
592 testDoc.getDocumentNode(),
593 expectedDoc.getDocumentNode(),
594 'commit (tree): ' + msg
595 );
596 if ( 'events' in caseItem ) {
597 caseItem.events.forEach( ( event ) => {
598 assert.strictEqual(
599 event.fired,
600 1,
601 'event ' + event[ 0 ] + ' on ' +
602 event.slice( 1 ).join( ',' ) + ': ' + msg
603 );
604 } );
605 }
606 // Rollback
607 testDoc.commit( tx.reversed() );
608 assert.equalLinearDataWithDom( testDoc.getStore(), testDoc.getFullData(), originalDoc.getFullData(), 'rollback (data): ' + msg );
609 assert.equalNodeTree(
610 testDoc.getDocumentNode(),
611 originalDoc.getDocumentNode(),
612 'rollback (tree): ' + msg
613 );
614 } else if ( 'exception' in caseItem ) {
615 assert.throws(
616 () => {
617 testDoc.commit( tx );
618 },
619 caseItem.exception,
620 'exception thrown: ' + msg
621 );
622 assert.equalLinearDataWithDom( testDoc.getStore(), testDoc.getFullData(), originalDoc.getFullData(), 'data unmodified: ' + msg );
623 assert.equalNodeTree(
624 testDoc.getDocumentNode(),
625 originalDoc.getDocumentNode(),
626 'tree unmodified: ' + msg
627 );
628 }
629 }
630 } );
631
632 // TODO: Fix the code so undoing unbold roundtrips properly, then fix this test to reflect that
633 QUnit.test( 'undo clear annotation', ( assert ) => {
634 const origData = [
635 { type: 'paragraph' },
636 [ 'x', [ ve.dm.example.boldHash, ve.dm.example.italicHash ] ],
637 { type: '/paragraph' }
638 ];
639 const doc = ve.dm.example.createExampleDocumentFromData( origData );
640 doc.store.hash( ve.dm.example.italic );
641 doc.store.hash( ve.dm.example.bold );
642 const tx = ve.dm.TransactionBuilder.static.newFromAnnotation(
643 doc,
644 new ve.Range( 1, 2 ),
645 'clear',
646 ve.dm.example.bold
647 );
648 doc.commit( tx );
649 doc.commit( tx.reversed() );
650 assert.deepEqual( doc.data.data, origData, 'Roundtrip difference undoing unbold under italic' );
651 } );