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

Merge pull request #1265 from ckeditor/t/1263

Internal: Changed `SchemaContext#concat` to `SchemaContext#push`. Closes #1263.
Piotr Jasiun 8 лет назад
Родитель
Сommit
b67379bbde
2 измененных файлов с 33 добавлено и 43 удалено
  1. 28 15
      packages/ckeditor5-engine/src/model/schema.js
  2. 5 28
      packages/ckeditor5-engine/tests/model/schema.js

+ 28 - 15
packages/ckeditor5-engine/src/model/schema.js

@@ -376,8 +376,7 @@ export default class Schema {
 	 *		schema.checkChild( model.document.getRoot(), paragraph ); // -> true
 	 *		schema.checkChild( model.document.getRoot(), paragraph ); // -> true
 	 *
 	 *
 	 * @fires checkChild
 	 * @fires checkChild
-	 * @param {module:engine/model/schema~SchemaContextDefinition|module:engine/model/schema~SchemaContext} context
-	 * Context in which the child will be checked.
+	 * @param {module:engine/model/schema~SchemaContextDefinition} context Context in which the child will be checked.
 	 * @param {module:engine/model/node~Node|String} def The child to check.
 	 * @param {module:engine/model/node~Node|String} def The child to check.
 	 */
 	 */
 	checkChild( context, def ) {
 	checkChild( context, def ) {
@@ -401,8 +400,7 @@ export default class Schema {
 	 *		schema.checkAttribute( textNode, 'bold' ); // -> true
 	 *		schema.checkAttribute( textNode, 'bold' ); // -> true
 	 *
 	 *
 	 * @fires checkAttribute
 	 * @fires checkAttribute
-	 * @param {module:engine/model/schema~SchemaContextDefinition|module:engine/model/schema~SchemaContext} context
-	 * Context in which the attribute will be checked.
+	 * @param {module:engine/model/schema~SchemaContextDefinition} context Context in which the attribute will be checked.
 	 * @param {String} attributeName
 	 * @param {String} attributeName
 	 */
 	 */
 	checkAttribute( context, attributeName ) {
 	checkAttribute( context, attributeName ) {
@@ -1164,18 +1162,31 @@ export class SchemaContext {
 	}
 	}
 
 
 	/**
 	/**
-	 * Returns new SchemaContext instance with additional items created from provided definition.
+	 * Returns new SchemaContext instance with additional item
+	 *
+	 * Item can be added as:
+	 *
+	 * 		const context = new SchemaContext( [ '$root' ] );
+	 *
+	 * 		// An element.
+	 * 		const fooElement = writer.createElement( 'fooElement' );
+	 * 		const newContext = context.push( fooElement ); // [ '$root', 'fooElement' ]
 	 *
 	 *
-	 * @param {String|module:engine/model/node~Node|module:engine/model/schema~SchemaContext|
-	 * Array<String|module:engine/model/node~Node>} definition Definition of item(s) that will be added to current context.
-	 * @returns {module:engine/model/schema~SchemaContext} New SchemaContext instance.
+	 * 		// A text node.
+	 * 		const text = writer.createText( 'foobar' );
+	 * 		const newContext = context.push( text ); // [ '$root', '$text' ]
+	 *
+	 * 		// A string (element name).
+	 * 		const newContext = context.push( 'barElement' ); // [ '$root', 'barElement' ]
+	 *
+	 * **Note** {module:engine/model/node~Node} that is already in the model tree will be added as the only item (without ancestors).
+	 *
+	 * @param {String|module:engine/model/node~Node|Array<String|module:engine/model/node~Node>} item Item that will be added
+	 * to current context.
+	 * @returns {module:engine/model/schema~SchemaContext} New SchemaContext instance with additional item.
 	 */
 	 */
-	concat( definition ) {
-		if ( !( definition instanceof SchemaContext ) && !Array.isArray( definition ) ) {
-			definition = [ definition ];
-		}
-
-		const ctx = new SchemaContext( definition );
+	push( item ) {
+		const ctx = new SchemaContext( [ item ] );
 
 
 		ctx._items = [ ...this._items, ...ctx._items ];
 		ctx._items = [ ...this._items, ...ctx._items ];
 
 
@@ -1231,6 +1242,8 @@ export class SchemaContext {
  * means that the context will be unrealistic (e.g. attributes of these nodes are not specified).
  * means that the context will be unrealistic (e.g. attributes of these nodes are not specified).
  * However, at times this may be the only way to define the context (e.g. when checking some
  * However, at times this may be the only way to define the context (e.g. when checking some
  * hypothetical situation).
  * hypothetical situation).
+ * * By defining a {@link module:engine/model/schema~SchemaContext} instance - in this case the same instance as provided
+ * will be return.
  *
  *
  * Examples of context definitions passed to the {@link module:engine/model/schema~Schema#checkChild `Schema#checkChild()`}
  * Examples of context definitions passed to the {@link module:engine/model/schema~Schema#checkChild `Schema#checkChild()`}
  * method:
  * method:
@@ -1278,7 +1291,7 @@ export class SchemaContext {
  *		// Check in [ rootElement, paragraphElement, textNode ].
  *		// Check in [ rootElement, paragraphElement, textNode ].
  *		schema.checkChild( [ ...positionInParagraph.getAncestors(), '$text' ], 'bold' );
  *		schema.checkChild( [ ...positionInParagraph.getAncestors(), '$text' ], 'bold' );
  *
  *
- * @typedef {module:engine/model/node~Node|module:engine/model/position~Position|
+ * @typedef {module:engine/model/node~Node|module:engine/model/position~Position|module:engine/model/schema~SchemaContext|
  * Array.<String|module:engine/model/node~Node>} module:engine/model/schema~SchemaContextDefinition
  * Array.<String|module:engine/model/node~Node>} module:engine/model/schema~SchemaContextDefinition
  */
  */
 
 

+ 5 - 28
packages/ckeditor5-engine/tests/model/schema.js

@@ -2755,11 +2755,11 @@ describe( 'SchemaContext', () => {
 		} );
 		} );
 	} );
 	} );
 
 
-	describe( 'concat()', () => {
+	describe( 'push()', () => {
 		it( 'creates new SchemaContext instance with new item - #string', () => {
 		it( 'creates new SchemaContext instance with new item - #string', () => {
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
 
 
-			const newCtx = ctx.concat( 'd' );
+			const newCtx = ctx.push( 'd' );
 
 
 			expect( newCtx ).to.instanceof( SchemaContext );
 			expect( newCtx ).to.instanceof( SchemaContext );
 			expect( newCtx ).to.not.equal( ctx );
 			expect( newCtx ).to.not.equal( ctx );
@@ -2771,7 +2771,7 @@ describe( 'SchemaContext', () => {
 			const node = new Text( 'd' );
 			const node = new Text( 'd' );
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
 
 
-			const newCtx = ctx.concat( node );
+			const newCtx = ctx.push( node );
 
 
 			expect( newCtx ).to.instanceof( SchemaContext );
 			expect( newCtx ).to.instanceof( SchemaContext );
 			expect( newCtx ).to.not.equal( ctx );
 			expect( newCtx ).to.not.equal( ctx );
@@ -2779,40 +2779,17 @@ describe( 'SchemaContext', () => {
 			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
 			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
 		} );
 		} );
 
 
-		it( 'creates new SchemaContext instance with new item - #node', () => {
+		it( 'creates new SchemaContext instance with new item - #element', () => {
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
 			const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
 			const parent = new Element( 'parent', null, new Element( 'd' ) );
 			const parent = new Element( 'parent', null, new Element( 'd' ) );
 
 
-			const newCtx = ctx.concat( parent.getChild( 0 ) );
+			const newCtx = ctx.push( parent.getChild( 0 ) );
 
 
 			expect( newCtx ).to.instanceof( SchemaContext );
 			expect( newCtx ).to.instanceof( SchemaContext );
 			expect( newCtx ).to.not.equal( ctx );
 			expect( newCtx ).to.not.equal( ctx );
 			expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
 			expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
 			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
 			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
 		} );
 		} );
-
-		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' ] );
-
-			const newCtx = ctx.concat( [ 'd', new Text( 'e' ), new Element( 'f' ) ] );
-
-			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' ] );
-		} );
 	} );
 	} );
 
 
 	describe( 'getNames()', () => {
 	describe( 'getNames()', () => {