8
0
Quellcode durchsuchen

Added tests to treeView.Text#same and treeView.Element#same methods. Update docs in treeView.Node.

Szymon Kupś vor 9 Jahren
Ursprung
Commit
ce1cdd4c2c

+ 18 - 5
packages/ckeditor5-engine/src/treeview/element.js

@@ -233,21 +233,34 @@ export default class Element extends Node {
 		return this._children.splice( index, number );
 	}
 
-	same( otherNode ) {
-		if ( !( otherNode instanceof Element ) ) {
+	/**
+	 * Checks if this element is the same as other element.
+	 * Both elements should have the same name and attributes to be considered as same.
+	 *
+	 * @param {Element} otherElement
+	 * @returns {Boolean}
+	 */
+	same( otherElement ) {
+		if ( !( otherElement instanceof Element ) ) {
 			return false;
 		}
 
-		if ( this.name != otherNode.name ) {
+		// If exactly the same Element is provided - return true immediately.
+		if ( this === otherElement ) {
+			return true;
+		}
+
+		// Check name and attributes.
+		if ( this.name != otherElement.name ) {
 			return false;
 		}
 
 		const thisNodeAttrKeys = this.getAttributeKeys();
-		const otherNodeAttrKeys = this.getAttributeKeys();
+		const otherNodeAttrKeys = otherElement.getAttributeKeys();
 		let count = 0;
 
 		for ( let key of thisNodeAttrKeys ) {
-			if ( this.getAttribute( key ) !== otherNode.getAttribute( key ) ) {
+			if ( this.getAttribute( key ) !== otherElement.getAttribute( key ) ) {
 				return false;
 			}
 

+ 7 - 0
packages/ckeditor5-engine/src/treeview/node.js

@@ -139,6 +139,13 @@ export default class Node {
 	 * @returns {treeView.Node} Clone of this node.
 	 */
 
+	/**
+	 * Checks if provided node is the same as this node.
+	 *
+	 * @method treeView.Node#same
+	 * @returns {Boolean} True if nodes are the same.
+	 */
+
 	/**
 	 * Fired when a node changes.
 	 *

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

@@ -57,11 +57,18 @@ export default class Text extends Node {
 		this._data = data;
 	}
 
+	/**
+	 * Checks if this text node is the same as other text node.
+	 * Both nodes should have the same data to be considered as same.
+	 *
+	 * @param otherNode
+	 * @returns {boolean}
+	 */
 	same( otherNode ) {
 		if ( !( otherNode instanceof Text ) ) {
 			return false;
 		}
 
-		return this.data === otherNode.data;
+		return this === otherNode || this.data === otherNode.data;
 	}
 }

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

@@ -103,6 +103,43 @@ describe( 'Element', () => {
 		} );
 	} );
 
+	describe( 'same', () => {
+		const el = new ViewElement( 'p', { foo: 'bar' } );
+
+		it( 'should return false when comparing to non-element', () => {
+			expect( el.same( null ) ).to.be.false;
+			expect( el.same( {} ) ).to.be.false;
+		} );
+
+		it( 'should return true when the same node is provided', () => {
+			expect( el.same( el ) ).to.be.true;
+		} );
+
+		it( 'should return true for element with same attributes and name', () => {
+			const other = new ViewElement( 'p', { foo: 'bar' } );
+			expect( el.same( other ) ).to.be.true;
+		} );
+
+		it( 'sould return false when name is not the same', () => {
+			const other = el.clone();
+			other.name = 'div';
+
+			expect( el.same( other ) ).to.be.false;
+		} );
+
+		it( 'should return false when attributes are not the same', () => {
+			const other1 = el.clone();
+			const other2 = el.clone();
+			const other3 = el.clone();
+			other1.setAttribute( 'baz', 'qux' );
+			other2.setAttribute( 'foo', 'not-bar' );
+			other3.removeAttribute( 'foo' );
+			expect( el.same( other1 ) ).to.be.false;
+			expect( el.same( other2 ) ).to.be.false;
+			expect( el.same( other3 ) ).to.be.false;
+		} );
+	} );
+
 	describe( 'children manipulation methods', () => {
 		let parent, el1, el2, el3, el4;
 

+ 26 - 0
packages/ckeditor5-engine/tests/treeview/text.js

@@ -31,6 +31,32 @@ describe( 'Element', () => {
 		} );
 	} );
 
+	describe( 'same', () => {
+		const text = new ViewText( 'foo' );
+
+		it( 'should return false when comparing to non-text', () => {
+			expect( text.same( null ) ).to.be.false;
+			expect( text.same( {} ) ).to.be.false;
+		} );
+
+		it( 'should return true when the same text node is provided', () => {
+			expect( text.same( text ) ).to.be.true;
+		} );
+
+		it( 'sould return true when data is the same', () => {
+			const other = new ViewText( 'foo' );
+
+			expect( text.same( other ) ).to.be.true;
+		} );
+
+		it( 'sould return false when data is not the same', () => {
+			const other = text.clone();
+			other.data = 'not-foo';
+
+			expect( text.same( other ) ).to.be.false;
+		} );
+	} );
+
 	describe( 'setText', () => {
 		it( 'should change the text', () => {
 			const text = new ViewText( 'foo' );