瀏覽代碼

Add tests for getVerticallyOverlappingCells() and getHorizontallyOverlappingCells().

Maciej Gołaszewski 5 年之前
父節點
當前提交
8176e23a45
共有 2 個文件被更改,包括 98 次插入9 次删除
  1. 9 8
      packages/ckeditor5-table/src/utils.js
  2. 89 1
      packages/ckeditor5-table/tests/utils.js

+ 9 - 8
packages/ckeditor5-table/src/utils.js

@@ -248,7 +248,7 @@ export function isSelectionRectangular( selectedTableCells, tableUtils ) {
 }
 
 /**
- * Returns cells that starts above and overlaps a given row.
+ * Returns slot info of cells that starts above and overlaps a given row.
  *
  * In a table below, passing `overlapRow = 3`
  *
@@ -264,11 +264,11 @@ export function isSelectionRectangular( selectedTableCells, tableUtils ) {
  *    4  │ o │ p │   │   │ q │
  *       └───┴───┴───┴───┴───┘
  *
- * will return cells: "j", "f", "k".
+ * will return slot info for cells: "j", "f", "k".
  *
  * @param {module:engine/model/element~Element} table The table to check.
  * @param {Number} overlapRow The index of the row to check.
- * @param {Number} [startRow=0] A row to start analysis if it is known where.
+ * @param {Number} [startRow=0] A row to start analysis. Use it when it is known that cells below that row will not overlap.
  * @returns {Array.<module:table/tablewalker~TableWalkerValue>}
  */
 export function getVerticallyOverlappingCells( table, overlapRow, startRow = 0 ) {
@@ -278,8 +278,9 @@ export function getVerticallyOverlappingCells( table, overlapRow, startRow = 0 )
 
 	for ( const slotInfo of tableWalker ) {
 		const { row, rowspan } = slotInfo;
+		const slotEndRow = row + rowspan - 1;
 
-		if ( rowspan > 1 && row + rowspan > overlapRow ) {
+		if ( row < overlapRow && overlapRow <= slotEndRow ) {
 			cells.push( slotInfo );
 		}
 	}
@@ -339,7 +340,7 @@ export function splitHorizontally( tableCell, splitRow, writer ) {
 }
 
 /**
- * Returns cells that starts before and overlaps a given column.
+ * Returns slot info of cells that starts before and overlaps a given column.
  *
  * In a table below, passing `overlapColumn = 3`
  *
@@ -358,7 +359,7 @@ export function splitHorizontally( tableCell, splitRow, writer ) {
  *                  ^
  *                  Overlap column to check
  *
- * will return cells: "b", "e", "i".
+ * will return slot info for cells: "b", "e", "i".
  *
  * @param {module:engine/model/element~Element} table The table to check.
  * @param {Number} overlapColumn The index of the column to check.
@@ -371,9 +372,9 @@ export function getHorizontallyOverlappingCells( table, overlapColumn ) {
 
 	for ( const slotInfo of tableWalker ) {
 		const { column, colspan } = slotInfo;
-		const endColumn = column + colspan - 1;
+		const slotEndColumn = column + colspan - 1;
 
-		if ( column < overlapColumn && overlapColumn <= endColumn ) {
+		if ( column < overlapColumn && overlapColumn <= slotEndColumn ) {
 			cellsToSplit.push( slotInfo );
 		}
 	}

+ 89 - 1
packages/ckeditor5-table/tests/utils.js

@@ -13,7 +13,7 @@ import { modelTable } from './_utils/utils';
 import {
 	getSelectedTableCells,
 	getTableCellsContainingSelection,
-	getSelectionAffectedTableCells
+	getSelectionAffectedTableCells, getVerticallyOverlappingCells, getHorizontallyOverlappingCells
 } from '../src/utils';
 
 describe( 'table utils', () => {
@@ -359,4 +359,92 @@ describe( 'table utils', () => {
 			expect( getSelectionAffectedTableCells( selection ) ).to.be.empty;
 		} );
 	} );
+
+	describe( 'getVerticallyOverlappingCells()', () => {
+		let table;
+
+		beforeEach( () => {
+			// +----+----+----+----+----+
+			// | 00 | 01 | 02 | 03 | 04 |
+			// +    +    +----+    +----+
+			// |    |    | 12 |    | 14 |
+			// +    +    +    +----+----+
+			// |    |    |    | 23 | 24 |
+			// +    +----+    +    +----+
+			// |    | 31 |    |    | 34 |
+			// +    +    +----+----+----+
+			// |    |    | 42 | 43 | 44 |
+			// +----+----+----+----+----+
+			setModelData( model, modelTable( [
+				[ { contents: '00', rowspan: 5 }, { contents: '01', rowspan: 3 }, '02', { contents: '03', rowspan: 2 }, '04' ],
+				[ { contents: '12', rowspan: 3 }, '14' ],
+				[ { contents: '23', rowspan: 2 }, '24' ],
+				[ { contents: '31', rowspan: 2 }, '34' ],
+				[ '42', '43', '44' ]
+			] ) );
+
+			table = modelRoot.getChild( 0 );
+		} );
+
+		it( 'should return empty array for no overlapping cells', () => {
+			const cellsInfo = getVerticallyOverlappingCells( table, 0 );
+
+			expect( cellsInfo ).to.be.empty;
+		} );
+
+		it( 'should return overlapping cells info for given overlapRow', () => {
+			const cellsInfo = getVerticallyOverlappingCells( table, 2 );
+
+			expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) ); // Cell 00
+			expect( cellsInfo[ 1 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) ); // Cell 01
+			expect( cellsInfo[ 2 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 12
+		} );
+
+		it( 'should ignore rows below startRow', () => {
+			const cellsInfo = getVerticallyOverlappingCells( table, 2, 1 );
+
+			expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 12
+		} );
+	} );
+
+	describe( 'getHorizontallyOverlappingCells()', () => {
+		let table;
+
+		beforeEach( () => {
+			// +----+----+----+----+----+
+			// | 00                     |
+			// +----+----+----+----+----+
+			// | 10           | 13      |
+			// +----+----+----+----+----+
+			// | 20 | 21           | 24 |
+			// +----+----+----+----+----+
+			// | 30      | 32      | 34 |
+			// +----+----+----+----+----+
+			// | 40 | 41 | 42 | 43 | 44 |
+			// +----+----+----+----+----+
+			setModelData( model, modelTable( [
+				[ { contents: '00', colspan: 5 } ],
+				[ { contents: '10', colspan: 3 }, { contents: '13', colspan: 2 } ],
+				[ '20', { contents: '21', colspan: 3 }, '24' ],
+				[ { contents: '30', colspan: 2 }, { contents: '32', colspan: 2 }, '34' ],
+				[ '40', '41', '42', '43', '44' ]
+			] ) );
+
+			table = modelRoot.getChild( 0 );
+		} );
+
+		it( 'should return empty array for no overlapping cells', () => {
+			const cellsInfo = getHorizontallyOverlappingCells( table, 0 );
+
+			expect( cellsInfo ).to.be.empty;
+		} );
+
+		it( 'should return overlapping cells info for given overlapColumn', () => {
+			const cellsInfo = getHorizontallyOverlappingCells( table, 2 );
+
+			expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) ); // Cell 00
+			expect( cellsInfo[ 1 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 10
+			expect( cellsInfo[ 2 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) ); // Cell 21
+		} );
+	} );
 } );