Browse Source

Merge pull request #39 from ckeditor/t/38

Feature: Integrated the command with `Schema#limits`. Closes #38.
Piotrek Koszuliński 8 years ago
parent
commit
1d05067a53

+ 8 - 2
packages/ckeditor5-enter/src/entercommand.js

@@ -24,7 +24,7 @@ export default class EnterCommand extends Command {
 		const batch = doc.batch();
 
 		doc.enqueueChanges( () => {
-			enterBlock( this.editor.data, batch, doc.selection );
+			enterBlock( this.editor.data, batch, doc.selection, doc.schema );
 
 			this.fire( 'afterExecute', { batch } );
 		} );
@@ -36,12 +36,18 @@ export default class EnterCommand extends Command {
 // @param {engine.controller.DataController} dataController
 // @param {module:engine/model/batch~Batch} batch A batch to which the deltas will be added.
 // @param {module:engine/model/selection~Selection} selection Selection on which the action should be performed.
-function enterBlock( dataController, batch, selection ) {
+// @param {module:engine/model/schema~Schema} schema
+function enterBlock( dataController, batch, selection, schema ) {
 	const isSelectionEmpty = selection.isCollapsed;
 	const range = selection.getFirstRange();
 	const startElement = range.start.parent;
 	const endElement = range.end.parent;
 
+	// Do nothing if selection starts or ends inside `limit` elements.
+	if ( schema.limits.has( startElement.name ) || schema.limits.has( endElement.name ) ) {
+		return;
+	}
+
 	// Don't touch the root.
 	if ( startElement.root == startElement ) {
 		if ( !isSelectionEmpty ) {

+ 34 - 0
packages/ckeditor5-enter/tests/entercommand.js

@@ -26,7 +26,17 @@ describe( 'EnterCommand', () => {
 				schema.registerItem( 'img', '$inline' );
 				schema.registerItem( 'p', '$block' );
 				schema.registerItem( 'h', '$block' );
+				schema.registerItem( 'inlineLimit' );
+				schema.registerItem( 'blockLimit' );
+
+				schema.allow( { name: 'inlineLimit', inside: 'p' } );
+				schema.allow( { name: '$text', inside: 'inlineLimit' } );
 				schema.allow( { name: '$text', inside: '$root' } );
+				schema.allow( { name: 'blockLimit', inside: '$root' } );
+				schema.allow( { name: 'p', inside: 'blockLimit' } );
+
+				schema.limits.add( 'inlineLimit' );
+				schema.limits.add( 'blockLimit' );
 			} );
 	} );
 
@@ -107,6 +117,30 @@ describe( 'EnterCommand', () => {
 				'<p>[]</p>'
 			);
 
+			test(
+				'should not break inline limit elements - collapsed',
+				'<p><inlineLimit>foo[]bar</inlineLimit></p>',
+				'<p><inlineLimit>foo[]bar</inlineLimit></p>'
+			);
+
+			test(
+				'should not break inline limit elements',
+				'<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
+				'<p><inlineLimit>foo[bar]baz</inlineLimit></p>'
+			);
+
+			test(
+				'should not break inline limit elements - selection partially inside',
+				'<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>',
+				'<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>'
+			);
+
+			test(
+				'should break paragraph in blockLimit',
+				'<blockLimit><p>foo[]bar</p></blockLimit>',
+				'<blockLimit><p>foo</p><p>[]bar</p></blockLimit>'
+			);
+
 			it( 'leaves one empty element after two were fully selected (backward)', () => {
 				setData( doc, '<p>[abc</p><p>def]</p>' );
 				// @TODO: Add option for setting selection direction to model utils.