8
0
Просмотр исходного кода

Added: Schema#itemExtends + some docs fixes in Schema.

Szymon Cofalik 9 лет назад
Родитель
Сommit
7826f3af8c

+ 25 - 3
packages/ckeditor5-engine/src/model/schema.js

@@ -438,7 +438,7 @@ export default class Schema {
 			/**
 			 * Item with specified name already exists in schema.
 			 *
-			 * @error schema-item-exists
+			 * @error model-schema-item-exists
 			 */
 			throw new CKEditorError( 'model-schema-item-exists: Item with specified name already exists in schema.' );
 		}
@@ -447,7 +447,7 @@ export default class Schema {
 			/**
 			 * Item with specified name does not exist in schema.
 			 *
-			 * @error schema-no-item
+			 * @error model-schema-no-item
 			 */
 			throw new CKEditorError( 'model-schema-no-item: Item with specified name does not exist in schema.' );
 		}
@@ -464,6 +464,28 @@ export default class Schema {
 		this._extensionChains.set( itemName, chain );
 	}
 
+	/**
+	 * Checks whether item of given name is extending item of another given name.
+	 *
+	 * @param {String} childItemName Name of the child item.
+	 * @param {String} parentItemName Name of the parent item.
+	 * @returns {Boolean} `true` if child item extends parent item, `false` otherwise.
+	 */
+	itemExtends( childItemName, parentItemName ) {
+		if ( !this.hasItem( childItemName ) || !this.hasItem( parentItemName ) ) {
+			/**
+			 * Item with specified name does not exist in schema.
+			 *
+			 * @error model-schema-no-item
+			 */
+			throw new CKEditorError( 'model-schema-no-item: Item with specified name does not exist in schema.' );
+		}
+
+		const chain = this._extensionChains.get( childItemName );
+
+		return chain.some( itemName => itemName == parentItemName );
+	}
+
 	/**
 	 * Returns {@link engine.model.SchemaItem schema item} that was registered in the schema under given name.
 	 * If item has not been found, throws error.
@@ -477,7 +499,7 @@ export default class Schema {
 			/**
 			 * Item with specified name does not exist in schema.
 			 *
-			 * @error schema-no-item
+			 * @error model-schema-no-item
 			 */
 			throw new CKEditorError( 'model-schema-no-item: Item with specified name does not exist in schema.' );
 		}

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

@@ -316,6 +316,35 @@ describe( 'check', () => {
 	} );
 } );
 
+describe( 'itemExtends', () => {
+	it( 'should return true if given item extends another given item', () => {
+		schema.registerItem( 'div', '$block' );
+		schema.registerItem( 'myDiv', 'div' );
+
+		expect( schema.itemExtends( 'div', '$block' ) ).to.be.true;
+		expect( schema.itemExtends( 'myDiv', 'div' ) ).to.be.true;
+		expect( schema.itemExtends( 'myDiv', '$block' ) ).to.be.true;
+	} );
+
+	it( 'should return false if given item does not extend another given item', () => {
+		schema.registerItem( 'div' );
+		schema.registerItem( 'myDiv', 'div' );
+
+		expect( schema.itemExtends( 'div', '$block' ) ).to.be.false;
+		expect( schema.itemExtends( 'div', 'myDiv' ) ).to.be.false;
+	} );
+
+	it( 'should throw if one or both given items are not registered in schema', () => {
+		expect( () => {
+			schema.itemExtends( 'foo', '$block' );
+		} ).to.throw( CKEditorError, /model-schema-no-item/ );
+
+		expect( () => {
+			schema.itemExtends( '$block', 'foo' );
+		} ).to.throw( CKEditorError, /model-schema-no-item/ );
+	} );
+} );
+
 describe( '_normalizeQueryPath', () => {
 	it( 'should normalize string with spaces to an array of strings', () => {
 		expect( Schema._normalizeQueryPath( '$root div strong' ) ).to.deep.equal( [ '$root', 'div', 'strong' ] );