瀏覽代碼

Added `SchemaContext#addItem` method.

Oskar Wróbel 7 年之前
父節點
當前提交
873d3f5f7e
共有 2 個文件被更改,包括 39 次插入0 次删除
  1. 9 0
      packages/ckeditor5-engine/src/model/schema.js
  2. 30 0
      packages/ckeditor5-engine/tests/model/schema.js

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

@@ -1091,6 +1091,15 @@ export class SchemaContext {
 	}
 
 	/**
+	 * Adds new item at the to of the context.
+	 *
+	 * @param {module:engine/model/node~Node|String} item Item to add.
+	 */
+	addItem( item ) {
+		this._items.push( mapContextItem( item ) );
+	}
+
+	/**
 	 * Gets an item on the given index.
 	 *
 	 * @returns {module:engine/model/schema~SchemaContextItem}

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

@@ -2459,6 +2459,36 @@ describe( 'SchemaContext', () => {
 		} );
 	} );
 
+	describe( 'addItem()', () => {
+		it( 'adds new item at the top of the context #text', () => {
+			const node = new Text( 'd' );
+
+			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
+
+			ctx.addItem( node );
+
+			expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
+		} );
+
+		it( 'adds new item at the top of the context #string', () => {
+			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
+
+			ctx.addItem( 'd' );
+
+			expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
+		} );
+
+		it( 'adds new item at the top of the context #node', () => {
+			const node = new Element( 'd' );
+
+			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
+
+			ctx.addItem( node );
+
+			expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
+		} );
+	} );
+
 	describe( 'getNames()', () => {
 		it( 'returns an iterator', () => {
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );