import { describe, it } from '@ephox/bedrock-client'; import { Arr, Optional } from '@ephox/katamari'; import { Hierarchy, Html, SugarElement } from '@ephox/sugar'; import { assert } from 'chai'; import * as TableDeleteAction from 'tinymce/core/delete/TableDeleteAction'; describe('browser.tinymce.core.delete.TableDeleteActionTest', () => { const fromHtml = (html: string, startPath: number[], startOffset: number, endPath: number[], endOffset: number) => { const elm = SugarElement.fromHtml(html); const sc = Hierarchy.follow(elm, startPath).getOrDie(); const ec = Hierarchy.follow(elm, endPath).getOrDie(); const rng = document.createRange(); rng.setStart(sc.dom, startOffset); rng.setEnd(ec.dom, endOffset); return TableDeleteAction.getActionFromRange(elm, rng); }; const fail = (message: string) => () => assert.fail(message); const assertNone = (x: Optional) => { assert.isTrue(x.isNone(), 'Is none'); }; const extractSingleCellTableAction = (actionOpt: Optional) => { return actionOpt.fold( fail('unexpected nothing'), (action) => action.fold( (_rng, cell) => Html.getOuter(cell), fail('unexpected action'), fail('unexpected action'), fail('unexpected action') ) ); }; const extractFullTableAction = (actionOpt: Optional) => { return actionOpt.fold( fail('unexpected nothing'), (action) => { return action.fold( fail('unexpected action'), (table) => Html.getOuter(table), fail('unexpected action'), fail('unexpected action') ); } ); }; const extractPartialTableAction = (actionOpt: Optional) => { return actionOpt.fold( fail('unexpected nothing'), (action) => { return action.fold( fail('unexpected action'), fail('unexpected action'), (cells, outsideDetails) => ({ cells: Arr.map(cells, Html.getOuter).join(''), otherContent: outsideDetails.map(({ rng }) => rng.extractContents().textContent).getOr(''), details: outsideDetails }), fail('unexpected action') ); } ); }; const extractMultiTableAction = (actionOpt: Optional) => { return actionOpt.fold( fail('unexpected nothing'), (action) => { return action.fold( fail('unexpected action'), fail('unexpected action'), fail('unexpected action'), (startTableCells, endTableCells, betweenRng) => ({ startCells: Arr.map(startTableCells, Html.getOuter).join(''), endCells: Arr.map(endTableCells, Html.getOuter).join(''), otherContent: betweenRng.extractContents().textContent }) ); } ); }; it('collapsed range should return none', () => { const action = fromHtml('
abc
', [ 0, 0, 0, 0 ], 0, [ 0, 0, 0, 0 ], 0); assertNone(action); }); it('select two out of three cells returns the partialTable action', () => { const action = fromHtml('
abc
', [ 0, 0, 0, 0 ], 0, [ 0, 0, 1, 0 ], 1); const { cells, otherContent, details } = extractPartialTableAction(action); assert.equal(cells, 'ab', 'Should be cells'); assert.isEmpty(otherContent); assert.isTrue(details.isNone(), 'No outside details'); }); it('select two out of three header cells returns the partialTable action', () => { const action = fromHtml('
abc
', [ 0, 0, 0, 0 ], 0, [ 0, 0, 1, 0 ], 1); const { cells, otherContent, details } = extractPartialTableAction(action); assert.equal(cells, 'ab', 'Should be cells'); assert.isEmpty(otherContent); assert.isTrue(details.isNone(), 'No outside details'); }); it('select three out of three cells returns the fullTable action', () => { const action = fromHtml('
abc
', [ 0, 0, 0, 0 ], 0, [ 0, 0, 2, 0 ], 1); const table = extractFullTableAction(action); assert.equal(table, '
abc
', 'should be table'); }); it('select between rows, not all cells', () => { const action = fromHtml( '
abc
def
', [ 0, 0, 1, 0 ], 0, [ 0, 1, 0, 0 ], 1 ); const { cells, otherContent, details } = extractPartialTableAction(action); assert.equal(cells, 'bcd', 'should be cells'); assert.isEmpty(otherContent); assert.isTrue(details.isNone(), 'No outside details'); }); it('select between rows, all cells', () => { const action = fromHtml( '
abc
def
', [ 0, 0, 0, 0 ], 0, [ 0, 1, 2, 0 ], 1 ); const table = extractFullTableAction(action); assert.equal(table, '
abc
def
', 'should be table'); }); it('TINY-6044: select between two tables, not all cells', () => { const action = fromHtml( '
' + '
ab
cd
' + 'foo' + '
e
f
' + '
', [ 0, 0, 0, 1, 0 ], 0, [ 2, 0, 0, 0, 0 ], 1 ); const { startCells, endCells, otherContent } = extractMultiTableAction(action); assert.equal(startCells, 'bcd'); assert.equal(endCells, 'e'); assert.equal(otherContent, 'foo'); }); it('TINY-6044: select between two tables, all cells', () => { const action = fromHtml( '
' + '
a
b
' + 'foo' + '
c
d
' + '
', [ 0, 0, 0, 0, 0 ], 0, [ 2, 0, 1, 0, 0 ], 1 ); const { startCells, endCells, otherContent } = extractMultiTableAction(action); assert.equal(startCells, 'ab'); assert.equal(endCells, 'cd'); assert.equal(otherContent, 'foo'); }); it('select table and content after', () => { const action = fromHtml( '
a
b
', [ 0, 0, 0, 0, 0 ], 0, [ 1 ], 1 ); const { cells, otherContent, details } = extractPartialTableAction(action); assert.equal(cells, 'a', 'should be cells from partially selected table'); assert.equal(otherContent, 'b'); assert.isTrue(details.isSome(), 'Has outside details'); }); it('select table and content before', () => { const action = fromHtml( '
a
b
', [ 0 ], 0, [ 1, 0, 0, 0, 0 ], 1 ); const { cells, otherContent, details } = extractPartialTableAction(action); assert.equal(cells, 'b', 'should be cells from partially selected table'); assert.equal(otherContent, 'a'); assert.isTrue(details.isSome(), 'Has outside details'); }); it('single cell table with all content selected', () => { const action = fromHtml( '
test
', [ 0, 0, 0, 0 ], 0, [ 0, 0, 0, 0 ], 4 ); const cell = extractSingleCellTableAction(action); assert.equal(cell, 'test', 'Should be cells'); }); });