Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Update the logic to not allow merging spanned cells.
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Nov 20, 2019
1 parent fcba087 commit c98cfa6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/commands/mergecellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ function getHorizontalCell( tableCell, direction, tableUtils ) {
const { column: rightCellColumn } = tableUtils.getCellLocation( cellOnRight );

const leftCellSpan = parseInt( cellOnLeft.getAttribute( 'colspan' ) || 1 );
const rightCellSpan = parseInt( cellOnRight.getAttribute( 'colspan' ) || 1 );

const isMergeWithBodyCell = direction == 'right' && rightCellColumn === headingColumns;
const isMergeWithHeadCell = direction == 'left' && leftCellColumn === ( headingColumns - 1 );
// We cannot merge cells if the result will extend over heading section.
const isMergeWithBodyCell = direction == 'right' && ( rightCellColumn + rightCellSpan > headingColumns );
const isMergeWithHeadCell = direction == 'left' && ( leftCellColumn + leftCellSpan > headingColumns - 1 );

if ( headingColumns && ( isMergeWithBodyCell || isMergeWithHeadCell ) ) {
return;
Expand Down
42 changes: 41 additions & 1 deletion tests/commands/mergecellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,43 @@ describe( 'MergeCellCommand', () => {

it( 'should be false if mergeable cell is in other table section then current cell', () => {
setData( model, modelTable( [
[ '00[]', '01' ],
[ '00[]', '01' ]
], { headingColumns: 1 } ) );

expect( command.isEnabled ).to.be.false;
} );

it( 'should be false if merged cell would cross heading section (mergeable cell with colspan)', () => {
setData( model, modelTable( [
[ '00[]', { colspan: 2, contents: '01' }, '02', '03' ]
], { headingColumns: 2 } ) );

expect( command.isEnabled ).to.be.false;
} );

it( 'should be true if merged cell would not cross heading section (mergeable cell with colspan)', () => {
setData( model, modelTable( [
[ '00[]', { colspan: 2, contents: '01' }, '02', '03' ]
], { headingColumns: 3 } ) );

expect( command.isEnabled ).to.be.true;
} );

it( 'should be false if merged cell would cross heading section (current cell with colspan)', () => {
setData( model, modelTable( [
[ { colspan: 2, contents: '00[]' }, '01', '02', '03' ]
], { headingColumns: 2 } ) );

expect( command.isEnabled ).to.be.false;
} );

it( 'should be true if merged cell would not cross heading section (current cell with colspan)', () => {
setData( model, modelTable( [
[ { colspan: 2, contents: '00[]' }, '01', '02', '03' ]
], { headingColumns: 3 } ) );

expect( command.isEnabled ).to.be.true;
} );
} );

describe( 'value', () => {
Expand Down Expand Up @@ -289,6 +321,14 @@ describe( 'MergeCellCommand', () => {

expect( command.isEnabled ).to.be.false;
} );

it( 'should be false if merged cell would cross heading section (mergeable cell with colspan)', () => {
setData( model, modelTable( [
[ { colspan: 2, contents: '00' }, '02[]', '03' ]
], { headingColumns: 2 } ) );

expect( command.isEnabled ).to.be.false;
} );
} );

describe( 'value', () => {
Expand Down

0 comments on commit c98cfa6

Please sign in to comment.