import { Assertions } from '@ephox/agar'; import { context, describe, it } from '@ephox/bedrock-client'; import { Optional } from '@ephox/katamari'; import { Hierarchy, SugarElement } from '@ephox/sugar'; import { assert } from 'chai'; import CaretPosition from 'tinymce/core/caret/CaretPosition'; import * as MergeBlocks from 'tinymce/core/delete/MergeBlocks'; import * as ViewBlock from '../../module/test/ViewBlock'; describe('browser.tinymce.core.delete.MergeBlocksTest', () => { const viewBlock = ViewBlock.bddSetup(); const setHtml = viewBlock.update; const assertHtml = (expectedHtml: string) => { Assertions.assertHtml('Should equal html', expectedHtml, viewBlock.get().innerHTML); }; const mergeBlocks = (forward: boolean, block1Path: number[], block2Path: number[]) => { const block1 = Hierarchy.follow(SugarElement.fromDom(viewBlock.get()), block1Path).getOrDie() as SugarElement; const block2 = Hierarchy.follow(SugarElement.fromDom(viewBlock.get()), block2Path).getOrDie() as SugarElement; return MergeBlocks.mergeBlocks(SugarElement.fromDom(viewBlock.get()), forward, block1, block2); }; const assertPosition = (position: Optional, expectedPath: number[], expectedOffset: number) => { const container = Hierarchy.follow(SugarElement.fromDom(viewBlock.get()), expectedPath).getOrDie(); Assertions.assertDomEq('Should be expected container', container, SugarElement.fromDom(position.getOrDie().container())); assert.equal(position.getOrDie().offset(), expectedOffset, 'Should be expected offset'); }; context('Merge forward', () => { it('Merge two simple blocks', () => { setHtml('

a

b

'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('

ab

'); }); it('Merge two simple blocks with br', () => { setHtml('

a

b

'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('

ab

'); }); it('Merge two complex blocks', () => { setHtml('

ab

cd

'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 1, 0 ], 1); assertHtml('

abcd

'); }); it('Merge two headers blocks', () => { setHtml('

a

b

'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('

ab

'); }); it('Merge two headers blocks first one empty', () => { setHtml('


b

'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 0 ], 0); assertHtml('

b

'); }); it('Merge two headers blocks second one empty', () => { setHtml('

a


'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('

a

'); }); it('Merge two headers complex blocks', () => { setHtml('

ab

cd

'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 1, 0 ], 1); assertHtml('

abcd

'); }); it('Merge two headers blocks first one empty second one complex', () => { setHtml('


ab

'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 0 ], 0); assertHtml('

ab

'); }); it('Merge two headers blocks second one empty first one complex', () => { setHtml('

ab


'); const pos = mergeBlocks(true, [ 0 ], [ 1 ]); assertPosition(pos, [ 0, 1, 0 ], 1); assertHtml('

ab

'); }); it('Merge two list items', () => { setHtml('
  • ab
  • cd
'); const pos = mergeBlocks(true, [ 0, 0 ], [ 0, 1 ]); assertPosition(pos, [ 0, 0, 1, 0 ], 1); assertHtml('
  • abcd
'); }); it('Merge paragraph into list item', () => { setHtml('
  • ab

cd

'); const pos = mergeBlocks(true, [ 0, 0 ], [ 1 ]); assertPosition(pos, [ 0, 0, 1, 0 ], 1); assertHtml('
  • abcd
'); }); it('Merge empty block into empty containing block', () => { setHtml('

'); const pos = mergeBlocks(true, [ 0 ], [ 0, 0 ]); assertPosition(pos, [ 0 ], 0); assertHtml('

'); }); it('Merge empty block into containing block', () => { setHtml('

c
'); const pos = mergeBlocks(true, [ 0 ], [ 0, 0 ]); assertPosition(pos, [ 0 ], 0); assertHtml('

c
'); }); it('Merge first empty item of nested list into containing list item', () => { setHtml('
    • a
'); const pos = mergeBlocks(true, [ 0, 0 ], [ 0, 0, 0, 0 ]); assertPosition(pos, [ 0, 0 ], 0); assertHtml('

    • a
'); }); }); context('Merge backwards', () => { it('Merge two simple blocks', () => { setHtml('

a

b

'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('

ab

'); }); it('Merge two simple blocks with br', () => { setHtml('

a

b

'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('

ab

'); }); it('Merge two complex blocks', () => { setHtml('

ab

cd

'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 1, 0 ], 1); assertHtml('

abcd

'); }); it('Merge two headers blocks', () => { setHtml('

a

b

'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('

ab

'); }); it('Merge two headers blocks first one empty', () => { setHtml('

a


'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('

a

'); }); it('Merge two headers blocks second one empty', () => { setHtml('


b

'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 0); assertHtml('

b

'); }); it('Merge two headers complex blocks', () => { setHtml('

ab

cd

'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 1, 0 ], 1); assertHtml('

abcd

'); }); it('Merge two headers blocks first one empty second one complex', () => { setHtml('

ab


'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 1, 0 ], 1); assertHtml('

ab

'); }); it('Merge two headers blocks second one empty first one complex', () => { setHtml('


ab

'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 0); assertHtml('

ab

'); }); it('Merge two list items', () => { setHtml('
  • ab
  • cd
'); const pos = mergeBlocks(false, [ 0, 1 ], [ 0, 0 ]); assertPosition(pos, [ 0, 0, 1, 0 ], 1); assertHtml('
  • abcd
'); }); it('Merge list item into paragraph', () => { setHtml('

ab

  • cd
'); const pos = mergeBlocks(false, [ 1, 0 ], [ 0 ]); assertPosition(pos, [ 0, 1, 0 ], 1); assertHtml('

abcd

'); }); it('Merge h1 into parent wrapper div', () => { setHtml('
a

b

'); const pos = mergeBlocks(false, [ 0, 1 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('
ab
'); }); it('Merge h1 inside a div into parent wrapper div', () => { setHtml('
a

b

'); const pos = mergeBlocks(false, [ 0, 1, 0 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('
ab
'); }); it('Merge div > div > div > h1 into root div', () => { setHtml('
a

b

'); const pos = mergeBlocks(false, [ 0, 1, 0, 0 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('
ab
'); }); it('Merge children until we find a block', () => { setHtml('
a
b

c

'); const pos = mergeBlocks(false, [ 1 ], [ 0 ]); assertPosition(pos, [ 0, 0 ], 1); assertHtml('
ab

c

'); }); }); });