Browse Source

Made Schema.isLimit() return true for all objects too (an object is a limit). See https://github.com/ckeditor/ckeditor5-engine/pull/1431#issuecomment-397871919.

Piotrek Koszuliński 7 years ago
parent
commit
2356ee42e7

+ 14 - 6
packages/ckeditor5-engine/src/model/schema.js

@@ -333,18 +333,24 @@ export default class Schema {
 
 	/**
 	 * Returns `true` if the given item is defined to be
-	 * a limit element by {@link module:engine/model/schema~SchemaItemDefinition}'s `isLimit` property.
+	 * a limit element by {@link module:engine/model/schema~SchemaItemDefinition}'s `isLimit` or `isObject` property
+	 * (all objects are also limits).
 	 *
 	 *		schema.isLimit( 'paragraph' ); // -> false
 	 *		schema.isLimit( '$root' ); // -> true
 	 *		schema.isLimit( editor.model.document.getRoot() ); // -> true
+	 *		schema.isLimit( 'image' ); // -> true
 	 *
 	 * @param {module:engine/model/item~Item|module:engine/model/schema~SchemaContextItem|String} item
 	 */
 	isLimit( item ) {
 		const def = this.getDefinition( item );
 
-		return !!( def && def.isLimit );
+		if ( !def ) {
+			return false;
+		}
+
+		return !!( def.isLimit || def.isObject );
 	}
 
 	/**
@@ -747,8 +753,8 @@ export default class Schema {
 				return parent;
 			}
 
-			// Do not split limit elements and objects.
-			if ( this.isLimit( parent ) || this.isObject( parent ) ) {
+			// Do not split limit elements.
+			if ( this.isLimit( parent ) ) {
 				return null;
 			}
 
@@ -987,9 +993,11 @@ mix( Schema, ObservableMixin );
  * Most block type items will inherit from `$block` (through `inheritAllFrom`).
  * * `isLimit` – can be understood as whether this element should not be split by <kbd>Enter</kbd>.
  * Examples of limit elements – `$root`, table cell, image caption, etc. In other words, all actions which happen inside
- * a limit element are limited to its content.
+ * a limit element are limited to its content. **Note:** all objects (`isObject`) are treated as limit elements too.
  * * `isObject` – whether item is "self-contained" and should be treated as a whole. Examples of object elements –
- * `image`, `table`, `video`, etc.
+ * `image`, `table`, `video`, etc. **Note:** an object is also a limit so
+ * {@link module:engine/model/schema~Schema#isLimit `isLimit()`}
+ * returns `true` for object elements automatically.
  *
  * # Generic items
  *

+ 1 - 1
packages/ckeditor5-engine/src/model/utils/deletecontent.js

@@ -185,7 +185,7 @@ function checkCanBeMerged( leftPos, rightPos, schema ) {
 	const rangeToCheck = new Range( leftPos, rightPos );
 
 	for ( const value of rangeToCheck.getWalker() ) {
-		if ( schema.isObject( value.item ) || schema.isLimit( value.item ) ) {
+		if ( schema.isLimit( value.item ) ) {
 			return false;
 		}
 	}

+ 17 - 1
packages/ckeditor5-engine/tests/model/schema.js

@@ -316,6 +316,14 @@ describe( 'Schema', () => {
 			expect( schema.isLimit( 'foo' ) ).to.be.true;
 		} );
 
+		it( 'returns true if an item was registered as an object element (because all objects are limits too)', () => {
+			schema.register( 'foo', {
+				isObject: true
+			} );
+
+			expect( schema.isLimit( 'foo' ) ).to.be.true;
+		} );
+
 		it( 'returns false if an item was not registered as a limit element', () => {
 			schema.register( 'foo' );
 
@@ -343,6 +351,14 @@ describe( 'Schema', () => {
 			expect( schema.isObject( 'foo' ) ).to.be.true;
 		} );
 
+		it( 'returns false if an item was registered as a limit (because not all limits are objects)', () => {
+			schema.register( 'foo', {
+				isLimit: true
+			} );
+
+			expect( schema.isObject( 'foo' ) ).to.be.false;
+		} );
+
 		it( 'returns false if an item was not registered as an object', () => {
 			schema.register( 'foo' );
 
@@ -2604,7 +2620,7 @@ describe( 'Schema', () => {
 		} );
 
 		it( 'image is block object', () => {
-			expect( schema.isLimit( 'image' ) ).to.be.false;
+			expect( schema.isLimit( 'image' ) ).to.be.true;
 			expect( schema.isBlock( 'image' ) ).to.be.true;
 			expect( schema.isObject( 'image' ) ).to.be.true;
 		} );