import { describe, it } from '@ephox/bedrock-client'; import { Arr } from '@ephox/katamari'; import { Css, Dimension, SelectorFilter, SelectorFind, SugarElement } from '@ephox/sugar'; import { TinyDom, TinyHooks } from '@ephox/wrap-mcagar'; import { assert } from 'chai'; import Editor from 'tinymce/core/api/Editor'; import * as TableTestUtils from '../../../module/table/TableTestUtils'; interface PartialTableModifiedEvent { readonly type: string; readonly structure: boolean; readonly style: boolean; } interface MergeCellTest { readonly before: string; readonly after: string; readonly expectedEvents: PartialTableModifiedEvent[]; } describe('browser.tinymce.models.dom.table.command.MergeCellCommandTest', () => { const hook = TinyHooks.bddSetupLight({ base_url: '/project/tinymce/js/tinymce', indent: false, setup: (ed: Editor) => ed.on('TableModified', logModifiedEvent), }, []); let modifiedEvents: PartialTableModifiedEvent[] = []; const logModifiedEvent = (event: PartialTableModifiedEvent) => { modifiedEvents.push({ type: event.type, structure: event.structure, style: event.style, }); }; const clearEvents = () => modifiedEvents = []; const defaultEvent: PartialTableModifiedEvent = { type: 'tablemodified', structure: true, style: false }; const testMerge = (editor: Editor, test: MergeCellTest) => { clearEvents(); editor.setContent(test.before); editor.selection.select(editor.dom.select('td[data-mce-selected]')[0], true); editor.selection.collapse(true); editor.execCommand('mceTableMergeCells'); assert.equal(cleanTableHtml(editor.getContent()), test.after); assert.deepEqual(modifiedEvents, test.expectedEvents); }; const cleanTableHtml = (html: string) => { return html.replace(/

( |]+>)<\/p>$/, ''); }; const getWidth = (elem: SugarElement) => Dimension.parse(Css.getRaw(elem, 'width').getOrDie(), [ 'relative' ]).getOrDie().value; it('TBA: Should merge all cells into one', () => { const editor = hook.editor(); testMerge(editor, { before: ( '' + '' + '' + '' + '' + '
a1b1
a2b2
' ), after: ( '' + '' + '' + '' + '
' + 'a1
b1
a2
b2' + '
' ), expectedEvents: [ defaultEvent ], }); }); it('TBA: Should merge cells in two cols/rows into one cell with colspan', () => { const editor = hook.editor(); testMerge(editor, { before: ( '' + '' + '' + '' + '' + '' + '
a1b1
a2b2
a3b3
' ), after: ( '' + '' + '' + '' + '' + '' + '
a1
b1
a2
b2
a3b3
' ), expectedEvents: [ defaultEvent ], }); }); it('TBA: Should merge b3+c3 but not reduce a2a3', () => { const editor = hook.editor(); testMerge(editor, { before: ( '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1c1
a2a3b2c2
b3c3
' ), after: ( '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1c1
a2a3b2c2
b3
c3
' ), expectedEvents: [ defaultEvent ], }); }); it('TINY-6901: Will apply correct size to cell with colspan after cell merge', () => { const editor = hook.editor(); const before = ( '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
' ); editor.setContent(before); const cols = SelectorFilter.descendants(TinyDom.body(editor), 'td[data-mce-selected]'); const totalColsWidth = Arr.foldl(cols, (acc, col) => acc + getWidth(col), 0); editor.selection.select(cols[0].dom, true); editor.selection.collapse(true); editor.execCommand('mceTableMergeCells'); const colspan = SelectorFind.descendant(TinyDom.body(editor), 'td[colspan="2"]').getOrDie(); assert.approximately(getWidth(colspan), totalColsWidth, 2, 'Check new cell is similar width the the two cells that were merged'); }); it('TINY-9459: Should not merge cells in table inside noneditable root', () => { TableTestUtils.withNoneditableRootEditor(hook.editor(), (editor) => { testMerge(editor, { before: ( '' + '' + '' + '' + '' + '
a1b1
a2b2
' ), after: ( '' + '' + '' + '' + '' + '
a1b1
a2b2
' ), expectedEvents: [ ] }); }); }); /* { message: 'Should remove all rowspans since the table is fully merged', before: ( '' + '' + '' + '' + '' + '
a1b1
b2
' ), after: ( '' + '' + '' + '' + '
a1b1b2
' ) }, { message: 'Should remove all colspans since the table is fully merged', before: ( '' + '' + '' + '' + '' + '
a1
a2b2
' ), after: ( '' + '' + '' + '' + '' + '
a1
a2b2
' ) }, { message: 'Should reduce rowspans to 2 keep the colspan and remove one tr', before: ( '' + '' + '' + '' + '' + '' + '
a1b1c1
c2
a3b3c3
' ), after: ( '' + '' + '' + '' + '' + '
a1b1c1c2c3
a3b3
' ) }, { message: 'Should reduce colspans to 2 keep the rowspan', before: ( '' + '' + '' + '' + '' + '' + '' + '
a1b1c1
a2
a3c3
c4
' ), after: ( '' + '' + '' + '' + '' + '' + '' + '
a1b1c1
a2
a3c3
c4
' ) }, */ /* { message: 'Should merge b1+c1 and reduce a2', before: ( '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1c1
a2
' ), after: ( '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1
c1
a2
' ) }, { message: 'Should merge a2+a3 and reduce b1', before: ( '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1
a2
a3
' ), after: ( '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1
a2
a3
' ) } */ });