Przeglądaj źródła

Allowed Template instance as a child of another Template.

Aleksander Nowodzinski 9 lat temu
rodzic
commit
1d5a738b5f

+ 10 - 2
packages/ckeditor5-ui/src/template.js

@@ -787,7 +787,7 @@ function clone( def ) {
 		// Also don't clone View instances if provided as a child of the Template. The template
 		// instance will be extracted from the View during the normalization and there's no need
 		// to clone it.
-		if ( value && ( value instanceof TemplateBinding || isView( value ) || isViewCollection( value ) ) ) {
+		if ( value && ( value instanceof TemplateBinding || isTemplate( value ) || isView( value ) || isViewCollection( value ) ) ) {
 			return value;
 		}
 	} );
@@ -831,7 +831,7 @@ function normalize( def ) {
 				children.add( def.children );
 			} else {
 				for ( let child of def.children ) {
-					if ( isView( child ) ) {
+					if ( isTemplate( child ) || isView( child ) ) {
 						children.add( child );
 					} else {
 						children.add( new Template( child ) );
@@ -1063,6 +1063,14 @@ function isView( item ) {
 	return item instanceof View;
 }
 
+// Checks if the item is an instance of {@link module:ui/template~Template}
+//
+// @private
+// @param {*} value Value to be checked.
+function isTemplate( item ) {
+	return item instanceof Template;
+}
+
 // Checks if the item is an instance of {@link module:ui/viewcollection~ViewCollection}
 //
 // @private

+ 24 - 0
packages/ckeditor5-ui/tests/template.js

@@ -484,6 +484,30 @@ describe( 'Template', () => {
 
 				expect( collection._parentElement ).to.equal( rendered );
 			} );
+
+			it( 'renders template children', () => {
+				const childTplA = new Template( {
+					tag: 'a'
+				} );
+
+				const childTplB = new Template( {
+					tag: 'b'
+				} );
+
+				const tpl = new Template( {
+					tag: 'p',
+					children: [
+						childTplA,
+						childTplB
+					]
+				} );
+
+				// Make sure child instances weren't cloned.
+				expect( tpl.children.get( 0 ) ).to.equal( childTplA );
+				expect( tpl.children.get( 1 ) ).to.equal( childTplB );
+
+				expect( normalizeHtml( tpl.render().outerHTML ) ).to.equal( '<p><a></a><b></b></p>' );
+			} );
 		} );
 
 		describe( 'bindings', () => {