import { Pipeline } from '@ephox/agar'; import { UnitTest } from '@ephox/bedrock'; import { LegacyUnit, TinyLoader } from '@ephox/mcagar'; import Tools from 'tinymce/core/api/util/Tools'; import Plugin from 'tinymce/plugins/table/Plugin'; import Theme from 'tinymce/themes/modern/Theme'; UnitTest.asynctest('browser.tinymce.plugins.table.ClipboardTest', function () { const success = arguments[arguments.length - 2]; const failure = arguments[arguments.length - 1]; const suite = LegacyUnit.createSuite(); Plugin(); Theme(); const cleanTableHtml = function (html) { return html.replace(/

( |]+>)<\/p>$/, ''); }; const selectOne = function (editor, start) { start = editor.$(start)[0]; editor.fire('mousedown', { target: start, button: 0 }); editor.fire('mouseup', { target: start, button: 0 }); LegacyUnit.setSelection(editor, start, 0); }; const selectRangeXY = function (editor, start, end) { start = editor.$(start)[0]; end = editor.$(end)[0]; editor.fire('mousedown', { target: start, button: 0 }); editor.fire('mouseover', { target: end, button: 0 }); editor.fire('mouseup', { target: end, button: 0 }); LegacyUnit.setSelection(editor, end, 0); }; suite.test('mceTablePasteRowBefore command', function (editor) { editor.focus(); editor.setContent( '' + '' + '' + '
12
23
' ); selectOne(editor, 'tr:nth-child(1) td'); editor.execCommand('mceTableCopyRow'); selectOne(editor, 'tr:nth-child(2) td'); editor.execCommand('mceTablePasteRowBefore'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '
12
12
23
' ); selectOne(editor, 'tr:nth-child(2) td'); editor.execCommand('mceTablePasteRowBefore'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '' + '
12
12
12
23
' ); }); suite.test('mceTablePasteRowAfter command', function (editor) { editor.setContent( '' + '' + '' + '
12
23
' ); selectOne(editor, 'tr:nth-child(1) td'); editor.execCommand('mceTableCopyRow'); selectOne(editor, 'tr:nth-child(2) td'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '
12
23
12
' ); selectOne(editor, 'tr:nth-child(2) td'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '' + '
12
23
12
12
' ); }); suite.test('mceTablePasteRowAfter command with thead and tfoot', function (editor) { editor.setContent( '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
Head1Head2
ab
cd
Foot1Foot2
' ); selectOne(editor, 'tbody tr:nth-child(1) td'); editor.execCommand('mceTableCopyRow'); selectOne(editor, 'tbody tr:nth-child(2) td'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
Head1Head2
ab
cd
ab
Foot1Foot2
' ); selectOne(editor, 'tbody tr:nth-child(2) td'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
Head1Head2
ab
cd
ab
ab
Foot1Foot2
' ); }); suite.test('mceTablePasteRowAfter from merged row source', function (editor) { editor.setContent( '' + '' + '' + '' + '' + '
1 23
12
' ); selectOne(editor, 'tr:nth-child(1) td'); editor.execCommand('mceTableCopyRow'); selectOne(editor, 'tr:nth-child(2) td:nth-child(2)'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '
1 23
12
1 23
' ); }); suite.test('mceTablePasteRowAfter from merged row source to merged row target', function (editor) { editor.setContent( '' + '' + '' + '' + '' + '
1 23
12
' ); selectOne(editor, 'tr:nth-child(1) td'); editor.execCommand('mceTableCopyRow'); selectOne(editor, 'tr:nth-child(1) td'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '
1 23
1 23
12 
' ); }); suite.test('mceTablePasteRowAfter to wider table', function (editor) { editor.setContent( '' + '' + '' + '' + '
1a2a3a
' + '' + '' + '' + '' + '
1b2b3b4b
' ); selectOne(editor, 'table:nth-child(1) tr:nth-child(1) td'); editor.execCommand('mceTableCopyRow'); selectOne(editor, 'table:nth-child(2) td'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '
1a2a3a
' + '' + '' + '' + '' + '' + '
1b2b3b4b
1a2a3a 
' ); }); suite.test('mceTablePasteRowAfter to narrower table', function (editor) { editor.setContent( '' + '' + '' + '' + '' + '
1a2a3a4a5a
1a2a5a
' + '' + '' + '' + '' + '
1b2b3b
' ); selectRangeXY(editor, 'table:nth-child(1) tr:nth-child(1) td:nth-child(1)', 'table:nth-child(1) tr:nth-child(2) td:nth-child(3)'); editor.execCommand('mceTableCopyRow'); selectOne(editor, 'table:nth-child(2) tr td'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '
1a2a3a4a5a
1a2a5a
' + '' + '' + '' + '' + '' + '' + '
1b2b3b  
1a2a3a4a5a
1a2a5a
' ); }); suite.test('Copy/paste several rows with multiple rowspans', function (editor) { editor.setContent( '' + '' + '' + '' + '' + '' + '' + '
123
23
12
12
' ); selectRangeXY(editor, 'table tr:nth-child(1) td:nth-child(1)', 'table tr:nth-child(3) td:nth-child(2)'); editor.execCommand('mceTableCopyRow'); selectOne(editor, 'table tr:nth-child(4) td'); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
123
23
12
12
123
23
12
' ); }); suite.test('row clipboard api', function (editor) { let clipboardRows; function createRow(cellContents) { const tr = editor.dom.create('tr'); Tools.each(cellContents, function (html) { tr.appendChild(editor.dom.create('td', null, html)); }); return tr; } editor.setContent( '' + '' + '' + '
12
23
' ); LegacyUnit.setSelection(editor, 'tr:nth-child(1) td', 0); editor.execCommand('mceTableCopyRow'); clipboardRows = editor.plugins.table.getClipboardRows(); LegacyUnit.equal(clipboardRows.length, 1); LegacyUnit.equal(clipboardRows[0].tagName, 'TR'); editor.plugins.table.setClipboardRows(clipboardRows.concat([ createRow(['a', 'b']), createRow(['c', 'd']) ])); LegacyUnit.setSelection(editor, 'tr:nth-child(2) td', 0); editor.execCommand('mceTablePasteRowAfter'); LegacyUnit.equal( cleanTableHtml(editor.getContent()), '' + '' + '' + '' + '' + '' + '' + '' + '
12
23
12
ab
cd
' ); }); TinyLoader.setup(function (editor, onSuccess, onFailure) { Pipeline.async({}, suite.toSteps(editor), onSuccess, onFailure); }, { plugins: 'table', indent: false, valid_styles: { '*': 'width,height,vertical-align,text-align,float,border-color,background-color,border,padding,border-spacing,border-collapse' }, skin_url: '/project/js/tinymce/skins/lightgray' }, success, failure); });