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

Throw when extending a template but the number of children does not match.

Aleksander Nowodzinski 9 лет назад
Родитель
Сommit
3923e847ab
2 измененных файлов с 140 добавлено и 102 удалено
  1. 7 2
      packages/ckeditor5-ui/src/template.js
  2. 133 100
      packages/ckeditor5-ui/tests/template.js

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

@@ -872,8 +872,13 @@ function extendTemplateDefinition( def, extDef, defSiblings ) {
 	}
 
 	if ( extDef.children ) {
-		if ( !def.children ) {
-			def.children = [];
+		if ( !def.children || def.children.length != extDef.children.length ) {
+			/**
+			 * The number of children in extended definition does not match.
+			 *
+			 * @error ui-template-extend-children-mismatch
+			 */
+			throw new CKEditorError( 'ui-template-extend-children-mismatch' );
 		}
 
 		extDef.children.forEach( ( extChildDef, index ) => {

+ 133 - 100
packages/ckeditor5-ui/tests/template.js

@@ -1417,51 +1417,90 @@ describe( 'Template', () => {
 				expect( el.outerHTML ).to.equal( '<p>A BXY</p>' );
 				expect( el.childNodes ).to.have.length( 2 );
 			} );
+		} );
 
-			it( 'appends new - simple', () => {
-				const el = extensionTest(
-					{
-						tag: 'p',
-						children: [
-							'foo'
-						]
-					},
-					{
-						children: [
-							'',
-							'bar'
-						]
-					},
-					'<p>foobar</p>'
-				);
-
-				expect( el.childNodes ).to.have.length( 2 );
+		describe( 'children', () => {
+			it( 'should throw when the number of children does not correspond', () => {
+				expect( () => {
+					extensionTest(
+						{
+							tag: 'p',
+							children: [
+								'foo'
+							]
+						},
+						{
+							children: [
+								'foo',
+								'bar'
+							]
+						},
+						'it should fail'
+					);
+				} ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
 			} );
 
-			it( 'appends new - complex', () => {
-				const el = extensionTest(
-					{
-						tag: 'p',
-						children: [
-							{ text: 'foo' },
-							'bar'
-						]
-					},
-					{
-						children: [
-							'',
-							'',
-							'C'
-						]
-					},
-					'<p>foobarC</p>'
-				);
+			it( 'should throw when no children in target but extending one', () => {
+				expect( () => {
+					extensionTest(
+						{
+							tag: 'p',
+						},
+						{
+							children: [
+								{
+									tag: 'b'
+								}
+							]
+						},
+						'it should fail'
+					);
+				} ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
+			} );
 
-				expect( el.childNodes ).to.have.length( 3 );
+			it( 'should throw when the number of children does not correspond on some deeper level', () => {
+				expect( () => {
+					extensionTest(
+						{
+							tag: 'p',
+							children: [
+								{
+									tag: 'span',
+									attributes: {
+										class: 'A'
+									},
+									children: [
+										'A',
+										{
+											tag: 'span',
+											attributes: {
+												class: 'AA'
+											},
+											children: [
+												'AA'
+											]
+										}
+									]
+								}
+							]
+						},
+						{
+							children: [
+								{
+									attributes: {
+										class: 'B'
+									},
+									children: [
+										'B'
+									]
+								}
+							]
+						},
+						'it should fail'
+					);
+				} ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
 			} );
-		} );
 
-		describe( 'children', () => {
 			it( 'extends existing - simple', () => {
 				extensionTest(
 					{
@@ -1540,74 +1579,68 @@ describe( 'Template', () => {
 				);
 			} );
 
-			it( 'appends new - no children', () => {
-				extensionTest(
-					{
-						tag: 'p'
-					},
-					{
-						children: [
-							{
-								tag: 'span',
-								attributes: {
-									class: 'bar'
-								}
+			it( 'allows extending a particular child', () => {
+				const template = new Template( {
+					tag: 'p',
+					children: [
+						{
+							tag: 'span',
+							attributes: {
+								class: 'foo'
 							}
-						]
-					},
-					'<p><span class="bar"></span></p>'
-				);
+						}
+					]
+				} );
+
+				Template.extend( template.definition.children[ 0 ], {
+					attributes: {
+						class: 'bar'
+					}
+				} );
+
+				expect( template.render().outerHTML ).to.equal( '<p><span class="foo bar"></span></p>' );
 			} );
 
-			it( 'appends new - element', () => {
-				extensionTest(
-					{
-						tag: 'p',
-						children: [
-							{
-								tag: 'span',
-								attributes: {
-									class: 'foo'
+			it( 'allows extending a particular child – recursively', () => {
+				const template = new Template( {
+					tag: 'p',
+					children: [
+						{
+							tag: 'span',
+							attributes: {
+								class: 'A'
+							},
+							children: [
+								'A',
+								{
+									tag: 'span',
+									attributes: {
+										class: 'AA'
+									},
+									children: [
+										'AA'
+									]
 								}
-							}
-						]
+							]
+						}
+					]
+				} );
+
+				Template.extend( template.definition.children[ 0 ], {
+					attributes: {
+						class: 'B',
 					},
-					{
-						children: [
-							{},
-							{
-								tag: 'span',
-								attributes: {
-									class: 'bar'
-								}
+					children: [
+						'B',
+						{
+							attributes: {
+								class: 'BB'
 							}
-						]
-					},
-					'<p><span class="foo"></span><span class="bar"></span></p>'
-				);
-			} );
+						}
+					]
+				} );
 
-			it( 'appends new - text', () => {
-				extensionTest(
-					{
-						tag: 'p',
-						children: [
-							{
-								tag: 'span',
-								attributes: {
-									class: 'foo'
-								}
-							}
-						]
-					},
-					{
-						children: [
-							{},
-							'A'
-						]
-					},
-					'<p><span class="foo"></span>A</p>'
-				);
+				expect( template.render().outerHTML ).to.equal( '<p><span class="A B">AB<span class="AA BB">AA</span></span></p>' );
 			} );
 		} );