import { Assertions, Chain, GeneralSteps, Logger, Pipeline } from '@ephox/agar';
import { Hierarchy, Element } from '@ephox/sugar';
import MergeBlocks from 'tinymce/core/delete/MergeBlocks';
import ViewBlock from '../../module/test/ViewBlock';
import { UnitTest } from '@ephox/bedrock';
UnitTest.asynctest('browser.tinymce.core.delete.MergeBlocksTest', function () {
const success = arguments[arguments.length - 2];
const failure = arguments[arguments.length - 1];
const viewBlock = ViewBlock();
const cSetHtml = function (html) {
return Chain.op(function () {
viewBlock.update(html);
});
};
const cAssertHtml = function (expectedHtml) {
return Chain.op(function () {
Assertions.assertHtml('Should equal html', expectedHtml, viewBlock.get().innerHTML);
});
};
const cMergeBlocks = function (forward, block1Path, block2Path) {
return Chain.mapper(function (viewBlock) {
const block1 = Hierarchy.follow(Element.fromDom(viewBlock.get()), block1Path).getOrDie();
const block2 = Hierarchy.follow(Element.fromDom(viewBlock.get()), block2Path).getOrDie();
return MergeBlocks.mergeBlocks(Element.fromDom(viewBlock.get()), forward, block1, block2);
});
};
const cAssertPosition = function (expectedPath, expectedOffset) {
return Chain.op(function (position) {
const container = Hierarchy.follow(Element.fromDom(viewBlock.get()), expectedPath).getOrDie();
Assertions.assertDomEq('Should be expected container', container, Element.fromDom(position.getOrDie().container()));
Assertions.assertEq('Should be expected offset', expectedOffset, position.getOrDie().offset());
});
};
viewBlock.attach();
Pipeline.async({}, [
Logger.t('Merge forward', GeneralSteps.sequence([
Logger.t('Merge two simple blocks', Chain.asStep(viewBlock, [
cSetHtml('
a
b
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge two simple blocks with br', Chain.asStep(viewBlock, [
cSetHtml('a
b
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge two complex blocks', Chain.asStep(viewBlock, [
cSetHtml('ab
cd
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 1, 0], 1),
cAssertHtml('abcd
')
])),
Logger.t('Merge two headers blocks', Chain.asStep(viewBlock, [
cSetHtml('a
b
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge two headers blocks first one empty', Chain.asStep(viewBlock, [
cSetHtml('
b
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 0], 0),
cAssertHtml('b
')
])),
Logger.t('Merge two headers blocks second one empty', Chain.asStep(viewBlock, [
cSetHtml('a
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 0], 1),
cAssertHtml('a
')
])),
Logger.t('Merge two headers complex blocks', Chain.asStep(viewBlock, [
cSetHtml('ab
cd
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 1, 0], 1),
cAssertHtml('abcd
')
])),
Logger.t('Merge two headers blocks first one empty second one complex', Chain.asStep(viewBlock, [
cSetHtml('
ab
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 0], 0),
cAssertHtml('ab
')
])),
Logger.t('Merge two headers blocks second one empty first one complex', Chain.asStep(viewBlock, [
cSetHtml('ab
'),
cMergeBlocks(true, [0], [1]),
cAssertPosition([0, 1, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge two list items', Chain.asStep(viewBlock, [
cSetHtml(''),
cMergeBlocks(true, [0, 0], [0, 1]),
cAssertPosition([0, 0, 1, 0], 1),
cAssertHtml('')
])),
Logger.t('Merge paragraph into list item', Chain.asStep(viewBlock, [
cSetHtml('cd
'),
cMergeBlocks(true, [0, 0], [1]),
cAssertPosition([0, 0, 1, 0], 1),
cAssertHtml('')
]))
])),
Logger.t('Merge backwards', GeneralSteps.sequence([
Logger.t('Merge two simple blocks', Chain.asStep(viewBlock, [
cSetHtml('a
b
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge two simple blocks with br', Chain.asStep(viewBlock, [
cSetHtml('a
b
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge two complex blocks', Chain.asStep(viewBlock, [
cSetHtml('ab
cd
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 1, 0], 1),
cAssertHtml('abcd
')
])),
Logger.t('Merge two headers blocks', Chain.asStep(viewBlock, [
cSetHtml('a
b
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge two headers blocks first one empty', Chain.asStep(viewBlock, [
cSetHtml('a
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 0], 1),
cAssertHtml('a
')
])),
Logger.t('Merge two headers blocks second one empty', Chain.asStep(viewBlock, [
cSetHtml('
b
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 0], 0),
cAssertHtml('b
')
])),
Logger.t('Merge two headers complex blocks', Chain.asStep(viewBlock, [
cSetHtml('ab
cd
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 1, 0], 1),
cAssertHtml('abcd
')
])),
Logger.t('Merge two headers blocks first one empty second one complex', Chain.asStep(viewBlock, [
cSetHtml('ab
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 1, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge two headers blocks second one empty first one complex', Chain.asStep(viewBlock, [
cSetHtml('
ab
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 0], 0),
cAssertHtml('ab
')
])),
Logger.t('Merge two list items', Chain.asStep(viewBlock, [
cSetHtml(''),
cMergeBlocks(false, [0, 1], [0, 0]),
cAssertPosition([0, 0, 1, 0], 1),
cAssertHtml('')
])),
Logger.t('Merge list item into paragraph', Chain.asStep(viewBlock, [
cSetHtml('ab
'),
cMergeBlocks(false, [1, 0], [0]),
cAssertPosition([0, 1, 0], 1),
cAssertHtml('abcd
')
])),
Logger.t('Merge h1 into parent wrapper div', Chain.asStep(viewBlock, [
cSetHtml('a
b
'),
cMergeBlocks(false, [0, 1], [0]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge h1 inside a div into parent wrapper div', Chain.asStep(viewBlock, [
cSetHtml(''),
cMergeBlocks(false, [0, 1, 0], [0]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge div > div > div > h1 into root div', Chain.asStep(viewBlock, [
cSetHtml(''),
cMergeBlocks(false, [0, 1, 0, 0], [0]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
')
])),
Logger.t('Merge children until we find a block', Chain.asStep(viewBlock, [
cSetHtml('a
b
c
'),
cMergeBlocks(false, [1], [0]),
cAssertPosition([0, 0], 1),
cAssertHtml('ab
c
')
]))
]))
], function () {
viewBlock.detach();
success();
}, failure);
});