Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel TreeModifier tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 ve.dm.TreeModifier.prototype.dump = function () {
8 const lines = [],
9 del = this.deletions,
10 ins = this.insertions;
11 function nodeTag( idx, node ) {
12 return ( idx === undefined ? '' : idx + ' ' ) + (
13 del.includes( node ) ? 'DEL ' :
14 ins.includes( node ) ? 'INS ' :
15 ''
16 );
17 }
18 function appendNodeLines( indent, node, idx ) {
19 const sp = '-\t'.repeat( indent );
20 if ( node.type === 'text' ) {
21 lines.push( sp + nodeTag( idx, node ) + 'VeDmTextNode(' + node.getOuterLength() + ')' );
22 return;
23 }
24 lines.push( sp + nodeTag( idx, node ) + node.constructor.name + '(' +
25 node.getOuterLength() + ')' );
26 if ( node.hasChildren() ) {
27 node.children.forEach( ( child, i ) => {
28 appendNodeLines( indent + 1, child, i );
29 } );
30 }
31 }
32 appendNodeLines( 0, this.document.documentNode );
33 lines.push( 'inserter: { path: [ ' + this.inserter.path.join( ', ' ) +
34 ' ], offset: ' + this.inserter.offset + ' }, ' +
35 'linear ' + this.inserter.linearOffset +
36 ', insertions: ' + JSON.stringify( this.insertedPositions ) );
37 lines.push( 'remover: { path: [ ' + this.remover.path.join( ', ' ) +
38 ' ], offset: ' + this.remover.offset + ' }, ' +
39 'linear ' + this.remover.linearOffset );
40 lines.push( 'insertedPositions: ' + JSON.stringify( this.insertedPositions ) );
41 lines.push( 'insertedNodes: ' + JSON.stringify( this.insertedNodes ) );
42 lines.push( 'adjustments: ' + JSON.stringify( this.adjustmentTree ) );
43 lines.push( 'adjusted inserter: ' + JSON.stringify( this.adjustInserterPosition( this.getRawInserterPosition() ) ) );
44 lines.push( 'adjusted remover: ' + JSON.stringify( this.adjustRemoverPosition( this.getRawRemoverPosition( {
45 path: this.remover.path,
46 offset: this.remover.offset,
47 node: this.remover.node
48 } ) ) ) );
49 ve.batchSplice( lines, lines.length, 0, this.data.data.map( ( item, i ) => i + ':' + JSON.stringify( item ) + ',' ) );
50 return lines.join( '\n' );
51 };
52
53 ve.test.utils.nodesByTypeSummary = function ( doc ) {
54 const nodesByType = ve.copy( doc.nodesByType );
55 for ( const type in nodesByType ) {
56 nodesByType[ type ] = nodesByType[ type ].length;
57 }
58 return nodesByType;
59 };
60
61 QUnit.module( 've.dm.TreeModifier' );
62
63 QUnit.test( 'treeDiff', ( assert ) => {
64 // old: <div><p>foobarbaz</p><p>qux</p></div><p>quux</p>
65 // new: <ul><li><p>foo</p><p>barBAZ</p><p>qux</p></li></ul><p>qUUx</p><p><img src='x'></p>
66 // tx: {-<div>-|+<ul><li>+}<p>foo{+</p><p>+}bar{-baz-|+BAZ+}</p><p>qux</p>{-</div>-|+</li></ul>+}<p>q{-uu-|+UU+}x</p>{+<p><img src='x'></p>+}
67 const origData = [
68 { type: 'div' },
69 { type: 'paragraph' },
70 ...'foobarbaz',
71 { type: '/paragraph' },
72 { type: 'paragraph' },
73 ...'qux',
74 { type: '/paragraph' },
75 { type: '/div' },
76 { type: 'paragraph' },
77 ...'quux',
78 { type: '/paragraph' }
79 ];
80 const tx = new ve.dm.Transaction( [
81 { type: 'replace', remove: [ { type: 'div' } ], insert: [ { type: 'list' }, { type: 'listItem' } ] },
82 { type: 'retain', length: 4 },
83 { type: 'replace', remove: [], insert: [ { type: '/paragraph' }, { type: 'paragraph' } ] },
84 { type: 'retain', length: 3 },
85 { type: 'replace', remove: [ ...'baz' ], insert: [ ...'baz' ] },
86 { type: 'retain', length: 6 },
87 { type: 'replace', remove: [ { type: '/div' } ], insert: [ { type: '/listItem' }, { type: '/list' } ] },
88 { type: 'retain', length: 2 },
89 { type: 'replace', remove: [ ...'uu' ], insert: [ ...'UU' ] },
90 { type: 'retain', length: 2 },
91 { type: 'replace', remove: [], insert: [
92 { type: 'paragraph' },
93 { type: 'inlineImage', attributes: { src: 'x' } },
94 { type: '/inlineImage' },
95 { type: '/paragraph' }
96 ] }
97 ] );
98
99 const treeDiff = [
100 // {-<div>-|+<ul><li>+}
101 [
102 { type: 'insertNode', isContent: false, at: [ 0 ], element: { type: 'list' } },
103 // { 0: { diff: 1 } }
104 { type: 'insertNode', isContent: false, at: [ 0, 0 ], element: { type: 'listItem' } }
105 ],
106 // Retain 4
107 [
108 { type: 'insertNode', isContent: false, at: [ 0, 0, 0 ], element: { type: 'paragraph' } },
109 { type: 'moveText', isContent: true, from: [ 1, 0, 0 ], to: [ 0, 0, 0, 0 ], length: 3 }
110 // { 0: { diff: 1 }, 1: { 0: { 0: { diff: -3 } } } }
111 ],
112 // {+</p><p>+}
113 [
114 { type: 'insertNode', isContent: false, at: [ 0, 0, 1 ], element: { type: 'paragraph' } }
115 ],
116 // Retain 3
117 [
118 { type: 'moveText', isContent: true, from: [ 1, 0, 0 ], length: 3, to: [ 0, 0, 1, 0 ] }
119 // { 0: { diff: 1 }, 1: { 0: { 0: { diff: -6 } } } }
120 ],
121 // {-baz-|+BAZ+}
122 [
123 { type: 'removeText', isContent: true, at: [ 1, 0, 0 ], data: [ ...'baz' ] },
124 // { 0: { diff: 1 }, 1: { 0: { 0: { diff: -9 } } } }
125 { type: 'insertText', isContent: true, at: [ 0, 0, 1, 3 ], data: [ ...'baz' ] }
126 ],
127 // Retain 6
128 [
129 { type: 'removeNode', isContent: false, at: [ 1, 0 ], element: { type: 'paragraph' } },
130 // { 0: { diff: 1 }, 1: { 0: { diff: -1 } } }
131 { type: 'moveNode', isContent: false, from: [ 1, 0 ], to: [ 0, 0, 2 ] }
132 // { 0: { diff: 1 }, 1: { 0: { diff: -2 } } }
133 ],
134 // {-</div>-|+</li></ul>+}
135 [
136 { type: 'removeNode', isContent: false, at: [ 1 ], element: { type: 'div' } }
137 // { 0: { diff: 1 }, 1: { diff: -1 } }
138 ],
139 // Retain 2
140 [],
141 // {-uu-|+UU+}
142 [
143 { type: 'removeText', isContent: true, at: [ 1, 1 ], data: [ ...'uu' ] },
144 // NOT THIS: { 0: { diff: 1 }, 1: { diff: -1 }, 2: { 1: { diff: -2 } } }
145 // { 0: { diff: 1 }, 1: { 1: { diff: -2 }, diff: -1 } }
146 { type: 'insertText', isContent: true, at: [ 1, 1 ], data: [ ...'UU' ] }
147 // { 0: { diff: 1 }, 1: { diff: -1 }, 2: { 1: { diff: 0 } } }
148 ],
149 // Retain 2
150 [],
151 // {+<p><img src='x'></p>+}
152 [
153 { type: 'insertNode', isContent: false, at: [ 2 ], element: { type: 'paragraph' } },
154 { type: 'insertNode', isContent: true, at: [ 2, 0 ], element: { type: 'inlineImage', attributes: { src: 'x' } } }
155 ],
156 // Implicit final retain
157 []
158 ];
159 const surface = new ve.dm.Surface(
160 ve.dm.example.createExampleDocumentFromData( origData )
161 );
162 const doc = surface.documentModel;
163 ve.dm.treeModifier.setup( doc );
164 let j = 0;
165 for ( let i = 0, iLen = tx.operations.length; i < iLen; i++ ) {
166 ve.dm.treeModifier.processLinearOperation( tx.operations[ i ] );
167 assert.deepEqual(
168 ve.dm.treeModifier.treeOps.slice( j ),
169 treeDiff[ i ],
170 'Linear operation ' + i + ': induced tree operations'
171 );
172 j = ve.dm.treeModifier.treeOps.length;
173 }
174 ve.dm.treeModifier.processImplicitFinalRetain();
175 assert.deepEqual(
176 ve.dm.treeModifier.treeOps.slice( j ),
177 treeDiff[ treeDiff.length - 1 ],
178 'Implicit final retain: induced tree operations'
179 );
180 } );
181
182 QUnit.test( 'modify', ( assert ) => {
183 function dumpTree( d ) {
184 // Build a tree modifier just for the .dump method (don't modify anything)
185 const treeModifier = new ve.dm.TreeModifier();
186 treeModifier.setup( d );
187 return treeModifier.dump();
188 }
189
190 const origData = [
191 { type: 'paragraph' },
192 ...'abcd',
193 { type: '/paragraph' },
194 { type: 'paragraph' },
195 ...'efg',
196 { type: '/paragraph' },
197 { type: 'paragraph' },
198 ...'hij',
199 { type: '/paragraph' },
200 { type: 'internalList' },
201 { type: '/internalList' }
202 ];
203 const surface = new ve.dm.Surface(
204 ve.dm.example.createExampleDocumentFromData( origData )
205 );
206 const doc = surface.documentModel;
207
208 const tx = new ve.dm.Transaction( [
209 { type: 'retain', length: 2 },
210 {
211 type: 'replace',
212 remove: [ 'b' ],
213 insert: [ ...'xy' ],
214 insertedDataOffset: 0,
215 insertedDataLength: 2
216 },
217 { type: 'retain', length: 2 },
218 {
219 type: 'replace',
220 remove: [ { type: '/paragraph' }, { type: 'paragraph' }, 'e' ],
221 insert: [ 'Z' ],
222 insertedDataOffset: 0,
223 insertedDataLength: 0
224 },
225 { type: 'retain', length: 4 },
226 {
227 type: 'replace',
228 remove: [ 'h' ],
229 insert: [
230 { type: 'inlineImage' },
231 { type: '/inlineImage' }
232 ],
233 insertedDataOffset: 0,
234 insertedDataLength: 2
235 },
236 { type: 'retain', length: 3 }
237 ] );
238 assert.deepEqual(
239 ve.test.utils.nodesByTypeSummary( doc ),
240 {
241 document: 1,
242 internalList: 1,
243 paragraph: 3,
244 text: 3
245 },
246 'Nodes by type before'
247 );
248
249 doc.commit( tx );
250 let actualTreeDump = dumpTree( doc );
251 doc.rebuildTree();
252 let expectedTreeDump = dumpTree( doc );
253 assert.strictEqual(
254 actualTreeDump,
255 expectedTreeDump,
256 'Modified tree matches rebuilt tree, forward'
257 );
258
259 assert.deepEqual(
260 ve.test.utils.nodesByTypeSummary( doc ),
261 {
262 document: 1,
263 inlineImage: 1,
264 internalList: 1,
265 paragraph: 2,
266 text: 2
267 },
268 'Nodes by type after transaction'
269 );
270
271 doc.commit( tx.reversed() );
272 actualTreeDump = dumpTree( doc );
273 doc.rebuildTree();
274 expectedTreeDump = dumpTree( doc );
275 assert.strictEqual(
276 actualTreeDump,
277 expectedTreeDump,
278 'Modified tree matches rebuilt tree, reversed'
279 );
280 assert.notStrictEqual(
281 tx.operations[ 3 ].remove[ 1 ],
282 doc.data.data[ 6 ],
283 'Inserted transaction data is not referenced into the linear data'
284 );
285
286 assert.deepEqual(
287 ve.test.utils.nodesByTypeSummary( doc ),
288 {
289 document: 1,
290 internalList: 1,
291 paragraph: 3,
292 text: 3
293 },
294 'Nodes by type after undo'
295 );
296 } );
297
298 QUnit.test( 'bare content', ( assert ) => {
299 const data = [ { type: 'paragraph' }, ...'Foo', { type: '/paragraph' } ];
300 const doc = ve.dm.example.createExampleDocumentFromData( data );
301 const tx = new ve.dm.Transaction( [
302 { type: 'replace', remove: [ { type: 'paragraph' } ], insert: [] },
303 { type: 'retain', length: 3 },
304 { type: 'replace', remove: [ { type: '/paragraph' } ], insert: [] }
305 ] );
306 assert.throws( () => {
307 doc.commit( tx );
308 }, /Error: Cannot insert text into a document node/, 'bare content' );
309 } );
310
311 QUnit.test( 'unbalanced insertion', ( assert ) => {
312 const data = [ { type: 'div' }, { type: '/div' } ];
313 const doc = ve.dm.example.createExampleDocumentFromData( data );
314 const tx = new ve.dm.Transaction( [
315 { type: 'retain', length: 1 },
316 { type: 'replace', remove: [], insert: [ { type: 'paragraph' }, { type: '/heading' } ] },
317 { type: 'retain', length: 1 }
318 ] );
319 assert.throws( () => {
320 doc.commit( tx );
321 }, /Expected closing for paragraph but got closing for heading/, 'unbalanced insertion' );
322 } );
323
324 QUnit.test( 'applyTreeOperation: ensureNotText', ( assert ) => {
325 const data = [
326 { type: 'paragraph' },
327 ...'fo',
328 { type: 'inlineImage' },
329 { type: '/inlineImage' },
330 'o',
331 { type: '/paragraph' }
332 ];
333 const expectData = ve.copy( data );
334 expectData[ 3 ].annotations = [ ve.dm.example.boldHash ];
335 const doc = ve.dm.example.createExampleDocumentFromData( data );
336 const tx = new ve.dm.Transaction( [
337 { type: 'retain', length: 3 },
338 {
339 type: 'replace',
340 remove: [
341 { type: 'inlineImage' },
342 { type: '/inlineImage' }
343 ],
344 insert: [
345 { type: 'inlineImage', annotations: [ ve.dm.example.boldHash ] },
346 { type: '/inlineImage' }
347 ]
348 },
349 { type: 'retain', length: 2 }
350 ] );
351 assert.deepEqual(
352 ve.test.utils.nodesByTypeSummary( doc ),
353 {
354 document: 1,
355 inlineImage: 1,
356 paragraph: 1,
357 text: 2
358 },
359 'Nodes by type before'
360 );
361 doc.commit( tx );
362 assert.deepEqual( doc.data.data, expectData, 'Bold inline element surrounded by text' );
363 assert.deepEqual(
364 ve.test.utils.nodesByTypeSummary( doc ),
365 {
366 document: 1,
367 inlineImage: 1,
368 paragraph: 1,
369 text: 2
370 },
371 'Nodes by type after'
372 );
373 } );
374
375 QUnit.test( 'setupBlockSlugs', ( assert ) => {
376 const doc = new ve.dm.Surface(
377 ve.dm.example.createExampleDocumentFromData( [] )
378 ).documentModel;
379
380 assert.deepEqual(
381 ve.test.utils.nodesByTypeSummary( doc ),
382 {
383 document: 1
384 },
385 'Nodes by type before'
386 );
387 doc.commit( new ve.dm.Transaction( [ {
388 type: 'replace',
389 remove: [],
390 insert: [ { type: 'paragraph' }, { type: '/paragraph' } ],
391 insertedDataOffset: 0,
392 insertedDataLength: 2
393 } ] ) );
394
395 assert.deepEqual(
396 doc.getDocumentNode().getChildren()[ 0 ].slugPositions,
397 { 0: true },
398 'Modified paragraph node contains a slug'
399 );
400
401 assert.deepEqual(
402 ve.test.utils.nodesByTypeSummary( doc ),
403 {
404 document: 1,
405 paragraph: 1
406 },
407 'Nodes by type before'
408 );
409 } );
410
411 QUnit.test( 'checkEqualData', ( assert ) => {
412 const data = [
413 {
414 type: 'paragraph',
415 originalDomElementsHash: 'h1111111111111111',
416 internal: { changesSinceLoad: 1 }
417 },
418 ...'foo',
419 {
420 type: 'mwReference',
421 attributes: {
422 mw: {
423 name: 'ref',
424 attrs: {},
425 body: { id: 'mw-reference-text-cite_note-1' }
426 },
427 listIndex: 0,
428 listGroup: 'mwReference/',
429 listKey: 'auto/0',
430 refGroup: '',
431 contentsUsed: true,
432 refListItemId: 'mw-reference-text-cite_note-1'
433 }
434 },
435 { type: '/mwReference' },
436 { type: '/paragraph' }
437 ];
438 const expectedData = ve.copy( data );
439 expectedData[ 0 ].originalDomElementsHash = 'h2222222222222222';
440 expectedData[ 0 ].internal.changesSinceLoad = 2;
441 delete expectedData[ 4 ].attributes.mw;
442 delete expectedData[ 4 ].attributes.contentsUsed;
443 ve.dm.TreeModifier.static.checkEqualData( data, expectedData );
444 assert.true( true, 'Normalized data compares equal' );
445 } );
446
447 QUnit.test( 'TreeCursor#crossIgnoredNodes', ( assert ) => {
448 const data = [
449 { type: 'paragraph' },
450 ...'foo',
451 { type: 'comment', attributes: { text: 'de' } },
452 { type: '/comment' },
453 { type: '/paragraph' }
454 ];
455 const doc = ve.dm.example.createExampleDocumentFromData( data );
456 const tx = new ve.dm.Transaction( [
457 { type: 'retain', length: 3 },
458 { type: 'replace', remove: [
459 'o',
460 { type: 'comment', attributes: { text: 'de' } },
461 { type: '/comment' }
462 ], insert: [] },
463 { type: 'retain', length: 1 }
464 ] );
465
466 assert.deepEqual(
467 ve.test.utils.nodesByTypeSummary( doc ),
468 {
469 comment: 1,
470 document: 1,
471 paragraph: 1,
472 text: 1
473 },
474 'Nodes by type before'
475 );
476
477 doc.commit( tx );
478 assert.deepEqual(
479 doc.data.data,
480 [].concat(
481 data.slice( 0, 3 ),
482 data.slice( 6 )
483 ),
484 'Can remove an inline node after a text node'
485 );
486
487 assert.deepEqual(
488 ve.test.utils.nodesByTypeSummary( doc ),
489 {
490 document: 1,
491 paragraph: 1,
492 text: 1
493 },
494 'Nodes by type after'
495 );
496 } );
497
498 QUnit.test( 'TreeCursor#normalizeCursor', ( assert ) => {
499 const data = [
500 { type: 'heading', attributes: { level: 1 } },
501 ...'ab',
502 { type: 'comment', attributes: { text: 'foo' } },
503 { type: '/comment' },
504 'd',
505 { type: '/heading' },
506 { type: 'paragraph' },
507 ...'ef',
508 { type: '/paragraph' }
509 ];
510 const doc = ve.dm.example.createExampleDocumentFromData( data );
511 const tx = new ve.dm.Transaction( [
512 { type: 'retain', length: 2 },
513 { type: 'replace', remove: [
514 'b',
515 { type: 'comment', attributes: { text: 'foo' } },
516 { type: '/comment' },
517 'd'
518 ], insert: [] },
519 { type: 'retain', length: 2 },
520 { type: 'replace', remove: [ 'e' ], insert: [] },
521 { type: 'retain', length: 1 }
522 ] );
523
524 assert.deepEqual(
525 ve.test.utils.nodesByTypeSummary( doc ),
526 {
527 comment: 1,
528 document: 1,
529 heading: 1,
530 paragraph: 1,
531 text: 3
532 },
533 'Nodes by type before'
534 );
535
536 doc.commit( tx );
537 assert.deepEqual(
538 doc.data.data,
539 [].concat(
540 data.slice( 0, 2 ),
541 data.slice( 6, 8 ),
542 data.slice( 9 )
543 ),
544 'Ignore removed nodes properly when normalizing from a text node'
545 );
546
547 assert.deepEqual(
548 ve.test.utils.nodesByTypeSummary( doc ),
549 {
550 document: 1,
551 heading: 1,
552 paragraph: 1,
553 text: 2
554 },
555 'Nodes by type after'
556 );
557 } );