浏览代码

Blocked adding children to the UIElement.

Oskar Wróbel 8 年之前
父节点
当前提交
ef86f165a0
共有 2 个文件被更改,包括 62 次插入6 次删除
  1. 23 1
      packages/ckeditor5-engine/src/view/uielement.js
  2. 39 5
      packages/ckeditor5-engine/tests/view/uielement.js

+ 23 - 1
packages/ckeditor5-engine/src/view/uielement.js

@@ -8,6 +8,8 @@
  */
 
 import Element from './element';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+import Node from './node';
 
 /**
  * UIElement class. It is used to represent features of UI not content of the document.
@@ -17,7 +19,11 @@ export default class UIElement extends Element {
 	/**
 	 * Creates new instance of UIElement.
 	 *
-	 * @see module:engine/view/element~Element
+	 * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-uielement-cannot-add` when third parameter is passed,
+	 * to inform that usage of UIElement is incorrect (adding child nodes to UIElement is forbidden).
+	 *
+	 * @param {String} name Node name.
+	 * @param {Object|Iterable} [attributes] Collection of attributes.
 	 */
 	constructor( name, attributes, children ) {
 		super( name, attributes, children );
@@ -30,6 +36,22 @@ export default class UIElement extends Element {
 		 */
 		this.getFillerOffset = getFillerOffset;
 	}
+
+	/**
+	 * Overrides {@link module:engine/view/element~Element#insertChildren} method.
+	 * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent adding any child nodes
+	 * to EmptyElement.
+	 */
+	insertChildren( index, nodes ) {
+		if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
+			/**
+			 * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
+			 *
+			 * @error view-uielement-cannot-add
+			 */
+			throw new CKEditorError( 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
+		}
+	}
 }
 
 // Returns `null` because block filler is not needed for EmptyElements.

+ 39 - 5
packages/ckeditor5-engine/tests/view/uielement.js

@@ -5,17 +5,17 @@
 
 import UIElement from '../../src/view/uielement';
 import Element from '../../src/view/element';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 describe( 'UIElement', () => {
-	let childElement, uiElement;
+	let uiElement;
 
 	beforeEach( () => {
-		childElement = new Element( 'b' );
 		uiElement = new UIElement( 'span', {
 			foo: 'bar',
 			style: 'border: 1px solid red;color: white;',
 			class: 'foo bar'
-		}, [ childElement ] );
+		} );
 	} );
 
 	describe( 'constructor()', () => {
@@ -26,8 +26,42 @@ describe( 'UIElement', () => {
 			expect( uiElement.getStyle( 'color' ) ).to.equal( 'white' );
 			expect( uiElement.hasClass( 'foo' ) ).to.true;
 			expect( uiElement.hasClass( 'bar' ) ).to.true;
-			expect( uiElement.childCount ).to.equal( 1 );
-			expect( uiElement.getChild( 0 ) ).to.equal( childElement )
+		} );
+
+		it( 'should throw if child elements are passed to constructor', () => {
+			expect( () => {
+				new UIElement( 'img', null, [ new Element( 'i' ) ] );
+			} ).to.throw( CKEditorError, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
+		} );
+	} );
+
+	describe( 'appendChildren()', () => {
+		it( 'should throw when try to append new child element', () => {
+			expect( () => {
+				uiElement.appendChildren( new Element( 'i' ) );
+			} ).to.throw( CKEditorError, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
+		} );
+	} );
+
+	describe( 'insertChildren()', () => {
+		it( 'should throw when try to insert new child element', () => {
+			expect( () => {
+				uiElement.insertChildren( 0, new Element( 'i' ) );
+			} ).to.throw( CKEditorError, 'view-uielement-cannot-add: Cannot add child nodes to UIElement instance.' );
+		} );
+	} );
+
+	describe( 'clone()', () => {
+		it( 'should be properly cloned', () => {
+			const newUIElement = uiElement.clone();
+
+			expect( newUIElement.name ).to.equal( 'span' );
+			expect( newUIElement.getAttribute( 'foo' ) ).to.equal( 'bar' );
+			expect( newUIElement.getStyle( 'border' ) ).to.equal( '1px solid red' );
+			expect( newUIElement.getStyle( 'color' ) ).to.equal( 'white' );
+			expect( newUIElement.hasClass( 'foo' ) ).to.true;
+			expect( newUIElement.hasClass( 'bar' ) ).to.true;
+			expect( newUIElement.isSimilar( uiElement ) ).to.true;
 		} );
 	} );