| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
- import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import MergeCellsCommand from '../../src/commands/mergecellscommand';
- import { modelTable } from '../_utils/utils';
- import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
- import TableSelection from '../../src/tableselection';
- import TableEditing from '../../src/tableediting';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- describe( 'MergeCellsCommand', () => {
- let editor, model, command, root, tableSelection;
- beforeEach( async () => {
- editor = await ModelTestEditor.create( {
- plugins: [ Paragraph, TableEditing, TableSelection ]
- } );
- model = editor.model;
- root = model.document.getRoot( 'main' );
- tableSelection = editor.plugins.get( TableSelection );
- command = new MergeCellsCommand( editor );
- } );
- afterEach( () => {
- return editor.destroy();
- } );
- describe( 'isEnabled', () => {
- it( 'should be false if collapsed selection in table cell', () => {
- setData( model, modelTable( [
- [ '00[]', '01' ]
- ] ) );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be false if only one table cell is selected', () => {
- setData( model, modelTable( [
- [ '00', '01' ]
- ] ) );
- selectNodes( [ [ 0, 0, 0 ] ] );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be true if at least two adjacent table cells are selected', () => {
- setData( model, modelTable( [
- [ '00', '01' ]
- ] ) );
- selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be true if many table cells are selected', () => {
- setData( model, modelTable( [
- [ '00', '01', '02', '03' ],
- [ '10', '11', '12', '13' ],
- [ '20', '21', '22', '23' ],
- [ '30', '31', '32', '33' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 1 ], [ 0, 0, 2 ],
- [ 0, 1, 1 ], [ 0, 1, 2 ],
- [ 0, 2, 1 ], [ 0, 2, 2 ],
- [ 0, 3, 1 ], [ 0, 3, 2 ]
- ] );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be false if at least one table cell is not selected from an area', () => {
- setData( model, modelTable( [
- [ '00', '01', '02', '03' ],
- [ '10', '11', '12', '13' ],
- [ '20', '21', '22', '23' ],
- [ '30', '31', '32', '33' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 1 ], [ 0, 0, 2 ],
- [ 0, 1, 2 ], // one table cell not selected from this row
- [ 0, 2, 1 ], [ 0, 2, 2 ],
- [ 0, 3, 1 ], [ 0, 3, 2 ]
- ] );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be false if table cells are not in adjacent rows', () => {
- setData( model, modelTable( [
- [ '00', '01' ],
- [ '10', '11' ]
- ] ) );
- selectNodes( [
- [ 0, 1, 0 ],
- [ 0, 0, 1 ]
- ] );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be false if table cells are not in adjacent columns', () => {
- setData( model, modelTable( [
- [ '00', '01', '02' ]
- ] ) );
- selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 2 ] ] );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be false if any table cell with colspan attribute extends over selection area', () => {
- setData( model, modelTable( [
- [ '00', { colspan: 2, contents: '01' } ],
- [ '10', '11', '12' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 0 ], [ 0, 0, 1 ],
- [ 0, 1, 0 ], [ 0, 1, 1 ]
- ] );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be true if none table cell with colspan attribute extends over selection area', () => {
- setData( model, modelTable( [
- [ '00', { colspan: 2, contents: '01' } ],
- [ '10', '11', '12' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 0 ], [ 0, 0, 1 ],
- [ 0, 1, 0 ], [ 0, 1, 1 ],
- [ 0, 1, 2 ]
- ] );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be true if first table cell is inside selection area', () => {
- setData( model, modelTable( [
- [ { colspan: 2, rowspan: 2, contents: '00' }, '02', '03' ],
- [ '12', '13' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 0 ], [ 0, 0, 1 ],
- [ 0, 1, 0 ]
- ] );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be false if any table cell with rowspan attribute extends over selection area', () => {
- setData( model, modelTable( [
- [ '00', { rowspan: 2, contents: '01' } ],
- [ '10' ]
- ] ) );
- selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be true if none table cell with rowspan attribute extends over selection area', () => {
- setData( model, modelTable( [
- [ '00', { rowspan: 2, contents: '01' } ],
- [ '10' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 0 ], [ 0, 0, 1 ],
- [ 0, 1, 0 ]
- ] );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be false if not in a cell', () => {
- setData( model, '<paragraph>11[]</paragraph>' );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be false if selection has cells from header and body sections', () => {
- setData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ], { headingRows: 1 } ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 0, 0 ] ),
- root.getNodeByPath( [ 0, 1, 0 ] )
- );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be false if more than 10 rows selected and some are in heading section', () => {
- setData( model, modelTable( [
- [ '0' ],
- [ '1' ],
- [ '2' ],
- [ '3' ],
- [ '4' ],
- [ '5' ],
- [ '6' ],
- [ '7' ],
- [ '8' ],
- [ '9' ],
- [ '10' ],
- [ '11' ],
- [ '12' ],
- [ '13' ],
- [ '14' ]
- ], { headingRows: 10 } ) );
- const tableSelection = editor.plugins.get( TableSelection );
- const modelRoot = model.document.getRoot();
- tableSelection._setCellSelection(
- modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
- modelRoot.getNodeByPath( [ 0, 12, 0 ] )
- );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be true if selection has cells only from column headers - rows in body section', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02', '03' ],
- [ '10', '11', '12', '13' ]
- ], { headingColumns: 2 } ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 0, 0 ] ),
- root.getNodeByPath( [ 0, 1, 1 ] )
- );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be false if selection has cells from column headers and other cells - rows in body section', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02', '03' ],
- [ '10', '11', '12', '13' ]
- ], { headingColumns: 2 } ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 0, 0 ] ),
- root.getNodeByPath( [ 0, 1, 2 ] )
- );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be true if selection has cells only from column headers - rows in header section', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02', '03' ],
- [ '10', '11', '12', '13' ]
- ], { headingColumns: 2, headingRows: 1 } ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 0, 0 ] ),
- root.getNodeByPath( [ 0, 0, 1 ] )
- );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'should be false if selection has cells only from column headers and other cells - rows in header section', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02', '03' ],
- [ '10', '11', '12', '13' ]
- ], { headingColumns: 2, headingRows: 1 } ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 0, 0 ] ),
- root.getNodeByPath( [ 0, 0, 2 ] )
- );
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'should be false if selection has cells from column headers, row headers and body sections', () => {
- setData( model, modelTable( [
- [ '00[]', '01', '02', '03' ],
- [ '10', '11', '12', '13' ]
- ], { headingColumns: 2, headingRows: 1 } ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 0, 0 ] ),
- root.getNodeByPath( [ 0, 1, 2 ] )
- );
- expect( command.isEnabled ).to.be.false;
- } );
- } );
- describe( 'execute()', () => {
- it( 'should merge simple table cell selection', () => {
- setData( model, modelTable( [
- [ '[]00', '01' ]
- ] ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 0, 0 ] ),
- root.getNodeByPath( [ 0, 0, 1 ] )
- );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ { colspan: 2, contents: '<paragraph>[00</paragraph><paragraph>01]</paragraph>' } ]
- ] ) );
- } );
- it( 'should merge selection with a cell with rowspan in the selection', () => {
- setData( model, modelTable( [
- [ '[]00', '01', '02' ],
- [ '10', { contents: '11', rowspan: 2 }, '12' ],
- [ '20', '22' ]
- ] ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 1, 0 ] ),
- root.getNodeByPath( [ 0, 2, 1 ] )
- );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ '00', '01', '02' ],
- [ {
- colspan: 3,
- contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
- '<paragraph>20</paragraph><paragraph>22]</paragraph>'
- } ]
- ] ) );
- } );
- it( 'should merge selection with a cell with rowspan in the selection (reverse selection)', () => {
- setData( model, modelTable( [
- [ '[]00', '01', '02' ],
- [ '10', { contents: '11', rowspan: 2 }, '12' ],
- [ '20', '22' ]
- ] ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 2, 1 ] ),
- root.getNodeByPath( [ 0, 1, 0 ] )
- );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ '00', '01', '02' ],
- [ {
- colspan: 3,
- contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
- '<paragraph>20</paragraph><paragraph>22]</paragraph>'
- } ]
- ] ) );
- } );
- it( 'should merge selection inside a table (properly calculate target rowspan/colspan)', () => {
- setData( model, modelTable( [
- [ '[]00', '01', '02', '03' ],
- [ '10', '11', { contents: '12', rowspan: 2 }, '13' ],
- [ '20', '21', '23' ],
- [ '30', '31', '32', '33' ]
- ] ) );
- tableSelection._setCellSelection(
- root.getNodeByPath( [ 0, 2, 1 ] ),
- root.getNodeByPath( [ 0, 1, 2 ] )
- );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ '00', '01', '02', '03' ],
- [ '10', {
- colspan: 2,
- rowspan: 2,
- contents: '<paragraph>[11</paragraph><paragraph>12</paragraph><paragraph>21]</paragraph>'
- }, '13' ],
- [ '20', '23' ],
- [ '30', '31', '32', '33' ]
- ] ) );
- } );
- it( 'should merge table cells - extend colspan attribute', () => {
- setData( model, modelTable( [
- [ { colspan: 2, contents: '00' }, '02', '03' ],
- [ '10', '11', '12', '13' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 0 ], [ 0, 0, 1 ],
- [ 0, 1, 0 ], [ 0, 1, 1 ], [ 0, 1, 2 ]
- ] );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ {
- colspan: 3,
- rowspan: 2,
- contents: '<paragraph>[00</paragraph>' +
- '<paragraph>02</paragraph>' +
- '<paragraph>10</paragraph>' +
- '<paragraph>11</paragraph>' +
- '<paragraph>12]</paragraph>'
- }, '03' ],
- [ '13' ]
- ] ) );
- } );
- it( 'should merge to a single paragraph - every cell is empty', () => {
- setData( model, modelTable( [
- [ '[]', '' ]
- ] ) );
- selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ]
- ] ) );
- } );
- it( 'should merge to a single paragraph - merged cell is empty', () => {
- setData( model, modelTable( [
- [ 'foo', '' ]
- ] ) );
- selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
- ] ) );
- } );
- it( 'should merge to a single paragraph - cell to which others are merged is empty', () => {
- setData( model, modelTable( [
- [ '', 'foo' ]
- ] ) );
- selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
- ] ) );
- } );
- it( 'should not merge empty blocks other then <paragraph> to a single block', () => {
- model.schema.register( 'block', {
- allowWhere: '$block',
- allowContentOf: '$block',
- isBlock: true
- } );
- setData( model, modelTable( [
- [ '<block>[]</block>', '<block></block>' ]
- ] ) );
- selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ]
- ] ) );
- } );
- describe( 'removing empty row', () => {
- it( 'should remove empty row if merging all table cells from that row', () => {
- setData( model, modelTable( [
- [ '00' ],
- [ '10' ],
- [ '20' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 0 ],
- [ 0, 1, 0 ],
- [ 0, 2, 0 ]
- ] );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [
- '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>'
- ]
- ] ) );
- } );
- it( 'should decrease rowspan if cell overlaps removed row', () => {
- setData( model, modelTable( [
- [ '00', { rowspan: 2, contents: '01' }, { rowspan: 3, contents: '02' } ],
- [ '10' ],
- [ '20', '21' ]
- ] ) );
- selectNodes( [
- [ 0, 0, 0 ],
- [ 0, 1, 0 ],
- [ 0, 2, 0 ]
- ] );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [
- { rowspan: 2, contents: '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>' },
- '01',
- { rowspan: 2, contents: '02' }
- ],
- [ '21' ]
- ] ) );
- } );
- it( 'should not decrease rowspan if cell from previous row does not overlaps removed row', () => {
- setData( model, modelTable( [
- [ '00', { rowspan: 2, contents: '01' } ],
- [ '10' ],
- [ '20', '21' ],
- [ '30', '31' ]
- ] ) );
- selectNodes( [
- [ 0, 2, 0 ], [ 0, 2, 1 ],
- [ 0, 3, 0 ], [ 0, 3, 1 ]
- ] );
- command.execute();
- assertEqualMarkup( getData( model ), modelTable( [
- [ '00', { rowspan: 2, contents: '01' } ],
- [ '10' ],
- [
- {
- colspan: 2,
- contents: '<paragraph>[20</paragraph><paragraph>21</paragraph>' +
- '<paragraph>30</paragraph><paragraph>31]</paragraph>'
- }
- ]
- ] ) );
- } );
- } );
- } );
- function selectNodes( paths ) {
- model.change( writer => {
- const ranges = paths.map( path => writer.createRangeOn( root.getNodeByPath( path ) ) );
- writer.setSelection( ranges );
- } );
- }
- } );
|