Przeglądaj źródła

Added support for mixed arrays of elements and strings as checkChild()'s context.

Piotrek Koszuliński 8 lat temu
rodzic
commit
8e793dce69

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

@@ -310,25 +310,28 @@ function getAllowedChildren( compiledRules, itemName ) {
 }
 
 function normalizeContext( ctx ) {
-	// See the comment in tests about specifying checkChild()'s context as an array.
 	if ( Array.isArray( ctx ) ) {
-		return ctx.map( nodeName => {
-			return {
-				name: nodeName,
-				* getAttributes() {}
-			};
-		} );
+		return ctx.map( mapContextItem );
 	}
 	// Item or position (PS. It's ok that Position#getAncestors() doesn't accept params).
 	else {
-		return ctx.getAncestors( { includeSelf: true } ).map( node => {
-			return {
-				name: node.is( 'text' ) ? '$text' : node.name,
-				* getAttributes() {
-					yield* node.getAttributes();
-				}
-			};
-		} );
+		return ctx.getAncestors( { includeSelf: true } ).map( mapContextItem );
+	}
+}
+
+function mapContextItem( ctxItem ) {
+	if ( typeof ctxItem == 'string' ) {
+		return {
+			name: ctxItem,
+			* getAttributes() {}
+		};
+	} else {
+		return {
+			name: ctxItem.is( 'text' ) ? '$text' : ctxItem.name,
+			* getAttributes() {
+				yield* ctxItem.getAttributes();
+			}
+		};
 	}
 }
 

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

@@ -317,6 +317,25 @@ describe( 'Schema', () => {
 			expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
 		} );
 
+		it( 'accepts an array of elements as a context', () => {
+			const contextInRoot = [ root1 ];
+			const contextInParagraph = [ root1, r1p1 ];
+
+			expect( schema.checkChild( contextInRoot, 'paragraph' ) ).to.be.true;
+			expect( schema.checkChild( contextInRoot, '$text' ) ).to.be.false;
+
+			expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
+			expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
+		} );
+
+		// Again, this is needed temporarily to handle current V->M conversion
+		it( 'accepts a mixed array of elements and strings as a context', () => {
+			const contextInParagraph = [ '$root', r1p1 ];
+
+			expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
+			expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
+		} );
+
 		it( 'accepts a node as a child', () => {
 			expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
 			expect( schema.checkChild( root1, new Text( 'foo' ) ) ).to.be.false;
@@ -1308,4 +1327,5 @@ describe( 'Schema', () => {
 	// * inheritAllFrom should also inherit is* props (see tests documentselection getNearestSelectionRange())
 	// * test the default abstract entities (in model.js)
 	// * see clipboardHolder definition (and rename it to the pastebin)
+	// * review insertContent's _tryAutoparagraphing()
 } );