Browse Source

Added "Schema#checkMerge()" method.

Kamil Piechaczek 7 years ago
parent
commit
484ccc265f

+ 17 - 0
packages/ckeditor5-engine/src/model/schema.js

@@ -411,6 +411,23 @@ export default class Schema {
 	}
 
 	/**
+	 * Checks whether given element (`elementToMerge`) can be merged to the specified base element (`baseElement`).
+	 *
+	 * @param {module:engine/model/Element~Element} baseElement The base element which will be merged.
+	 * @param {module:engine/model/Element~Element} elementToMerge The element to check whether can be merged with the base element.
+	 * @returns {Boolean}
+	 */
+	checkMerge( baseElement, elementToMerge ) {
+		for ( const child of elementToMerge.getChildren() ) {
+			if ( !this.checkChild( baseElement, child ) ) {
+				return false;
+			}
+		}
+
+		return true;
+	}
+
+	/**
 	 * Allows registering a callback to the {@link #checkChild} method calls.
 	 *
 	 * Callbacks allow you to implement rules which are not otherwise possible to achieve

+ 41 - 0
packages/ckeditor5-engine/tests/model/schema.js

@@ -641,6 +641,47 @@ describe( 'Schema', () => {
 		} );
 	} );
 
+	describe( 'checkMerge()', () => {
+		beforeEach( () => {
+			schema.register( '$root' );
+			schema.register( '$block', {
+				allowIn: '$root'
+			} );
+			schema.register( 'paragraph', {
+				allowWhere: '$block'
+			} );
+			schema.register( '$text', {
+				allowIn: 'paragraph'
+			} );
+			schema.register( 'blockQuote', {
+				allowWhere: '$block',
+				allowContentOf: '$root'
+			} );
+			schema.register( 'listItem', {
+				inheritAllFrom: '$block'
+			} );
+		} );
+
+		it( 'returns false if a block is not allowed in other block', () => {
+			const paragraph = new Element( 'paragraph', null, [
+				new Text( 'xyz' )
+			] );
+			const blockQuote = new Element( 'blockQuote', null, [ paragraph ] );
+			const listItem = new Element( 'listItem' );
+
+			expect( schema.checkMerge( listItem, blockQuote ) ).to.equal( false );
+		} );
+
+		it( 'returns true if a block is allowed in other block', () => {
+			const paragraph = new Element( 'paragraph', null, [
+				new Text( 'xyz' )
+			] );
+			const listItem = new Element( 'listItem' );
+
+			expect( schema.checkMerge( listItem, paragraph ) ).to.equal( false );
+		} );
+	} );
+
 	describe( 'getLimitElement()', () => {
 		let model, doc, root;