Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/dm/lineardata/ve.dm.ElementLinearData.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor ElementLinearData tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.dm.ElementLinearData' );
8
9 /* Tests */
10
11 QUnit.test( 'getAnnotationsFromOffset', ( assert ) => {
12 const cases = [
13 {
14 msg: [ 'bold #1', 'bold #2' ],
15 data: [
16 [ 'a', [ { type: 'textStyle/bold' } ] ],
17 [ 'b', [ { type: 'textStyle/bold' } ] ]
18 ],
19 expected: [
20 [ { type: 'textStyle/bold' } ],
21 [ { type: 'textStyle/bold' } ]
22 ]
23 },
24 {
25 msg: [ 'bold #3', 'italic #1' ],
26 data: [
27 [ 'a', [ { type: 'textStyle/bold' } ] ],
28 [ 'b', [ { type: 'textStyle/italic' } ] ]
29 ],
30 expected: [
31 [ { type: 'textStyle/bold' } ],
32 [ { type: 'textStyle/italic' } ]
33 ]
34 },
35 {
36 msg: [ 'bold, italic & underline' ],
37 data: [
38 [
39 'a',
40 [
41 { type: 'textStyle/bold' },
42 { type: 'textStyle/italic' },
43 { type: 'textStyle/underline' }
44 ]
45 ]
46 ],
47 expected: [
48 [
49 { type: 'textStyle/bold' },
50 { type: 'textStyle/italic' },
51 { type: 'textStyle/underline' }
52 ]
53 ]
54 },
55 {
56 msg: [ 'unannotated element', 'annotated element', 'annotated close element', 'unannotated element' ],
57 data: [
58 { type: 'paragraph' },
59 { type: 'break', annotations: [ { type: 'textStyle/bold' } ] },
60 { type: '/break' },
61 { type: '/paragraph' }
62 ],
63 expected: [
64 [],
65 [
66 { type: 'textStyle/bold' }
67 ],
68 [
69 { type: 'textStyle/bold' }
70 ],
71 []
72 ]
73 },
74 {
75 msg: [ 'unannotated element', 'annotated element', 'annotated close element (ignored)', 'unannotated element' ],
76 ignoreClose: true,
77 data: [
78 { type: 'paragraph' },
79 { type: 'break', annotations: [ { type: 'textStyle/bold' } ] },
80 { type: '/break' },
81 { type: '/paragraph' }
82 ],
83 expected: [
84 [],
85 [
86 { type: 'textStyle/bold' }
87 ],
88 [],
89 []
90 ]
91 }
92 ];
93
94 // Run tests
95 cases.forEach( ( caseItem, i ) => {
96 const data = ve.dm.example.preprocessAnnotations( caseItem.data );
97 const doc = new ve.dm.Document( data );
98 if ( i === 0 ) {
99 assert.notStrictEqual(
100 doc.data.getAnnotationsFromOffset( 0, caseItem.ignoreClose ).getHashes(),
101 doc.data.getAnnotationsFromOffset( 0, caseItem.ignoreClose ).getHashes(),
102 'annotation set hashes are not equal by reference'
103 );
104 }
105 for ( let j = 0; j < doc.getData().length; j++ ) {
106 const annotations = doc.data.getAnnotationsFromOffset( j, caseItem.ignoreClose );
107 assert.deepEqual( annotations,
108 ve.dm.example.createAnnotationSet( doc.getStore(), caseItem.expected[ j ] ),
109 caseItem.msg[ j ]
110 );
111 }
112 } );
113 } );
114
115 QUnit.test( 'getAnnotationsFromRange', ( assert ) => {
116 const cases = [
117 {
118 msg: 'single annotations',
119 data: [
120 [ 'a', [ { type: 'textStyle/bold' } ] ],
121 [ 'b', [ { type: 'textStyle/bold' } ] ]
122 ],
123 expected: [ { type: 'textStyle/bold' } ]
124 },
125 {
126 msg: 'single annotation with non-content data',
127 data: [
128 { type: 'paragraph' },
129 [ 'a', [ { type: 'textStyle/bold' } ] ],
130 [ 'b', [ { type: 'textStyle/bold' } ] ],
131 { type: '/paragraph' }
132 ],
133 expected: [ { type: 'textStyle/bold' } ]
134 },
135 {
136 msg: 'multiple annotations',
137 data: [
138 [
139 'a',
140 [
141 { type: 'textStyle/bold' },
142 { type: 'textStyle/italic' }
143 ]
144 ],
145 [
146 'b',
147 [
148 { type: 'textStyle/bold' },
149 { type: 'textStyle/italic' }
150 ]
151 ]
152 ],
153 expected: [
154 { type: 'textStyle/bold' },
155 { type: 'textStyle/italic' }
156 ]
157 },
158 {
159 msg: 'lowest common coverage',
160 data: [
161 [
162 'a',
163 [
164 { type: 'textStyle/bold' },
165 { type: 'textStyle/italic' }
166 ]
167 ],
168 [
169 'b',
170 [
171 { type: 'textStyle/bold' },
172 { type: 'textStyle/italic' },
173 { type: 'textStyle/underline' }
174 ]
175 ]
176 ],
177 expected: [
178 { type: 'textStyle/bold' },
179 { type: 'textStyle/italic' }
180 ]
181 },
182 {
183 msg: 'no common coverage due to plain character at the start',
184 data: [
185 'a',
186 [
187 'b',
188 [
189 { type: 'textStyle/bold' },
190 { type: 'textStyle/italic' },
191 { type: 'textStyle/underline' }
192 ]
193 ],
194 [
195 'c',
196 [
197 { type: 'textStyle/bold' },
198 { type: 'textStyle/italic' }
199 ]
200 ]
201 ],
202 expected: []
203 },
204 {
205 msg: 'no common coverage due to plain character in the middle',
206 data: [
207 [
208 'a',
209 [
210 { type: 'textStyle/bold' },
211 { type: 'textStyle/italic' },
212 { type: 'textStyle/underline' }
213 ]
214 ],
215 [ 'b' ],
216 [
217 'c',
218 [
219 { type: 'textStyle/bold' },
220 { type: 'textStyle/italic' }
221 ]
222 ]
223 ],
224 expected: []
225 },
226 {
227 msg: 'no common coverage due to plain character at the end',
228 data: [
229 [
230 'a',
231 [
232 { type: 'textStyle/bold' },
233 { type: 'textStyle/italic' }
234 ]
235 ],
236 [
237 'b',
238 [
239 { type: 'textStyle/bold' },
240 { type: 'textStyle/italic' },
241 { type: 'textStyle/underline' }
242 ]
243 ],
244 [ 'c' ]
245 ],
246 expected: []
247 },
248 {
249 msg: 'no common coverage due to mismatched annotations',
250 data: [
251 [ 'a', [ { type: 'textStyle/bold' } ] ],
252 [ 'b', [ { type: 'textStyle/italic' } ] ]
253 ],
254 expected: []
255 },
256 {
257 msg: 'no common coverage due to un-annotated content node',
258 data: [
259 [ 'a', [ { type: 'textStyle/bold' } ] ],
260 { type: 'inlineImage' },
261 { type: '/inlineImage' }
262 ],
263 expected: []
264 },
265 {
266 msg: 'branch node is ignored',
267 data: [
268 [ 'a', [ { type: 'textStyle/bold' } ] ],
269 { type: 'paragraph' },
270 { type: '/paragraph' }
271 ],
272 expected: [ { type: 'textStyle/bold' } ]
273 },
274 {
275 msg: 'annotations are collected using all with mismatched annotations',
276 data: [
277 [ 'a', [ { type: 'textStyle/bold' } ] ],
278 [ 'b', [ { type: 'textStyle/italic' } ] ]
279 ],
280 all: true,
281 expected: [
282 { type: 'textStyle/bold' },
283 { type: 'textStyle/italic' }
284 ]
285 },
286 {
287 msg: 'annotations are collected using all, even with a plain character at the start',
288 data: [
289 'a',
290 [ 'b', [ { type: 'textStyle/bold' } ] ],
291 [ 'c', [ { type: 'textStyle/italic' } ] ]
292 ],
293 all: true,
294 expected: [
295 { type: 'textStyle/bold' },
296 { type: 'textStyle/italic' }
297 ]
298 },
299 {
300 msg: 'annotations are collected using all, even with a plain character in the middle',
301 data: [
302 [ 'a', [ { type: 'textStyle/bold' } ] ],
303 'b',
304 [ 'c', [ { type: 'textStyle/italic' } ] ]
305 ],
306 all: true,
307 expected: [
308 { type: 'textStyle/bold' },
309 { type: 'textStyle/italic' }
310 ]
311 },
312 {
313 msg: 'annotations are collected using all, even with a plain character at the end',
314 data: [
315 [ 'a', [ { type: 'textStyle/bold' } ] ],
316 [ 'b', [ { type: 'textStyle/italic' } ] ],
317 'c'
318 ],
319 all: true,
320 expected: [
321 { type: 'textStyle/bold' },
322 { type: 'textStyle/italic' }
323 ]
324 },
325 {
326 msg: 'no common coverage from all plain characters',
327 data: [ ...'ab' ],
328 expected: []
329 },
330 {
331 msg: 'no common coverage using all from all plain characters',
332 data: [ ...'ab' ],
333 all: true,
334 expected: []
335 },
336 {
337 msg: 'contents of ignoreChildren nodes are skipped',
338 data: [
339 { type: 'exampleIgnoreChildren' },
340 [ 'a', [ { type: 'textStyle/bold' } ] ],
341 [ 'b', [ { type: 'textStyle/bold' } ] ],
342 { type: '/exampleIgnoreChildren' }
343 ],
344 expected: []
345 },
346 {
347 msg: 'contents of ignoreChildren nodes are skipped in all mode too',
348 data: [
349 { type: 'exampleIgnoreChildren' },
350 [ 'a', [ { type: 'textStyle/bold' } ] ],
351 [ 'b', [ { type: 'textStyle/italic' } ] ],
352 { type: '/exampleIgnoreChildren' }
353 ],
354 all: true,
355 expected: []
356 }
357 ];
358
359 cases.forEach( ( caseItem ) => {
360 const data = ve.dm.example.preprocessAnnotations( caseItem.data );
361 const doc = new ve.dm.Document( data );
362 assert.deepEqual(
363 doc.data.getAnnotationsFromRange( new ve.Range( 0, caseItem.data.length ), caseItem.all ).getHashes(),
364 ve.dm.example.createAnnotationSet( doc.getStore(), caseItem.expected ).getHashes(),
365 caseItem.msg
366 );
367 } );
368 } );
369
370 QUnit.test( 'getInsertionAnnotationsFromRange', ( assert ) => {
371 const u = ve.dm.example.underline,
372 // <h1>:0 a:1 b:2 c:3 d:4 e:5 f:6 </h1>:7 <p>:8 g:9 </p>:10 <div>:11 </div>:12
373 html = '<h1>ab<u>cd</u>ef</h1><p><u>g</u></p><div></div>',
374 cases = [
375 { range: [ 1, 1 ], expected: [], msg: 'plain start at block start' },
376 { range: [ 2, 2 ], expected: [], msg: 'plain interior' },
377 { range: [ 3, 3 ], expected: [], msg: 'plain end before u' },
378 { range: [ 3, 3 ], startAfterAnnotations: true, expected: [ u ], msg: 'u start' },
379 { range: [ 4, 4 ], expected: [ u ], msg: 'u interior' },
380 { range: [ 5, 5 ], expected: [ u ], msg: 'u end' },
381 { range: [ 5, 5 ], startAfterAnnotations: true, expected: [], msg: 'after u' },
382 { range: [ 6, 6 ], expected: [], msg: 'plain start after u' },
383 { range: [ 7, 7 ], expected: [], msg: 'plain end at block end' },
384 { range: [ 9, 9 ], expected: [], msg: 'block start before u' },
385 { range: [ 9, 9 ], startAfterAnnotations: true, expected: [ u ], msg: 'u start at block start' },
386 { range: [ 10, 10 ], expected: [ u ], msg: 'u end before block end' },
387 { range: [ 10, 10 ], startAfterAnnotations: true, expected: [], msg: 'after u before block end' },
388 { range: [ 12, 12 ], expected: [], msg: 'empty block' },
389 { range: [ 2, 3 ], expected: [], msg: 'forward to u start' },
390 { range: [ 3, 2 ], expected: [], msg: 'backward to u start' },
391 { range: [ 2, 4 ], expected: [], msg: 'forward past u start' },
392 { range: [ 4, 2 ], expected: [], msg: 'backward past u start' },
393 { range: [ 3, 4 ], expected: [ u ], msg: 'forward to u end' },
394 { range: [ 4, 3 ], expected: [ u ], msg: 'backward to u end' },
395 { range: [ 3, 5 ], expected: [ u ], msg: 'forward past u end' },
396 { range: [ 5, 3 ], expected: [ u ], msg: 'backward past u end' }
397 ];
398
399 const linearData = ve.dm.converter.getModelFromDom(
400 ve.createDocumentFromHtml( html )
401 ).data;
402 cases.forEach( ( caseItem ) => {
403 const observed = linearData.getInsertionAnnotationsFromRange(
404 new ve.Range( caseItem.range[ 0 ], caseItem.range[ 1 ] ),
405 caseItem.startAfterAnnotations
406 ).get().map( ( annotation ) => ( {
407 type: annotation.element.type,
408 attributes: annotation.element.attributes
409 } ) );
410 assert.deepEqual( observed, caseItem.expected, caseItem.msg );
411 } );
412 } );
413
414 QUnit.test( 'getAnnotatedRangeFromOffset', ( assert ) => {
415 const cases = [
416 {
417 msg: 'a bold word',
418 data: [
419 // 0
420 'a',
421 // 1
422 [ 'b', [ { type: 'textStyle/bold' } ] ],
423 // 2
424 [ 'o', [ { type: 'textStyle/bold' } ] ],
425 // 3
426 [ 'l', [ { type: 'textStyle/bold' } ] ],
427 // 4
428 [ 'd', [ { type: 'textStyle/bold' } ] ],
429 // 5
430 'w',
431 // 6
432 'o',
433 // 7
434 'r',
435 // 8
436 'd'
437 ],
438 annotation: { type: 'textStyle/bold' },
439 offset: 3,
440 expected: new ve.Range( 1, 5 )
441 },
442 {
443 msg: 'a linked',
444 data: [
445 // 0
446 'x',
447 // 1
448 'x',
449 // 2
450 'x',
451 // 3
452 [ 'l', [ { type: 'link' } ] ],
453 // 4
454 [ 'i', [ { type: 'link' } ] ],
455 // 5
456 [ 'n', [ { type: 'link' } ] ],
457 // 6
458 [ 'k', [ { type: 'link' } ] ],
459 // 7
460 'x',
461 // 8
462 'x',
463 // 9
464 'x'
465 ],
466 annotation: { type: 'link' },
467 offset: 3,
468 expected: new ve.Range( 3, 7 )
469 },
470 {
471 msg: 'bold over an annotated leaf node',
472 data: [
473 // 0
474 'h',
475 // 1
476 [ 'b', [ { type: 'textStyle/bold' } ] ],
477 // 2
478 [ 'o', [ { type: 'textStyle/bold' } ] ],
479 // 3
480 {
481 type: 'inlineImage',
482 attributes: { src: ve.dm.example.imgSrc },
483 annotations: [ { type: 'textStyle/bold' } ]
484 },
485 // 4
486 { type: '/inlineImage' },
487 // 5
488 [ 'l', [ { type: 'textStyle/bold' } ] ],
489 // 6
490 [ 'd', [ { type: 'textStyle/bold' } ] ],
491 // 7
492 'i'
493 ],
494 annotation: { type: 'textStyle/bold' },
495 offset: 3,
496 expected: new ve.Range( 1, 7 )
497 }
498 ];
499
500 cases.forEach( ( caseItem ) => {
501 const data = ve.dm.example.preprocessAnnotations( caseItem.data );
502 const doc = new ve.dm.Document( data );
503 assert.equalRange(
504 doc.data.getAnnotatedRangeFromOffset( caseItem.offset,
505 ve.dm.example.createAnnotation( caseItem.annotation ) ),
506 caseItem.expected,
507 caseItem.msg
508 );
509 } );
510 } );
511
512 QUnit.test( 'trimOuterSpaceFromRange', ( assert ) => {
513 const data = [
514 // 0
515 { type: 'paragraph' },
516 // 1
517 ' ',
518 // 2
519 'F',
520 // 3
521 'o',
522 // 4
523 'o',
524 // 5
525 ' ',
526 // 6
527 ' ',
528 // 7
529 [ ' ', ve.dm.example.bold ],
530 // 8
531 [ ' ', ve.dm.example.italic ],
532 // 9
533 [ 'B', ve.dm.example.italic ],
534 // 10
535 'a',
536 // 11
537 'r',
538 // 12
539 ' ',
540 // 13
541 { type: '/paragraph' }
542 // 14
543 ],
544 cases = [
545 {
546 msg: 'Word without spaces is untouched',
547 range: new ve.Range( 2, 5 ),
548 trimmed: new ve.Range( 2, 5 )
549 },
550 {
551 msg: 'Consecutive words with spaces in between but not at the edges are untouched',
552 range: new ve.Range( 2, 12 ),
553 trimmed: new ve.Range( 2, 12 )
554 },
555 {
556 msg: 'Single space is trimmed from the start',
557 range: new ve.Range( 1, 4 ),
558 trimmed: new ve.Range( 2, 4 )
559 },
560 {
561 msg: 'Single space is trimmed from the end',
562 range: new ve.Range( 3, 6 ),
563 trimmed: new ve.Range( 3, 5 )
564 },
565 {
566 msg: 'Single space is trimmed from both sides',
567 range: new ve.Range( 1, 6 ),
568 trimmed: new ve.Range( 2, 5 )
569 },
570 {
571 msg: 'Different number of spaces trimmed on each side',
572 range: new ve.Range( 1, 7 ),
573 trimmed: new ve.Range( 2, 5 )
574 },
575 {
576 msg: 'Annotated spaces are trimmed correctly from the end',
577 range: new ve.Range( 3, 9 ),
578 trimmed: new ve.Range( 3, 5 )
579 },
580 {
581 msg: 'Annotated spaces are trimmed correctly from the start',
582 range: new ve.Range( 7, 10 ),
583 trimmed: new ve.Range( 9, 10 )
584 },
585 {
586 msg: 'Trimming annotated spaces at the end and plain spaces at the start',
587 range: new ve.Range( 1, 9 ),
588 trimmed: new ve.Range( 2, 5 )
589 },
590 {
591 msg: 'Spaces are trimmed from the ends but not in the middle',
592 range: new ve.Range( 1, 13 ),
593 trimmed: new ve.Range( 2, 12 )
594 },
595 {
596 msg: 'All-whitespace range is trimmed to empty range',
597 range: new ve.Range( 5, 9 ),
598 trimmed: new ve.Range( 5 )
599 }
600 ];
601
602 const linearData = ve.dm.example.preprocessAnnotations( data );
603 const elementData = new ve.dm.ElementLinearData( linearData.getStore(), linearData.getData() );
604 cases.forEach( ( caseItem ) => {
605 assert.equalRange(
606 elementData.trimOuterSpaceFromRange( caseItem.range ),
607 caseItem.trimmed,
608 caseItem.msg
609 );
610 } );
611 } );
612
613 QUnit.test( 'isContentOffset', ( assert ) => {
614 const data = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), [
615 { type: 'heading' },
616 'a',
617 { type: 'inlineImage' },
618 { type: '/inlineImage' },
619 ...'bc',
620 { type: '/heading' },
621 { type: 'paragraph' },
622 { type: '/paragraph' },
623 { type: 'preformatted' },
624 { type: 'inlineImage' },
625 { type: '/inlineImage' },
626 { type: '/preformatted' },
627 { type: 'list' },
628 { type: 'listItem' },
629 { type: '/listItem' },
630 { type: '/list' },
631 { type: 'alienBlock' },
632 { type: '/alienBlock' },
633 { type: 'table' },
634 { type: 'tableRow' },
635 { type: 'tableCell' },
636 { type: 'alienBlock' },
637 { type: '/alienBlock' },
638 { type: '/tableCell' },
639 { type: '/tableRow' },
640 { type: '/table' }
641 ] ),
642 cases = [
643 { msg: 'left of document', expected: false },
644 { msg: 'beginning of content branch', expected: true },
645 { msg: 'left of non-text inline leaf', expected: true },
646 { msg: 'inside non-text inline leaf', expected: false },
647 { msg: 'right of non-text inline leaf', expected: true },
648 { msg: 'between characters', expected: true },
649 { msg: 'end of content branch', expected: true },
650 { msg: 'between content branches', expected: false },
651 { msg: 'inside empty content branch', expected: true },
652 { msg: 'between content branches', expected: false },
653 { msg: 'beginning of content branch, left of inline leaf', expected: true },
654 { msg: 'inside content branch with non-text inline leaf', expected: false },
655 { msg: 'end of content branch, right of non-content leaf', expected: true },
656 { msg: 'between content, non-content branches', expected: false },
657 { msg: 'between parent, child branches, descending', expected: false },
658 { msg: 'inside empty non-content branch', expected: false },
659 { msg: 'between parent, child branches, ascending', expected: false },
660 { msg: 'between non-content branch, non-content leaf', expected: false },
661 { msg: 'inside non-content leaf', expected: false },
662 { msg: 'between non-content branches', expected: false },
663 { msg: 'between non-content branches', expected: false },
664 { msg: 'between non-content branches', expected: false },
665 { msg: 'inside non-content branch before non-content leaf', expected: false },
666 { msg: 'inside non-content leaf', expected: false },
667 { msg: 'inside non-content branch after non-content leaf', expected: false },
668 { msg: 'between non-content branches', expected: false },
669 { msg: 'between non-content branches', expected: false },
670 { msg: 'right of document', expected: false }
671 ];
672
673 cases.forEach( ( caseItem, i ) => {
674 const left = data.getData( i - 1 ) ? ( data.getData( i - 1 ).type || data.getCharacterData( i - 1 ) ) : '[start]';
675 const right = data.getData( i ) ? ( data.getData( i ).type || data.getCharacterData( i ) ) : '[end]';
676 assert.strictEqual(
677 data.isContentOffset( i ),
678 caseItem.expected,
679 caseItem.msg + ' (' + left + '|' + right + ' @ ' + i + ')'
680 );
681 } );
682 } );
683
684 QUnit.test( 'isStructuralOffset', ( assert ) => {
685 const data = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), [
686 { type: 'heading' },
687 'a',
688 { type: 'inlineImage' },
689 { type: '/inlineImage' },
690 ...'bc',
691 { type: '/heading' },
692 { type: 'paragraph' },
693 { type: '/paragraph' },
694 { type: 'preformatted' },
695 { type: 'inlineImage' },
696 { type: '/inlineImage' },
697 { type: '/preformatted' },
698 { type: 'list' },
699 { type: 'listItem' },
700 { type: '/listItem' },
701 { type: '/list' },
702 { type: 'alienBlock' },
703 { type: '/alienBlock' },
704 { type: 'table' },
705 { type: 'tableRow' },
706 { type: 'tableCell' },
707 { type: 'alienBlock' },
708 { type: '/alienBlock' },
709 { type: '/tableCell' },
710 { type: '/tableRow' },
711 { type: '/table' }
712 ] ),
713 cases = [
714 { msg: 'left of document', expected: [ true, true ] },
715 { msg: 'beginning of content branch', expected: [ false, false ] },
716 { msg: 'left of non-text inline leaf', expected: [ false, false ] },
717 { msg: 'inside non-text inline leaf', expected: [ false, false ] },
718 { msg: 'right of non-text inline leaf', expected: [ false, false ] },
719 { msg: 'between characters', expected: [ false, false ] },
720 { msg: 'end of content branch', expected: [ false, false ] },
721 { msg: 'between content branches', expected: [ true, true ] },
722 { msg: 'inside empty content branch', expected: [ false, false ] },
723 { msg: 'between content branches', expected: [ true, true ] },
724 { msg: 'beginning of content branch, left of inline leaf', expected: [ false, false ] },
725 { msg: 'inside content branch with non-text inline leaf', expected: [ false, false ] },
726 { msg: 'end of content branch, right of inline leaf', expected: [ false, false ] },
727 { msg: 'between content, non-content branches', expected: [ true, true ] },
728 { msg: 'between parent, child branches, descending', expected: [ true, false ] },
729 { msg: 'inside empty non-content branch', expected: [ true, true ] },
730 { msg: 'between parent, child branches, ascending', expected: [ true, false ] },
731 { msg: 'between non-content branch, non-content leaf', expected: [ true, true ] },
732 { msg: 'inside non-content leaf', expected: [ false, false ] },
733 { msg: 'between non-content branches', expected: [ true, true ] },
734 { msg: 'between non-content branches', expected: [ true, false ] },
735 { msg: 'between non-content branches', expected: [ true, false ] },
736 { msg: 'inside non-content branch before non-content leaf', expected: [ true, true ] },
737 { msg: 'inside non-content leaf', expected: [ false, false ] },
738 { msg: 'inside non-content branch after non-content leaf', expected: [ true, true ] },
739 { msg: 'between non-content branches', expected: [ true, false ] },
740 { msg: 'between non-content branches', expected: [ true, false ] },
741 { msg: 'right of document', expected: [ true, true ] }
742 ];
743
744 cases.forEach( ( caseItem, i ) => {
745 const left = data.getData( i - 1 ) ? ( data.getData( i - 1 ).type || data.getCharacterData( i - 1 ) ) : '[start]';
746 const right = data.getData( i ) ? ( data.getData( i ).type || data.getCharacterData( i ) ) : '[end]';
747 assert.strictEqual(
748 data.isStructuralOffset( i ),
749 caseItem.expected[ 0 ],
750 caseItem.msg + ' (' + left + '|' + right + ' @ ' + i + ')'
751 );
752 assert.strictEqual(
753 data.isStructuralOffset( i, true ),
754 caseItem.expected[ 1 ],
755 caseItem.msg + ', unrestricted (' + left + '|' + right + ' @ ' + i + ')'
756 );
757 } );
758 } );
759
760 QUnit.test( 'getCharacterData', ( assert ) => {
761 const data = [ { type: 'paragraph' }, 'a', [ 'b', [ 0 ] ], { type: '/paragraph' } ],
762 expected = [ '', 'a', 'b', '' ],
763 linearData = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), data );
764
765 for ( let i = 0; i < data.length; i++ ) {
766 assert.strictEqual(
767 linearData.getCharacterData( i ), expected[ i ]
768 );
769 }
770 } );
771
772 QUnit.test( 'isPlainText', ( assert ) => {
773 const doc = ve.dm.example.createExampleDocument();
774
775 assert.strictEqual( doc.data.isPlainText( new ve.Range( 1, 2 ), false ), true, 'Plain text' );
776 assert.strictEqual( doc.data.isPlainText( new ve.Range( 1, 3 ), true ), false, 'Annotated text' );
777 assert.strictEqual( doc.data.isPlainText( new ve.Range( 2, 3 ), true, undefined, true ), true, 'Annotated text, ignoring covering annotations' );
778 assert.strictEqual( doc.data.isPlainText( new ve.Range( 9, 11 ), false ), false, 'Paragraph and text (no content nodes)' );
779 assert.strictEqual( doc.data.isPlainText( new ve.Range( 9, 11 ), true ), true, 'Paragraph and text (content nodes allowed)' );
780 assert.strictEqual( doc.data.isPlainText( new ve.Range( 12, 26 ), false ), false, 'List (no content nodes)' );
781 assert.strictEqual( doc.data.isPlainText( new ve.Range( 12, 26 ), true ), true, 'List (content nodes allowed)' );
782 } );
783
784 QUnit.test( 'getText', ( assert ) => {
785 const doc = ve.dm.example.createExampleDocument();
786
787 assert.strictEqual( doc.data.getText( false, new ve.Range( 2, 11 ) ), 'bcd' );
788 assert.strictEqual( doc.data.getText( true, new ve.Range( 2, 11 ) ), 'bc d'.replace( / /g, '\n' ) );
789 assert.strictEqual( doc.data.getText( false ), 'abcdefghijklm' );
790 assert.strictEqual(
791 doc.data.getText( true ),
792 ' abc d e f g h i j k l m '.replace( / /g, '\n' )
793 );
794 } );
795
796 QUnit.test( 'getSourceText', ( assert ) => {
797 const data = new ve.dm.ElementLinearData(
798 new ve.dm.HashValueStore(),
799 [
800 { type: 'paragraph' }, ...'foo', { type: '/paragraph' },
801 { type: 'paragraph' }, ...'bar', { type: '/paragraph' },
802 { type: 'internalList' }, { type: '/internalList' }
803 ]
804 ),
805 cases = [
806 {
807 msg: 'Whole document',
808 range: undefined,
809 expected: 'foo\nbar'
810 },
811 {
812 msg: 'Simple text range',
813 range: new ve.Range( 1, 4 ),
814 expected: 'foo'
815 },
816 {
817 msg: 'Newline spanning',
818 range: new ve.Range( 3, 7 ),
819 expected: 'o\nb'
820 },
821 {
822 msg: 'Whole line',
823 range: new ve.Range( 0, 5 ),
824 expected: 'foo\n'
825 }
826 ];
827
828 cases.forEach( ( caseItem ) => {
829 assert.strictEqual(
830 data.getSourceText( caseItem.range ),
831 caseItem.expected,
832 caseItem.msg
833 );
834 } );
835 } );
836
837 QUnit.test( 'isContentData', ( assert ) => {
838 const cases = [
839 {
840 msg: 'simple paragraph',
841 data: [ { type: 'paragraph' }, 'a', { type: '/paragraph' } ],
842 expected: false
843 },
844 {
845 msg: 'plain text',
846 data: [ ...'abc' ],
847 expected: true
848 },
849 {
850 msg: 'annotated text',
851 data: [ [ 'a', { '{"type:"bold"}': { type: 'bold' } } ] ],
852 expected: true
853 },
854 {
855 msg: 'non-text leaf',
856 data: [ 'a', { type: 'inlineImage' }, { type: '/inlineImage' }, 'c' ],
857 expected: true
858 }
859 ];
860
861 cases.forEach( ( caseItem ) => {
862 const data = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), caseItem.data );
863 assert.strictEqual(
864 data.isContentData(), caseItem.expected, caseItem.msg
865 );
866 } );
867 } );
868
869 QUnit.test( 'getRelativeOffset', ( assert ) => {
870 const cases = [
871 {
872 msg: 'document without any valid offsets returns -1',
873 offset: 0,
874 distance: 1,
875 data: [],
876 callback: function () {
877 return false;
878 },
879 expected: -1
880 },
881 {
882 msg: 'document with all valid offsets returns offset + distance',
883 offset: 0,
884 distance: 2,
885 data: [ ...'ab' ],
886 callback: function () {
887 return true;
888 },
889 expected: 2
890 },
891 {
892 msg: 'document with offset inside an ignoreChildren doesn\'t leave it and returns -1',
893 offset: 7,
894 distance: 1,
895 data: [
896 'a',
897 { type: 'internalList' },
898 { type: 'internalItem' },
899 { type: 'paragraph', internal: { generated: 'wrapper' } },
900 ...'abc',
901 { type: '/paragraph' },
902 { type: '/internalItem' },
903 { type: '/internalList' }
904 ],
905 callback: ve.dm.ElementLinearData.prototype.isContentOffset,
906 // The results here look incorrect. It should "turn around" and return 7.
907 // It should only return -1 if the internalItem has no valid offsets inside (it is empty).
908 expected: -1
909 }
910 ];
911
912 cases.forEach( ( caseItem ) => {
913 const data = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), caseItem.data );
914 if ( 'expected' in caseItem ) {
915 assert.strictEqual(
916 data.getRelativeOffset(
917 caseItem.offset,
918 caseItem.distance,
919 caseItem.callback
920 ),
921 caseItem.expected,
922 caseItem.msg
923 );
924 } else if ( 'exception' in caseItem ) {
925 assert.throws(
926 () => {
927 data.getRelativeOffset(
928 caseItem.offset,
929 caseItem.distance,
930 caseItem.callback
931 );
932 },
933 caseItem.exception,
934 caseItem.msg
935 );
936 }
937 } );
938 } );
939
940 QUnit.test( 'getRelativeContentOffset', ( assert ) => {
941 const simpleDoc = ve.dm.example.createExampleDocument(),
942 annDoc = ve.dm.example.createExampleDocument( 'annotationData' ),
943 cases = [
944 {
945 msg: 'invalid starting offset with zero distance gets corrected',
946 offset: 0,
947 distance: 0,
948 expected: 1
949 },
950 {
951 msg: 'invalid starting offset with zero distance gets corrected',
952 offset: 61,
953 distance: 0,
954 expected: 60
955 },
956 {
957 msg: 'valid offset with zero distance returns same offset',
958 offset: 2,
959 distance: 0,
960 expected: 2
961 },
962 {
963 msg: 'invalid starting offset gets corrected',
964 offset: 0,
965 distance: -1,
966 expected: 1
967 },
968 {
969 msg: 'invalid starting offset gets corrected',
970 offset: 61,
971 distance: 1,
972 expected: 60
973 },
974 {
975 msg: 'stop at left edge if already valid',
976 offset: 1,
977 distance: -1,
978 expected: 1
979 },
980 {
981 msg: 'stop at right edge if already valid',
982 offset: 60,
983 distance: 1,
984 expected: 60
985 },
986 {
987 msg: 'first content offset is farthest left',
988 offset: 2,
989 distance: -2,
990 expected: 1
991 },
992 {
993 msg: 'last content offset is farthest right',
994 offset: 59,
995 distance: 2,
996 expected: 60
997 },
998 {
999 msg: '1 right within text',
1000 offset: 1,
1001 distance: 1,
1002 expected: 2
1003 },
1004 {
1005 msg: '2 right within text',
1006 offset: 1,
1007 distance: 2,
1008 expected: 3
1009 },
1010 {
1011 msg: '1 left within text',
1012 offset: 2,
1013 distance: -1,
1014 expected: 1
1015 },
1016 {
1017 msg: '2 left within text',
1018 offset: 3,
1019 distance: -2,
1020 expected: 1
1021 },
1022 {
1023 msg: '1 right over elements',
1024 offset: 4,
1025 distance: 1,
1026 expected: 10
1027 },
1028 {
1029 msg: '2 right over elements',
1030 offset: 4,
1031 distance: 2,
1032 expected: 11
1033 },
1034 {
1035 msg: '1 left over elements',
1036 offset: 10,
1037 distance: -1,
1038 expected: 4
1039 },
1040 {
1041 msg: '2 left over elements',
1042 offset: 10,
1043 distance: -2,
1044 expected: 3
1045 },
1046 {
1047 msg: 'Skips over nested ignoreChildren nodes',
1048 doc: annDoc,
1049 offset: 10,
1050 distance: 1,
1051 expected: 24
1052 },
1053 {
1054 msg: 'Skips over nested ignoreChildren nodes (reverse)',
1055 doc: annDoc,
1056 offset: 23,
1057 distance: -1,
1058 expected: 9
1059 }
1060 ];
1061
1062 cases.forEach( ( caseItem ) => {
1063 const doc = caseItem.doc || simpleDoc;
1064 assert.strictEqual(
1065 doc.data.getRelativeContentOffset( caseItem.offset, caseItem.distance ),
1066 caseItem.expected,
1067 caseItem.msg
1068 );
1069 } );
1070 } );
1071
1072 QUnit.test( 'getNearestContentOffset', ( assert ) => {
1073 const doc = ve.dm.example.createExampleDocument(),
1074 cases = [
1075 {
1076 msg: 'unspecified direction results in shortest distance',
1077 offset: 0,
1078 direction: 0,
1079 expected: 1
1080 },
1081 {
1082 msg: 'unspecified direction results in shortest distance',
1083 offset: 5,
1084 direction: 0,
1085 expected: 4
1086 },
1087 {
1088 msg: 'positive direction results in next valid offset to the right',
1089 offset: 5,
1090 direction: 1,
1091 expected: 10
1092 },
1093 {
1094 msg: 'negative direction results in next valid offset to the left',
1095 offset: 5,
1096 direction: -1,
1097 expected: 4
1098 },
1099 {
1100 msg: 'valid offset without direction returns same offset',
1101 offset: 1,
1102 expected: 1
1103 },
1104 {
1105 msg: 'valid offset with positive direction returns same offset',
1106 offset: 1,
1107 direction: 1,
1108 expected: 1
1109 },
1110 {
1111 msg: 'valid offset with negative direction returns same offset',
1112 offset: 1,
1113 direction: -1,
1114 expected: 1
1115 }
1116 ];
1117
1118 cases.forEach( ( caseItem ) => {
1119 assert.strictEqual(
1120 doc.data.getNearestContentOffset( caseItem.offset, caseItem.direction ),
1121 caseItem.expected,
1122 caseItem.msg
1123 );
1124 } );
1125 } );
1126
1127 QUnit.test( 'getRelativeStructuralOffset', ( assert ) => {
1128 const doc = ve.dm.example.createExampleDocument(),
1129 cases = [
1130 {
1131 msg: 'invalid starting offset with zero distance gets corrected',
1132 offset: 1,
1133 distance: 0,
1134 expected: 5
1135 },
1136 {
1137 msg: 'invalid starting offset with zero distance gets corrected',
1138 offset: 60,
1139 distance: 0,
1140 expected: 61
1141 },
1142 {
1143 msg: 'valid offset with zero distance returns same offset',
1144 offset: 0,
1145 distance: 0,
1146 expected: 0
1147 },
1148 {
1149 msg: 'invalid starting offset gets corrected',
1150 offset: 2,
1151 distance: -1,
1152 expected: 0
1153 },
1154 {
1155 msg: 'invalid starting offset gets corrected',
1156 offset: 59,
1157 distance: 1,
1158 expected: 61
1159 },
1160 {
1161 msg: 'first structural offset is farthest left',
1162 offset: 5,
1163 distance: -2,
1164 expected: 0
1165 },
1166 {
1167 msg: 'last structural offset is farthest right',
1168 offset: 62,
1169 distance: 2,
1170 expected: 63
1171 },
1172 {
1173 msg: '1 right',
1174 offset: 0,
1175 distance: 1,
1176 expected: 5
1177 },
1178 {
1179 msg: '1 right, unrestricted',
1180 offset: 5,
1181 distance: 1,
1182 unrestricted: true,
1183 expected: 9
1184 },
1185 {
1186 msg: '2 right',
1187 offset: 0,
1188 distance: 2,
1189 expected: 6
1190 },
1191 {
1192 msg: '2 right, unrestricted',
1193 offset: 0,
1194 distance: 2,
1195 unrestricted: true,
1196 expected: 9
1197 },
1198 {
1199 msg: '1 left',
1200 offset: 61,
1201 distance: -1,
1202 expected: 58
1203 },
1204 {
1205 msg: '1 left, unrestricted',
1206 offset: 9,
1207 distance: -1,
1208 unrestricted: true,
1209 expected: 5
1210 },
1211 {
1212 msg: '2 left',
1213 offset: 61,
1214 distance: -2,
1215 expected: 55
1216 },
1217 {
1218 msg: '2 left, unrestricted',
1219 offset: 9,
1220 distance: -2,
1221 unrestricted: true,
1222 expected: 0
1223 }
1224 ];
1225
1226 cases.forEach( ( caseItem ) => {
1227 assert.strictEqual(
1228 doc.data.getRelativeStructuralOffset(
1229 caseItem.offset, caseItem.distance, caseItem.unrestricted
1230 ),
1231 caseItem.expected,
1232 caseItem.msg
1233 );
1234 } );
1235 } );
1236
1237 QUnit.test( 'getNearestStructuralOffset', ( assert ) => {
1238 const doc = ve.dm.example.createExampleDocument(),
1239 cases = [
1240 {
1241 msg: 'unspecified direction results in shortest distance',
1242 offset: 1,
1243 direction: 0,
1244 expected: 0
1245 },
1246 {
1247 msg: 'unspecified direction results in shortest distance',
1248 offset: 4,
1249 direction: 0,
1250 expected: 5
1251 },
1252 {
1253 msg: 'unspecified direction results in shortest distance, unrestricted',
1254 offset: 8,
1255 direction: 0,
1256 unrestricted: true,
1257 expected: 9
1258 },
1259 {
1260 msg: 'unspecified direction results in shortest distance, unrestricted',
1261 offset: 6,
1262 direction: 0,
1263 unrestricted: true,
1264 expected: 5
1265 },
1266 {
1267 msg: 'positive direction results in next valid offset to the right',
1268 offset: 1,
1269 direction: 1,
1270 expected: 5
1271 },
1272 {
1273 msg: 'positive direction results in next valid offset to the right',
1274 offset: 4,
1275 direction: 1,
1276 expected: 5
1277 },
1278 {
1279 msg: 'positive direction results in next valid offset to the right, unrestricted',
1280 offset: 7,
1281 direction: 1,
1282 unrestricted: true,
1283 expected: 9
1284 },
1285 {
1286 msg: 'negative direction results in next valid offset to the left',
1287 offset: 1,
1288 direction: -1,
1289 expected: 0
1290 },
1291 {
1292 msg: 'negative direction results in next valid offset to the left',
1293 offset: 4,
1294 direction: -1,
1295 expected: 0
1296 },
1297 {
1298 msg: 'negative direction results in next valid offset to the left, unrestricted',
1299 offset: 6,
1300 direction: -1,
1301 unrestricted: true,
1302 expected: 5
1303 },
1304 {
1305 msg: 'valid offset without direction returns same offset',
1306 offset: 0,
1307 expected: 0
1308 },
1309 {
1310 msg: 'valid offset with positive direction returns same offset',
1311 offset: 0,
1312 direction: 1,
1313 expected: 0
1314 },
1315 {
1316 msg: 'valid offset with negative direction returns same offset',
1317 offset: 0,
1318 direction: -1,
1319 expected: 0
1320 },
1321 {
1322 msg: 'valid offset without direction returns same offset, unrestricted',
1323 offset: 0,
1324 unrestricted: true,
1325 expected: 0
1326 },
1327 {
1328 msg: 'valid offset with positive direction returns same offset, unrestricted',
1329 offset: 0,
1330 direction: 1,
1331 unrestricted: true,
1332 expected: 0
1333 },
1334 {
1335 msg: 'valid offset with negative direction returns same offset, unrestricted',
1336 offset: 0,
1337 direction: -1,
1338 unrestricted: true,
1339 expected: 0
1340 }
1341 ];
1342
1343 cases.forEach( ( caseItem ) => {
1344 assert.strictEqual(
1345 doc.data.getNearestStructuralOffset(
1346 caseItem.offset, caseItem.direction, caseItem.unrestricted
1347 ),
1348 caseItem.expected,
1349 caseItem.msg
1350 );
1351 } );
1352 } );
1353
1354 QUnit.test( 'getWordRange', ( assert ) => {
1355 const store = new ve.dm.HashValueStore(),
1356 cases = [
1357 {
1358 phrase: 'visual editor test',
1359 msg: 'simple Latin word',
1360 offset: 10,
1361 expected: 'editor'
1362 },
1363 {
1364 phrase: 'visual editor test',
1365 msg: 'cursor at start of word',
1366 offset: 7,
1367 expected: 'editor'
1368 },
1369 {
1370 phrase: 'visual editor test',
1371 msg: 'cursor at end of word',
1372 offset: 13,
1373 expected: 'editor'
1374 },
1375 {
1376 phrase: 'visual editor test',
1377 msg: 'cursor at start of text',
1378 offset: 0,
1379 expected: 'visual'
1380 },
1381 {
1382 phrase: 'visual editor test',
1383 msg: 'cursor at end of text',
1384 offset: 18,
1385 expected: 'test'
1386 },
1387 {
1388 phrase: 'Computer-aided design',
1389 msg: 'hyphenated Latin word',
1390 offset: 12,
1391 expected: 'aided'
1392 },
1393 {
1394 phrase: 'Water (l\'eau) is',
1395 msg: 'apostrophe and parentheses (Latin)',
1396 offset: 8,
1397 expected: 'l\'eau'
1398 },
1399 {
1400 phrase: 'Water (H2O) is',
1401 msg: 'number in word (Latin)',
1402 offset: 9,
1403 expected: 'H2O'
1404 },
1405 {
1406 phrase: 'The \'word\' is',
1407 msg: 'apostrophes as single quotes',
1408 offset: 7,
1409 expected: 'word'
1410 },
1411 {
1412 phrase: 'Some "double" quotes',
1413 msg: 'double quotes',
1414 offset: 8,
1415 expected: 'double'
1416 },
1417 {
1418 phrase: 'Wikipédia l\'encyclopédie libre',
1419 msg: 'extended Latin word',
1420 offset: 15,
1421 expected: 'l\'encyclopédie'
1422 },
1423 {
1424 phrase: 'Wikipédia l\'encyclopédie libre',
1425 msg: 'Extend characters (i.e. letter + accent)',
1426 offset: 15,
1427 expected: 'l\'encyclopédie'
1428 },
1429 {
1430 phrase: 'Википедия свободная энциклопедия',
1431 msg: 'Cyrillic word',
1432 offset: 14,
1433 expected: 'свободная'
1434 },
1435 {
1436 phrase: 'την ελεύθερη εγκυκλοπαίδεια',
1437 msg: 'Greek word',
1438 offset: 7,
1439 expected: 'ελεύθερη'
1440 },
1441 {
1442 phrase: '우리 모두의 백과사전',
1443 msg: 'Hangul word',
1444 offset: 4,
1445 expected: '모두의'
1446 },
1447 {
1448 phrase: 'This: ٠١٢٣٤٥٦٧٨٩ means 0123456789',
1449 msg: 'Eastern Arabic numerals',
1450 offset: 13,
1451 expected: '٠١٢٣٤٥٦٧٨٩'
1452 },
1453 {
1454 phrase: 'Latinカタカナwrapped',
1455 msg: 'Latin-wrapped Katakana word',
1456 offset: 7,
1457 expected: 'カタカナ'
1458 },
1459 {
1460 phrase: '维基百科',
1461 msg: 'Hanzi characters (cursor in middle)',
1462 offset: 2,
1463 expected: '基'
1464 },
1465 {
1466 phrase: '维基百科',
1467 msg: 'Hanzi characters (cursor at end)',
1468 offset: 4,
1469 expected: '科'
1470 },
1471 {
1472 phrase: 'a b',
1473 msg: 'Single-char word before cursor',
1474 offset: 1,
1475 expected: 'a'
1476 },
1477 {
1478 phrase: 'a b',
1479 msg: 'Single-char word after cursor',
1480 offset: 2,
1481 expected: 'b'
1482 },
1483 {
1484 phrase: '佢地嘅𨋢壞咗',
1485 msg: 'Surrogate-pair word character before cursor',
1486 offset: 5,
1487 expected: '𨋢'
1488 },
1489 {
1490 phrase: '"𨋢"=lip1',
1491 msg: 'Surrogate-pair word character after cursor',
1492 offset: 1,
1493 expected: '𨋢'
1494 },
1495 {
1496 phrase: '"\uD83D\uDE00"=GRINNING_FACE',
1497 msg: 'Surrogate-pair non-word character before cursor',
1498 offset: 3,
1499 expected: ''
1500 },
1501 {
1502 phrase: '"\uD83D\uDE00"=GRINNING_FACE',
1503 msg: 'Surrogate-pair non-word character after cursor',
1504 offset: 1,
1505 expected: ''
1506 },
1507 {
1508 phrase: 'Costs £1,234.00 each',
1509 msg: 'formatted number sequence',
1510 offset: 11,
1511 expected: '1,234.00'
1512 },
1513 {
1514 phrase: 'Reset index_of variable',
1515 msg: 'underscore-joined word',
1516 offset: 8,
1517 expected: 'index_of'
1518 }
1519 ];
1520
1521 cases.forEach( ( caseItem ) => {
1522 // Construct the text (inside a paragraph, because getNearestContentOffset assumes
1523 // text cannot be at the very start or end of the data).
1524 const data = caseItem.phrase.split( '' );
1525 data.unshift( { type: 'paragraph' } );
1526 data.push( { type: '/paragraph' } );
1527 const elementLinearData = new ve.dm.ElementLinearData( store, data );
1528 // Adjust offsets to account for the paragraph tag
1529 const range = elementLinearData.getWordRange( caseItem.offset + 1 );
1530 const word = caseItem.phrase.slice( range.start - 1, range.end - 1 );
1531 assert.strictEqual( word, caseItem.expected,
1532 caseItem.msg + ': ' +
1533 caseItem.phrase.slice( 0, caseItem.offset ) + '│' +
1534 caseItem.phrase.slice( caseItem.offset, caseItem.phrase.length ) +
1535 ' → ' + caseItem.expected
1536 );
1537 } );
1538 } );
1539
1540 QUnit.test( 'sanitize', ( assert ) => {
1541 const bold = { type: 'textStyle/bold', attributes: { nodeName: 'b' } },
1542 cases = [
1543 {
1544 html: '<p style="text-shadow: 0 0 1px #000;">F<b style="color:blue;">o</b>o</p>',
1545 data: [
1546 { type: 'paragraph' },
1547 'F', [ 'o', [ 'h49981eab0f8056ff' ] ], 'o',
1548 { type: '/paragraph' },
1549 { type: 'internalList' },
1550 { type: '/internalList' }
1551 ],
1552 store: { h49981eab0f8056ff: bold },
1553 rules: { removeOriginalDomElements: true },
1554 msg: 'Original DOM elements removed'
1555 },
1556 {
1557 html: '<p>B<span rel="ve:Alien">a</span>r<img src="//upload.wikimedia.org/wikipedia/commons/b/b3/Wikipedia-logo-v2-en.svg"/></p>',
1558 data: [
1559 { type: 'paragraph' },
1560 ...'Br',
1561 { type: '/paragraph' },
1562 { type: 'internalList' },
1563 { type: '/internalList' }
1564 ],
1565 rules: {
1566 blacklist: {
1567 alienInline: true,
1568 inlineImage: true
1569 }
1570 },
1571 msg: 'Blacklisted nodes removed'
1572 },
1573 {
1574 html: '<p>B<i><b>a</b>z</i></p>',
1575 data: [
1576 { type: 'paragraph' },
1577 ...'Baz',
1578 { type: '/paragraph' },
1579 { type: 'internalList' },
1580 { type: '/internalList' }
1581 ],
1582 rules: { plainText: true },
1583 msg: 'Annotations removed in plainText mode'
1584 },
1585 {
1586 html: '<p><b>a<span rel="ve:Alien">b</span>c</b></p>',
1587 data: [
1588 { type: 'paragraph' },
1589 [ 'a', [ 'h49981eab0f8056ff' ] ],
1590 {
1591 type: 'alienInline',
1592 annotations: [ 'h49981eab0f8056ff' ]
1593 },
1594 { type: '/alienInline' },
1595 [ 'c', [ 'h49981eab0f8056ff' ] ],
1596 { type: '/paragraph' },
1597 { type: 'internalList' },
1598 { type: '/internalList' }
1599 ],
1600 rules: {
1601 removeOriginalDomElements: true,
1602 blacklist: {}
1603 },
1604 msg: 'Remapping annotations on content nodes'
1605 },
1606 {
1607 html: '<p><b>a<i></i>c</b></p>',
1608 data: [
1609 { type: 'paragraph' },
1610 [ 'a', [ 'h49981eab0f8056ff' ] ],
1611 [ 'c', [ 'h49981eab0f8056ff' ] ],
1612 { type: '/paragraph' },
1613 {
1614 type: 'removableAlienMeta',
1615 internal: {
1616 loadMetaParentHash: 'hd25d21d36fa98e7a',
1617 loadMetaParentOffset: 1
1618 },
1619 annotations: [ 'h49981eab0f8056ff' ]
1620 },
1621 { type: '/removableAlienMeta' },
1622 { type: 'internalList' },
1623 { type: '/internalList' }
1624 ],
1625 rules: {
1626 removeOriginalDomElements: true,
1627 allowMetadata: true
1628 },
1629 msg: 'Remapping annotations on moved meta nodes'
1630 },
1631 {
1632 html: '<p><b>a<i></i>c</b></p>',
1633 data: [
1634 { type: 'paragraph' },
1635 [ 'a', [ 'h49981eab0f8056ff' ] ],
1636 [ 'c', [ 'h49981eab0f8056ff' ] ],
1637 { type: '/paragraph' },
1638 { type: 'internalList' },
1639 { type: '/internalList' }
1640 ],
1641 rules: {
1642 removeOriginalDomElements: true,
1643 allowMetadata: false
1644 },
1645 msg: 'Removing moved meta nodes too when removing other metadata'
1646 },
1647 {
1648 html: '<h1>Bar</h1><h2>Baz</h2><p>Quux</p>',
1649 data: [
1650 { type: 'paragraph' },
1651 ...'Bar',
1652 { type: '/paragraph' },
1653 { type: 'paragraph' },
1654 ...'Baz',
1655 { type: '/paragraph' },
1656 { type: 'paragraph' },
1657 ...'Quux',
1658 { type: '/paragraph' },
1659 { type: 'internalList' },
1660 { type: '/internalList' }
1661 ],
1662 rules: { plainText: true },
1663 msg: 'Headings converted to paragraph in plainText mode'
1664 },
1665 {
1666 html: '<p>Bar</p><p>Baz</p><p>Quux</p>',
1667 data: [
1668 { type: 'paragraph' },
1669 ...'Bar',
1670 { type: '/paragraph' },
1671 { type: 'internalList' },
1672 { type: '/internalList' }
1673 ],
1674 rules: { singleLine: true },
1675 msg: 'Extra lines truncated in singleline mode'
1676 },
1677 {
1678 html: '<h1>Bar</h1>',
1679 data: [
1680 // TODO: non-relevant attributes should be discarded, T130377
1681 { type: 'paragraph', attributes: { level: 1 } },
1682 ...'Bar',
1683 { type: '/paragraph' },
1684 { type: 'internalList' },
1685 { type: '/internalList' }
1686 ],
1687 rules: { conversions: { heading: 'paragraph' } },
1688 msg: 'Explicit conversion: heading->paragraph'
1689 },
1690 {
1691 html: '<p>Foo</p><p></p><h1></h1><p>Bar</p>',
1692 data: [
1693 { type: 'paragraph' },
1694 ...'Foo',
1695 { type: '/paragraph' },
1696 { type: 'paragraph' },
1697 ...'Bar',
1698 { type: '/paragraph' },
1699 { type: 'internalList' },
1700 { type: '/internalList' }
1701 ],
1702 msg: 'Empty content nodes are stripped'
1703 },
1704 {
1705 html: '<ul><li></li></ul>',
1706 data: [
1707 { type: 'list', attributes: { style: 'bullet' } },
1708 { type: 'listItem' },
1709 { type: 'paragraph', internal: { generated: 'wrapper' } },
1710 { type: '/paragraph' },
1711 { type: '/listItem' },
1712 { type: '/list' },
1713 { type: 'internalList' },
1714 { type: '/internalList' }
1715 ],
1716 msg: 'Empty, but generated, content nodes are preserved'
1717 },
1718 {
1719 html: '<ul><li><br></li></ul>',
1720 data: [
1721 { type: 'list', attributes: { style: 'bullet' } },
1722 { type: 'listItem' },
1723 { type: 'paragraph', internal: { generated: 'wrapper' } },
1724 { type: '/paragraph' },
1725 { type: '/listItem' },
1726 { type: '/list' },
1727 { type: 'internalList' },
1728 { type: '/internalList' }
1729 ],
1730 msg: 'Line breaks in wrapper paragraphs are discarded'
1731 },
1732 {
1733 html: '<div>Foo</div>',
1734 data: [
1735 { type: 'paragraph' },
1736 ...'Foo',
1737 { type: '/paragraph' },
1738 { type: 'internalList' },
1739 { type: '/internalList' }
1740 ],
1741 rules: { blacklist: { div: true } },
1742 msg: 'Wrapper paragraph becomes real paragraph when unwrapped due to blacklist'
1743 },
1744 {
1745 html: '<p><span style="color:red;" class="red">Foo</span></p>',
1746 data: [
1747 { type: 'paragraph' },
1748 ...'Foo',
1749 { type: '/paragraph' },
1750 { type: 'internalList' },
1751 { type: '/internalList' }
1752 ],
1753 rules: { removeOriginalDomElements: true },
1754 msg: 'Span stripped when removing original DOM elements'
1755 },
1756 {
1757 html: '<p><span style="color:red;"><span style="color:red;">Foo</span></span></p>',
1758 data: [
1759 { type: 'paragraph' },
1760 ...'Foo',
1761 { type: '/paragraph' },
1762 { type: 'internalList' },
1763 { type: '/internalList' }
1764 ],
1765 rules: { removeOriginalDomElements: true },
1766 msg: 'Double annotation sanitized'
1767 },
1768 {
1769 html: '<p><b>1<b>2</b>3</b></p>',
1770 data: [
1771 { type: 'paragraph' },
1772 [ '1', [ 'h49981eab0f8056ff' ] ],
1773 [ '2', [ 'h49981eab0f8056ff' ] ],
1774 [ '3', [ 'h49981eab0f8056ff' ] ],
1775 { type: '/paragraph' },
1776 { type: 'internalList' },
1777 { type: '/internalList' }
1778 ],
1779 rules: { removeOriginalDomElements: true },
1780 msg: 'Double annotation appears just once'
1781 },
1782 {
1783 html: '<p>F<br>o</p><h1>B<br>a</h1><p>B<br></p>',
1784 data: [
1785 { type: 'paragraph' },
1786 'F',
1787 { type: '/paragraph' },
1788 { type: 'paragraph' },
1789 'o',
1790 { type: '/paragraph' },
1791 { type: 'heading', attributes: { level: 1 } },
1792 'B',
1793 { type: '/heading' },
1794 { type: 'heading', attributes: { level: 1 } },
1795 'a',
1796 { type: '/heading' },
1797 { type: 'paragraph' },
1798 'B',
1799 { type: '/paragraph' },
1800 { type: 'internalList' },
1801 { type: '/internalList' }
1802 ],
1803 rules: { removeOriginalDomElements: true },
1804 msg: 'Breaks split content branch nodes'
1805 },
1806 {
1807 html: '<p>Foo\nBar\n <b>Baz \nQ&nbsp;uu\nx</b></p>',
1808 data: [
1809 { type: 'paragraph' },
1810 ...'Foo Bar ',
1811 [ 'B', [ ve.dm.example.annHash( 'b' ) ] ],
1812 [ 'a', [ ve.dm.example.annHash( 'b' ) ] ],
1813 [ 'z', [ ve.dm.example.annHash( 'b' ) ] ],
1814 [ ' ', [ ve.dm.example.annHash( 'b' ) ] ],
1815 [ 'Q', [ ve.dm.example.annHash( 'b' ) ] ],
1816 [ ' ', [ ve.dm.example.annHash( 'b' ) ] ],
1817 [ 'u', [ ve.dm.example.annHash( 'b' ) ] ],
1818 [ 'u', [ ve.dm.example.annHash( 'b' ) ] ],
1819 [ ' ', [ ve.dm.example.annHash( 'b' ) ] ],
1820 [ 'x', [ ve.dm.example.annHash( 'b' ) ] ],
1821 { type: '/paragraph' },
1822 { type: 'internalList' },
1823 { type: '/internalList' }
1824 ],
1825 msg: 'Newline characters and NBSPs are replaced with spaces and/or stripped where necessary'
1826 },
1827 {
1828 html: '<p>Foo\nBar\n </p><pre>Baz \nQu&nbsp;ux<!--comment-->\nWhee</pre><p>A&nbsp;&nbsp;B&nbsp;&nbsp;&nbsp;C</p>',
1829 data: [
1830 { type: 'paragraph' },
1831 ...'Foo Bar',
1832 { type: '/paragraph' },
1833 { type: 'preformatted' },
1834 ...'Baz \nQu\u00a0ux',
1835 { type: 'comment', attributes: { text: 'comment' } },
1836 { type: '/comment' },
1837 '\n', ...'Whee',
1838 { type: '/preformatted' },
1839 { type: 'paragraph' },
1840 ...'A \u00a0B \u00a0 C',
1841 { type: '/paragraph' },
1842 { type: 'internalList' },
1843 { type: '/internalList' }
1844 ],
1845 msg: 'Newline characters and NBSPs are not stripped when they are meaningful'
1846 },
1847 {
1848 html: '<p>Foo</p> \n\t <p>Bar</p>',
1849 data: [
1850 { type: 'paragraph' },
1851 ...'Foo',
1852 { type: '/paragraph' },
1853 { type: 'paragraph' },
1854 ...'Bar',
1855 { type: '/paragraph' },
1856 { type: 'internalList' },
1857 { type: '/internalList' }
1858 ],
1859 msg: 'HTML whitespace is stripped'
1860 },
1861 {
1862 html: '<p>Foo</p> \n\t <p>Bar</p>',
1863 data: [
1864 { type: 'paragraph', internal: { whitespace: [ undefined, undefined, undefined, ' \n\t ' ] } },
1865 ...'Foo',
1866 { type: '/paragraph' },
1867 { type: 'paragraph', internal: { whitespace: [ ' \n\t ' ] } },
1868 ...'Bar',
1869 { type: '/paragraph' },
1870 { type: 'internalList' },
1871 { type: '/internalList' }
1872 ],
1873 rules: { preserveHtmlWhitespace: true },
1874 msg: 'HTML whitespace is preserved when preserveHtmlWhitespace is used'
1875 },
1876 {
1877 html: '<p>Foo</p><p><br /></p><p>Bar</p>',
1878 data: [
1879 { type: 'paragraph' },
1880 ...'Foo',
1881 { type: '/paragraph' },
1882 { type: 'paragraph' },
1883 { type: '/paragraph' },
1884 { type: 'paragraph' },
1885 ...'Bar',
1886 { type: '/paragraph' },
1887 { type: 'internalList' },
1888 { type: '/internalList' }
1889 ],
1890 rules: { keepEmptyContentBranches: true },
1891 msg: 'Blank line (Chrome style) preserved with keepEmptyContentBranches'
1892 },
1893 {
1894 html: '<p>Foo<br /><br />Bar</p>',
1895 data: [
1896 { type: 'paragraph' },
1897 ...'Foo',
1898 { type: '/paragraph' },
1899 { type: 'paragraph' },
1900 { type: '/paragraph' },
1901 { type: 'paragraph' },
1902 ...'Bar',
1903 { type: '/paragraph' },
1904 { type: 'internalList' },
1905 { type: '/internalList' }
1906 ],
1907 rules: { keepEmptyContentBranches: true },
1908 msg: 'Blank line (Firefox style) preserved with keepEmptyContentBranches'
1909 },
1910 {
1911 html: '<p>A<meta foo="bar" />B</p>',
1912 data: [
1913 { type: 'paragraph' },
1914 ...'AB',
1915 { type: '/paragraph' },
1916 { type: 'internalList' },
1917 { type: '/internalList' }
1918 ],
1919 rules: {},
1920 msg: 'Metadata is sanitized if allowMetadata is false'
1921 },
1922 {
1923 html: '<p>A<meta foo="bar" />B</p>',
1924 data: [
1925 { type: 'paragraph' },
1926 ...'AB',
1927 { type: '/paragraph' },
1928 {
1929 type: 'alienMeta',
1930 internal: {
1931 loadMetaParentHash: 'h069d094b1fb89d8a',
1932 loadMetaParentOffset: 1
1933 }
1934 },
1935 { type: '/alienMeta' },
1936 { type: 'internalList' },
1937 { type: '/internalList' }
1938 ],
1939 rules: { allowMetadata: true },
1940 msg: 'Metadata is not sanitized if allowMetadata is true'
1941 }
1942 ];
1943
1944 cases.forEach( ( caseItem ) => {
1945 const model = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( caseItem.html ) );
1946 const data = model.data;
1947 data.sanitize( caseItem.rules || {} );
1948 assert.equalLinearData( data.data, caseItem.data, caseItem.msg + ': data' );
1949 if ( caseItem.store ) {
1950 const actualStore = {};
1951 for ( const key in caseItem.store ) {
1952 actualStore[ key ] = data.getStore().value( key ).element;
1953 }
1954 assert.deepEqualWithDomElements( actualStore, caseItem.store, caseItem.msg + ': store' );
1955 }
1956 } );
1957 } );
1958
1959 QUnit.test( 'countNonInternalElements', ( assert ) => {
1960 const cases = [
1961 {
1962 data: [
1963 { type: 'paragraph' },
1964 'F', [ 'o', [ 0 ] ], 'o',
1965 { type: '/paragraph' },
1966 { type: 'internalList' },
1967 { type: '/internalList' }
1968 ],
1969 expected: 5,
1970 msg: 'Counting non-internal elements - no internal data'
1971 },
1972 {
1973 data: [
1974 { type: 'paragraph' },
1975 ...'Fo',
1976 { type: '/paragraph' },
1977 { type: 'internalList' },
1978 { type: 'internalItem' },
1979 { type: 'paragraph' },
1980 'a',
1981 { type: '/paragraph' },
1982 { type: '/internalItem' },
1983 { type: '/internalList' }
1984 ],
1985 expected: 4,
1986 msg: 'Counting non-internal elements'
1987 }
1988 ];
1989
1990 cases.forEach( ( caseItem ) => {
1991 const data = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), caseItem.data );
1992 assert.strictEqual( data.countNonInternalElements(), caseItem.expected, caseItem.msg );
1993 } );
1994 } );
1995
1996 QUnit.test( 'hasContent', ( assert ) => {
1997 const cases = [
1998 {
1999 data: [],
2000 expected: false,
2001 msg: 'Completely empty document has no content'
2002 },
2003 {
2004 data: [
2005 { type: 'internalList' },
2006 { type: '/internalList' }
2007 ],
2008 expected: false,
2009 msg: 'Internal list only document has no content'
2010 },
2011 {
2012 data: [
2013 { type: 'paragraph' },
2014 { type: '/paragraph' },
2015 { type: 'internalList' },
2016 { type: '/internalList' }
2017 ],
2018 expected: false,
2019 msg: 'Real world empty document has no content'
2020 },
2021 {
2022 data: [
2023 { type: 'paragraph' },
2024 'F', [ 'o', [ 0 ] ], 'o',
2025 { type: '/paragraph' },
2026 { type: 'internalList' },
2027 { type: '/internalList' }
2028 ],
2029 expected: true,
2030 msg: 'Document with text has content'
2031 },
2032 {
2033 data: [
2034 { type: 'alienBlock' },
2035 { type: '/alienBlock' },
2036 { type: 'internalList' },
2037 { type: '/internalList' }
2038 ],
2039 expected: true,
2040 msg: 'Document with non-ContentBranchNode (alienBlock) node has content'
2041 },
2042 {
2043 data: [
2044 { type: 'paragraph' },
2045 { type: '/paragraph' },
2046 { type: 'internalList' },
2047 { type: 'internalItem' },
2048 { type: 'paragraph' },
2049 'a',
2050 { type: '/paragraph' },
2051 { type: '/internalItem' },
2052 { type: '/internalList' }
2053 ],
2054 expected: false,
2055 msg: 'Empty document with internal data has no content'
2056 }
2057 ];
2058
2059 cases.forEach( ( caseItem ) => {
2060 const data = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), caseItem.data );
2061 assert.strictEqual( data.hasContent(), caseItem.expected, caseItem.msg );
2062 } );
2063 } );
2064
2065 QUnit.test( 'getAnnotationHashesFromOffset', ( assert ) => {
2066 const boldHash = 'h49981eab0f8056ff',
2067 italicHash = 'hefd27ef3bf2041dd',
2068 linearData = ve.dm.example.preprocessAnnotations( ve.copy( ve.dm.example.data ) ),
2069 elementData = new ve.dm.ElementLinearData( linearData.getStore(), linearData.getData() ),
2070 cases = [
2071 {
2072 msg: '0 has no annotations',
2073 offset: 0,
2074 ignoreClose: true,
2075 expected: []
2076 },
2077 {
2078 msg: '2 has a BoldAnnotation',
2079 offset: 2,
2080 ignoreClose: true,
2081 expected: [ boldHash ]
2082 },
2083 {
2084 msg: '3 contains an ItalicAnnotation',
2085 offset: 3,
2086 ignoreClose: true,
2087 expected: [ italicHash ]
2088 }
2089 ],
2090 errorCases = [
2091 {
2092 msg: '-1 throws as out of bounds',
2093 offset: -1,
2094 ignoreClose: true,
2095 exception: ''
2096 },
2097 {
2098 msg: '64 throws as out of bounds',
2099 offset: 64,
2100 ignoreClose: true,
2101 exception: ''
2102 }
2103 ];
2104
2105 cases.forEach( ( caseItem ) => {
2106 assert.deepEqual(
2107 elementData.getAnnotationHashesFromOffset( caseItem.offset, caseItem.ignoreClose ),
2108 caseItem.expected,
2109 caseItem.msg
2110 );
2111 } );
2112
2113 errorCases.forEach( ( errorCaseItem ) => {
2114 assert.throws(
2115 () => {
2116 elementData.getAnnotationHashesFromOffset( errorCaseItem.offset, errorCaseItem.ignoreClose );
2117 }, new Error( 'offset ' + errorCaseItem.offset + ' out of bounds' ),
2118 errorCaseItem.msg
2119 );
2120 } );
2121 } );
2122
2123 QUnit.test( 'getUsedStoreValues', ( assert ) => {
2124 const bold = new ve.dm.BoldAnnotation( { type: 'textStyle/bold', attributes: { nodeName: 'b' } } ),
2125 italic = new ve.dm.ItalicAnnotation( { type: 'textStyle/italic', attributes: { nodeName: 'i' } } ),
2126 linearData = ve.dm.example.preprocessAnnotations( ve.copy( ve.dm.example.data ) ),
2127 elementData = new ve.dm.ElementLinearData( linearData.getStore(), linearData.getData() ),
2128 cases = [
2129 {
2130 msg: 'no range (whole document) contains everything',
2131 expected: {
2132 h49981eab0f8056ff: bold,
2133 hefd27ef3bf2041dd: italic
2134 }
2135 },
2136 {
2137 msg: '2-4 contains bold and italic',
2138 range: new ve.Range( 2, 4 ),
2139 expected: {
2140 h49981eab0f8056ff: bold,
2141 hefd27ef3bf2041dd: italic
2142 }
2143 },
2144 {
2145 msg: '2-3 contains bold',
2146 range: new ve.Range( 2, 3 ),
2147 expected: {
2148 h49981eab0f8056ff: bold
2149 }
2150 },
2151 {
2152 msg: '3-4 contains italic',
2153 range: new ve.Range( 3, 4 ),
2154 expected: {
2155 hefd27ef3bf2041dd: italic
2156 }
2157 },
2158 {
2159 msg: '5-10 contains nothing',
2160 range: new ve.Range( 5, 10 ),
2161 expected: {}
2162 }
2163 ];
2164
2165 function getElement( ann ) {
2166 return ann.element;
2167 }
2168
2169 cases.forEach( ( caseItem ) => {
2170 assert.deepEqual(
2171 ve.copy( elementData.getUsedStoreValues( caseItem.range ), getElement ),
2172 ve.copy( caseItem.expected, getElement ),
2173 caseItem.msg
2174 );
2175 } );
2176
2177 } );
2178
2179 QUnit.test( 'compareElements and compareElementsUnannotated', ( assert ) => {
2180 const store = new ve.dm.HashValueStore(),
2181 cases = [
2182 {
2183 a: '母',
2184 b: '母',
2185 comparison: true,
2186 msg: 'Identical unannotated characters'
2187 },
2188 {
2189 a: '다',
2190 b: '가',
2191 comparison: false,
2192 msg: 'Non-identical unannotated characters'
2193 },
2194 {
2195 a: [ 'F', [ ve.dm.example.boldHash ] ],
2196 b: [ 'F', [ ve.dm.example.boldHash ] ],
2197 comparison: true,
2198 msg: 'Identically-annotated identical characters'
2199 },
2200 {
2201 a: [ 'F', [ ve.dm.example.boldHash ] ],
2202 b: [ 'F', [ ve.dm.example.italicHash ] ],
2203 comparison: false,
2204 comparisonUnannotated: true,
2205 msg: 'Identical characters, differently-annotated'
2206 },
2207 {
2208 a: [ 'F', [ ve.dm.example.boldHash ] ],
2209 b: [ 'F', [ ve.dm.example.strongHash ] ],
2210 comparison: true,
2211 msg: 'Identical characters, comparably-annotated'
2212 },
2213 {
2214 a: [ 'F', [ ve.dm.example.boldHash ] ],
2215 b: [ 'G', [ ve.dm.example.boldHash ] ],
2216 comparison: false,
2217 msg: 'Different characters, identically-annotated'
2218 },
2219 {
2220 a: [ 'F', [ ve.dm.example.boldHash ] ],
2221 b: [ 'G', [ ve.dm.example.strongHash ] ],
2222 comparison: false,
2223 msg: 'Different characters, comparably-annotated'
2224 },
2225 {
2226 a: 'F',
2227 b: [ 'G', [ ve.dm.example.boldHash ] ],
2228 comparison: false,
2229 msg: 'Different characters, one annotated, one not'
2230 },
2231 {
2232 a: 'F',
2233 b: [ 'F', [ ve.dm.example.boldHash ] ],
2234 comparison: false,
2235 comparisonUnannotated: true,
2236 msg: 'Identical characters, one annotated, one not'
2237 },
2238 {
2239 a: { type: 'paragraph' },
2240 b: 'F',
2241 comparison: false,
2242 msg: 'Element with character'
2243 },
2244 {
2245 a: { type: 'paragraph' },
2246 b: [ 'F', [ ve.dm.example.boldHash ] ],
2247 comparison: false,
2248 msg: 'Element with annotated character'
2249 },
2250 {
2251 a: { type: 'paragraph' },
2252 b: { type: 'paragraph' },
2253 comparison: true,
2254 msg: 'Identical opening paragraphs'
2255 },
2256 {
2257 a: { type: 'inlineImage', annotations: [ ve.dm.example.boldHash ] },
2258 b: { type: 'inlineImage', annotations: [ ve.dm.example.boldHash ] },
2259 comparison: true,
2260 msg: 'Identical elements, identically-annotated'
2261 },
2262 {
2263 a: { type: 'inlineImage', annotations: [ ve.dm.example.boldHash ] },
2264 b: { type: 'inlineImage', annotations: [ ve.dm.example.strongHash ] },
2265 comparison: true,
2266 msg: 'Identical elements, comparably-annotated'
2267 },
2268 {
2269 a: { type: 'inlineImage', annotations: [ ve.dm.example.boldHash ] },
2270 b: { type: 'inlineImage', annotations: [ ve.dm.example.italicHash ] },
2271 comparison: false,
2272 comparisonUnannotated: true,
2273 msg: 'Identical elements, differently-annotated'
2274 },
2275 {
2276 a: { type: 'inlineImage', annotations: [ ve.dm.example.boldHash ] },
2277 b: { type: 'inlineImage' },
2278 comparison: false,
2279 comparisonUnannotated: true,
2280 msg: 'Identical elements, one annotated, one not'
2281 },
2282 {
2283 a: { type: 'heading' },
2284 b: { type: 'heading' },
2285 comparison: true,
2286 msg: 'Identical opening elements'
2287 },
2288 {
2289 a: { type: 'heading' },
2290 b: { type: '/heading' },
2291 comparison: false,
2292 msg: 'Matching opening and closing elements'
2293 },
2294 {
2295 a: { type: 'heading', attributes: { level: 3 } },
2296 b: { type: 'heading', attributes: { level: 3 } },
2297 comparison: true,
2298 msg: 'Identical elements with identical attributes'
2299 },
2300 {
2301 a: { type: 'heading', attributes: { level: 3 } },
2302 b: { type: 'heading', attributes: { level: 2 } },
2303 comparison: false,
2304 comparisonForTranslate: true,
2305 msg: 'Identical elements with non-identical attributes'
2306 },
2307 {
2308 a: { type: 'heading', attributes: { level: 3 } },
2309 b: { type: 'heading' },
2310 comparison: false,
2311 comparisonForTranslate: true,
2312 msg: 'Identical elements, one without an attribute'
2313 }
2314 ];
2315
2316 store.hash( new ve.dm.BoldAnnotation( ve.dm.example.bold ) );
2317 store.hash( new ve.dm.BoldAnnotation( ve.dm.example.strong ) );
2318 store.hash( new ve.dm.ItalicAnnotation( ve.dm.example.italic ) );
2319
2320 cases.forEach( ( caseItem ) => {
2321 assert.strictEqual(
2322 ve.dm.ElementLinearData.static.compareElements( caseItem.a, caseItem.b, store ),
2323 caseItem.comparison,
2324 caseItem.msg
2325 );
2326 assert.strictEqual(
2327 ve.dm.ElementLinearData.static.compareElementsUnannotated( caseItem.a, caseItem.b ),
2328 caseItem.comparisonUnannotated || caseItem.comparison,
2329 caseItem.msg + ' (unannotated)'
2330 );
2331 assert.strictEqual(
2332 ve.dm.Transaction.static.compareElementsForTranslate( caseItem.a, caseItem.b ),
2333 caseItem.comparisonForTranslate || caseItem.comparisonUnannotated || caseItem.comparison,
2334 caseItem.msg + ' (compareElementsForTranslate)'
2335 );
2336 } );
2337 } );
2338
2339 // TODO: ve.dm.ElementLinearData#setAnnotationsAtOffset
2340 // TODO: ve.dm.ElementLinearData#getCharacterData
2341 // TODO: ve.dm.ElementLinearData#getAnnotatedRangeFromSelection
2342 // TODO: ve.dm.ElementLinearData#getNearestContentOffset
2343 // TODO: ve.dm.ElementLinearData#remapInternalListIndexes
2344 // TODO: ve.dm.ElementLinearData#remapInternalListKeys
2345 // TODO: ve.dm.ElementLinearData#cloneElements