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

Commit c9c6954

Browse files
committed
Merge pull request #225 from aStewartDesign-t/ckeditor5/5744
Fix: The `MergeCellCommand` will not merge column header cells with body cells.
2 parents 63da06f + c98cfa6 commit c9c6954

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/commands/mergecellcommand.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ export default class MergeCellCommand extends Command {
153153
// @param {String} direction
154154
// @returns {module:engine/model/node~Node|null}
155155
function getHorizontalCell( tableCell, direction, tableUtils ) {
156+
const tableRow = tableCell.parent;
157+
const table = tableRow.parent;
156158
const horizontalCell = direction == 'right' ? tableCell.nextSibling : tableCell.previousSibling;
159+
const headingColumns = table.getAttribute( 'headingColumns' ) || 0;
157160

158161
if ( !horizontalCell ) {
159162
return;
@@ -168,6 +171,15 @@ function getHorizontalCell( tableCell, direction, tableUtils ) {
168171
const { column: rightCellColumn } = tableUtils.getCellLocation( cellOnRight );
169172

170173
const leftCellSpan = parseInt( cellOnLeft.getAttribute( 'colspan' ) || 1 );
174+
const rightCellSpan = parseInt( cellOnRight.getAttribute( 'colspan' ) || 1 );
175+
176+
// We cannot merge cells if the result will extend over heading section.
177+
const isMergeWithBodyCell = direction == 'right' && ( rightCellColumn + rightCellSpan > headingColumns );
178+
const isMergeWithHeadCell = direction == 'left' && ( leftCellColumn + leftCellSpan > headingColumns - 1 );
179+
180+
if ( headingColumns && ( isMergeWithBodyCell || isMergeWithHeadCell ) ) {
181+
return;
182+
}
171183

172184
// The cell on the right must have index that is distant to the cell on the left by the left cell's width (colspan).
173185
const cellsAreTouching = leftCellColumn + leftCellSpan === rightCellColumn;

tests/commands/mergecellcommand.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,46 @@ describe( 'MergeCellCommand', () => {
9393

9494
expect( command.isEnabled ).to.be.false;
9595
} );
96+
97+
it( 'should be false if mergeable cell is in other table section then current cell', () => {
98+
setData( model, modelTable( [
99+
[ '00[]', '01' ]
100+
], { headingColumns: 1 } ) );
101+
102+
expect( command.isEnabled ).to.be.false;
103+
} );
104+
105+
it( 'should be false if merged cell would cross heading section (mergeable cell with colspan)', () => {
106+
setData( model, modelTable( [
107+
[ '00[]', { colspan: 2, contents: '01' }, '02', '03' ]
108+
], { headingColumns: 2 } ) );
109+
110+
expect( command.isEnabled ).to.be.false;
111+
} );
112+
113+
it( 'should be true if merged cell would not cross heading section (mergeable cell with colspan)', () => {
114+
setData( model, modelTable( [
115+
[ '00[]', { colspan: 2, contents: '01' }, '02', '03' ]
116+
], { headingColumns: 3 } ) );
117+
118+
expect( command.isEnabled ).to.be.true;
119+
} );
120+
121+
it( 'should be false if merged cell would cross heading section (current cell with colspan)', () => {
122+
setData( model, modelTable( [
123+
[ { colspan: 2, contents: '00[]' }, '01', '02', '03' ]
124+
], { headingColumns: 2 } ) );
125+
126+
expect( command.isEnabled ).to.be.false;
127+
} );
128+
129+
it( 'should be true if merged cell would not cross heading section (current cell with colspan)', () => {
130+
setData( model, modelTable( [
131+
[ { colspan: 2, contents: '00[]' }, '01', '02', '03' ]
132+
], { headingColumns: 3 } ) );
133+
134+
expect( command.isEnabled ).to.be.true;
135+
} );
96136
} );
97137

98138
describe( 'value', () => {
@@ -273,6 +313,22 @@ describe( 'MergeCellCommand', () => {
273313

274314
expect( command.isEnabled ).to.be.false;
275315
} );
316+
317+
it( 'should be false if mergeable cell is in other table section then current cell', () => {
318+
setData( model, modelTable( [
319+
[ '00', '01[]' ],
320+
], { headingColumns: 1 } ) );
321+
322+
expect( command.isEnabled ).to.be.false;
323+
} );
324+
325+
it( 'should be false if merged cell would cross heading section (mergeable cell with colspan)', () => {
326+
setData( model, modelTable( [
327+
[ { colspan: 2, contents: '00' }, '02[]', '03' ]
328+
], { headingColumns: 2 } ) );
329+
330+
expect( command.isEnabled ).to.be.false;
331+
} );
276332
} );
277333

278334
describe( 'value', () => {

0 commit comments

Comments
 (0)