Kaynağa Gözat

Allowed SchemaContext as a context of SchemaContext.

Oskar Wróbel 7 yıl önce
ebeveyn
işleme
74a66eb6a1

+ 7 - 8
packages/ckeditor5-engine/src/model/schema.js

@@ -193,16 +193,11 @@ export default class Schema {
 		this.decorate( 'checkAttribute' );
 
 		this.on( 'checkAttribute', ( evt, args ) => {
-			if ( !( args[ 0 ] instanceof SchemaContext ) ) {
-				args[ 0 ] = new SchemaContext( args[ 0 ] );
-			}
+			args[ 0 ] = new SchemaContext( args[ 0 ] );
 		}, { priority: 'highest' } );
 
 		this.on( 'checkChild', ( evt, args ) => {
-			if ( !( args[ 0 ] instanceof SchemaContext ) ) {
-				args[ 0 ] = new SchemaContext( args[ 0 ] );
-			}
-
+			args[ 0 ] = new SchemaContext( args[ 0 ] );
 			args[ 1 ] = this.getDefinition( args[ 1 ] );
 		}, { priority: 'highest' } );
 	}
@@ -1072,9 +1067,13 @@ export class SchemaContext {
 	/**
 	 * Creates an instance of the context.
 	 *
-	 * @param {module:engine/model/schema~SchemaContextDefinition} context
+	 * @param {module:engine/model/schema~SchemaContextDefinition|module:engine/model/schema~SchemaContext} context
 	 */
 	constructor( context ) {
+		if ( context instanceof SchemaContext ) {
+			return context;
+		}
+
 		if ( Array.isArray( context ) ) {
 			if ( context[ 0 ] && typeof context[ 0 ] != 'string' && context[ 0 ].is( 'documentFragment' ) ) {
 				context.shift();

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

@@ -2455,6 +2455,14 @@ describe( 'SchemaContext', () => {
 			expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
 		} );
 
+		it( 'creates context based on a SchemaContext instance', () => {
+			const previousCtx = new SchemaContext( [ 'a', 'b', 'c' ] );
+
+			const ctx = new SchemaContext( previousCtx );
+
+			expect( ctx ).to.equal( previousCtx );
+		} );
+
 		it( 'filters out DocumentFragment when it is a first item of context - array', () => {
 			const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );