Procházet zdrojové kódy

Implemented the isHeadingColumnCell() helper.

Aleksander Nowodzinski před 5 roky
rodič
revize
8ab02b7c7d

+ 16 - 0
packages/ckeditor5-table/src/commands/utils.js

@@ -114,3 +114,19 @@ export function addDefaultUnitToNumericValue( value, defaultUnit ) {
 
 	return `${ numericValue }${ defaultUnit }`;
 }
+
+/**
+ * Checks if a table cell belongs to the heading column section.
+ *
+ * @param {module:table/tableutils~TableUtils} tableUtils
+ * @param {module:engine/model/element~Element} tableCell
+ * @param {module:engine/model/element~Element} table
+ * @returns {Boolean}
+ */
+export function isHeadingColumnCell( tableUtils, tableCell ) {
+	const table = tableCell.parent.parent;
+	const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
+	const { column } = tableUtils.getCellLocation( tableCell );
+
+	return !!headingColumns && column < headingColumns;
+}

+ 55 - 6
packages/ckeditor5-table/tests/commands/utils.js

@@ -4,20 +4,27 @@
  */
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+import TableUtils from '../../src/tableutils';
 import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
-
 import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
-
-import { findAncestor } from '../../src/commands/utils';
+import {
+	findAncestor,
+	isHeadingColumnCell
+} from '../../src/commands/utils';
 
 describe( 'commands utils', () => {
-	let editor, model;
+	let editor, model, modelRoot, tableUtils;
 
 	beforeEach( () => {
-		return ModelTestEditor.create()
+		return ModelTestEditor
+			.create( {
+				plugins: [ TableUtils ]
+			} )
 			.then( newEditor => {
 				editor = newEditor;
 				model = editor.model;
+				modelRoot = model.document.getRoot();
+				tableUtils = editor.plugins.get( TableUtils );
 
 				defaultSchema( model.schema );
 				defaultConversion( editor.conversion );
@@ -28,7 +35,7 @@ describe( 'commands utils', () => {
 		return editor.destroy();
 	} );
 
-	describe( 'getParentTable()', () => {
+	describe( 'findAncestor()', () => {
 		it( 'should return undefined if not in table', () => {
 			setData( model, '<paragraph>foo[]</paragraph>' );
 
@@ -44,4 +51,46 @@ describe( 'commands utils', () => {
 			expect( parentTable.is( 'table' ) ).to.be.true;
 		} );
 	} );
+
+	describe( 'isHeadingColumnCell()', () => {
+		it( 'shoud return "true" for heading column cell', () => {
+			setData( model, modelTable( [
+				[ '00', '01', '02', '03' ]
+			], { headingColumns: 2 } ) );
+
+			const tableCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
+
+			expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.true;
+		} );
+
+		it( 'shoud return "true" for heading column cell with colspan', () => {
+			setData( model, modelTable( [
+				[ { colspan: 2, contents: '00' }, '01', '02', '03' ]
+			], { headingColumns: 2 } ) );
+
+			const tableCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
+
+			expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.true;
+		} );
+
+		it( 'shoud return "false" for regular column cell', () => {
+			setData( model, modelTable( [
+				[ '00', '01', '02', '03' ]
+			], { headingColumns: 2 } ) );
+
+			const tableCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
+
+			expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.false;
+		} );
+
+		it( 'shoud return "false" for regular column cell with colspan', () => {
+			setData( model, modelTable( [
+				[ '00', { colspan: 2, contents: '01' }, '02', '03' ]
+			], { headingColumns: 1 } ) );
+
+			const tableCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
+
+			expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.false;
+		} );
+	} );
 } );