瀏覽代碼

Added more tests with row/col-spanned cells.

Kuba Niegowski 5 年之前
父節點
當前提交
bde3c3acb5

+ 178 - 60
packages/ckeditor5-table/tests/commands/selectcolumncommand.js

@@ -80,7 +80,7 @@ describe( 'SelectColumnCommand', () => {
 			] );
 		} );
 
-		it( 'should select a given column from a table start', () => {
+		it( 'should select a column of table cell with a collapsed selection in first table cell', () => {
 			setData( model, modelTable( [
 				[ '[]00', '01' ],
 				[ '10', '11' ],
@@ -96,7 +96,7 @@ describe( 'SelectColumnCommand', () => {
 			] );
 		} );
 
-		it( 'should select a given column from a table start when selection is at the end', () => {
+		it( 'should select a column of table cell with a collapsed selection in last cell in the first column', () => {
 			setData( model, modelTable( [
 				[ '00', '01' ],
 				[ '10', '11' ],
@@ -112,7 +112,7 @@ describe( 'SelectColumnCommand', () => {
 			] );
 		} );
 
-		it( 'should select last column', () => {
+		it( 'should select a column of table cell with collapsed selection in the first cell of the last column', () => {
 			setData( model, modelTable( [
 				[ '00', '01[]' ],
 				[ '10', '11' ]
@@ -126,47 +126,189 @@ describe( 'SelectColumnCommand', () => {
 			] );
 		} );
 
-		it( 'should select col-spanned cells', () => {
-			// +----+----+----+----+
-			// | 00                |
-			// +----+----+----+----+
-			// | 10           | 13 |
-			// +----+----+----+----+
-			// | 20      | 22 | 23 |
-			// +----+----+----+----+
-			// | 30 | 31      | 33 |
-			// +----+----+----+----+
-			// | 40 | 41 | 42 | 43 |
-			// +----+----+----+----+
-			setData( model, modelTable( [
-				[ { colspan: 4, contents: '00' } ],
-				[ { colspan: 3, contents: '10' }, '13' ],
-				[ { colspan: 2, contents: '20' }, '22', '23' ],
-				[ '30', { colspan: 2, contents: '31' }, '33' ],
-				[ '40', '41[]', '42', '43' ]
-			] ) );
+		describe( 'with col-spanned cells', () => {
+			beforeEach( () => {
+				// +----+----+----+----+
+				// | 00                |
+				// +----+----+----+----+
+				// | 10           | 13 |
+				// +----+----+----+----+
+				// | 20      | 22 | 23 |
+				// +----+----+----+----+
+				// | 30 | 31      | 33 |
+				// +----+----+----+----+
+				// | 40 | 41 | 42 | 43 |
+				// +----+----+----+----+
+				setData( model, modelTable( [
+					[ { colspan: 4, contents: '00' } ],
+					[ { colspan: 3, contents: '10' }, '13' ],
+					[ { colspan: 2, contents: '20' }, '22', '23' ],
+					[ '30', { colspan: 2, contents: '31' }, '33' ],
+					[ '40', '41', '42', '43' ]
+				] ) );
+			} );
 
-			command.execute();
+			it( 'should select only one column if only one cell is selected', () => {
+				// Selection in cell 10.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
+					modelRoot.getNodeByPath( [ 0, 1, 0 ] )
+				);
 
-			/* eslint-disable no-multi-spaces */
-			assertSelectedCells( model, [
-				[ 0          ],
-				[ 0,       0 ],
-				[ 0,    0, 0 ],
-				[ 0, 1,    0 ],
-				[ 0, 1, 0, 0 ]
-			] );
-			/* eslint-enable no-multi-spaces */
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0          ],
+					[ 1,       0 ],
+					[ 0,    0, 0 ],
+					[ 0, 0,    0 ],
+					[ 0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 1          ],
+					[ 1,       0 ],
+					[ 1,    0, 0 ],
+					[ 1, 0,    0 ],
+					[ 1, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
+
+			it( 'should not select col-spanned columns that start in other column', () => {
+				// Selection in cell 42.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 4, 2 ] ),
+					modelRoot.getNodeByPath( [ 0, 4, 2 ] )
+				);
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0          ],
+					[ 0,       0 ],
+					[ 0,    0, 0 ],
+					[ 0, 0,    0 ],
+					[ 0, 0, 1, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0          ],
+					[ 0,       0 ],
+					[ 0,    1, 0 ],
+					[ 0, 0,    0 ],
+					[ 0, 0, 1, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
+
+			it( 'should not select col-spanned columns that start in other column but include those that start in selected column', () => {
+				// Selection in cell 41.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 4, 1 ] ),
+					modelRoot.getNodeByPath( [ 0, 4, 1 ] )
+				);
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0          ],
+					[ 0,       0 ],
+					[ 0,    0, 0 ],
+					[ 0, 0,    0 ],
+					[ 0, 1, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0          ],
+					[ 0,       0 ],
+					[ 0,    0, 0 ],
+					[ 0, 1,    0 ],
+					[ 0, 1, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
+
+			it( 'should select properly for multiple not spanned cells selected', () => {
+				// Selection in cells 40 - 41.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 4, 0 ] ),
+					modelRoot.getNodeByPath( [ 0, 4, 1 ] )
+				);
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0          ],
+					[ 0,       0 ],
+					[ 0,    0, 0 ],
+					[ 0, 0,    0 ],
+					[ 1, 1, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 1          ],
+					[ 1,       0 ],
+					[ 1,    0, 0 ],
+					[ 1, 1,    0 ],
+					[ 1, 1, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
+
+			it( 'should select properly for multiple cells selected including spanned one', () => {
+				// Selection in cells 31 - 33.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 3, 1 ] ),
+					modelRoot.getNodeByPath( [ 0, 3, 2 ] )
+				);
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0          ],
+					[ 0,       0 ],
+					[ 0,    0, 0 ],
+					[ 0, 1,    1 ],
+					[ 0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0          ],
+					[ 0,       1 ],
+					[ 0,    1, 1 ],
+					[ 0, 1,    1 ],
+					[ 0, 1, 1, 1 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
 		} );
 
 		describe( 'with multiple columns selected', () => {
-			it( 'should properly select middle columns', () => {
+			beforeEach( () => {
 				setData( model, modelTable( [
 					[ '00', '01', '02', '03' ],
 					[ '10', '11', '12', '13' ],
 					[ '20', '21', '22', '23' ]
 				] ) );
+			} );
 
+			it( 'should properly select middle columns', () => {
 				tableSelection._setCellSelection(
 					modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
 					modelRoot.getNodeByPath( [ 0, 0, 2 ] )
@@ -182,12 +324,6 @@ describe( 'SelectColumnCommand', () => {
 			} );
 
 			it( 'should properly select middle columns in reversed order', () => {
-				setData( model, modelTable( [
-					[ '00', '01', '02', '03' ],
-					[ '10', '11', '12', '13' ],
-					[ '20', '21', '22', '23' ]
-				] ) );
-
 				tableSelection._setCellSelection(
 					modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
 					modelRoot.getNodeByPath( [ 0, 0, 1 ] )
@@ -203,12 +339,6 @@ describe( 'SelectColumnCommand', () => {
 			} );
 
 			it( 'should properly select tailing columns', () => {
-				setData( model, modelTable( [
-					[ '00', '01', '02', '03' ],
-					[ '10', '11', '12', '13' ],
-					[ '20', '21', '22', '23' ]
-				] ) );
-
 				tableSelection._setCellSelection(
 					modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
 					modelRoot.getNodeByPath( [ 0, 0, 3 ] )
@@ -224,12 +354,6 @@ describe( 'SelectColumnCommand', () => {
 			} );
 
 			it( 'should properly select beginning columns', () => {
-				setData( model, modelTable( [
-					[ '00', '01', '02', '03' ],
-					[ '10', '11', '12', '13' ],
-					[ '20', '21', '22', '23' ]
-				] ) );
-
 				tableSelection._setCellSelection(
 					modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
 					modelRoot.getNodeByPath( [ 0, 0, 1 ] )
@@ -245,12 +369,6 @@ describe( 'SelectColumnCommand', () => {
 			} );
 
 			it( 'should properly select multiple columns from square selection', () => {
-				setData( model, modelTable( [
-					[ '00', '01', '02' ],
-					[ '10', '11', '12' ],
-					[ '20', '21', '22' ]
-				] ) );
-
 				tableSelection._setCellSelection(
 					modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
 					modelRoot.getNodeByPath( [ 0, 1, 2 ] )
@@ -259,9 +377,9 @@ describe( 'SelectColumnCommand', () => {
 				command.execute();
 
 				assertSelectedCells( model, [
-					[ 0, 1, 1 ],
-					[ 0, 1, 1 ],
-					[ 0, 1, 1 ]
+					[ 0, 1, 1, 0 ],
+					[ 0, 1, 1, 0 ],
+					[ 0, 1, 1, 0 ]
 				] );
 			} );
 

+ 160 - 29
packages/ckeditor5-table/tests/commands/selectrowcommand.js

@@ -64,7 +64,7 @@ describe( 'SelectRowCommand', () => {
 	} );
 
 	describe( 'execute()', () => {
-		it( 'should select a given row', () => {
+		it( 'should select a row of a table cell with a collapsed selection', () => {
 			setData( model, modelTable( [
 				[ '00', '01' ],
 				[ '[]10', '11' ],
@@ -80,7 +80,7 @@ describe( 'SelectRowCommand', () => {
 			] );
 		} );
 
-		it( 'should select a given row from a table start', () => {
+		it( 'should select a row of table cell with a collapsed selection in first table cell', () => {
 			setData( model, modelTable( [
 				[ '[]00', '01' ],
 				[ '10', '11' ],
@@ -96,7 +96,7 @@ describe( 'SelectRowCommand', () => {
 			] );
 		} );
 
-		it( 'should select a given row from a table start when selection is at the end', () => {
+		it( 'should select a row of table cell with a collapsed selection in last cell in the first column', () => {
 			setData( model, modelTable( [
 				[ '00', '01[]' ],
 				[ '10', '11' ],
@@ -112,7 +112,7 @@ describe( 'SelectRowCommand', () => {
 			] );
 		} );
 
-		it( 'should select last row', () => {
+		it( 'should select a row of table cell with collapsed selection in the first cell of the last column', () => {
 			setData( model, modelTable( [
 				[ '00', '01' ],
 				[ '[]10', '11' ]
@@ -126,33 +126,164 @@ describe( 'SelectRowCommand', () => {
 			] );
 		} );
 
-		it( 'should select row-spanned cells', () => {
-			// +----+----+----+----+----+
-			// | 00 | 01 | 02 | 03 | 04 |
-			// +    +    +    +----+----+
-			// |    |    |    | 13 | 14 |
-			// +    +    +----+    +----+
-			// |    |    | 22 |    | 24 |
-			// +    +----+----+----+----+
-			// |    | 31 | 32 | 33 | 34 |
-			// +----+----+----+----+----+
-			setData( model, modelTable( [
-				[ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
-				[ { rowspan: 2, contents: '13' }, '14[]' ],
-				[ '22', '23', '24' ],
-				[ '30', '31', '32', '33', '34' ]
-			] ) );
+		describe( 'with row-spanned cells', () => {
+			beforeEach( () => {
+				// +----+----+----+----+----+
+				// | 00 | 01 | 02 | 03 | 04 |
+				// +    +    +    +----+----+
+				// |    |    |    | 13 | 14 |
+				// +    +    +----+    +----+
+				// |    |    | 22 |    | 24 |
+				// +    +----+----+----+----+
+				// |    | 31 | 32 | 33 | 34 |
+				// +----+----+----+----+----+
+				setData( model, modelTable( [
+					[ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
+					[ { rowspan: 2, contents: '13' }, '14' ],
+					[ '22', '23', '24' ],
+					[ '30', '31', '32', '33', '34' ]
+				] ) );
+			} );
 
-			command.execute();
+			it( 'should select only one row if only one cell is selected', () => {
+				// Selection in cell 01.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
+					modelRoot.getNodeByPath( [ 0, 0, 1 ] )
+				);
 
-			/* eslint-disable no-multi-spaces */
-			assertSelectedCells( model, [
-				[ 0, 0, 0, 0, 0 ],
-				[          1, 1 ],
-				[       0,    0 ],
-				[    0, 0, 0, 0 ]
-			] );
-			/* eslint-enable no-multi-spaces */
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0, 1, 0, 0, 0 ],
+					[          0, 0 ],
+					[       0,    0 ],
+					[    0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 1, 1, 1, 1, 1 ],
+					[          0, 0 ],
+					[       0,    0 ],
+					[    0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
+
+			it( 'should not select row-spanned rows that start in other row', () => {
+				// Selection in cell 24.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 2, 1 ] ),
+					modelRoot.getNodeByPath( [ 0, 2, 1 ] )
+				);
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0, 0, 0, 0, 0 ],
+					[          0, 0 ],
+					[       0,    1 ],
+					[    0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0, 0, 0, 0, 0 ],
+					[          0, 0 ],
+					[       1,    1 ],
+					[    0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
+
+			it( 'should not select row-spanned rows that start in other row but include those that start in selected row', () => {
+				// Selection in cell 14.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
+					modelRoot.getNodeByPath( [ 0, 1, 1 ] )
+				);
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0, 0, 0, 0, 0 ],
+					[          0, 1 ],
+					[       0,    0 ],
+					[    0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0, 0, 0, 0, 0 ],
+					[          1, 1 ],
+					[       0,    0 ],
+					[    0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
+
+			it( 'should select properly for multiple not spanned cells selected', () => {
+				// Selection in cells 04 - 14.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 0, 4 ] ),
+					modelRoot.getNodeByPath( [ 0, 1, 1 ] )
+				);
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0, 0, 0, 0, 1 ],
+					[          0, 1 ],
+					[       0,    0 ],
+					[    0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 1, 1, 1, 1, 1 ],
+					[          1, 1 ],
+					[       0,    0 ],
+					[    0, 0, 0, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
+
+			it( 'should select properly for multiple cells selected including spanned one', () => {
+				// Selection in cells 13 - 33.
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
+					modelRoot.getNodeByPath( [ 0, 3, 2 ] )
+				);
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0, 0, 0, 0, 0 ],
+					[          1, 0 ],
+					[       0,    0 ],
+					[    0, 0, 1, 0 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+
+				command.execute();
+
+				/* eslint-disable no-multi-spaces */
+				assertSelectedCells( model, [
+					[ 0, 0, 0, 0, 0 ],
+					[          1, 1 ],
+					[       1,    1 ],
+					[    1, 1, 1, 1 ]
+				] );
+				/* eslint-enable no-multi-spaces */
+			} );
 		} );
 
 		describe( 'with multiple rows selected', () => {