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

Commit

Permalink
Merge pull request #225 from aStewartDesign-t/ckeditor5/5744
Browse files Browse the repository at this point in the history
Fix: The `MergeCellCommand` will not merge column header cells with body cells.
  • Loading branch information
jodator committed Nov 21, 2019
2 parents 63da06f + c98cfa6 commit c9c6954
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/commands/mergecellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ export default class MergeCellCommand extends Command {
// @param {String} direction
// @returns {module:engine/model/node~Node|null}
function getHorizontalCell( tableCell, direction, tableUtils ) {
const tableRow = tableCell.parent;
const table = tableRow.parent;
const horizontalCell = direction == 'right' ? tableCell.nextSibling : tableCell.previousSibling;
const headingColumns = table.getAttribute( 'headingColumns' ) || 0;

if ( !horizontalCell ) {
return;
Expand All @@ -168,6 +171,15 @@ 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 );

// 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;
}

// The cell on the right must have index that is distant to the cell on the left by the left cell's width (colspan).
const cellsAreTouching = leftCellColumn + leftCellSpan === rightCellColumn;
Expand Down
56 changes: 56 additions & 0 deletions tests/commands/mergecellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,46 @@ describe( 'MergeCellCommand', () => {

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

it( 'should be false if mergeable cell is in other table section then current cell', () => {
setData( model, modelTable( [
[ '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 @@ -273,6 +313,22 @@ describe( 'MergeCellCommand', () => {

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

it( 'should be false if mergeable cell is in other table section then current cell', () => {
setData( model, modelTable( [
[ '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( [
[ { colspan: 2, contents: '00' }, '02[]', '03' ]
], { headingColumns: 2 } ) );

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

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

0 comments on commit c9c6954

Please sign in to comment.