Jelajahi Sumber

Tests: Dissalow table in table test for upcast converter.

Maciej Gołaszewski 7 tahun lalu
induk
melakukan
bc946b565f

+ 12 - 0
packages/ckeditor5-table/src/converters/upcasttable.js

@@ -44,6 +44,12 @@ export default function upcastTable() {
 
 			// Insert element on allowed position.
 			const splitResult = conversionApi.splitToAllowedParent( table, data.modelCursor );
+
+			// When there is no split result it means that we can't insert element to model tree, so let's skip it.
+			if ( !splitResult ) {
+				return;
+			}
+
 			conversionApi.writer.insert( table, splitResult.position );
 			conversionApi.consumable.consume( viewTable, { name: true } );
 
@@ -102,6 +108,12 @@ export function upcastTableCell( elementName ) {
 
 			// Insert element on allowed position.
 			const splitResult = conversionApi.splitToAllowedParent( tableCell, data.modelCursor );
+
+			// When there is no split result it means that we can't insert element to model tree, so let's skip it.
+			if ( !splitResult ) {
+				return;
+			}
+
 			conversionApi.writer.insert( tableCell, splitResult.position );
 			conversionApi.consumable.consume( viewTableCell, { name: true } );
 

+ 21 - 5
packages/ckeditor5-table/tests/_utils/utils.js

@@ -12,6 +12,7 @@ import {
 	downcastTableHeadingRowsChange
 } from '../../src/converters/downcast';
 import upcastTable, { upcastTableCell } from '../../src/converters/upcasttable';
+import { upcastElementToElement } from '../../../ckeditor5-engine/src/conversion/upcast-converters';
 
 /**
  * Returns a model representation of a table shorthand notation:
@@ -154,24 +155,38 @@ export function formattedViewTable( tableData, attributes ) {
 	return formatTable( viewTable( tableData, attributes ) );
 }
 
-export function defaultSchema( schema ) {
+export function defaultSchema( schema, registerParagraph = true ) {
 	schema.register( 'table', {
 		allowWhere: '$block',
 		allowAttributes: [ 'headingRows', 'headingColumns' ],
+		isLimit: true,
 		isObject: true
 	} );
 
-	schema.register( 'tableRow', { allowIn: 'table' } );
+	schema.register( 'tableRow', {
+		allowIn: 'table',
+		isLimit: true
+	} );
 
 	schema.register( 'tableCell', {
 		allowIn: 'tableRow',
-		allowContentOf: '$block',
 		allowAttributes: [ 'colspan', 'rowspan' ],
 		isLimit: true
 	} );
 
+	// Allow all $block content inside table cell.
 	schema.extend( '$block', { allowIn: 'tableCell' } );
-	schema.register( 'paragraph', { inheritAllFrom: '$block' } );
+
+	// Disallow table in table.
+	schema.addChildCheck( ( context, childDefinition ) => {
+		if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
+			return false;
+		}
+	} );
+
+	if ( registerParagraph ) {
+		schema.register( 'paragraph', { inheritAllFrom: '$block' } );
+	}
 }
 
 export function defaultConversion( conversion, asWidget = false ) {
@@ -182,13 +197,14 @@ export function defaultConversion( conversion, asWidget = false ) {
 	conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget } ) );
 
 	// Table row conversion.
+	conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
 	conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget } ) );
 	conversion.for( 'downcast' ).add( downcastRemoveRow( { asWidget } ) );
 
 	// Table cell conversion.
-	conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget } ) );
 	conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
 	conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
+	conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget } ) );
 
 	// Table attributes conversion.
 	conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );

+ 31 - 22
packages/ckeditor5-table/tests/converters/upcasttable.js

@@ -8,7 +8,7 @@ import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversio
 import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 import upcastTable, { upcastTableCell } from '../../src/converters/upcasttable';
-import { formatTable } from '../_utils/utils';
+import { defaultSchema, formatTable } from '../_utils/utils';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 
 describe( 'upcastTable()', () => {
@@ -24,37 +24,20 @@ describe( 'upcastTable()', () => {
 				model = editor.model;
 
 				const conversion = editor.conversion;
-				const schema = model.schema;
 
-				schema.register( 'table', {
-					allowWhere: '$block',
-					allowAttributes: [ 'headingRows', 'headingColumns' ],
-					isLimit: true,
-					isObject: true
-				} );
-
-				schema.register( 'tableRow', {
-					allowIn: 'table',
-					isLimit: true
-				} );
-
-				schema.register( 'tableCell', {
-					allowIn: 'tableRow',
-					allowAttributes: [ 'colspan', 'rowspan' ],
-					isLimit: true
-				} );
-
-				schema.extend( '$block', { allowIn: 'tableCell' } );
+				defaultSchema( model.schema, false );
 
+				// Table conversion.
 				conversion.for( 'upcast' ).add( upcastTable() );
 
-				// Table row upcast only since downcast conversion is done in `downcastTable()`.
+				// Table row conversion.
 				conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
 
 				// Table cell conversion.
 				conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
 				conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
 
+				// Table attributes conversion.
 				conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
 				conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
 
@@ -293,6 +276,32 @@ describe( 'upcastTable()', () => {
 		);
 	} );
 
+	it( 'should strip table in table', () => {
+		editor.setData(
+			'<table>' +
+				'<tr>' +
+					'<td>' +
+						'<table>' +
+							'<tr>' +
+								'<td>tableception</td>' +
+							'</tr>' +
+						'</table>' +
+					'</td>' +
+				'</tr>' +
+			'</table>'
+		);
+
+		expectModel(
+			'<table>' +
+				'<tableRow>' +
+					'<tableCell>' +
+						'<paragraph>tableception</paragraph>' +
+					'</tableCell>' +
+				'</tableRow>' +
+			'</table>'
+		);
+	} );
+
 	describe( 'headingColumns', () => {
 		it( 'should properly calculate heading columns', () => {
 			editor.setData(