Selaa lähdekoodia

Added TreeModel.Element.appendChildren.

Piotr Jasiun 10 vuotta sitten
vanhempi
sitoutus
f3f74cb4e0

+ 13 - 0
packages/ckeditor5-engine/src/treemodel/element.js

@@ -77,6 +77,19 @@ export default class Element extends Node {
 		return this._children.indexOf( node );
 	}
 
+	/**
+	 * {@link treeModel.Element#insert Insert} a child node or a list of child nodes at the end of this node and sets
+	 * the parent of these nodes to this element.
+	 *
+	 * Note that the list of children can be modified only in elements not yet attached to the document.
+	 * All attached nodes should be modified using the {@link treeModel.operation.InsertOperation}.
+	 *
+	 * @param {treeModel.NodesSet} nodes The list of nodes to be inserted.
+	 */
+	appendChildren( nodes ) {
+		this.insertChildren( this.getChildCount(), nodes );
+	}
+
 	/**
 	 * Inserts a list of child nodes on the given index and sets the parent of these nodes to this element.
 	 *

+ 15 - 0
packages/ckeditor5-engine/tests/treemodel/element.js

@@ -62,6 +62,21 @@ describe( 'Element', () => {
 		} );
 	} );
 
+	describe( 'appendChildren', () => {
+		it( 'should add children to the end of the element', () => {
+			let element = new Element( 'elem', [], [ 'xy' ] );
+			element.appendChildren( 'foo' );
+
+			expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
+			expect( element.getChildCount() ).to.equal( 5 );
+			expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
+			expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'y' );
+			expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'f' );
+			expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
+			expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'o' );
+		} );
+	} );
+
 	describe( 'removeChildren', () => {
 		it( 'should remove children from the element and return them as a NodeList', () => {
 			let element = new Element( 'elem', [], [ 'foobar' ] );