Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor Base method tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've' );
8
9 /* Tests */
10
11 // ve.getProp: Tested upstream (OOjs)
12
13 // ve.setProp: Tested upstream (OOjs)
14
15 // ve.cloneObject: Tested upstream (OOjs)
16
17 // ve.getObjectValues: Tested upstream (OOjs)
18
19 // ve.compare: Tested upstream (OOjs)
20
21 // ve.copy: Tested upstream (OOjs)
22
23 // ve.isPlainObject: Tested upstream (jQuery)
24
25 // ve.isEmptyObject: Tested upstream (jQuery)
26
27 // ve.extendObject: Tested upstream (jQuery)
28
29 QUnit.test( 'compareClassLists', ( assert ) => {
30 const cases = [
31 {
32 args: [ '', '' ],
33 expected: true
34 },
35 {
36 args: [ '', [] ],
37 expected: true
38 },
39 {
40 args: [ [], [] ],
41 expected: true
42 },
43 {
44 args: [ '', [ '' ] ],
45 expected: true
46 },
47 {
48 args: [ [], [ '' ] ],
49 expected: true
50 },
51 {
52 args: [ 'foo', '' ],
53 expected: false
54 },
55 {
56 args: [ 'foo', 'foo' ],
57 expected: true
58 },
59 {
60 args: [ 'foo', 'bar' ],
61 expected: false
62 },
63 {
64 args: [ 'foo', 'foo bar' ],
65 expected: false
66 },
67 {
68 args: [ 'foo', [ 'foo' ] ],
69 expected: true
70 },
71 {
72 args: [ [ 'foo' ], 'bar' ],
73 expected: false
74 },
75 {
76 args: [ 'foo', [ 'foo', 'bar' ] ],
77 expected: false
78 },
79 {
80 args: [ 'foo', [ 'foo', 'foo' ] ],
81 expected: true
82 },
83 {
84 args: [ [ 'foo' ], 'foo foo' ],
85 expected: true
86 },
87 {
88 args: [ 'foo bar foo', 'foo foo' ],
89 expected: false
90 }
91 ];
92
93 cases.forEach( ( caseItem ) => {
94 assert.strictEqual( ve.compareClassLists.apply( ve, caseItem.args ), caseItem.expected );
95 } );
96 } );
97
98 QUnit.test( 'isInstanceOfAny', ( assert ) => {
99 function Foo() {}
100 OO.initClass( Foo );
101
102 function Bar() {}
103 OO.initClass( Bar );
104
105 function SpecialFoo() {}
106 OO.inheritClass( SpecialFoo, Foo );
107
108 function VerySpecialFoo() {}
109 OO.inheritClass( VerySpecialFoo, SpecialFoo );
110
111 assert.strictEqual(
112 ve.isInstanceOfAny( new Foo(), [ Foo ] ),
113 true,
114 'Foo is an instance of Foo'
115 );
116
117 assert.strictEqual(
118 ve.isInstanceOfAny( new SpecialFoo(), [ Foo ] ),
119 true,
120 'SpecialFoo is an instance of Foo'
121 );
122
123 assert.strictEqual(
124 ve.isInstanceOfAny( new SpecialFoo(), [ Bar ] ),
125 false,
126 'SpecialFoo is not an instance of Bar'
127 );
128
129 assert.strictEqual(
130 ve.isInstanceOfAny( new SpecialFoo(), [ Bar, Foo ] ),
131 true,
132 'SpecialFoo is an instance of Bar or Foo'
133 );
134
135 assert.strictEqual(
136 ve.isInstanceOfAny( new VerySpecialFoo(), [ Bar, Foo ] ),
137 true,
138 'VerySpecialFoo is an instance of Bar or Foo'
139 );
140
141 assert.strictEqual(
142 ve.isInstanceOfAny( new VerySpecialFoo(), [ Foo, SpecialFoo ] ),
143 true,
144 'VerySpecialFoo is an instance of Foo or SpecialFoo'
145 );
146
147 assert.strictEqual(
148 ve.isInstanceOfAny( new VerySpecialFoo(), [] ),
149 false,
150 'VerySpecialFoo is not an instance of nothing'
151 );
152 } );
153
154 QUnit.test( 'getDomAttributes', ( assert ) => {
155 assert.deepEqual(
156 ve.getDomAttributes( $.parseHTML( '<div string="foo" empty number="0"></div>' )[ 0 ] ),
157 { string: 'foo', empty: '', number: '0' },
158 'getDomAttributes() returns object with correct attributes'
159 );
160 } );
161
162 QUnit.test( 'setDomAttributes', ( assert ) => {
163 const sample = $.parseHTML( '<div foo="one" bar="two" baz="three"></div>' )[ 0 ];
164
165 let target = {};
166 ve.setDomAttributes( target, { add: 'foo' } );
167 assert.deepEqual( target, {}, 'ignore incompatible target object' );
168
169 target = document.createElement( 'div' );
170 ve.setDomAttributes( target, { string: 'foo', empty: '', number: 0 } );
171 assert.deepEqual(
172 ve.getDomAttributes( target ),
173 { string: 'foo', empty: '', number: '0' },
174 'add attributes'
175 );
176
177 target = sample.cloneNode();
178 ve.setDomAttributes( target, { foo: null, bar: 'update', baz: undefined, add: 'yay' } );
179 assert.deepEqual(
180 ve.getDomAttributes( target ),
181 { bar: 'update', add: 'yay' },
182 'add, update, and remove attributes'
183 );
184
185 target = sample.cloneNode();
186 ve.setDomAttributes( target, { onclick: 'alert(1);', foo: 'update', add: 'whee' }, [ 'foo', 'add' ] );
187 assert.strictEqual( target.hasAttribute( 'onclick' ), false, 'allow list affects creating attributes' );
188 assert.deepEqual(
189 ve.getDomAttributes( target ),
190 { foo: 'update', bar: 'two', baz: 'three', add: 'whee' },
191 'allow list does not affect pre-existing attributes'
192 );
193
194 target = document.createElement( 'div' );
195 ve.setDomAttributes( target, { Foo: 'add', Bar: 'add' }, [ 'bar' ] );
196 assert.deepEqual(
197 ve.getDomAttributes( target ),
198 { bar: 'add' },
199 'allow list is case-insensitive'
200 );
201
202 target = sample.cloneNode();
203 ve.setDomAttributes( target, { foo: 'update', bar: null }, [ 'bar', 'baz' ] );
204 assert.propEqual(
205 ve.getDomAttributes( target ),
206 { foo: 'one', baz: 'three' },
207 'allow list affects removal/updating of attributes'
208 );
209 } );
210
211 QUnit.test( 'sparseSplice', ( assert ) => {
212 // Convert a sparse array of primitives to an array of strings, with '' for holes.
213 // This is needed because QUnit.equiv treats holes as equivalent to undefined.
214 function mapToString( flatArray ) {
215 const strings = [];
216 for ( let j = 0, jLen = flatArray.length; j < jLen; j++ ) {
217 strings.push( Object.prototype.hasOwnProperty.call( flatArray, j ) ? String( flatArray[ j ] ) : '' );
218 }
219 return strings;
220 }
221 function runTest( arr, offset, remove, data, expectedReturn, expectedArray, msg ) {
222 const testArr = arr.slice();
223
224 const observedReturn = ve.sparseSplice( testArr, offset, remove, data );
225 assert.deepEqual(
226 mapToString( observedReturn ),
227 mapToString( expectedReturn ),
228 msg + ': return'
229 );
230 assert.deepEqual(
231 mapToString( testArr ),
232 mapToString( expectedArray ),
233 msg + ': modification'
234 );
235 }
236 /* eslint-disable no-sparse-arrays */
237 const scratch = [ 4, , 5, , 6 ];
238 const cases = [
239 // arr, offset, remove, data, expectedReturn, expectedArray, msg
240 [ [], 0, 0, [ , 3 ], [], [ , 3 ], 'insert empty, leading hole' ],
241 [ [], 0, 0, [ 1, , 3 ], [], [ 1, , 3 ], 'insert empty, middle hole' ],
242 // Note: the first trailing comma does not create a hole
243 [ [], 0, 0, [ 1, , ], [], [ 1, , ], 'insert empty, trailing hole' ],
244 [ [ 4, , 5 ], 0, 0, [ 1, , 3 ], [], [ 1, , 3, 4, , 5 ], 'insert start' ],
245 [ [ 0, , 4 ], 1, 0, [ 1, , 3 ], [], [ 0, 1, , 3, , 4 ], 'insert mid' ],
246 [ [ 0, , 4 ], 3, 0, [ 1, , 3 ], [], [ 0, , 4, 1, , 3 ], 'insert end' ],
247
248 [ [ 4, , 5, , 6 ], 0, 4, [ 1, , 3 ], [ 4, , 5, , ], [ 1, , 3, 6 ], 'diff<0 start' ],
249 [ [ 4, , , 5, , 6 ], 1, 4, [ 1, , 3 ], [ , , 5, , ], [ 4, 1, , 3, 6 ], 'diff<0 mid' ],
250 [ [ 4, , 5, , 6 ], 1, 4, [ 1, , 3 ], [ , 5, , 6 ], [ 4, 1, , 3 ], 'diff<0 end' ],
251
252 [ [ 4, , 5, , 6 ], 0, 2, [ 1, , 3 ], [ 4, , ], [ 1, , 3, 5, , 6 ], 'diff>0 start' ],
253 [ [ 4, , 5, , 6 ], 1, 2, [ 1, , 3 ], [ , 5 ], [ 4, 1, , 3, , 6 ], 'diff>0 mid' ],
254 [ [ 4, , 5, , 6 ], 3, 2, [ 1, , 3 ], [ , 6 ], [ 4, , 5, 1, , 3 ], 'diff>0 end' ],
255
256 [ [ 4, , 5, , 6 ], 0, 3, [ 1, , 3 ], [ 4, , 5 ], [ 1, , 3, , 6 ], 'diff=0 start' ],
257 [ [ 4, , 5, , 6 ], 1, 3, [ 1, , 3 ], [ , 5, , ], [ 4, 1, , 3, 6 ], 'diff=0 mid' ],
258 [ [ 4, , 5, , 6 ], 2, 3, [ 1, , 3 ], [ 5, , 6 ], [ 4, , 1, , 3 ], 'diff=0 end' ],
259 [ scratch, 0, 0, scratch, [], [ 4, , 5, , 6, 4, , 5, , 6 ], 'reference-identical arr and data' ]
260 ];
261 /* eslint-enable no-sparse-arrays */
262
263 assert.notDeepEqual(
264 // eslint-disable-next-line no-sparse-arrays
265 mapToString( [ 1, , ] ),
266 mapToString( [ 1, undefined ] ),
267 'holes look different to undefined'
268 );
269 cases.forEach( ( caseItem ) => {
270 runTest.apply( null, caseItem );
271 } );
272 } );
273
274 QUnit.test( 'batchSplice', ( assert ) => {
275 const actual = [ ...'abcde' ],
276 expected = actual.slice( 0 );
277
278 let actualRet = ve.batchSplice( actual, 1, 1, [] );
279 let expectedRet = expected.splice( 1, 1 );
280 assert.deepEqual( expectedRet, actualRet, 'Removing 1 element (return value)' );
281 assert.deepEqual( expected, actual, 'Removing 1 element (array)' );
282
283 actualRet = ve.batchSplice( actual, 3, 2, [ ...'wxyz' ] );
284 expectedRet = expected.splice( 3, 2, ...'wxyz' );
285 assert.deepEqual( expectedRet, actualRet, 'Replacing 2 elements with 4 elements (return value)' );
286 assert.deepEqual( expected, actual, 'Replacing 2 elements with 4 elements (array)' );
287
288 actualRet = ve.batchSplice( actual, 0, 0, [ ...'Foo' ] );
289 expectedRet = expected.splice( 0, 0, ...'Foo' );
290 assert.deepEqual( expectedRet, actualRet, 'Inserting 3 elements (return value)' );
291 assert.deepEqual( expected, actual, 'Inserting 3 elements (array)' );
292
293 const bigArr = [];
294 for ( let i = 0; i < 2100; i++ ) {
295 bigArr[ i ] = i;
296 }
297 actualRet = ve.batchSplice( actual, 2, 3, bigArr );
298 expectedRet = expected.splice( 2, 3, ...bigArr.slice( 0, 1050 ) );
299 expected.splice( 1052, 0, ...bigArr.slice( 1050 ) );
300 assert.deepEqual( expectedRet, actualRet, 'Replacing 3 elements with 2100 elements (return value)' );
301 assert.deepEqual( expected, actual, 'Replacing 3 elements with 2100 elements (array)' );
302 } );
303
304 QUnit.test( 'batchPush', ( assert ) => {
305
306 let actual = [];
307 let actualRet = ve.batchPush( actual, [ 1, 2, 3 ] );
308 assert.strictEqual( actualRet, 3, 'Adding to an empty array: return' );
309 assert.deepEqual( actual, [ 1, 2, 3 ], 'Adding to an empty array: value' );
310
311 actual = [ 1 ];
312 actualRet = ve.batchPush( actual, [ 1, 2, 3 ] );
313 assert.strictEqual( actualRet, 4, 'Adding to a non-empty array: return' );
314 assert.deepEqual( actual, [ 1, 1, 2, 3 ], 'Adding to a non-empty array: value' );
315
316 // batchPush takes a separate codepath for really long arrays, make sure it's behaving similarly:
317
318 const bigArr = [];
319 for ( let i = 0; i < 2100; i++ ) {
320 bigArr[ i ] = i;
321 }
322
323 actual = [ 'a' ];
324 actualRet = ve.batchPush( actual, bigArr );
325 assert.strictEqual( actualRet, 2101, 'Adding a huge array: return' );
326 assert.strictEqual( actual[ 0 ], 'a', 'Adding a huge array: first value' );
327 assert.strictEqual( actual[ actual.length - 1 ], 2099, 'Adding a huge array: last value' );
328 } );
329
330 QUnit.test( 'insertIntoArray', ( assert ) => {
331 let target = [ ...'abc' ];
332 ve.insertIntoArray( target, 0, [ ...'xy' ] );
333 assert.deepEqual( target, [ ...'xyabc' ], 'insert at start' );
334
335 target = [ ...'abc' ];
336 ve.insertIntoArray( target, 2, [ ...'xy' ] );
337 assert.deepEqual( target, [ ...'abxyc' ], 'insert into the middle' );
338
339 target = [ ...'abc' ];
340 ve.insertIntoArray( target, 10, [ ...'xy' ] );
341 assert.deepEqual( target, [ ...'abcxy' ], 'insert beyond end' );
342 } );
343
344 QUnit.test( 'escapeHtml', ( assert ) => {
345 assert.strictEqual( ve.escapeHtml( ' "script\' <foo & bar> ' ), ' &quot;script&#039; &lt;foo &amp; bar&gt; ' );
346 } );
347
348 QUnit.test( 'addHeadTag', ( assert ) => {
349 const cases = [
350 {
351 msg: 'no wrapper',
352 html: '<p>foo</p>',
353 expected: '<head><meta foo/></head><p>foo</p>'
354 },
355 {
356 msg: 'body only',
357 html: '<body><p>foo</p></body>',
358 expected: '<head><meta foo/></head><body><p>foo</p></body>'
359 },
360 {
361 msg: 'head & body',
362 html: '<head></head><body><p>foo</p></body>',
363 expected: '<head><meta foo/></head><body><p>foo</p></body>'
364 },
365 {
366 msg: 'html & body',
367 html: '<html><body><p>foo</p></body></html>',
368 expected: '<html><head><meta foo/></head><body><p>foo</p></body></html>'
369 },
370 {
371 msg: 'html, head & body',
372 html: '<html><head></head><body><p>foo</p></body></html>',
373 expected: '<html><head><meta foo/></head><body><p>foo</p></body></html>'
374 }
375 ];
376
377 cases.forEach( ( caseItem ) => {
378 assert.strictEqual( ve.addHeadTag( caseItem.html, '<meta foo/>' ), caseItem.expected, caseItem.msg );
379 } );
380 } );
381
382 QUnit.test( 'createDocumentFromHtml', ( assert ) => {
383 const cases = [
384 {
385 msg: 'simple document with doctype, head and body',
386 html: '<!doctype html><html lang="en"><head><title>Foo</title></head><body><p>Bar</p></body></html>',
387 head: '<title>Foo</title>',
388 body: '<p>Bar</p>',
389 htmlAttributes: {
390 lang: 'en'
391 }
392 },
393 {
394 msg: 'simple document without doctype',
395 html: '<html lang="en"><head><title>Foo</title></head><body><p>Bar</p></body></html>',
396 head: '<title>Foo</title>',
397 body: '<p>Bar</p>',
398 htmlAttributes: {
399 lang: 'en'
400 }
401 },
402 {
403 msg: 'document with missing closing tags and missing <html> tag',
404 html: '<!doctype html><head><title>Foo</title><base href="yay"><body><p>Bar<b>Baz',
405 head: '<title>Foo</title><base href="yay" />',
406 body: '<p>Bar<b>Baz</b></p>',
407 htmlAttributes: {}
408 },
409 {
410 msg: 'empty string results in empty document',
411 html: '',
412 head: '',
413 body: '',
414 htmlAttributes: {}
415 },
416 {
417 msg: 'meta tag stays in body',
418 html: '<meta/><p>foo</p>',
419 head: '',
420 body: '<meta/><p>foo</p>',
421 htmlAttributes: {}
422 },
423 {
424 msg: 'body wrapping not broken by custom tag',
425 html: '<meta/><p>foo<bodything/></p>',
426 head: '',
427 body: '<meta/><p>foo<bodything/></p>',
428 htmlAttributes: {}
429 }
430 ];
431
432 cases.forEach( ( caseItem ) => {
433 const doc = ve.createDocumentFromHtml( caseItem.html, true );
434 const attributes = $( 'html', doc ).get( 0 ).attributes;
435 const attributesObject = {};
436 for ( let i = 0; i < attributes.length; i++ ) {
437 attributesObject[ attributes[ i ].name ] = attributes[ i ].value;
438 }
439 const expectedHead = $( '<head>' ).html( caseItem.head ).get( 0 );
440 const expectedBody = $( '<body>' ).html( caseItem.body ).get( 0 );
441 assert.equalDomElement( $( 'head', doc ).get( 0 ), expectedHead, caseItem.msg + ' (head)' );
442 assert.equalDomElement( $( 'body', doc ).get( 0 ), expectedBody, caseItem.msg + ' (body)' );
443 assert.deepEqual( attributesObject, caseItem.htmlAttributes, caseItem.msg + ' (html attributes)' );
444 } );
445 } );
446
447 QUnit.test( 'resolveUrl', ( assert ) => {
448 const cases = [
449 {
450 base: 'http://example.com',
451 href: 'foo',
452 resolved: 'http://example.com/foo',
453 msg: 'Simple href with domain as base'
454 },
455 {
456 base: 'http://example.com/bar',
457 href: 'foo',
458 resolved: 'http://example.com/foo',
459 msg: 'Simple href with page as base'
460 },
461 {
462 base: 'http://example.com/bar/',
463 href: 'foo',
464 resolved: 'http://example.com/bar/foo',
465 msg: 'Simple href with directory as base'
466 },
467 {
468 base: 'http://example.com/bar/',
469 href: './foo',
470 resolved: 'http://example.com/bar/foo',
471 msg: './ in href'
472 },
473 {
474 base: 'http://example.com/bar/',
475 href: '../foo',
476 resolved: 'http://example.com/foo',
477 msg: '../ in href'
478 },
479 {
480 base: 'http://example.com/bar/',
481 href: '/foo',
482 resolved: 'http://example.com/foo',
483 msg: 'href starting with /'
484 },
485 {
486 base: 'http://example.com/bar/',
487 href: '//example.org/foo',
488 resolved: 'http://example.org/foo',
489 msg: 'protocol-relative href'
490 },
491 {
492 base: 'http://example.com/bar/',
493 href: 'https://example.org/foo',
494 resolved: 'https://example.org/foo',
495 msg: 'href with protocol'
496 }
497 ];
498
499 cases.forEach( ( caseItem ) => {
500 const doc = ve.createDocumentFromHtml( '' );
501 doc.head.appendChild( $( '<base>', doc ).attr( 'href', caseItem.base )[ 0 ] );
502 assert.strictEqual( ve.resolveUrl( caseItem.href, doc ), caseItem.resolved, caseItem.msg );
503 } );
504 } );
505
506 QUnit.test( 'resolveAttributes', ( assert ) => {
507 const cases = [
508 {
509 base: 'http://example.com',
510 html: '<div><a href="foo">foo</a></div><a href="bar">bar</a><img src="baz">',
511 resolved: '<div><a href="http://example.com/foo">foo</a></div><a href="http://example.com/bar">bar</a><img src="http://example.com/baz">',
512 msg: 'href and src resolved'
513 },
514 {
515 base: 'http://example.com',
516 html: '<a href="foo">foo</a>',
517 resolved: '<a href="http://example.com/foo">foo</a>',
518 msg: 'href resolved on self (unwrapped)'
519 }
520 ];
521
522 cases.forEach( ( caseItem ) => {
523 const doc = ve.createDocumentFromHtml( '' );
524 doc.head.appendChild( $( '<base>', doc ).attr( 'href', caseItem.base )[ 0 ] );
525 const div = document.createElement( 'div' );
526 div.innerHTML = caseItem.html;
527 ve.resolveAttributes( div.childNodes, doc, ve.dm.Converter.static.computedAttributes );
528 assert.strictEqual(
529 div.innerHTML,
530 caseItem.resolved,
531 caseItem.msg
532 );
533 } );
534 } );
535
536 QUnit.test( 'fixBase', ( assert ) => {
537 const cases = [
538 {
539 targetBase: '//example.org/foo',
540 sourceBase: 'https://example.com',
541 fixedBase: 'https://example.org/foo',
542 msg: 'Protocol-relative base is made absolute'
543 },
544 {
545 targetBase: 'http://example.org/foo',
546 sourceBase: 'https://example.com',
547 fixedBase: 'http://example.org/foo',
548 msg: 'Fully specified base is left alone'
549 },
550 {
551 // No targetBase
552 sourceBase: 'https://example.com',
553 fallbackBase: 'https://example.org/foo',
554 fixedBase: 'https://example.org/foo',
555 msg: 'When base is missing, fallback base is used'
556 }
557 ];
558
559 cases.forEach( ( caseItem ) => {
560 const targetDoc = ve.createDocumentFromHtml( '' );
561 const sourceDoc = ve.createDocumentFromHtml( '' );
562 let expectedBase = caseItem.fixedBase;
563 if ( caseItem.targetBase ) {
564 targetDoc.head.appendChild( $( '<base>', targetDoc ).attr( 'href', caseItem.targetBase )[ 0 ] );
565 if ( targetDoc.baseURI ) {
566 // baseURI is valid, so we expect it to be untouched
567 expectedBase = targetDoc.baseURI;
568 }
569 }
570 if ( caseItem.sourceBase ) {
571 sourceDoc.head.appendChild( $( '<base>', sourceDoc ).attr( 'href', caseItem.sourceBase )[ 0 ] );
572 }
573 ve.fixBase( targetDoc, sourceDoc, caseItem.fallbackBase );
574 assert.strictEqual( targetDoc.baseURI, expectedBase, caseItem.msg );
575 } );
576 } );
577
578 QUnit.test( 'isUriComponentValid', ( assert ) => {
579 assert.strictEqual( ve.isUriComponentValid( 'Foo' ), true, '"Foo" is a valid URI component' );
580 assert.strictEqual( ve.isUriComponentValid( 'Foo%20Bar' ), true, '"Foo%20Bar" is a valid URI component' );
581 assert.strictEqual( ve.isUriComponentValid( '%E0%A4%A' ), false, '"%E0%A4%A" is an invalid URI component' );
582 } );
583
584 QUnit.test( 'safeDecodeURIComponent', ( assert ) => {
585 assert.strictEqual( ve.safeDecodeURIComponent( 'Foo' ), 'Foo', '"Foo" is successfully URI decoded' );
586 assert.strictEqual( ve.safeDecodeURIComponent( 'Foo%20Bar' ), 'Foo Bar', '"Foo%20Bar" is successfully URI decoded' );
587 assert.strictEqual( ve.safeDecodeURIComponent( '%E0%A4%A' ), '%E0%A4%A', '"%E0%A4%A" is not URI decoded, just returned as-is' );
588 } );
589
590 QUnit.test( 'isBlockElement/isVoidElement', ( assert ) => {
591 assert.strictEqual( ve.isBlockElement( 'div' ), true, '"div" is a block element' );
592 assert.strictEqual( ve.isBlockElement( 'SPAN' ), false, '"SPAN" is not a block element' );
593 assert.strictEqual( ve.isBlockElement( 'a' ), false, '"a" is not a block element' );
594 assert.strictEqual( ve.isBlockElement( document.createElement( 'div' ) ), true, '<div> is a block element' );
595 assert.strictEqual( ve.isBlockElement( document.createElement( 'span' ) ), false, '<span> is not a block element' );
596
597 assert.strictEqual( ve.isVoidElement( 'img' ), true, '"img" is a void element' );
598 assert.strictEqual( ve.isVoidElement( 'DIV' ), false, '"DIV" is not a void element' );
599 assert.strictEqual( ve.isVoidElement( 'span' ), false, '"span" is not a void element' );
600 assert.strictEqual( ve.isVoidElement( document.createElement( 'img' ) ), true, '<img> is a void element' );
601 assert.strictEqual( ve.isVoidElement( document.createElement( 'div' ) ), false, '<div> is not a void element' );
602 } );
603
604 // TODO: ve.getByteOffset
605
606 // TODO: ve.getClusterOffset
607
608 QUnit.test( 'graphemeSafeSubstring', ( assert ) => {
609 const text = '12\ud860\udee245\ud860\udee2789\ud860\udee2bc',
610 cases = [
611 {
612 msg: 'start and end inside multibyte',
613 start: 3,
614 end: 12,
615 expected: [ '\ud860\udee245\ud860\udee2789\ud860\udee2', '45\ud860\udee2789' ]
616 },
617 {
618 msg: 'start and end next to multibyte',
619 start: 4,
620 end: 11,
621 expected: [ '45\ud860\udee2789', '45\ud860\udee2789' ]
622 },
623 {
624 msg: 'complete string',
625 start: 0,
626 end: text.length,
627 expected: [ text, text ]
628 },
629 {
630 msg: 'collapsed selection inside multibyte',
631 start: 3,
632 end: 3,
633 expected: [ '\ud860\udee2', '' ]
634 }
635 ];
636
637 cases.forEach( ( caseItem ) => {
638 assert.strictEqual(
639 ve.graphemeSafeSubstring( text, caseItem.start, caseItem.end, true ),
640 caseItem.expected[ 0 ],
641 caseItem.msg + ' (outer)'
642 );
643 assert.strictEqual(
644 ve.graphemeSafeSubstring( text, caseItem.start, caseItem.end, false ),
645 caseItem.expected[ 1 ],
646 caseItem.msg + ' (inner)'
647 );
648 } );
649 } );
650
651 QUnit.test( 'getCommonAncestor', ( assert ) => {
652 const doc = ve.createDocumentFromHtml( '<html><div><p>AA<i><b>BB<img src="#"></b></i>CC</p>DD</div>EE</html>' );
653 const cases = [
654 { nodes: 'b b', ancestor: 'b' },
655 { nodes: 'b i', ancestor: 'i' },
656 { nodes: 'textB img', ancestor: 'b' },
657 { nodes: 'p textD', ancestor: 'div' },
658 { nodes: 'textC img', ancestor: 'p' },
659 { nodes: 'textC b', ancestor: 'p' },
660 { nodes: 'textC textD', ancestor: 'div' },
661 { nodes: 'textA textB', ancestor: 'p' },
662 { nodes: 'textA img', ancestor: 'p' },
663 { nodes: 'img textE', ancestor: 'body' },
664 { nodes: 'textA textB textC textD', ancestor: 'div' },
665 { nodes: 'textA i b textC', ancestor: 'p' },
666 { nodes: 'body div head p', ancestor: 'html' },
667 { nodes: 'b null', ancestor: 'null' },
668 { nodes: 'null b', ancestor: 'null' },
669 { nodes: 'b i null', ancestor: 'null' },
670 { nodes: 'b null i', ancestor: 'null' },
671 { nodes: 'b unattached', ancestor: 'null' },
672 { nodes: 'unattached b', ancestor: 'null' }
673 ];
674 const nodes = {};
675 nodes.html = doc.documentElement;
676 nodes.head = doc.head;
677 nodes.body = doc.body;
678 nodes.div = doc.getElementsByTagName( 'div' )[ 0 ];
679 nodes.p = doc.getElementsByTagName( 'p' )[ 0 ];
680 nodes.b = doc.getElementsByTagName( 'b' )[ 0 ];
681 nodes.i = doc.getElementsByTagName( 'i' )[ 0 ];
682 nodes.img = doc.getElementsByTagName( 'img' )[ 0 ];
683 nodes.textA = nodes.p.childNodes[ 0 ];
684 nodes.textB = nodes.b.childNodes[ 0 ];
685 nodes.textC = nodes.p.childNodes[ 2 ];
686 nodes.textD = nodes.div.childNodes[ 1 ];
687 nodes.textE = nodes.body.childNodes[ 1 ];
688 nodes.null = null;
689 nodes.unattached = doc.createElement( 'div' ).appendChild( doc.createElement( 'span' ) );
690 function getNode( name ) {
691 return nodes[ name ];
692 }
693
694 cases.forEach( ( caseItem ) => {
695 const testNodes = caseItem.nodes.split( /\s+/ ).map( getNode );
696 const ancestorNode = nodes[ caseItem.ancestor ];
697 assert.strictEqual(
698 ve.getCommonAncestor.apply( null, testNodes ),
699 ancestorNode,
700 caseItem.nodes + ' -> ' + caseItem.ancestor
701 );
702 } );
703
704 // Test no-argument case
705 assert.strictEqual( ve.getCommonAncestor(), null, 'No nodes' );
706 } );
707
708 QUnit.test( 'getCommonStartSequenceLength', ( assert ) => {
709 const cases = [
710 {
711 sequences: [ [ 0, 1, 2 ], [ 0, 1, 2 ], [ '0', 1, 2 ] ],
712 commonLength: 0,
713 title: 'No common start sequence'
714 },
715 {
716 sequences: [ [ 1, 2, 3 ], [] ],
717 commonLength: 0,
718 title: 'Empty sequence'
719 },
720 {
721 sequences: [ [ 'five', 6 ], [ 'five' ] ],
722 commonLength: 1,
723 title: 'Differing lengths'
724 },
725 {
726 sequences: [ [ 1, 2 ] ],
727 commonLength: 2,
728 title: 'Single sequence'
729 },
730 {
731 sequences: [ 'Cymru', 'Cymry', 'Cymraes', 'Cymro', 'Cymraeg' ],
732 commonLength: 4,
733 title: 'String sequences'
734 },
735 {
736 sequences: [],
737 commonLength: 0,
738 title: 'No sequences'
739 }
740 ];
741
742 cases.forEach( ( caseItem ) => {
743 assert.strictEqual(
744 ve.getCommonStartSequenceLength( caseItem.sequences ),
745 caseItem.commonLength,
746 caseItem.title
747 );
748 } );
749 } );
750
751 QUnit.test( 'adjacentDomPosition', ( assert ) => {
752 // In the following tests, the html is put inside the top-level div as innerHTML. Then
753 // ve.adjacentDomPosition is called with the position just inside the div (i.e.
754 // { node: div, offset: 0 } for forward direction tests, and
755 // { node: div, offset: div.childNodes.length } for reverse direction tests). The result
756 // of the first call is passed into the function again, and so on iteratively until the
757 // function returns null. The 'path' properties are a list of descent offsets to find a
758 // particular position node from the top-level div. E.g. a path of [ 5, 7 ] refers to the
759 // node div.childNodes[ 5 ].childNodes[ 7 ] .
760 const cases = [
761 {
762 title: 'Simple p node',
763 html: '<p>x</p>',
764 options: { stop: function () {
765 return true;
766 } },
767 expectedOffsetPaths: [
768 [ 0 ],
769 [ 0, 0 ],
770 [ 0, 0, 0 ],
771 [ 0, 0, 1 ],
772 [ 0, 1 ],
773 [ 1 ]
774 ]
775 },
776 {
777 title: 'Filtered descent',
778 html: '<div class="x">foo</div><div class="y">bar</div>',
779 options: { stop: function () {
780 return true;
781 }, noDescend: '.x' },
782 expectedOffsetPaths: [
783 [ 0 ],
784 [ 1 ],
785 [ 1, 0 ],
786 [ 1, 0, 0 ],
787 [ 1, 0, 1 ],
788 [ 1, 0, 2 ],
789 [ 1, 0, 3 ],
790 [ 1, 1 ],
791 [ 2 ]
792 ]
793 },
794 {
795 title: 'Empty tags and heavy nesting',
796 html: '<div><br/><p>foo <b>bar <i>baz</i></b></p></div>',
797 options: { stop: function () {
798 return true;
799 } },
800 expectedOffsetPaths: [
801 [ 0 ],
802 [ 0, 0 ],
803 // The <br/> tag is void, so should get skipped
804 [ 0, 1 ],
805 [ 0, 1, 0 ],
806 [ 0, 1, 0, 0 ],
807 [ 0, 1, 0, 1 ],
808 [ 0, 1, 0, 2 ],
809 [ 0, 1, 0, 3 ],
810 [ 0, 1, 0, 4 ],
811 [ 0, 1, 1 ],
812 [ 0, 1, 1, 0 ],
813 [ 0, 1, 1, 0, 0 ],
814 [ 0, 1, 1, 0, 1 ],
815 [ 0, 1, 1, 0, 2 ],
816 [ 0, 1, 1, 0, 3 ],
817 [ 0, 1, 1, 0, 4 ],
818 [ 0, 1, 1, 1 ],
819 [ 0, 1, 1, 1, 0 ],
820 [ 0, 1, 1, 1, 0, 0 ],
821 [ 0, 1, 1, 1, 0, 1 ],
822 [ 0, 1, 1, 1, 0, 2 ],
823 [ 0, 1, 1, 1, 0, 3 ],
824 [ 0, 1, 1, 1, 1 ],
825 [ 0, 1, 1, 2 ],
826 [ 0, 1, 2 ],
827 [ 0, 2 ],
828 [ 1 ]
829 ]
830 }
831 ];
832
833 const div = document.createElement( 'div' );
834 div.contentEditable = 'true';
835
836 for ( const direction in { forward: undefined, backward: undefined } ) {
837 cases.forEach( ( caseItem ) => {
838 div.innerHTML = caseItem.html;
839 const offsetPaths = [];
840 let position = {
841 node: div,
842 offset: direction === 'backward' ? div.childNodes.length : 0
843 };
844 while ( position.node !== null ) {
845 offsetPaths.push(
846 ve.getOffsetPath( div, position.node, position.offset )
847 );
848 position = ve.adjacentDomPosition(
849 position,
850 direction === 'backward' ? -1 : 1,
851 caseItem.options
852 );
853 }
854 assert.deepEqual(
855 offsetPaths,
856 (
857 direction === 'backward' ?
858 caseItem.expectedOffsetPaths.slice().reverse() :
859 caseItem.expectedOffsetPaths
860 ),
861 caseItem.title + ' (' + direction + ')'
862 );
863 } );
864 }
865 } );
866
867 QUnit.test( 'deepFreeze', ( assert ) => {
868 const data = [
869 { type: 'heading', attributes: { level: 1 } },
870 ...'Foo',
871 { type: '/heading' }
872 ];
873
874 const originalData = ve.copy( data );
875 let frozen = ve.deepFreeze( data, true );
876
877 assert.deepEqual( frozen, originalData, 'Frozen data is equal to original data' );
878 assert.strictEqual( frozen, data, 'Result is same object as input' );
879
880 assert.throws( () => {
881 data[ 0 ].attributes.level = 2;
882 }, Error, 'Can\'t change data attribute' );
883
884 assert.throws( () => {
885 delete frozen[ 0 ].attributes;
886 }, Error, 'Can\'t delete property' );
887
888 frozen.splice( 3, 1, 'b' );
889
890 assert.strictEqual( frozen[ 3 ], 'b', 'Data can be spliced' );
891 assert.strictEqual( data[ 3 ], 'b', 'Original object affected by splice' );
892
893 frozen = ve.deepFreeze( data );
894
895 assert.throws( () => {
896 frozen.splice( 3, 1, 'c' );
897 }, Error, 'Can\'t splice if root is frozen' );
898
899 frozen = ve.deepFreeze( data );
900 assert.true( true, 'Freezing for a second time does not throw' );
901 } );
902
903 QUnit.test( 'deepFreeze (on cyclic structure)', ( assert ) => {
904 const realFreeze = ve.deepFreeze;
905
906 const cyclic = { foo: 'bar' };
907 cyclic.self = cyclic;
908
909 let count;
910
911 ve.deepFreeze = function () {
912 count++;
913 return realFreeze.apply( ve, arguments );
914 };
915 try {
916 count = 0;
917 ve.deepFreeze( cyclic );
918 assert.strictEqual( count, 1, 'Did not recurse into self' );
919 } finally {
920 ve.deepFreeze = realFreeze;
921 }
922 } );
923
924 QUnit.test( 'deepFreeze (recursive, aliased)', ( assert ) => {
925 let foo = { bar: {} };
926 const realFreeze = ve.deepFreeze;
927
928 ve.deepFreeze = function ( x ) {
929 return x;
930 };
931 try {
932 foo = realFreeze( foo );
933 assert.true( Object.isFrozen( foo.bar ), 'Recursed into aliased version' );
934 } finally {
935 ve.deepFreeze = realFreeze;
936 }
937 } );
938
939 QUnit.test( 've.minimizeRects', ( assert ) => {
940 const cases = [
941 {
942 rects: [
943 // left, top, width, height
944 [ 0, 0, 100, 100 ],
945 // Identical square removed
946 [ 0, 0, 100, 100 ],
947
948 [ 200, 0, 100, 100 ],
949 // Contained in previous rect
950 [ 210, 10, 90, 90 ],
951
952 // Contained in next rect
953 [ 410, 0, 90, 90 ],
954 [ 400, 0, 100, 100 ],
955
956 // Horizontally adjacent
957 [ 0, 200, 100, 100 ],
958 [ 100, 200, 100, 100 ],
959 [ 200, 200, 100, 100 ],
960
961 // Vertically adjacent
962 [ 0, 400, 100, 100 ],
963 [ 0, 500, 100, 100 ],
964 [ 0, 600, 100, 100 ]
965 ],
966 expected: [
967 // Identical square removed
968 [ 0, 0, 100, 100 ],
969 // Contained in previous rect
970 [ 200, 0, 100, 100 ],
971 // Contained in next rect
972 [ 400, 0, 100, 100 ],
973 // Horizontally adjacent
974 [ 0, 200, 300, 100 ],
975 // Vertically adjacent
976 [ 0, 400, 100, 300 ]
977 ]
978 }
979 ];
980
981 function expand( rect ) {
982 return {
983 left: rect[ 0 ],
984 top: rect[ 1 ],
985 width: rect[ 2 ],
986 height: rect[ 3 ],
987 right: rect[ 0 ] + rect[ 2 ],
988 bottom: rect[ 1 ] + rect[ 3 ]
989 };
990 }
991
992 cases.forEach( ( caseItem ) => {
993 caseItem.rects = caseItem.rects.map( expand );
994 caseItem.expected = caseItem.expected.map( expand );
995
996 const rectsBefore = ve.copy( caseItem.rects );
997 const actual = ve.minimizeRects( caseItem.rects );
998 assert.deepEqual( caseItem.rects, rectsBefore, 'Input not modified' );
999 assert.deepEqual( actual, caseItem.expected, 'List minifed' );
1000 } );
1001 } );