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(
'
'
);
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()),
'' +
'' +
'| 1 | 2 |
' +
'| 1 | 2 |
' +
'| 2 | 3 |
' +
'' +
'
'
);
selectOne(editor, 'tr:nth-child(2) td');
editor.execCommand('mceTablePasteRowBefore');
LegacyUnit.equal(
cleanTableHtml(editor.getContent()),
'' +
'' +
'| 1 | 2 |
' +
'| 1 | 2 |
' +
'| 1 | 2 |
' +
'| 2 | 3 |
' +
'' +
'
'
);
});
suite.test('mceTablePasteRowAfter command', function (editor) {
editor.setContent(
''
);
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()),
'' +
'' +
'| 1 | 2 |
' +
'| 2 | 3 |
' +
'| 1 | 2 |
' +
'' +
'
'
);
selectOne(editor, 'tr:nth-child(2) td');
editor.execCommand('mceTablePasteRowAfter');
LegacyUnit.equal(
cleanTableHtml(editor.getContent()),
'' +
'' +
'| 1 | 2 |
' +
'| 2 | 3 |
' +
'| 1 | 2 |
' +
'| 1 | 2 |
' +
'' +
'
'
);
});
suite.test('mceTablePasteRowAfter command with thead and tfoot', function (editor) {
editor.setContent(
'' +
'' +
'| Head1 | Head2 |
' +
'' +
'' +
'| a | b |
' +
'| c | d |
' +
'' +
'' +
'| Foot1 | Foot2 |
' +
'' +
'
'
);
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()),
'' +
'' +
'| Head1 | Head2 |
' +
'' +
'' +
'| a | b |
' +
'| c | d |
' +
'| a | b |
' +
'' +
'' +
'| Foot1 | Foot2 |
' +
'' +
'
'
);
selectOne(editor, 'tbody tr:nth-child(2) td');
editor.execCommand('mceTablePasteRowAfter');
LegacyUnit.equal(
cleanTableHtml(editor.getContent()),
'' +
'' +
'| Head1 | Head2 |
' +
'' +
'' +
'| a | b |
' +
'| c | d |
' +
'| a | b |
' +
'| a | b |
' +
'' +
'' +
'| Foot1 | Foot2 |
' +
'' +
'
'
);
});
suite.test('mceTablePasteRowAfter from merged row source', function (editor) {
editor.setContent(
'' +
'' +
'| 1 2 | 3 |
' +
'| 1 | 2 |
' +
'' +
'
'
);
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 2 | 3 |
' +
'| 1 | 2 |
' +
'| 1 2 | 3 |
' +
'' +
'
'
);
});
suite.test('mceTablePasteRowAfter from merged row source to merged row target', function (editor) {
editor.setContent(
'' +
'' +
'| 1 2 | 3 |
' +
'| 1 | 2 |
' +
'' +
'
'
);
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 2 | 3 |
' +
'| 1 2 | 3 |
' +
'| 1 | 2 | |
' +
'' +
'
'
);
});
suite.test('mceTablePasteRowAfter to wider table', function (editor) {
editor.setContent(
'' +
'' +
'| 1a | 2a | 3a |
' +
'' +
'
' +
'' +
'' +
'| 1b | 2b | 3b | 4b |
' +
'' +
'
'
);
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()),
'' +
'' +
'| 1a | 2a | 3a |
' +
'' +
'
' +
'' +
'' +
'| 1b | 2b | 3b | 4b |
' +
'| 1a | 2a | 3a | |
' +
'' +
'
'
);
});
suite.test('mceTablePasteRowAfter to narrower table', function (editor) {
editor.setContent(
'' +
'' +
'| 1a | 2a | 3a | 4a | 5a |
' +
'| 1a | 2a | 5a |
' +
'' +
'
' +
'' +
'' +
'| 1b | 2b | 3b |
' +
'' +
'
'
);
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()),
'' +
'' +
'| 1a | 2a | 3a | 4a | 5a |
' +
'| 1a | 2a | 5a |
' +
'' +
'
' +
'' +
'' +
'| 1b | 2b | 3b | | |
' +
'| 1a | 2a | 3a | 4a | 5a |
' +
'| 1a | 2a | 5a |
' +
'' +
'
'
);
});
suite.test('Copy/paste several rows with multiple rowspans', function (editor) {
editor.setContent(
'' +
'' +
'| 1 | 2 | 3 |
' +
'| 2 | 3 |
' +
'| 1 | 2 |
' +
'| 1 | 2 |
' +
'' +
'
'
);
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()),
'' +
'' +
'| 1 | 2 | 3 |
' +
'| 2 | 3 |
' +
'| 1 | 2 |
' +
'| 1 | 2 |
' +
'| 1 | 2 | 3 |
' +
'| 2 | 3 |
' +
'| 1 | 2 |
' +
'' +
'
'
);
});
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(
''
);
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()),
'' +
'' +
'| 1 | 2 |
' +
'| 2 | 3 |
' +
'| 1 | 2 |
' +
'| a | b |
' +
'| c | d |
' +
'' +
'
'
);
});
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);
});