import { context, describe, it } from '@ephox/bedrock-client'; import { SelectorFind, Width } from '@ephox/sugar'; import { TinyAssertions, TinyDom, TinyHooks } from '@ephox/wrap-mcagar'; import { assert } from 'chai'; import Editor from 'tinymce/core/api/Editor'; interface TableCommandMap { readonly mceTableInsertColBefore: number; readonly mceTableInsertColAfter: number; readonly mceTableDeleteCol: number; } describe('browser.tinymce.models.dom.table.ModifyColumnsTableResizeTest', () => { /** * Test the width of tables before and after column operations * There is a 2 pixel delta allowed from the expected width, to account for table borders */ const baseSettings = { base_url: '/project/tinymce/js/tinymce', }; const getTableWidth = (editor: Editor) => SelectorFind.child(TinyDom.body(editor), 'table').map(Width.get).getOrDie(); const assertWidth = (editor: Editor, multiplier: number, command: string) => { const beforeWidth = getTableWidth(editor); editor.selection.select(editor.dom.select('td[data-mce-selected]')[0], true); editor.execCommand(command); if (multiplier === 0) { TinyAssertions.assertContent(editor, ''); } else { const afterWidth = getTableWidth(editor); assert.approximately(afterWidth, beforeWidth * multiplier, 2, 'Assert table width'); } }; const performCommandsAndAssertWidths = (editor: Editor, content: string, multipliers: TableCommandMap) => { editor.setContent(content); assertWidth(editor, multipliers.mceTableInsertColBefore, 'mceTableInsertColBefore'); editor.setContent(content); assertWidth(editor, multipliers.mceTableInsertColAfter, 'mceTableInsertColAfter'); editor.setContent(content); assertWidth(editor, multipliers.mceTableDeleteCol, 'mceTableDeleteCol'); }; // TODO: more complex tests for colspan #TINY-6949 context('Responsive table', () => { context('table_column_resizing=preservetable', () => { const hook = TinyHooks.bddSetupLight({ ...baseSettings, table_sizing_mode: 'responsive', table_column_resizing: 'preservetable', }, []); it('TINY-6711: will resize table because responsive tables cannot honour this setting', () => { const editor = hook.editor(); const content = ( `
` ); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1.5, mceTableInsertColAfter: 1.5, mceTableDeleteCol: 0.5 }); }); it('TINY-6711: will resize when inserting multiple columns', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 2, mceTableInsertColAfter: 2, mceTableDeleteCol: 0 }); }); it('TINY-6711: will function with a colgroup', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 2, mceTableInsertColAfter: 2, mceTableDeleteCol: 0 }); }); it('TINY-6711: will function with a colspan', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1.5, mceTableInsertColAfter: 1.5, mceTableDeleteCol: 0.5 }); }); }); context('table_column_resizing=resizetable', () => { const hook = TinyHooks.bddSetupLight({ ...baseSettings, table_sizing_mode: 'responsive', table_column_resizing: 'resizetable', }, []); it('TINY-6711: should resize table when inserting a column', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1.5, mceTableInsertColAfter: 1.5, mceTableDeleteCol: 0.5 }); }); it('TINY-6711: will resize when inserting multiple columns', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 2, mceTableInsertColAfter: 2, mceTableDeleteCol: 0 }); }); it('TINY-6711: will function with a colgroup', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 2, mceTableInsertColAfter: 2, mceTableDeleteCol: 0 }); }); it('TINY-6711: will function with a colspan', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1.5, mceTableInsertColAfter: 1.5, mceTableDeleteCol: 0.5 }); }); }); }); context('Fixed width table', () => { context('table_column_resizing=preservetable', () => { const hook = TinyHooks.bddSetupLight({ ...baseSettings, table_sizing_mode: 'fixed', table_column_resizing: 'preservetable', }, []); it('TINY-6711: should preserve table width when inserting a column', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1, mceTableInsertColAfter: 1, mceTableDeleteCol: 1 }); }); it('TINY-6711: should preserve table width when inserting multiple columns', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1, mceTableInsertColAfter: 1, mceTableDeleteCol: 0 }); }); it('TINY-6711: will preserve width with a colgroup', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1, mceTableInsertColAfter: 1, mceTableDeleteCol: 0 }); }); it('TINY-6711: will preserve width with a colspan', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1, mceTableInsertColAfter: 1, mceTableDeleteCol: 1 }); }); }); context('table_column_resizing=resizetable', () => { const hook = TinyHooks.bddSetupLight({ ...baseSettings, table_sizing_mode: 'fixed', table_column_resizing: 'resizetable', }, []); it('TINY-6711: should resize table when inserting a column', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1.5, mceTableInsertColAfter: 1.5, mceTableDeleteCol: 0.5 }); }); it('TINY-6711: should resize table when inserting multiple columns', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 2, mceTableInsertColAfter: 2, mceTableDeleteCol: 0 }); }); it('TINY-6711: should resize table when using a colgroup', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 2, mceTableInsertColAfter: 2, mceTableDeleteCol: 0 }); }); it('TINY-6711: should resize table by single column width with a colspan', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { // 184/(184+242) === 0.432 so the table should be adjusted by 43.2% mceTableInsertColBefore: 1.432, mceTableInsertColAfter: 1.432, mceTableDeleteCol: 0.568 }); }); }); }); context('Relative table', () => { context('table_column_resizing=preservetable', () => { const hook = TinyHooks.bddSetupLight({ ...baseSettings, table_sizing_mode: 'relative', table_column_resizing: 'preservetable', }, []); it('TINY-6711: should preserve table width when inserting a column', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1, mceTableInsertColAfter: 1, mceTableDeleteCol: 1 }); }); it('TINY-6711: should preserve table width when inserting multiple columns', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1, mceTableInsertColAfter: 1, mceTableDeleteCol: 0 }); }); it('TINY-6711: should preserve table width when using a colgroup', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1, mceTableInsertColAfter: 1, mceTableDeleteCol: 0 }); }); it('TINY-6711: should preserve table width when using a colspan', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1, mceTableInsertColAfter: 1, mceTableDeleteCol: 1 }); }); }); context('table_column_resizing=resizetable', () => { const hook = TinyHooks.bddSetupLight({ ...baseSettings, table_sizing_mode: 'relative', table_column_resizing: 'resizetable', }, []); it('TINY-6711: should resize table when inserting a column', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1.5, mceTableInsertColAfter: 1.5, mceTableDeleteCol: 0.5 }); }); it('TINY-6711: should resize table when inserting multiple columns', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 2, mceTableInsertColAfter: 2, mceTableDeleteCol: 0 }); }); it('TINY-6711: should resize table when using a colgroup', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 2, mceTableInsertColAfter: 2, mceTableDeleteCol: 0 }); }); it('TINY-6711: should resize table width when using a colspan', () => { const editor = hook.editor(); const content = (`
`); performCommandsAndAssertWidths(editor, content, { mceTableInsertColBefore: 1.5, mceTableInsertColAfter: 1.5, mceTableDeleteCol: 0.5 }); }); }); }); });