Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel Transaction tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.dm.TransactionBuilder' );
8
9 /* Helper methods */
10
11 ve.test.utils.runTransactionBuilderTests = function ( assert, cases ) {
12 for ( const msg in cases ) {
13 const txBuilder = new ve.dm.TransactionBuilder();
14 for ( let i = 0; i < cases[ msg ].calls.length; i++ ) {
15 txBuilder[ cases[ msg ].calls[ i ][ 0 ] ].apply( txBuilder, cases[ msg ].calls[ i ].slice( 1 ) );
16 }
17 assert.deepEqualWithDomElements( txBuilder.getTransaction().getOperations(), cases[ msg ].ops, msg + ': operations match' );
18 }
19 };
20
21 ve.test.utils.runTransactionConstructorTests = function ( assert, constructor, cases, testRange ) {
22 for ( const msg in cases ) {
23 const doc = cases[ msg ].args[ 0 ];
24 const args = cases[ msg ].args;
25 if ( cases[ msg ].ops ) {
26 const tx = constructor.apply( null, args );
27 assert.equalLinearDataWithDom( doc.getStore(), tx.getOperations(), cases[ msg ].ops, msg + ': operations match' );
28 if ( testRange ) {
29 assert.equalRange(
30 tx.getModifiedRange( doc ),
31 cases[ msg ].range || new ve.Range( args[ 1 ], args[ 1 ] + args[ 2 ].length ),
32 msg + ': range matches'
33 );
34 }
35 } else if ( cases[ msg ].exception ) {
36 assert.throws( () => {
37 constructor.apply( ve.dm.Transaction, args );
38 }, cases[ msg ].exception, msg + ': throw exception' );
39 }
40 }
41 };
42
43 /* Tests */
44
45 QUnit.test( 'newFromInsertion', ( assert ) => {
46 const doc = ve.dm.example.createExampleDocument(),
47 isolationDoc = ve.dm.example.createExampleDocument( 'isolationData' ),
48 complexTableDoc = ve.dm.example.createExampleDocument( 'complexTable' ),
49 doc2 = new ve.dm.Document(
50 ve.dm.example.preprocessAnnotations( [ { type: 'paragraph' }, { type: '/paragraph' }, { type: 'internalList' }, { type: '/internalList' } ] )
51 ),
52 doc3 = new ve.dm.Document(
53 ve.dm.example.preprocessAnnotations( [ { type: 'paragraph' }, ...'Foo', { type: '/paragraph' }, { type: 'internalList' }, { type: '/internalList' } ] )
54 ),
55 cases = {
56 'paragraph before first element': {
57 args: [ doc, 0, [ { type: 'paragraph' }, '1', { type: '/paragraph' } ] ],
58 ops: [
59 {
60 type: 'replace',
61 remove: [],
62 insert: [ { type: 'paragraph' }, '1', { type: '/paragraph' } ]
63 },
64 { type: 'retain', length: 63 }
65 ]
66 },
67 'paragraph after last element': {
68 args: [ doc, 61, [ { type: 'paragraph' }, '1', { type: '/paragraph' } ] ],
69 ops: [
70 { type: 'retain', length: 61 },
71 {
72 type: 'replace',
73 remove: [],
74 insert: [ { type: 'paragraph' }, '1', { type: '/paragraph' } ]
75 },
76 { type: 'retain', length: 2 }
77 ]
78 },
79 'split paragraph': {
80 args: [ doc, 10, [ '1', { type: '/paragraph' }, { type: 'paragraph' } ] ],
81 ops: [
82 { type: 'retain', length: 10 },
83 {
84 type: 'replace',
85 remove: [],
86 insert: [ '1', { type: '/paragraph' }, { type: 'paragraph' } ]
87 },
88 { type: 'retain', length: 53 }
89 ]
90 },
91 'paragraph inside a heading closes and reopens heading': {
92 args: [ doc, 2, [ { type: 'paragraph' }, ...'FOO', { type: '/paragraph' } ] ],
93 ops: [
94 { type: 'retain', length: 2 },
95 {
96 type: 'replace',
97 remove: [],
98 insert: [ { type: '/heading' }, { type: 'paragraph' }, ...'FOO', { type: '/paragraph' }, { type: 'heading', attributes: { level: 1 } } ],
99 insertedDataOffset: 1,
100 insertedDataLength: 5
101 },
102 { type: 'retain', length: 61 }
103 ],
104 range: new ve.Range( 3, 8 )
105 },
106 'paragraph inside a list moves in front of list': {
107 args: [ doc, 13, [ { type: 'paragraph' }, ...'FOO', { type: '/paragraph' } ] ],
108 ops: [
109 { type: 'retain', length: 12 },
110 {
111 type: 'replace',
112 remove: [],
113 insert: [ { type: 'paragraph' }, ...'FOO', { type: '/paragraph' } ]
114 },
115 { type: 'retain', length: 51 }
116 ],
117 range: new ve.Range( 12, 17 )
118 },
119 'tableCell inside the document is wrapped in a table, tableSection and tableRow': {
120 args: [ doc, 43, [ { type: 'tableCell', attributes: { style: 'data' } }, { type: 'paragraph' }, ...'FOO', { type: '/paragraph' }, { type: '/tableCell' } ] ],
121 ops: [
122 { type: 'retain', length: 43 },
123 {
124 type: 'replace',
125 remove: [],
126 insert: [ { type: 'table' }, { type: 'tableSection', attributes: { style: 'body' } }, { type: 'tableRow' }, { type: 'tableCell', attributes: { style: 'data' } }, { type: 'paragraph' }, ...'FOO', { type: '/paragraph' }, { type: '/tableCell' }, { type: '/tableRow' }, { type: '/tableSection' }, { type: '/table' } ],
127 insertedDataOffset: 3,
128 insertedDataLength: 7
129 },
130 { type: 'retain', length: 20 }
131 ],
132 range: new ve.Range( 46, 53 )
133 },
134 'tableCell inside a paragraph is wrapped in a table, tableSection and tableRow and moves outside of paragraph': {
135 args: [ doc, 52, [ { type: 'tableCell', attributes: { style: 'data' } }, { type: 'paragraph' }, ...'FOO', { type: '/paragraph' }, { type: '/tableCell' } ] ],
136 ops: [
137 { type: 'retain', length: 53 },
138 {
139 type: 'replace',
140 remove: [],
141 insert: [ { type: 'table' }, { type: 'tableSection', attributes: { style: 'body' } }, { type: 'tableRow' }, { type: 'tableCell', attributes: { style: 'data' } }, { type: 'paragraph' }, ...'FOO', { type: '/paragraph' }, { type: '/tableCell' }, { type: '/tableRow' }, { type: '/tableSection' }, { type: '/table' } ],
142 insertedDataOffset: 3,
143 insertedDataLength: 7
144 },
145 { type: 'retain', length: 10 }
146 ],
147 range: new ve.Range( 56, 63 )
148 },
149 'text at a structural location in the document is wrapped in a paragraph': {
150 args: [ doc, 0, [ ...'FOO' ] ],
151 ops: [
152 {
153 type: 'replace',
154 remove: [],
155 insert: [
156 { type: 'paragraph', internal: { generated: 'wrapper' } },
157 ...'FOO',
158 { type: '/paragraph' }
159 ],
160 insertedDataOffset: 1,
161 insertedDataLength: 3
162 },
163 { type: 'retain', length: 63 }
164 ],
165 range: new ve.Range( 1, 4 )
166 },
167 'text inside a paragraph is not wrapped in a paragraph': {
168 args: [ doc, 16, [ ...'FOO' ] ],
169 ops: [
170 { type: 'retain', length: 16 },
171 {
172 type: 'replace',
173 remove: [],
174 insert: [ ...'FOO' ]
175 },
176 { type: 'retain', length: 47 }
177 ]
178 },
179 'text inside a heading is not wrapped in a paragraph': {
180 args: [ doc, 2, [ ...'FOO' ] ],
181 ops: [
182 { type: 'retain', length: 2 },
183 {
184 type: 'replace',
185 remove: [],
186 insert: [ ...'FOO' ]
187 },
188 { type: 'retain', length: 61 }
189 ]
190 },
191 'paragraph in the middle of an insertion is balanced': {
192 args: [ doc, 16, [ 'F', { type: 'paragraph' }, 'O', { type: '/paragraph' }, 'O' ] ],
193 ops: [
194 { type: 'retain', length: 16 },
195 {
196 type: 'replace',
197 remove: [],
198 insert: [
199 'F', { type: '/paragraph' },
200 { type: 'paragraph' }, 'O', { type: '/paragraph' },
201 { type: 'paragraph', internal: { generated: 'wrapper' } }, 'O'
202 ]
203 },
204 { type: 'retain', length: 47 }
205 ],
206 range: new ve.Range( 16, 23 )
207 },
208 'text inside a tableSection moves all the way to the end of the table and is wrapped in a paragraph': {
209 args: [ doc, 34, [ ...'FOO' ] ],
210 ops: [
211 { type: 'retain', length: 37 },
212 {
213 type: 'replace',
214 remove: [],
215 insert: [ { type: 'paragraph', internal: { generated: 'wrapper' } }, ...'FOO', { type: '/paragraph' } ],
216 insertedDataOffset: 1,
217 insertedDataLength: 3
218 },
219 { type: 'retain', length: 26 }
220 ],
221 range: new ve.Range( 38, 41 )
222 },
223 'insert two complete paragraphs into start of paragraph moves insertion point left': {
224 args: [ doc, 10, [ { type: 'paragraph' }, ...'FOO', { type: '/paragraph' }, { type: 'paragraph' }, ...'BAR', { type: '/paragraph' } ] ],
225 ops: [
226 { type: 'retain', length: 9 },
227 {
228 type: 'replace',
229 remove: [],
230 insert: [ { type: 'paragraph' }, ...'FOO', { type: '/paragraph' }, { type: 'paragraph' }, ...'BAR', { type: '/paragraph' } ]
231 },
232 { type: 'retain', length: 54 }
233 ],
234 range: new ve.Range( 9, 19 )
235 },
236 'insert text, close paragraph and open heading into end of paragraph moves insertion point right': {
237 args: [ doc, 57, [ ...'FOO', { type: '/paragraph' }, { type: 'heading', attributes: { level: 1 } }, ...'BAR' ] ],
238 ops: [
239 { type: 'retain', length: 58 },
240 {
241 type: 'replace',
242 remove: [],
243 insert: [
244 { type: 'paragraph', internal: { generated: 'wrapper' } }, ...'FOO', { type: '/paragraph' },
245 { type: 'heading', attributes: { level: 1 } }, ...'BAR', { type: '/heading' }
246 ],
247 insertedDataOffset: 1,
248 insertedDataLength: 8
249 },
250 { type: 'retain', length: 5 }
251 ],
252 range: new ve.Range( 59, 67 )
253 },
254 'insert heading and incomplete paragraph into heading': {
255 args: [ doc, 2, [ { type: 'heading', attributes: { level: 1 } }, ...'FOO', { type: '/heading' }, { type: 'paragraph' }, ...'BAR' ] ],
256 ops: [
257 { type: 'retain', length: 2 },
258 {
259 type: 'replace',
260 remove: [],
261 insert: [
262 { type: '/heading' }, { type: 'heading', attributes: { level: 1 } }, ...'FOO', { type: '/heading' },
263 { type: 'paragraph' }, ...'BAR', { type: '/paragraph' },
264 { type: 'heading', attributes: { level: 1 } }
265 ],
266 insertedDataOffset: 1,
267 insertedDataLength: 9
268 },
269 { type: 'retain', length: 61 }
270 ],
271 range: new ve.Range( 3, 12 )
272 },
273 'inserting two paragraphs into a document with just an empty paragraph': {
274 args: [ doc2, 1, [ ...'FOO', { type: '/paragraph' }, { type: 'paragraph' }, ...'BAR' ] ],
275 ops: [
276 { type: 'retain', length: 1 },
277 {
278 type: 'replace',
279 remove: [],
280 insert: [ ...'FOO', { type: '/paragraph' }, { type: 'paragraph' }, ...'BAR' ]
281 },
282 { type: 'retain', length: 3 }
283 ]
284 },
285 'inserting three paragraphs into a document with just an empty paragraph': {
286 args: [ doc2, 1, [ ...'FOO', { type: '/paragraph' }, { type: 'paragraph' }, ...'BAR', { type: '/paragraph' }, { type: 'paragraph' }, ...'BAZ' ] ],
287 ops: [
288 { type: 'retain', length: 1 },
289 {
290 type: 'replace',
291 remove: [],
292 insert: [ ...'FOO', { type: '/paragraph' }, { type: 'paragraph' }, ...'BAR', { type: '/paragraph' }, { type: 'paragraph' }, ...'BAZ' ]
293 },
294 { type: 'retain', length: 3 }
295 ]
296 },
297 'inserting one paragraph into empty paragraph replaces it': {
298 args: [ doc2, 1, [ { type: 'paragraph' }, ...'FOO', { type: '/paragraph' } ] ],
299 ops: [
300 {
301 type: 'replace',
302 remove: [ { type: 'paragraph' }, { type: '/paragraph' } ],
303 insert: [ { type: 'paragraph' }, ...'FOO', { type: '/paragraph' } ]
304 },
305 { type: 'retain', length: 2 }
306 ],
307 range: new ve.Range( 0, 5 )
308 },
309 'inserting paragraph at end of paragraph moves insertion point forward': {
310 args: [ doc3, 4, [ { type: 'paragraph' }, ...'BAR', { type: '/paragraph' } ] ],
311 ops: [
312 { type: 'retain', length: 5 },
313 {
314 type: 'replace',
315 remove: [],
316 insert: [ { type: 'paragraph' }, ...'BAR', { type: '/paragraph' } ]
317 },
318 { type: 'retain', length: 2 }
319 ],
320 range: new ve.Range( 5, 10 )
321 },
322 'inserting paragraph into middle of paragraph splits paragraph': {
323 args: [ doc3, 2, [ { type: 'paragraph' }, ...'BAR', { type: '/paragraph' } ] ],
324 ops: [
325 { type: 'retain', length: 2 },
326 {
327 type: 'replace',
328 remove: [],
329 insert: [ { type: '/paragraph' }, { type: 'paragraph' }, ...'BAR', { type: '/paragraph' }, { type: 'paragraph' } ],
330 insertedDataOffset: 1,
331 insertedDataLength: 5
332 },
333 { type: 'retain', length: 5 }
334 ],
335 range: new ve.Range( 3, 8 )
336 },
337 'inserting paragraph into middle of list splits list': {
338 args: [ isolationDoc, 11, [ { type: 'paragraph' }, ...'BAR', { type: '/paragraph' } ] ],
339 ops: [
340 { type: 'retain', length: 11 },
341 {
342 type: 'replace',
343 remove: [],
344 insert: [ { type: '/list' }, { type: 'paragraph' }, ...'BAR', { type: '/paragraph' }, { type: 'list', attributes: { style: 'bullet' } } ],
345 insertedDataOffset: 1,
346 insertedDataLength: 5
347 },
348 { type: 'retain', length: 235 }
349 ],
350 range: new ve.Range( 12, 17 )
351 },
352 'inserting paragraph between table cells splits table, tableSection and tableRow': {
353 args: [ complexTableDoc, 40, [ { type: 'paragraph' }, ...'BAR', { type: '/paragraph' } ] ],
354 ops: [
355 { type: 'retain', length: 40 },
356 {
357 type: 'replace',
358 remove: [],
359 insert: [
360 { type: '/tableRow' }, { type: '/tableSection' }, { type: '/table' },
361 { type: 'paragraph' }, ...'BAR', { type: '/paragraph' },
362 { type: 'table' }, { type: 'tableSection', attributes: { style: 'body' } }, { type: 'tableRow' }
363 ],
364 insertedDataOffset: 3,
365 insertedDataLength: 5
366 },
367 { type: 'retain', length: 13 }
368 ],
369 range: new ve.Range( 43, 48 )
370 }
371 // TODO test cases for unclosed openings
372 // TODO test cases for (currently failing) unopened closings use case
373 // TODO analyze other possible cases (substrings of linmod data)
374 };
375
376 for ( const key in cases ) {
377 for ( let i = 0; i < cases[ key ].ops.length; i++ ) {
378 if ( cases[ key ].ops[ i ].remove ) {
379 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].remove, doc.getStore() );
380 }
381 if ( cases[ key ].ops[ i ].insert ) {
382 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].insert, doc.getStore() );
383 }
384 }
385 }
386 ve.test.utils.runTransactionConstructorTests( assert, ve.dm.TransactionBuilder.static.newFromInsertion, cases, true );
387 } );
388
389 QUnit.test( 'newFromRemoval', ( assert ) => {
390 const doc = ve.dm.example.createExampleDocument( 'data' ),
391 alienDoc = ve.dm.example.createExampleDocument( 'alienData' ),
392 alienWithEmptyDoc = ve.dm.example.createExampleDocument( 'alienWithEmptyData' ),
393 internalDoc = ve.dm.example.createExampleDocument( 'internalData' ),
394 cases = {
395 'content in first element': {
396 args: [ doc, new ve.Range( 1, 3 ) ],
397 ops: [
398 { type: 'retain', length: 1 },
399 {
400 type: 'replace',
401 remove: [
402 'a',
403 [ 'b', [ ve.dm.example.bold ] ]
404 ],
405 insert: []
406 },
407 { type: 'retain', length: 60 }
408 ]
409 },
410 'content in last element': {
411 args: [ doc, new ve.Range( 59, 60 ) ],
412 ops: [
413 { type: 'retain', length: 59 },
414 {
415 type: 'replace',
416 remove: [ 'm' ],
417 insert: []
418 },
419 { type: 'retain', length: 3 }
420 ]
421 },
422 'first element': {
423 args: [ doc, new ve.Range( 0, 5 ) ],
424 ops: [
425 {
426 type: 'replace',
427 remove: [
428 { type: 'heading', attributes: { level: 1 } },
429 'a',
430 [ 'b', [ ve.dm.example.bold ] ],
431 [ 'c', [ ve.dm.example.italic ] ],
432 { type: '/heading' }
433 ],
434 insert: []
435 },
436 { type: 'retain', length: 58 }
437 ]
438 },
439 'middle element with image': {
440 args: [ doc, new ve.Range( 38, 42 ) ],
441 ops: [
442 { type: 'retain', length: 38 },
443 {
444 type: 'replace',
445 remove: [
446 'h',
447 ve.dm.example.image.data,
448 { type: '/inlineImage' },
449 'i'
450 ],
451 insert: []
452 },
453 { type: 'retain', length: 21 }
454 ]
455 },
456 'extra openings': {
457 args: [ doc, new ve.Range( 0, 7 ) ],
458 ops: [
459 {
460 type: 'replace',
461 remove: [
462 { type: 'heading', attributes: { level: 1 } },
463 'a',
464 [ 'b', [ ve.dm.example.bold ] ],
465 [ 'c', [ ve.dm.example.italic ] ],
466 { type: '/heading' }
467 ],
468 insert: []
469 },
470 { type: 'retain', length: 58 }
471 ]
472 },
473 'last element': {
474 args: [ doc, new ve.Range( 58, 61 ) ],
475 ops: [
476 { type: 'retain', length: 58 },
477 {
478 type: 'replace',
479 remove: [ { type: 'paragraph' }, 'm', { type: '/paragraph' } ],
480 insert: []
481 },
482 { type: 'retain', length: 2 }
483 ]
484 },
485 'extra closings': {
486 args: [ doc, new ve.Range( 31, 39 ) ],
487 ops: [
488 { type: 'retain', length: 38 },
489 {
490 type: 'replace',
491 remove: [ 'h' ],
492 insert: []
493 },
494 { type: 'retain', length: 24 }
495 ]
496 },
497 'merge last two elements': {
498 args: [ doc, new ve.Range( 57, 59 ) ],
499 ops: [
500 { type: 'retain', length: 57 },
501 {
502 type: 'replace',
503 remove: [ { type: '/paragraph' }, { type: 'paragraph' } ],
504 insert: []
505 },
506 { type: 'retain', length: 4 }
507 ]
508 },
509 'strip out of paragraph in tableCell and paragraph in listItem': {
510 args: [ doc, new ve.Range( 10, 16 ) ],
511 ops: [
512 { type: 'retain', length: 10 },
513 {
514 type: 'replace',
515 remove: [ 'd' ],
516 insert: []
517 },
518 { type: 'retain', length: 4 },
519 {
520 type: 'replace',
521 remove: [ 'e' ],
522 insert: []
523 },
524 { type: 'retain', length: 47 }
525 ]
526 },
527 'over first alien into paragraph': {
528 args: [ alienDoc, new ve.Range( 0, 4 ) ],
529 ops: [
530 {
531 type: 'replace',
532 remove: [ { type: 'alienBlock', originalDomElements: $.parseHTML( '<foobar />' ) }, { type: '/alienBlock' } ],
533 insert: []
534 },
535 { type: 'retain', length: 1 },
536 {
537 type: 'replace',
538 remove: [ 'a' ],
539 insert: []
540 },
541 { type: 'retain', length: alienDoc.data.getLength() - 4 }
542 ]
543 },
544 'out of paragraph over last alien': {
545 args: [ alienDoc, new ve.Range( 6, 10 ) ],
546 ops: [
547 { type: 'retain', length: 6 },
548 {
549 type: 'replace',
550 remove: [ 'b' ],
551 insert: []
552 },
553 { type: 'retain', length: 1 },
554 {
555 type: 'replace',
556 remove: [ { type: 'alienBlock', originalDomElements: $.parseHTML( '<foobar />' ) }, { type: '/alienBlock' } ],
557 insert: []
558 },
559 { type: 'retain', length: alienDoc.data.getLength() - 10 }
560 ]
561 },
562 'out of empty paragraph over last alien': {
563 args: [ alienWithEmptyDoc, new ve.Range( 1, 6 ) ],
564 ops: [
565 { type: 'retain', length: 1 },
566 {
567 type: 'replace',
568 remove: [ { type: '/paragraph' }, { type: 'paragraph' }, 'a', { type: 'alienInline', originalDomElements: $.parseHTML( '<foobar />' ) }, { type: '/alienInline' } ],
569 insert: []
570 },
571 { type: 'retain', length: alienWithEmptyDoc.data.getLength() - 6 }
572 ]
573 },
574 'merging two paragraphs inside definitionListItems': {
575 args: [ doc, new ve.Range( 47, 51 ) ],
576 ops: [
577 { type: 'retain', length: 47 },
578 {
579 type: 'replace',
580 remove: [ { type: '/paragraph' }, { type: '/definitionListItem' }, { type: 'definitionListItem', attributes: { style: 'definition' } }, { type: 'paragraph' } ],
581 insert: []
582 },
583 { type: 'retain', length: 12 }
584 ]
585 },
586 'merging two paragraphs while also deleting some content': {
587 args: [ doc, new ve.Range( 56, 59 ) ],
588 ops: [
589 { type: 'retain', length: 56 },
590 {
591 type: 'replace',
592 remove: [ 'l', { type: '/paragraph' }, { type: 'paragraph' } ],
593 insert: []
594 },
595 { type: 'retain', length: 4 }
596 ]
597 },
598 'removing from a heading into a paragraph': {
599 args: [ doc, new ve.Range( 2, 57 ) ],
600 ops: [
601 { type: 'retain', length: 2 },
602 {
603 type: 'replace',
604 remove: doc.getData().slice( 2, 4 ),
605 insert: []
606 },
607 { type: 'retain', length: 1 },
608 {
609 type: 'replace',
610 remove: doc.getData().slice( 5, 55 ),
611 insert: []
612 },
613 { type: 'retain', length: 1 },
614 {
615 type: 'replace',
616 remove: [ 'l' ],
617 insert: []
618 },
619 { type: 'retain', length: 6 }
620 ]
621 },
622 'removing content from a paragraph in the middle': {
623 args: [ doc, new ve.Range( 56, 57 ) ],
624 ops: [
625 { type: 'retain', length: 56 },
626 {
627 type: 'replace',
628 remove: [ 'l' ],
629 insert: []
630 },
631 { type: 'retain', length: 6 }
632 ]
633 },
634 'selection including internal nodes doesn\'t remove them': {
635 args: [ internalDoc, new ve.Range( 2, 24 ) ],
636 ops: [
637 { type: 'retain', length: 2 },
638 {
639 type: 'replace',
640 remove: [
641 ...'oo',
642 { type: '/paragraph' }
643 ],
644 insert: []
645 },
646 { type: 'retain', length: 16 },
647 {
648 type: 'replace',
649 remove: [
650 { type: 'paragraph' },
651 ...'Qu'
652 ],
653 insert: []
654 },
655 { type: 'retain', length: 3 }
656 ]
657 },
658 'selection ending with internal nodes': {
659 args: [ internalDoc, new ve.Range( 2, 21 ) ],
660 ops: [
661 { type: 'retain', length: 2 },
662 {
663 type: 'replace',
664 remove: [
665 ...'oo'
666 ],
667 insert: []
668 },
669 { type: 'retain', length: 23 }
670 ]
671 },
672 'selection starting with internal nodes': {
673 args: [ internalDoc, new ve.Range( 5, 24 ) ],
674 ops: [
675 { type: 'retain', length: 22 },
676 {
677 type: 'replace',
678 remove: [
679 ...'Qu'
680 ],
681 insert: []
682 },
683 { type: 'retain', length: 3 }
684 ]
685 },
686 'selection of just internal nodes returns a no-op transaction': {
687 args: [ internalDoc, new ve.Range( 5, 21 ) ],
688 ops: [
689 { type: 'retain', length: 27 }
690 ]
691 }
692 };
693
694 for ( const key in cases ) {
695 for ( let i = 0; i < cases[ key ].ops.length; i++ ) {
696 const store = cases[ key ].args[ 0 ].getStore();
697 if ( cases[ key ].ops[ i ].remove ) {
698 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].remove, store );
699 }
700 if ( cases[ key ].ops[ i ].insert ) {
701 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].insert, store );
702 }
703 }
704 }
705 ve.test.utils.runTransactionConstructorTests( assert, ve.dm.TransactionBuilder.static.newFromRemoval, cases );
706 } );
707
708 QUnit.test( 'newFromReplacement', ( assert ) => {
709 const doc = ve.dm.example.createExampleDocument(),
710 metaDoc = ve.dm.example.createExampleDocument( 'withMeta' ),
711
712 cases = {
713 'replace, preserving metadata': {
714 args: [ metaDoc, new ve.Range( 6, 21 ), [ ...'XY' ] ],
715 ops: [
716 { type: 'retain', length: 4 },
717 {
718 type: 'replace',
719 insert: metaDoc.getData( new ve.Range( 6, 21 ) )
720 .filter( ( item ) => item.type === 'alienMeta' ||
721 item.type === '/alienMeta' ),
722 remove: []
723 },
724 { type: 'retain', length: 2 },
725 {
726 type: 'replace',
727 insert: [ ...'XY' ],
728 remove: metaDoc.getData( new ve.Range( 6, 21 ) )
729 },
730 { type: 'retain', length: 14 }
731 ]
732 },
733 'replace, removing metadata': {
734 args: [ metaDoc, new ve.Range( 6, 21 ), [ ...'XY' ], true ],
735 ops: [
736 { type: 'retain', length: 6 },
737 {
738 type: 'replace',
739 insert: [ ...'XY' ],
740 remove: metaDoc.getData( new ve.Range( 6, 21 ) )
741 },
742 { type: 'retain', length: 14 }
743 ]
744 }
745 };
746
747 for ( const key in cases ) {
748 for ( let i = 0; i < cases[ key ].ops.length; i++ ) {
749 if ( cases[ key ].ops[ i ].remove ) {
750 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].remove, doc.getStore() );
751 }
752 if ( cases[ key ].ops[ i ].insert ) {
753 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].insert, doc.getStore() );
754 }
755 }
756 }
757 ve.test.utils.runTransactionConstructorTests( assert, ve.dm.TransactionBuilder.static.newFromReplacement, cases, false );
758 } );
759
760 QUnit.test( 'newFromDocumentInsertion', ( assert ) => {
761 const doc = ve.dm.example.createExampleDocument( 'internalData' ),
762 bold = ve.dm.example.createAnnotation( ve.dm.example.bold ),
763 whee = [ { type: 'paragraph' }, ...'Whee', { type: '/paragraph' } ],
764 wheeItem = [ { type: 'internalItem' }, ...whee, { type: '/internalItem' } ],
765 cases = [
766 {
767 msg: 'simple insertion',
768 doc: 'internalData',
769 offset: 7,
770 range: new ve.Range( 7, 12 ),
771 modify: function ( newDoc ) {
772 // Change "Bar" to "Bazaar"
773 newDoc.commit( ve.dm.TransactionBuilder.static.newFromInsertion(
774 newDoc, 3, [ ...'zaa' ]
775 ) );
776 },
777 removalOps: [
778 { type: 'retain', length: 7 },
779 {
780 type: 'replace',
781 remove: doc.getData( new ve.Range( 7, 12 ) ),
782 insert: []
783 },
784 { type: 'retain', length: 15 }
785 ],
786 expectedOps: [
787 { type: 'retain', length: 6 },
788 {
789 type: 'replace',
790 remove: [
791 ...doc.getData( new ve.Range( 6, 7 ) ),
792 ...doc.getData( new ve.Range( 12, 20 ) )
793 ],
794 insert: [
795 ...doc.getData( new ve.Range( 6, 10 ) ),
796 ...'zaa',
797 ...doc.getData( new ve.Range( 10, 20 ) )
798 ]
799 },
800 { type: 'retain', length: 7 }
801 ]
802 },
803 {
804 msg: 'simple annotation',
805 doc: 'internalData',
806 offset: 14,
807 range: new ve.Range( 14, 19 ),
808 modify: function ( newDoc ) {
809 // Bold the first two characters
810 newDoc.commit( ve.dm.TransactionBuilder.static.newFromAnnotation(
811 newDoc, new ve.Range( 1, 3 ), 'set', bold
812 ) );
813 },
814 removalOps: [
815 { type: 'retain', length: 14 },
816 {
817 type: 'replace',
818 remove: doc.getData( new ve.Range( 14, 19 ) ),
819 insert: []
820 },
821 { type: 'retain', length: 8 }
822 ],
823 expectedOps: [
824 { type: 'retain', length: 6 },
825 {
826 type: 'replace',
827 remove: [
828 ...doc.getData( new ve.Range( 6, 14 ) ),
829 ...doc.getData( new ve.Range( 19, 20 ) )
830 ],
831 insert: [
832 ...doc.getData( new ve.Range( 6, 15 ) ),
833 [ doc.data.getData( 15 ), [ ve.dm.example.boldHash ] ],
834 [ doc.data.getData( 16 ), [ ve.dm.example.boldHash ] ],
835 ...doc.getData( new ve.Range( 17, 20 ) )
836 ]
837 },
838 { type: 'retain', length: 7 }
839 ],
840 expectedStoreItems: {
841 h49981eab0f8056ff: bold
842 }
843 },
844 {
845 msg: 'insertion into internal list',
846 doc: 'internalData',
847 offset: 21,
848 range: new ve.Range( 21, 27 ),
849 modify: function ( newDoc ) {
850 const insertion = newDoc.internalList.getItemInsertion( 'test', 'whee', whee );
851 newDoc.commit( insertion.transaction );
852 },
853 removalOps: [
854 { type: 'retain', length: 21 },
855 {
856 type: 'replace',
857 remove: doc.getData( new ve.Range( 21, 27 ) ),
858 insert: []
859 }
860 ],
861 expectedOps: [
862 { type: 'retain', length: 6 },
863 {
864 type: 'replace',
865 remove: doc.getData( new ve.Range( 6, 20 ) ),
866 insert: [
867 ...doc.getData( new ve.Range( 6, 20 ) ),
868 ...wheeItem
869 ]
870 },
871 { type: 'retain', length: 1 },
872 {
873 type: 'replace',
874 remove: [],
875 insert: doc.getData( new ve.Range( 21, 27 ) )
876 }
877 ]
878 },
879 {
880 msg: 'change in internal list',
881 doc: 'internalData',
882 offset: 21,
883 range: new ve.Range( 21, 27 ),
884 modify: function ( newDoc ) {
885 newDoc.commit( ve.dm.TransactionBuilder.static.newFromInsertion(
886 newDoc, 12, [ ...'!!!' ]
887 ) );
888 },
889 removalOps: [
890 { type: 'retain', length: 21 },
891 {
892 type: 'replace',
893 remove: doc.getData( new ve.Range( 21, 27 ) ),
894 insert: []
895 }
896 ],
897 expectedOps: [
898 { type: 'retain', length: 6 },
899 {
900 type: 'replace',
901 remove: doc.getData( new ve.Range( 6, 20 ) ),
902 insert: [
903 ...doc.getData( new ve.Range( 6, 11 ) ),
904 ...'!!!',
905 ...doc.getData( new ve.Range( 11, 20 ) )
906 ]
907 },
908 { type: 'retain', length: 1 },
909 {
910 type: 'replace',
911 remove: [],
912 insert: doc.getData( new ve.Range( 21, 27 ) )
913 }
914 ]
915 },
916 {
917 msg: 'insertion into internal list from slice within internal list',
918 doc: 'internalData',
919 offset: 7,
920 range: new ve.Range( 7, 12 ),
921 modify: function ( newDoc ) {
922 const insertion = newDoc.internalList.getItemInsertion( 'test', 'whee', whee );
923 newDoc.commit( insertion.transaction );
924 },
925 removalOps: [
926 { type: 'retain', length: 7 },
927 {
928 type: 'replace',
929 remove: doc.getData( new ve.Range( 7, 12 ) ),
930 insert: []
931 },
932 { type: 'retain', length: 15 }
933 ],
934 expectedOps: [
935 { type: 'retain', length: 6 },
936 {
937 type: 'replace',
938 remove: [
939 ...doc.getData( new ve.Range( 6, 7 ) ),
940 ...doc.getData( new ve.Range( 12, 20 ) )
941 ],
942 insert: [
943 ...doc.getData( new ve.Range( 6, 20 ) ),
944 ...wheeItem
945 ]
946 },
947 { type: 'retain', length: 7 }
948 ]
949 },
950 {
951 msg: 'insertion from unrelated document',
952 doc: 'internalData',
953 offset: 0,
954 newDocData: [
955 { type: 'paragraph' }, ...'Foo', { type: '/paragraph' },
956 { type: 'internalList' }, { type: '/internalList' }
957 ],
958 removalOps: [],
959 expectedOps: [
960 {
961 type: 'replace',
962 remove: [],
963 insert: [ { type: 'paragraph' }, ...'Foo', { type: '/paragraph' } ]
964 },
965 { type: 'retain', length: 6 },
966 {
967 type: 'replace',
968 remove: doc.getData( new ve.Range( 6, 20 ) ),
969 insert: doc.getData( new ve.Range( 6, 20 ) )
970 },
971 { type: 'retain', length: 7 }
972 ]
973 },
974 {
975 msg: 'insertion from unrelated document with annotation',
976 doc: 'internalData',
977 offset: 0,
978 newDocData: [
979 { type: 'paragraph' }, 'F',
980 [ 'o', [ ve.dm.example.bold ] ],
981 'o', { type: '/paragraph' },
982 { type: 'internalList' }, { type: '/internalList' }
983 ],
984 removalOps: [],
985 expectedOps: [
986 {
987 type: 'replace',
988 remove: [],
989 insert: [ { type: 'paragraph' }, 'F', [ 'o', [ ve.dm.example.boldHash ] ], 'o', { type: '/paragraph' } ]
990 },
991 { type: 'retain', length: 6 },
992 {
993 type: 'replace',
994 remove: doc.getData( new ve.Range( 6, 20 ) ),
995 insert: doc.getData( new ve.Range( 6, 20 ) )
996 },
997 { type: 'retain', length: 7 }
998 ],
999 expectedStoreItems: {
1000 h49981eab0f8056ff: bold
1001 }
1002 }
1003 ];
1004
1005 cases.forEach( ( caseItem ) => {
1006 const caseDoc = ve.dm.example.createExampleDocument( caseItem.doc );
1007 let doc2, removalOps, store2;
1008 if ( caseItem.newDocData ) {
1009 store2 = new ve.dm.HashValueStore();
1010 doc2 = new ve.dm.Document( ve.dm.example.preprocessAnnotations( caseItem.newDocData, store2 ) );
1011 removalOps = [];
1012 } else if ( caseItem.range ) {
1013 doc2 = caseDoc.cloneFromRange( caseItem.range );
1014 caseItem.modify( doc2 );
1015 const removalTx = ve.dm.TransactionBuilder.static.newFromRemoval( caseDoc, caseItem.range );
1016 caseDoc.commit( removalTx );
1017 removalOps = removalTx.getOperations();
1018 }
1019
1020 assert.deepEqualWithDomElements( removalOps, caseItem.removalOps, caseItem.msg + ': removal' );
1021
1022 const tx = ve.dm.TransactionBuilder.static.newFromDocumentInsertion( caseDoc, caseItem.offset, doc2 );
1023 assert.deepEqualWithDomElements( tx.getOperations(), caseItem.expectedOps, caseItem.msg + ': transaction' );
1024
1025 const actualStoreItems = {};
1026 const expectedStoreItems = caseItem.expectedStoreItems || {};
1027 for ( const hash in expectedStoreItems ) {
1028 actualStoreItems[ hash ] = caseDoc.store.value( hash );
1029 expectedStoreItems[ hash ].store = store2;
1030 }
1031 assert.deepEqual( actualStoreItems, expectedStoreItems, caseItem.msg + ': store items' );
1032 } );
1033 } );
1034
1035 QUnit.test( 'newFromAttributeChanges', ( assert ) => {
1036 const doc = ve.dm.example.createExampleDocument(),
1037 cases = {
1038 'first element': {
1039 args: [ doc, 0, { level: 2 } ],
1040 ops: [
1041 {
1042 type: 'attribute',
1043 key: 'level',
1044 from: 1,
1045 to: 2
1046 },
1047 { type: 'retain', length: 63 }
1048 ]
1049 },
1050 'middle element': {
1051 args: [ doc, 17, { style: 'number' } ],
1052 ops: [
1053 { type: 'retain', length: 17 },
1054 {
1055 type: 'attribute',
1056 key: 'style',
1057 from: 'bullet',
1058 to: 'number'
1059 },
1060 { type: 'retain', length: 46 }
1061 ]
1062 },
1063 'multiple attributes': {
1064 args: [ doc, 17, { style: 'number', level: 1 } ],
1065 ops: [
1066 { type: 'retain', length: 17 },
1067 {
1068 type: 'attribute',
1069 key: 'style',
1070 from: 'bullet',
1071 to: 'number'
1072 },
1073 {
1074 type: 'attribute',
1075 key: 'level',
1076 from: undefined,
1077 to: 1
1078 },
1079 { type: 'retain', length: 46 }
1080 ]
1081 },
1082 'non-element': {
1083 args: [ doc, 1, { level: 2 } ],
1084 exception: Error
1085 },
1086 'closing element': {
1087 args: [ doc, 4, { level: 2 } ],
1088 exception: Error
1089 }
1090 };
1091
1092 ve.test.utils.runTransactionConstructorTests( assert, ve.dm.TransactionBuilder.static.newFromAttributeChanges, cases );
1093
1094 const val = { foo: { bar: 'baz' } };
1095 const tx = ve.dm.TransactionBuilder.static.newFromAttributeChanges( doc, 0, val );
1096 val.foo.bar = 'qux';
1097 assert.deepEqual(
1098 tx.getOperations(),
1099 [
1100 { type: 'attribute', key: 'foo', from: undefined, to: { bar: 'baz' } },
1101 { type: 'retain', length: 63 }
1102 ],
1103 'Attribute value mutated after transaction built (T193062)'
1104 );
1105 } );
1106
1107 QUnit.test( 'newFromAnnotation', ( assert ) => {
1108 const boldAnnotation = ve.dm.example.createAnnotation( ve.dm.example.bold ),
1109 strong = { type: 'textStyle/bold', attributes: { nodeName: 'strong' } },
1110 strongAnnotation = ve.dm.example.createAnnotation( strong ),
1111 doc = ve.dm.example.createExampleDocument(),
1112 doc2 = ve.dm.example.createExampleDocumentFromData( [
1113 { type: 'paragraph' },
1114 [ 'F', [ ve.dm.example.bold ] ],
1115 [ 'o', [ ve.dm.example.bold ] ],
1116 [ 'o', [ ve.dm.example.bold ] ],
1117 [ 'B', [ strong ] ],
1118 [ 'a', [ strong ] ],
1119 [ 'r', [ strong ] ],
1120 { type: '/paragraph' },
1121 { type: 'internalList' },
1122 { type: '/internalList' }
1123 ] ),
1124 annotationDoc = ve.dm.example.createExampleDocument( 'annotationData' ),
1125 cases = {
1126 'over plain text': {
1127 args: [ doc, new ve.Range( 1, 2 ), 'set', boldAnnotation ],
1128 ops: [
1129 { type: 'retain', length: 1 },
1130 {
1131 type: 'replace',
1132 remove: [ 'a' ],
1133 insert: [ [ 'a', [ ve.dm.example.boldHash ] ] ]
1134 },
1135 { type: 'retain', length: 61 }
1136 ]
1137 },
1138 'over partially annotated text': {
1139 args: [ doc, new ve.Range( 1, 4 ), 'set', boldAnnotation ],
1140 ops: [
1141 { type: 'retain', length: 1 },
1142 {
1143 type: 'replace',
1144 remove: [ 'a' ],
1145 insert: [ [ 'a', [ ve.dm.example.boldHash ] ] ]
1146 },
1147 { type: 'retain', length: 1 },
1148 {
1149 type: 'replace',
1150 remove: [ [ 'c', [ ve.dm.example.italicHash ] ] ],
1151 insert: [ [ 'c', [ ve.dm.example.italicHash, ve.dm.example.boldHash ] ] ]
1152 },
1153 { type: 'retain', length: 59 }
1154 ]
1155 },
1156 'comparable annotation over partially annotated text': {
1157 args: [ doc, new ve.Range( 1, 4 ), 'set', strongAnnotation ],
1158 ops: [
1159 { type: 'retain', length: 1 },
1160 {
1161 type: 'replace',
1162 remove: [ 'a' ],
1163 insert: [ [ 'a', [ ve.dm.example.strongHash ] ] ]
1164 },
1165 { type: 'retain', length: 1 },
1166 {
1167 type: 'replace',
1168 remove: [ [ 'c', [ ve.dm.example.italicHash ] ] ],
1169 insert: [ [ 'c', [ ve.dm.example.italicHash, ve.dm.example.strongHash ] ] ]
1170 },
1171 { type: 'retain', length: 59 }
1172 ]
1173 },
1174 'adjacent comparable annotations not cleared together': {
1175 args: [ doc2, new ve.Range( 1, 7 ), 'clear', strongAnnotation ],
1176 ops: [
1177 { type: 'retain', length: 4 },
1178 {
1179 type: 'replace',
1180 remove: [
1181 [ 'B', [ ve.dm.example.strongHash ] ],
1182 [ 'a', [ ve.dm.example.strongHash ] ],
1183 [ 'r', [ ve.dm.example.strongHash ] ]
1184 ],
1185 insert: [ ...'Bar' ]
1186 },
1187 { type: 'retain', length: 3 }
1188 ]
1189 },
1190 'over elements': {
1191 args: [ doc, new ve.Range( 4, 9 ), 'set', boldAnnotation ],
1192 ops: [
1193 { type: 'retain', length: 63 }
1194 ]
1195 },
1196 'over elements and content': {
1197 args: [ doc, new ve.Range( 3, 11 ), 'set', boldAnnotation ],
1198 ops: [
1199 { type: 'retain', length: 3 },
1200 {
1201 type: 'replace',
1202 remove: [ [ 'c', [ ve.dm.example.italicHash ] ] ],
1203 insert: [ [ 'c', [ ve.dm.example.italicHash, ve.dm.example.boldHash ] ] ]
1204 },
1205 { type: 'retain', length: 6 },
1206 {
1207 type: 'replace',
1208 remove: [ 'd' ],
1209 insert: [ [ 'd', [ ve.dm.example.boldHash ] ] ]
1210 },
1211 { type: 'retain', length: 52 }
1212 ]
1213 },
1214 'over content and content element (image)': {
1215 args: [ doc, new ve.Range( 38, 42 ), 'set', boldAnnotation ],
1216 ops: [
1217 { type: 'retain', length: 38 },
1218 {
1219 type: 'replace',
1220 remove: [
1221 'h',
1222 { type: 'inlineImage', attributes: {
1223 src: ve.dm.example.imgSrc,
1224 alt: 'Example',
1225 width: 100,
1226 height: 50
1227 } },
1228 { type: '/inlineImage' },
1229 'i'
1230 ],
1231 insert: [
1232 [ 'h', [ ve.dm.example.boldHash ] ],
1233 { type: 'inlineImage', attributes: {
1234 src: ve.dm.example.imgSrc,
1235 alt: 'Example',
1236 width: 100,
1237 height: 50
1238 }, annotations: [ ve.dm.example.boldHash ] },
1239 { type: '/inlineImage', annotations: [ ve.dm.example.boldHash ] },
1240 [ 'i', [ ve.dm.example.boldHash ] ]
1241 ]
1242 },
1243 { type: 'retain', length: 21 }
1244 ]
1245 },
1246 'over content and unannotatable content element (unboldable node)': {
1247 args: [ annotationDoc, new ve.Range( 1, 9 ), 'set', boldAnnotation ],
1248 ops: [
1249 { type: 'retain', length: 1 },
1250 {
1251 type: 'replace',
1252 remove: [ ...'Foo' ],
1253 insert: [
1254 [ 'F', [ ve.dm.example.boldHash ] ],
1255 [ 'o', [ ve.dm.example.boldHash ] ],
1256 [ 'o', [ ve.dm.example.boldHash ] ]
1257 ]
1258 },
1259 { type: 'retain', length: 2 },
1260 {
1261 type: 'replace',
1262 remove: [ ...'Bar' ],
1263 insert: [
1264 [ 'B', [ ve.dm.example.boldHash ] ],
1265 [ 'a', [ ve.dm.example.boldHash ] ],
1266 [ 'r', [ ve.dm.example.boldHash ] ]
1267 ]
1268 },
1269 { type: 'retain', length: 21 }
1270 ]
1271 },
1272 'over handles own children nodes': {
1273 args: [ annotationDoc, new ve.Range( 1, 27 ), 'set', boldAnnotation ],
1274 ops: [
1275 { type: 'retain', length: 1 },
1276 {
1277 type: 'replace',
1278 remove: [ ...'Foo' ],
1279 insert: [
1280 [ 'F', [ ve.dm.example.boldHash ] ],
1281 [ 'o', [ ve.dm.example.boldHash ] ],
1282 [ 'o', [ ve.dm.example.boldHash ] ]
1283 ]
1284 },
1285 { type: 'retain', length: 2 },
1286 {
1287 type: 'replace',
1288 remove: [ ...'Bar' ],
1289 insert: [
1290 [ 'B', [ ve.dm.example.boldHash ] ],
1291 [ 'a', [ ve.dm.example.boldHash ] ],
1292 [ 'r', [ ve.dm.example.boldHash ] ]
1293 ]
1294 },
1295 { type: 'retain', length: 15 },
1296 {
1297 type: 'replace',
1298 remove: [ ...'Baz' ],
1299 insert: [
1300 [ 'B', [ ve.dm.example.boldHash ] ],
1301 [ 'a', [ ve.dm.example.boldHash ] ],
1302 [ 'z', [ ve.dm.example.boldHash ] ]
1303 ]
1304 },
1305 { type: 'retain', length: 3 }
1306 ]
1307 }
1308 };
1309
1310 ve.test.utils.runTransactionConstructorTests( assert, ve.dm.TransactionBuilder.static.newFromAnnotation, cases );
1311 } );
1312
1313 QUnit.test( 'newFromContentBranchConversion', ( assert ) => {
1314 const doc = ve.dm.example.createExampleDocument(),
1315 doc2 = ve.dm.example.createExampleDocument( 'inlineAtEdges' ),
1316 cases = {
1317 'range inside a heading, convert to paragraph': {
1318 args: [ doc, new ve.Range( 1, 2 ), 'paragraph' ],
1319 ops: [
1320 {
1321 type: 'replace',
1322 remove: [ { type: 'heading', attributes: { level: 1 } } ],
1323 insert: [ { type: 'paragraph' } ]
1324 },
1325 { type: 'retain', length: 3 },
1326 {
1327 type: 'replace',
1328 remove: [ { type: '/heading' } ],
1329 insert: [ { type: '/paragraph' } ]
1330 },
1331 { type: 'retain', length: 58 }
1332 ]
1333 },
1334 'range inside a heading 1, convert to heading 2': {
1335 args: [ doc, new ve.Range( 1, 2 ), 'heading', { level: 2 } ],
1336 ops: [
1337 {
1338 type: 'attribute',
1339 key: 'level',
1340 from: 1,
1341 to: 2
1342 },
1343 { type: 'retain', length: 63 }
1344 ]
1345 },
1346 'range around 2 paragraphs, convert to preformatted': {
1347 args: [ doc, new ve.Range( 50, 58 ), 'preformatted' ],
1348 ops: [
1349 { type: 'retain', length: 50 },
1350 {
1351 type: 'replace',
1352 remove: [ { type: 'paragraph' } ],
1353 insert: [ { type: 'preformatted' } ]
1354 },
1355 { type: 'retain', length: 1 },
1356 {
1357 type: 'replace',
1358 remove: [ { type: '/paragraph' } ],
1359 insert: [ { type: '/preformatted' } ]
1360 },
1361 { type: 'retain', length: 2 },
1362 {
1363 type: 'replace',
1364 remove: [ { type: 'paragraph' } ],
1365 insert: [ { type: 'preformatted' } ]
1366 },
1367 { type: 'retain', length: 1 },
1368 {
1369 type: 'replace',
1370 remove: [ { type: '/paragraph' } ],
1371 insert: [ { type: '/preformatted' } ]
1372 },
1373 { type: 'retain', length: 5 }
1374 ]
1375 },
1376 'zero-length range before inline node at the start': {
1377 args: [ doc2, new ve.Range( 1 ), 'heading', { level: 2 } ],
1378 ops: [
1379 {
1380 type: 'replace',
1381 remove: [ { type: 'paragraph' } ],
1382 insert: [ { type: 'heading', attributes: { level: 2 } } ]
1383 },
1384 { type: 'retain', length: 7 },
1385 {
1386 type: 'replace',
1387 remove: [ { type: '/paragraph' } ],
1388 insert: [ { type: '/heading' } ]
1389 },
1390 { type: 'retain', length: 2 }
1391 ]
1392 },
1393 'zero-length range inside inline node at the start': {
1394 args: [ doc2, new ve.Range( 2 ), 'heading', { level: 2 } ],
1395 ops: [
1396 {
1397 type: 'replace',
1398 remove: [ { type: 'paragraph' } ],
1399 insert: [ { type: 'heading', attributes: { level: 2 } } ]
1400 },
1401 { type: 'retain', length: 7 },
1402 {
1403 type: 'replace',
1404 remove: [ { type: '/paragraph' } ],
1405 insert: [ { type: '/heading' } ]
1406 },
1407 { type: 'retain', length: 2 }
1408 ]
1409 },
1410 'zero-length range after inline node at the start': {
1411 args: [ doc2, new ve.Range( 3 ), 'heading', { level: 2 } ],
1412 ops: [
1413 {
1414 type: 'replace',
1415 remove: [ { type: 'paragraph' } ],
1416 insert: [ { type: 'heading', attributes: { level: 2 } } ]
1417 },
1418 { type: 'retain', length: 7 },
1419 {
1420 type: 'replace',
1421 remove: [ { type: '/paragraph' } ],
1422 insert: [ { type: '/heading' } ]
1423 },
1424 { type: 'retain', length: 2 }
1425 ]
1426 },
1427 'zero-length range before inline node at the end': {
1428 args: [ doc2, new ve.Range( 6 ), 'heading', { level: 2 } ],
1429 ops: [
1430 {
1431 type: 'replace',
1432 remove: [ { type: 'paragraph' } ],
1433 insert: [ { type: 'heading', attributes: { level: 2 } } ]
1434 },
1435 { type: 'retain', length: 7 },
1436 {
1437 type: 'replace',
1438 remove: [ { type: '/paragraph' } ],
1439 insert: [ { type: '/heading' } ]
1440 },
1441 { type: 'retain', length: 2 }
1442 ]
1443 },
1444 'zero-length range inside inline node at the end': {
1445 args: [ doc2, new ve.Range( 7 ), 'heading', { level: 2 } ],
1446 ops: [
1447 {
1448 type: 'replace',
1449 remove: [ { type: 'paragraph' } ],
1450 insert: [ { type: 'heading', attributes: { level: 2 } } ]
1451 },
1452 { type: 'retain', length: 7 },
1453 {
1454 type: 'replace',
1455 remove: [ { type: '/paragraph' } ],
1456 insert: [ { type: '/heading' } ]
1457 },
1458 { type: 'retain', length: 2 }
1459 ]
1460 },
1461 'zero-length range after inline node at the end': {
1462 args: [ doc2, new ve.Range( 8 ), 'heading', { level: 2 } ],
1463 ops: [
1464 {
1465 type: 'replace',
1466 remove: [ { type: 'paragraph' } ],
1467 insert: [ { type: 'heading', attributes: { level: 2 } } ]
1468 },
1469 { type: 'retain', length: 7 },
1470 {
1471 type: 'replace',
1472 remove: [ { type: '/paragraph' } ],
1473 insert: [ { type: '/heading' } ]
1474 },
1475 { type: 'retain', length: 2 }
1476 ]
1477 }
1478 };
1479
1480 for ( const key in cases ) {
1481 for ( let i = 0; i < cases[ key ].ops.length; i++ ) {
1482 const store = cases[ key ].args[ 0 ].getStore();
1483 if ( cases[ key ].ops[ i ].remove ) {
1484 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].remove, store );
1485 }
1486 if ( cases[ key ].ops[ i ].insert ) {
1487 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].insert, store );
1488 }
1489 }
1490 }
1491 ve.test.utils.runTransactionConstructorTests(
1492 assert,
1493 ve.dm.TransactionBuilder.static.newFromContentBranchConversion,
1494 cases
1495 );
1496 } );
1497
1498 QUnit.test( 'newFromWrap', ( assert ) => {
1499 const doc = ve.dm.example.createExampleDocument(),
1500 metaDoc = ve.dm.example.createExampleDocument( 'withMeta' ),
1501 listMetaDoc = ve.dm.example.createExampleDocument( 'listWithMeta' ),
1502 listDoc = ve.dm.example.createExampleDocumentFromData(
1503 listMetaDoc.getData().filter( ( item ) => item.type !== 'alienMeta' &&
1504 item.type !== '/alienMeta' )
1505 ),
1506 cases = {
1507 'changes a heading to a paragraph': {
1508 args: [ doc, new ve.Range( 1, 4 ), [ { type: 'heading', attributes: { level: 1 } } ], [ { type: 'paragraph' } ], [], [] ],
1509 ops: [
1510 { type: 'replace', remove: [ { type: 'heading', attributes: { level: 1 } } ], insert: [ { type: 'paragraph' } ] },
1511 { type: 'retain', length: 3 },
1512 { type: 'replace', remove: [ { type: '/heading' } ], insert: [ { type: '/paragraph' } ] },
1513 { type: 'retain', length: 58 }
1514 ]
1515 },
1516 'unwraps a list': {
1517 args: [ doc, new ve.Range( 13, 25 ), [ { type: 'list' } ], [], [ { type: 'listItem' } ], [] ],
1518 ops: [
1519 { type: 'retain', length: 12 },
1520 {
1521 type: 'replace',
1522 remove: [ { type: 'list', attributes: { style: 'bullet' } }, { type: 'listItem' } ],
1523 insert: []
1524 },
1525 { type: 'retain', length: 10 },
1526 { type: 'replace', remove: [ { type: '/listItem' }, { type: '/list' } ], insert: [] },
1527 { type: 'retain', length: 37 }
1528 ]
1529 },
1530 'unwraps a multiple-item list': {
1531 args: [ listDoc, new ve.Range( 1, 11 ), [ { type: 'list' } ], [], [ { type: 'listItem', attributes: { styles: [ 'bullet' ] } } ], [] ],
1532 ops: [
1533 { type: 'replace',
1534 remove: [ { type: 'list' }, { type: 'listItem', attributes: { styles: [ 'bullet' ] } } ],
1535 insert: []
1536 },
1537 { type: 'retain', length: 3 },
1538 { type: 'replace',
1539 remove: [ { type: '/listItem' }, { type: 'listItem', attributes: { styles: [ 'bullet' ] } } ],
1540 insert: []
1541 },
1542 { type: 'retain', length: 3 },
1543 { type: 'replace',
1544 remove: [ { type: '/listItem' }, { type: '/list' } ],
1545 insert: []
1546 },
1547 { type: 'retain', length: 2 }
1548 ]
1549 },
1550 'replaces a table with a list': {
1551 args: [ doc, new ve.Range( 9, 33 ), [ { type: 'table' }, { type: 'tableSection', attributes: { style: 'body' } }, { type: 'tableRow' }, { type: 'tableCell', attributes: { style: 'data' } } ], [ { type: 'list' }, { type: 'listItem' } ], [], [] ],
1552 ops: [
1553 { type: 'retain', length: 5 },
1554 { type: 'replace', remove: [ { type: 'table' }, { type: 'tableSection', attributes: { style: 'body' } }, { type: 'tableRow' }, { type: 'tableCell', attributes: { style: 'data' } } ], insert: [ { type: 'list' }, { type: 'listItem' } ] },
1555 { type: 'retain', length: 24 },
1556 { type: 'replace', remove: [ { type: '/tableCell' }, { type: '/tableRow' }, { type: '/tableSection' }, { type: '/table' } ], insert: [ { type: '/listItem' }, { type: '/list' } ] },
1557 { type: 'retain', length: 26 }
1558 ]
1559 },
1560 'wraps two adjacent paragraphs in a list': {
1561 args: [ doc, new ve.Range( 55, 61 ), [], [ { type: 'list', attributes: { style: 'number' } } ], [], [ { type: 'listItem' } ] ],
1562 ops: [
1563 { type: 'retain', length: 55 },
1564 {
1565 type: 'replace',
1566 remove: [],
1567 insert: [ { type: 'list', attributes: { style: 'number' } }, { type: 'listItem' } ]
1568 },
1569 { type: 'retain', length: 3 },
1570 { type: 'replace', remove: [], insert: [ { type: '/listItem' }, { type: 'listItem' } ] },
1571 { type: 'retain', length: 3 },
1572 { type: 'replace', remove: [], insert: [ { type: '/listItem' }, { type: '/list' } ] },
1573 { type: 'retain', length: 2 }
1574 ]
1575 },
1576 'wraps two adjacent paragraphs in a definitionList': {
1577 args: [ doc, new ve.Range( 55, 61 ), [], [ { type: 'definitionList' } ], [], [ { type: 'definitionListItem', attributes: { style: 'term' } } ] ],
1578 ops: [
1579 { type: 'retain', length: 55 },
1580 {
1581 type: 'replace',
1582 remove: [],
1583 insert: [ { type: 'definitionList' }, { type: 'definitionListItem', attributes: { style: 'term' } } ]
1584 },
1585 { type: 'retain', length: 3 },
1586 {
1587 type: 'replace',
1588 remove: [],
1589 insert: [ { type: '/definitionListItem' }, { type: 'definitionListItem', attributes: { style: 'term' } } ]
1590 },
1591 { type: 'retain', length: 3 },
1592 { type: 'replace', remove: [], insert: [ { type: '/definitionListItem' }, { type: '/definitionList' } ] },
1593 { type: 'retain', length: 2 }
1594 ]
1595 },
1596 'metadata is preserved on wrap': {
1597 args: [
1598 metaDoc,
1599 new ve.Range( 4, 23 ),
1600 [],
1601 [ { type: 'list' } ],
1602 [ { type: 'paragraph' } ],
1603 [ { type: 'listItem' }, { type: 'heading', level: 1 } ]
1604 ],
1605 ops: [
1606 { type: 'retain', length: 4 },
1607 {
1608 type: 'replace',
1609 remove: [ { type: 'paragraph' } ],
1610 insert: [
1611 { type: 'list' },
1612 { type: 'listItem' },
1613 { type: 'heading', level: 1 }
1614 ]
1615 },
1616 { type: 'retain', length: 3 },
1617 {
1618 type: 'replace',
1619 remove: [ { type: '/paragraph' } ],
1620 insert: [
1621 { type: '/heading' },
1622 { type: '/listItem' }
1623 ]
1624 },
1625 { type: 'retain', length: 2 },
1626 {
1627 type: 'replace',
1628 remove: [ { type: 'paragraph' } ],
1629 insert: [
1630 { type: 'listItem' },
1631 { type: 'heading', level: 1 }
1632 ]
1633 },
1634 { type: 'retain', length: 3 },
1635 {
1636 type: 'replace',
1637 remove: [ { type: '/paragraph' } ],
1638 insert: [
1639 { type: '/heading' },
1640 { type: '/listItem' }
1641 ]
1642 },
1643 { type: 'retain', length: 2 },
1644 {
1645 type: 'replace',
1646 remove: [ { type: 'paragraph' } ],
1647 insert: [
1648 { type: 'listItem' },
1649 { type: 'heading', level: 1 }
1650 ]
1651 },
1652 { type: 'retain', length: 3 },
1653 {
1654 type: 'replace',
1655 remove: [ { type: '/paragraph' } ],
1656 insert: [
1657 { type: '/heading' },
1658 { type: '/listItem' },
1659 { type: '/list' }
1660 ]
1661 },
1662 { type: 'retain', length: 12 }
1663 ]
1664 },
1665 'metadata is preserved on unwrap': {
1666 args: [ listMetaDoc, new ve.Range( 3, 35 ), [ { type: 'list' } ], [], [ { type: 'listItem', attributes: { styles: [ 'bullet' ] } } ], [] ],
1667 ops: [
1668 { type: 'retain', length: 2 },
1669 { type: 'replace', remove: [ { type: 'list' } ], insert: [] },
1670 { type: 'retain', length: 2 },
1671 { type: 'replace', remove: [ { type: 'listItem', attributes: { styles: [ 'bullet' ] } } ], insert: [] },
1672 { type: 'retain', length: 11 },
1673 { type: 'replace', remove: [ { type: '/listItem' } ], insert: [] },
1674 { type: 'retain', length: 2 },
1675 { type: 'replace', remove: [ { type: 'listItem', attributes: { styles: [ 'bullet' ] } } ], insert: [] },
1676 { type: 'retain', length: 11 },
1677 { type: 'replace', remove: [ { type: '/listItem' } ], insert: [] },
1678 { type: 'retain', length: 2 },
1679 { type: 'replace', remove: [ { type: '/list' } ], insert: [] },
1680 { type: 'retain', length: 4 }
1681 ]
1682 },
1683 'checks integrity of unwrapOuter parameter': {
1684 args: [ doc, new ve.Range( 13, 32 ), [ { type: 'table' } ], [], [], [] ],
1685 exception: Error
1686 },
1687 'checks integrity of unwrapEach parameter': {
1688 args: [ doc, new ve.Range( 13, 32 ), [ { type: 'list' } ], [], [ { type: 'paragraph' } ], [] ],
1689 exception: Error
1690 },
1691 'checks that unwrapOuter fits before the range': {
1692 args: [ doc, new ve.Range( 1, 4 ), [ { type: 'listItem' }, { type: 'paragraph' } ], [], [], [] ],
1693 exception: Error
1694 }
1695 };
1696
1697 for ( const key in cases ) {
1698 for ( let i = 0; cases[ key ].ops && i < cases[ key ].ops.length; i++ ) {
1699 if ( cases[ key ].ops[ i ].remove ) {
1700 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].remove, doc.getStore() );
1701 }
1702 if ( cases[ key ].ops[ i ].insert ) {
1703 ve.dm.example.preprocessAnnotations( cases[ key ].ops[ i ].insert, doc.getStore() );
1704 }
1705 }
1706 }
1707 ve.test.utils.runTransactionConstructorTests(
1708 assert,
1709 ve.dm.TransactionBuilder.static.newFromWrap,
1710 cases
1711 );
1712 } );
1713
1714 QUnit.test( 'pushRetain', ( assert ) => {
1715 const cases = {
1716 retain: {
1717 calls: [ [ 'pushRetain', 5 ] ],
1718 ops: [ { type: 'retain', length: 5 } ],
1719 diff: 0
1720 },
1721 'multiple retain': {
1722 calls: [ [ 'pushRetain', 5 ], [ 'pushRetain', 3 ] ],
1723 ops: [ { type: 'retain', length: 8 } ],
1724 diff: 0
1725 }
1726 };
1727 ve.test.utils.runTransactionBuilderTests( assert, cases );
1728 } );
1729
1730 QUnit.test( 'pushReplacement', ( assert ) => {
1731 const doc = new ve.dm.Document( [ { type: 'paragraph' }, ...'abc', { type: '/paragraph' } ] ),
1732 doc2 = new ve.dm.Document( [ { type: 'paragraph' }, ...'abcghi', { type: '/paragraph' } ] ),
1733 cases = {
1734 insert: {
1735 calls: [
1736 [ 'pushReplacement', doc, 0, 0, [ { type: 'paragraph' }, ...'abc', { type: '/paragraph' } ] ]
1737 ],
1738 ops: [
1739 {
1740 type: 'replace',
1741 remove: [],
1742 insert: [ { type: 'paragraph' }, ...'abc', { type: '/paragraph' } ]
1743 }
1744 ],
1745 diff: 5
1746 },
1747 'multiple insert': {
1748 calls: [
1749 [ 'pushReplacement', doc, 0, 0, [ { type: 'paragraph' }, ...'ab' ] ],
1750 [ 'pushReplacement', doc, 0, 0, [ 'c', { type: '/paragraph' } ] ]
1751 ],
1752 ops: [
1753 {
1754 type: 'replace',
1755 remove: [],
1756 insert: [ { type: 'paragraph' }, ...'abc', { type: '/paragraph' } ]
1757 }
1758 ],
1759 diff: 5
1760 },
1761 'insert and retain': {
1762 calls: [
1763 [ 'pushRetain', 1 ],
1764 [ 'pushReplacement', doc, 0, 0, [ ...'abc' ] ]
1765 ],
1766 ops: [
1767 { type: 'retain', length: 1 },
1768 { type: 'replace', remove: [], insert: [ ...'abc' ] }
1769 ],
1770 diff: 3
1771 },
1772 remove: {
1773 calls: [
1774 [ 'pushReplacement', doc, 0, 5, [] ]
1775 ],
1776 ops: [
1777 {
1778 type: 'replace',
1779 remove: [ { type: 'paragraph' }, ...'abc', { type: '/paragraph' } ],
1780 insert: []
1781 }
1782 ],
1783 diff: -5
1784 },
1785 'multiple remove': {
1786 calls: [
1787 [ 'pushReplacement', doc, 0, 3, [] ],
1788 [ 'pushReplacement', doc, 3, 2, [] ]
1789 ],
1790 ops: [
1791 {
1792 type: 'replace',
1793 remove: [ { type: 'paragraph' }, ...'abc', { type: '/paragraph' } ],
1794 insert: []
1795 }
1796 ],
1797 diff: -5
1798 },
1799 'retain and remove': {
1800 calls: [
1801 [ 'pushRetain', 1 ],
1802 [ 'pushReplacement', doc, 1, 3, [] ]
1803 ],
1804 ops: [
1805 { type: 'retain', length: 1 },
1806 { type: 'replace', remove: [ ...'abc' ], insert: [] }
1807 ],
1808 diff: -3
1809 },
1810 replace: {
1811 calls: [
1812 [ 'pushReplacement', doc, 1, 3, [ ...'def' ] ]
1813 ],
1814 ops: [
1815 {
1816 type: 'replace',
1817 remove: [ ...'abc' ],
1818 insert: [ ...'def' ]
1819 }
1820 ],
1821 diff: 0
1822 },
1823 'multiple replace': {
1824 calls: [
1825 [ 'pushReplacement', doc2, 1, 3, [ ...'def' ] ],
1826 [ 'pushReplacement', doc2, 4, 3, [ ...'jkl' ] ]
1827 ],
1828 ops: [
1829 {
1830 type: 'replace',
1831 remove: [ ...'abcghi' ],
1832 insert: [ ...'defjkl' ]
1833 }
1834 ],
1835 diff: 0
1836 }
1837 };
1838
1839 ve.test.utils.runTransactionBuilderTests( assert, cases );
1840 } );
1841
1842 QUnit.test( 'pushReplaceElementAttribute', ( assert ) => {
1843 const cases = {
1844 'replace element attribute': {
1845 calls: [
1846 [ 'pushReplaceElementAttribute', 'style', 'bullet', 'number' ]
1847 ],
1848 ops: [
1849 {
1850 type: 'attribute',
1851 key: 'style',
1852 from: 'bullet',
1853 to: 'number'
1854 }
1855 ],
1856 diff: 0
1857 },
1858 'replace multiple element attributes': {
1859 calls: [
1860 [ 'pushReplaceElementAttribute', 'style', 'bullet', 'number' ],
1861 [ 'pushReplaceElementAttribute', 'level', 1, 2 ]
1862 ],
1863 ops: [
1864 {
1865 type: 'attribute',
1866 key: 'style',
1867 from: 'bullet',
1868 to: 'number'
1869 },
1870 {
1871 type: 'attribute',
1872 key: 'level',
1873 from: 1,
1874 to: 2
1875 }
1876 ],
1877 diff: 0
1878 }
1879 };
1880
1881 ve.test.utils.runTransactionBuilderTests( assert, cases );
1882 } );
1883
1884 QUnit.test( 'isNoOp', ( assert ) => {
1885 const doc = ve.dm.example.createExampleDocument(),
1886 metaDoc = ve.dm.example.createExampleDocument( 'withMeta' ),
1887 listMetaDoc = ve.dm.example.createExampleDocument( 'listWithMeta' );
1888
1889 [ doc, metaDoc, listMetaDoc ].forEach( ( d, i ) => {
1890 const isListMetaDoc = ( i === 2 );
1891
1892 let tx = ve.dm.TransactionBuilder.static.newFromReplacement(
1893 d, new ve.Range( 1 ), [], false
1894 );
1895 assert.strictEqual( tx.isNoOp(), true );
1896
1897 tx = ve.dm.TransactionBuilder.static.newFromInsertion(
1898 d, 1, []
1899 );
1900 assert.strictEqual( tx.isNoOp(), true );
1901
1902 tx = ve.dm.TransactionBuilder.static.newFromRemoval(
1903 d, new ve.Range( 1 ), false
1904 );
1905 assert.strictEqual( tx.isNoOp(), true );
1906
1907 if ( !isListMetaDoc ) {
1908 tx = ve.dm.TransactionBuilder.static.newFromDocumentInsertion(
1909 d, 1,
1910 ve.dm.example.createExampleDocument(), new ve.Range( 0 )
1911 );
1912 assert.strictEqual( tx.isNoOp(), true );
1913 }
1914
1915 tx = ve.dm.TransactionBuilder.static.newFromAttributeChanges(
1916 d, isListMetaDoc ? 5 : 0, {}
1917 );
1918 assert.strictEqual( tx.isNoOp(), true );
1919
1920 tx = ve.dm.TransactionBuilder.static.newFromAnnotation(
1921 d, new ve.Range( 1 ), 'set', new ve.dm.ItalicAnnotation()
1922 );
1923 assert.strictEqual( tx.isNoOp(), true );
1924 } );
1925 } );
1926
1927 QUnit.test( 'operations/build from operations', ( assert ) => {
1928 const TBstatic = ve.dm.TransactionBuilder.static,
1929 doc = ve.dm.example.createExampleDocument(),
1930 underline = ve.dm.example.createAnnotation( ve.dm.example.underline ),
1931 cases = [
1932 {
1933 msg: 'Replace annotated text',
1934 method: 'newFromReplacement',
1935 args: [ doc, new ve.Range( 1, 4 ), [ ...'Foo' ] ],
1936 expected: [
1937 { type: 'retain', length: 1 },
1938 {
1939 type: 'replace',
1940 remove: [ 'a', [ 'b', [ ve.dm.example.boldHash ] ], [ 'c', [ ve.dm.example.italicHash ] ] ],
1941 insert: [ ...'Foo' ]
1942 },
1943 { type: 'retain', length: 59 }
1944 ]
1945 },
1946 {
1947 msg: 'New paragraph',
1948 method: 'newFromInsertion',
1949 args: [ doc, 0, [ { type: 'paragraph' }, { type: '/paragraph' } ] ],
1950 roundTripArgs: [ doc, 0, [ { type: 'paragraph' }, { type: '/paragraph' } ] ],
1951 expected: [
1952 {
1953 type: 'replace',
1954 remove: [],
1955 insert: [ { type: 'paragraph' }, { type: '/paragraph' } ]
1956 },
1957 { type: 'retain', length: 63 }
1958 ]
1959 },
1960 {
1961 msg: 'New alien preserves originalDomElements',
1962 method: 'newFromInsertion',
1963 args: [ doc, 0, [ { type: 'alienBlock' }, { type: '/alienBlock' } ] ],
1964 expected: [
1965 {
1966 type: 'replace',
1967 remove: [],
1968 insert: [ { type: 'alienBlock' }, { type: '/alienBlock' } ]
1969 },
1970 { type: 'retain', length: 63 }
1971 ]
1972 },
1973 {
1974 msg: 'Add annotation',
1975 method: 'newFromAnnotation',
1976 args: [ doc, new ve.Range( 1, 4 ), 'set', underline ],
1977 expected: [
1978 { type: 'retain', length: 1 },
1979 {
1980 type: 'replace',
1981 remove: [
1982 'a',
1983 [ 'b', [ ve.dm.example.boldHash ] ],
1984 [ 'c', [ ve.dm.example.italicHash ] ]
1985 ],
1986 insert: [
1987 [ 'a', [ ve.dm.example.underlineHash ] ],
1988 [ 'b', [ ve.dm.example.underlineHash, ve.dm.example.boldHash ] ],
1989 [ 'c', [ ve.dm.example.underlineHash, ve.dm.example.italicHash ] ]
1990 ]
1991 },
1992 { type: 'retain', length: 59 }
1993 ]
1994 }
1995 ];
1996
1997 cases.forEach( ( caseItem ) => {
1998 let tx = TBstatic[ caseItem.method ].apply( TBstatic, caseItem.args );
1999 const ops = ve.copy( tx.operations );
2000 assert.deepEqual( ops, caseItem.expected, caseItem.msg + ': operations' );
2001 if ( caseItem.roundTripArgs ) {
2002 tx = TBstatic[ caseItem.method ].apply( TBstatic, caseItem.roundTripArgs );
2003 }
2004 assert.deepEqual( new ve.dm.Transaction( ops ), tx, caseItem.msg + ': build from operations' );
2005 } );
2006 } );
2007
2008 QUnit.test( 'newFromRemoval preserving metadata', ( assert ) => {
2009 function removeBoringProperties( operations ) {
2010 return operations.map( ( op ) => {
2011 if ( !op.insert ) {
2012 return op;
2013 }
2014 op = ve.copy( op );
2015 [ ...op.insert, ...op.remove ].forEach( ( item ) => {
2016 delete item.originalDomElementsHash;
2017 delete item.internal;
2018 } );
2019 return op;
2020 } );
2021 }
2022 const cases = [
2023 {
2024 msg: 'Removal ending at meta',
2025 html: '<p>foo</p><meta><p>bar</p>',
2026 range: new ve.Range( 0, 7 ),
2027 ops: [
2028 { type: 'replace', insert: [
2029 { type: 'alienMeta' },
2030 { type: '/alienMeta' }
2031 ], remove: [
2032 { type: 'paragraph' },
2033 ...'foo',
2034 { type: '/paragraph' },
2035 { type: 'alienMeta' },
2036 { type: '/alienMeta' }
2037 ] },
2038 { type: 'retain', length: 7 }
2039 ]
2040 },
2041 {
2042 msg: 'Removal beginning at meta',
2043 html: '<p>foo</p><meta><p>bar</p>',
2044 range: new ve.Range( 5, 13 ),
2045 ops: [
2046 { type: 'retain', length: 5 },
2047 { type: 'replace', insert: [
2048 { type: 'alienMeta' },
2049 { type: '/alienMeta' }
2050 ], remove: [
2051 { type: 'alienMeta' },
2052 { type: '/alienMeta' },
2053 { type: 'paragraph' },
2054 ...'bar',
2055 { type: '/paragraph' }
2056 ] },
2057 { type: 'retain', length: 2 }
2058 ]
2059 },
2060 {
2061 msg: 'Removal across meta',
2062 html: '<p>foo</p><meta><p>bar</p><p>baz</p>',
2063 range: new ve.Range( 0, 12 ),
2064 ops: [
2065 { type: 'replace', insert: [
2066 { type: 'alienMeta' },
2067 { type: '/alienMeta' }
2068 ], remove: [
2069 { type: 'paragraph' },
2070 ...'foo',
2071 { type: '/paragraph' },
2072 { type: 'alienMeta' },
2073 { type: '/alienMeta' },
2074 { type: 'paragraph' },
2075 ...'bar',
2076 { type: '/paragraph' }
2077 ] },
2078 { type: 'retain', length: 7 }
2079 ]
2080 },
2081 {
2082 msg: 'Merge /ul/li/p nodes across meta',
2083 html: '<ul><li><p>foo</p></li><li><p></p></li></ul><p></p><div><div><meta></div></div><ul><li><p>bar</p></li></ul>',
2084 range: new ve.Range( 4, 25 ),
2085 ops: [
2086 { type: 'retain', length: 2 },
2087 { type: 'replace', remove: [], insert: [
2088 { type: 'alienMeta' },
2089 { type: '/alienMeta' }
2090 ] },
2091 { type: 'retain', length: 2 },
2092 { type: 'replace', insert: [], remove: [
2093 ...'oo',
2094 { type: '/paragraph' },
2095 { type: '/listItem' },
2096 { type: 'listItem' },
2097 { type: 'paragraph' },
2098 { type: '/paragraph' },
2099 { type: '/listItem' },
2100 { type: '/list' },
2101 { type: 'paragraph' },
2102 { type: '/paragraph' },
2103 { type: 'div' },
2104 { type: 'div' },
2105 { type: 'alienMeta' },
2106 { type: '/alienMeta' },
2107 { type: '/div' },
2108 { type: '/div' },
2109 { type: 'list', attributes: { style: 'bullet' } },
2110 { type: 'listItem' },
2111 { type: 'paragraph' },
2112 'b'
2113 ] },
2114 { type: 'retain', length: 7 }
2115 ]
2116 }
2117 ];
2118
2119 cases.forEach( ( caseItem ) => {
2120 const doc = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( caseItem.html ) );
2121 const tx = ve.dm.TransactionBuilder.static.newFromRemoval( doc, caseItem.range );
2122 assert.deepEqual(
2123 removeBoringProperties( tx.operations ),
2124 caseItem.ops,
2125 caseItem.msg
2126 );
2127 } );
2128 } );