Przeglądaj źródła

Changed SchemaContext#addItem to SchemaContext#concat.

Oskar Wróbel 8 lat temu
rodzic
commit
998ac7c0c0

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

@@ -1164,12 +1164,22 @@ export class SchemaContext {
 	}
 
 	/**
-	 * Adds new item at the to of the context.
+	 * Returns new SchemaContext instance with additional items created from provided definition.
 	 *
-	 * @param {module:engine/model/node~Node|String} item Item to add.
+	 * @param {String|module:engine/model/node~Node|~SchemaContextDefinition|Array<String|module:engine/model/node~Node>} definition
+	 * Definition of item(s) that will be added to current context.
+	 * @returns {~SchemaContext} New SchemaContext instance.
 	 */
-	addItem( item ) {
-		this._items = [ ...this._items, mapContextItem( item ) ];
+	concat( definition ) {
+		if ( !( definition instanceof SchemaContext ) && !Array.isArray( definition ) ) {
+			definition = [ definition ];
+		}
+
+		const ctx = new SchemaContext( definition );
+
+		ctx._items = [ ...this._items, ...ctx._items ];
+
+		return ctx;
 	}
 
 	/**

+ 42 - 12
packages/ckeditor5-engine/tests/model/schema.js

@@ -2755,33 +2755,63 @@ describe( 'SchemaContext', () => {
 		} );
 	} );
 
-	describe( 'addItem()', () => {
-		it( 'adds new item at the top of the context #text', () => {
-			const node = new Text( 'd' );
+	describe( 'concat()', () => {
+		it( 'creates new SchemaContext instance with new item - #string', () => {
+			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
+
+			const newCtx = ctx.concat( 'd' );
+
+			expect( newCtx ).to.instanceof( SchemaContext );
+			expect( newCtx ).to.not.equal( ctx );
+			expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
+		} );
 
+		it( 'creates new SchemaContext instance with new item - #text', () => {
+			const node = new Text( 'd' );
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
 
-			ctx.addItem( node );
+			const newCtx = ctx.concat( node );
 
-			expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
+			expect( newCtx ).to.instanceof( SchemaContext );
+			expect( newCtx ).to.not.equal( ctx );
+			expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
 		} );
 
-		it( 'adds new item at the top of the context #string', () => {
+		it( 'creates new SchemaContext instance with new item - #node', () => {
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
+			const parent = new Element( 'parent', null, new Element( 'd' ) );
 
-			ctx.addItem( 'd' );
+			const newCtx = ctx.concat( parent.getChild( 0 ) );
 
-			expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
+			expect( newCtx ).to.instanceof( SchemaContext );
+			expect( newCtx ).to.not.equal( ctx );
+			expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
 		} );
 
-		it( 'adds new item at the top of the context #node', () => {
-			const node = new Element( 'd' );
+		it( 'creates new SchemaContext instance with new item - #SchemaContext', () => {
+			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
+			const schemaContext = new SchemaContext( [ 'd', 'e' ] );
+
+			const newCtx = ctx.concat( schemaContext );
 
+			expect( newCtx ).to.instanceof( SchemaContext );
+			expect( newCtx ).to.not.equal( ctx );
+			expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd', 'e' ] );
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
+		} );
+
+		it( 'creates new SchemaContext instance with new item - #array', () => {
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
 
-			ctx.addItem( node );
+			const newCtx = ctx.concat( [ 'd', new Text( 'e' ), new Element( 'f' ) ] );
 
-			expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
+			expect( newCtx ).to.instanceof( SchemaContext );
+			expect( newCtx ).to.not.equal( ctx );
+			expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd', '$text', 'f' ] );
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
 		} );
 	} );