Browse Source

Allowed SchemaContext as a checkChild and checkAttribute context.

Oskar Wróbel 7 years ago
parent
commit
d06b9f325e

+ 12 - 5
packages/ckeditor5-engine/src/model/schema.js

@@ -193,11 +193,16 @@ export default class Schema {
 		this.decorate( 'checkAttribute' );
 
 		this.on( 'checkAttribute', ( evt, args ) => {
-			args[ 0 ] = new SchemaContext( args[ 0 ] );
+			if ( !( args[ 0 ] instanceof SchemaContext ) ) {
+				args[ 0 ] = new SchemaContext( args[ 0 ] );
+			}
 		}, { priority: 'highest' } );
 
 		this.on( 'checkChild', ( evt, args ) => {
-			args[ 0 ] = new SchemaContext( args[ 0 ] );
+			if ( !( args[ 0 ] instanceof SchemaContext ) ) {
+				args[ 0 ] = new SchemaContext( args[ 0 ] );
+			}
+
 			args[ 1 ] = this.getDefinition( args[ 1 ] );
 		}, { priority: 'highest' } );
 	}
@@ -375,8 +380,9 @@ export default class Schema {
 	 *		schema.checkChild( model.document.getRoot(), paragraph ); // -> true
 	 *
 	 * @fires checkChild
-	 * @param {module:engine/model/schema~SchemaContextDefinition} context Context in which the child will be checked.
-	 * @param {module:engine/model/node~Node|String} child The child to check.
+	 * @param {module:engine/model/schema~SchemaContextDefinition|module:engine/model/schema~SchemaContext} context
+	 * Context in which the child will be checked.
+	 * @param {module:engine/model/node~Node|String} def The child to check.
 	 */
 	checkChild( context, def ) {
 		// Note: context and child are already normalized here to a SchemaContext and SchemaCompiledItemDefinition.
@@ -399,7 +405,8 @@ export default class Schema {
 	 *		schema.checkAttribute( textNode, 'bold' ); // -> true
 	 *
 	 * @fires checkAttribute
-	 * @param {module:engine/model/schema~SchemaContextDefinition} context
+	 * @param {module:engine/model/schema~SchemaContextDefinition|module:engine/model/schema~SchemaContext} context
+	 * Context in which the attribute will be checked.
 	 * @param {String} attributeName
 	 */
 	checkAttribute( context, attributeName ) {

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

@@ -377,6 +377,17 @@ describe( 'Schema', () => {
 			expect( schema.checkChild( root1, '$text' ) ).to.be.false;
 		} );
 
+		it( 'accepts a schemaContext instance as a context', () => {
+			const rootContext = new SchemaContext( Position.createAt( root1 ) );
+			const paragraphContext = new SchemaContext( Position.createAt( r1p1 ) );
+
+			expect( schema.checkChild( rootContext, 'paragraph' ) ).to.be.true;
+			expect( schema.checkChild( rootContext, '$text' ) ).to.be.false;
+
+			expect( schema.checkChild( paragraphContext, '$text' ) ).to.be.true;
+			expect( schema.checkChild( paragraphContext, 'paragraph' ) ).to.be.false;
+		} );
+
 		it( 'accepts a position as a context', () => {
 			const posInRoot = Position.createAt( root1 );
 			const posInParagraph = Position.createAt( r1p1 );
@@ -466,6 +477,14 @@ describe( 'Schema', () => {
 			expect( schema.checkAttribute( posInParagraph, 'align' ) ).to.be.true;
 		} );
 
+		it( 'accepts a schemaContext instance as a context', () => {
+			const rootContext = new SchemaContext( Position.createAt( root1 ) );
+			const paragraphContext = new SchemaContext( Position.createAt( r1p1 ) );
+
+			expect( schema.checkAttribute( rootContext, 'align' ) ).to.be.false;
+			expect( schema.checkAttribute( paragraphContext, 'align' ) ).to.be.true;
+		} );
+
 		it( 'accepts an array of node names as a context', () => {
 			const contextInRoot = [ '$root' ];
 			const contextInParagraph = [ '$root', 'paragraph' ];