Selaa lähdekoodia

Changed cloneNode to clone. Added tests to Element.clone() method.

Szymon Kupś 10 vuotta sitten
vanhempi
sitoutus
3033189415

+ 10 - 3
packages/ckeditor5-engine/src/treeview/element.js

@@ -65,16 +65,23 @@ export default class Element extends Node {
 		}
 	}
 
-	cloneNode( deep ) {
+	/**
+	 * Clones provided element.
+	 *
+	 * @param {Boolean} deep If set to `true` clones element and all its children recursively. When set to `false`,
+	 * element will be cloned without any children.
+	 * @returns {Element} Clone of this element.
+	 */
+	clone( deep ) {
 		const childrenClone = [];
 
 		if ( deep ) {
 			for ( let child of this.getChildren() ) {
-				childrenClone.push( child.cloneNode( deep ) );
+				childrenClone.push( child.clone( deep ) );
 			}
 		}
 
-		return new this( this.name, this._attrs, childrenClone );
+		return new Element( this.name, this._attrs, childrenClone );
 	}
 
 	/**

+ 8 - 1
packages/ckeditor5-engine/src/treeview/node.js

@@ -116,7 +116,7 @@ export default class Node {
 	}
 
 	remove() {
-		this.parent.removeChildren( this.getIndex() )
+		this.parent.removeChildren( this.getIndex() );
 	}
 
 	/**
@@ -132,6 +132,13 @@ export default class Node {
 		}
 	}
 
+	/**
+	 * Clones this node.
+	 *
+	 * @method treeView.Node#clone
+	 * @returns {treeView.Node} Clone of this node.
+	 */
+
 	/**
 	 * Fired when a node changes.
 	 *

+ 8 - 3
packages/ckeditor5-engine/src/treeview/text.js

@@ -33,8 +33,13 @@ export default class Text extends Node {
 		this._data = data;
 	}
 
-	cloneNode() {
-		return new this( this.data );
+	/**
+	 * Clones this node.
+	 *
+	 * @returns {Text} Text node that is a clone of this node.
+	 */
+	clone() {
+		return new Text( this.data );
 	}
 
 	/**
@@ -53,7 +58,7 @@ export default class Text extends Node {
 	}
 
 	same( otherNode ) {
-		if ( !otherNode instanceof Text ) {
+		if ( !( otherNode instanceof Text ) ) {
 			return false;
 		}
 

+ 50 - 0
packages/ckeditor5-engine/tests/treeview/element.js

@@ -53,6 +53,56 @@ describe( 'Element', () => {
 		} );
 	} );
 
+	describe( 'clone', () => {
+		it( 'should clone element', () => {
+			const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' } );
+			const clone = el.clone();
+
+			expect( clone ).to.not.equal( el );
+			expect( clone.name ).to.equal( el.name );
+			expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
+			expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
+		} );
+
+		it( 'should deeply clone element', () => {
+			const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' }, [
+				new ViewElement( 'b', { attr: 'baz' } ),
+				new ViewElement( 'span', { attr: 'qux' } )
+			] );
+			const count = el.getChildCount();
+			const clone = el.clone( true );
+
+			expect( clone ).to.not.equal( el );
+			expect( clone.name ).to.equal( el.name );
+			expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
+			expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
+			expect( clone.getChildCount() ).to.equal( count );
+
+			for ( let i = 0; i < count; i++ ) {
+				const child = el.getChild( i );
+				const clonedChild = clone.getChild( i );
+
+				expect( clonedChild ).to.not.equal( child );
+				expect( clonedChild.name ).to.equal( child.name );
+				expect( clonedChild.getAttribute( 'attr' ) ).to.equal( child.getAttribute( 'attr' ) );
+			}
+		} );
+
+		it( 'shouldn\'t clone any children when deep copy is not performed', () => {
+			const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' }, [
+				new ViewElement( 'b', { attr: 'baz' } ),
+				new ViewElement( 'span', { attr: 'qux' } )
+			] );
+			const clone = el.clone( false );
+
+			expect( clone ).to.not.equal( el );
+			expect( clone.name ).to.equal( el.name );
+			expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
+			expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
+			expect( clone.getChildCount() ).to.equal( 0 );
+		} );
+	} );
+
 	describe( 'children manipulation methods', () => {
 		let parent, el1, el2, el3, el4;