Ver Fonte

Updated treeView.Element#isSimilar method to work with class set.

Szymon Kupś há 9 anos atrás
pai
commit
c51d12cb37

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

@@ -295,25 +295,28 @@ export default class Element extends Node {
 			return true;
 		}
 
-		// Check name and attributes.
+		// Check element name.
 		if ( this.name != otherElement.name ) {
 			return false;
 		}
 
-		const thisNodeAttrKeys = this.getAttributeKeys();
-		const otherNodeAttrKeys = otherElement.getAttributeKeys();
-		let count = 0;
+		// Check number of attributes and classes.
+		if ( this._attrs.size !== otherElement._attrs.size || this._classes.size !== otherElement._classes.size ) {
+			return false;
+		}
 
-		for ( let key of thisNodeAttrKeys ) {
-			if ( this.getAttribute( key ) !== otherElement.getAttribute( key ) ) {
+		// Check if attributes are the same.
+		for ( let key of this._attrs.keys() ) {
+			if ( !otherElement._attrs.has( key ) || otherElement._attrs.get( key ) !== this._attrs.get( key ) ) {
 				return false;
 			}
-
-			count++;
 		}
 
-		if ( count != utils.count( otherNodeAttrKeys ) ) {
-			return false;
+		// Check if classes are the same.
+		for ( let className of this._classes ) {
+			if ( !otherElement._classes.has( className ) ) {
+				return false;
+			}
 		}
 
 		return true;

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

@@ -156,6 +156,19 @@ describe( 'Element', () => {
 			expect( el.isSimilar( other2 ) ).to.be.false;
 			expect( el.isSimilar( other3 ) ).to.be.false;
 		} );
+
+		it( 'should compare class attribute', () => {
+			const el1 = new ViewElement( 'p' );
+			const el2 = new ViewElement( 'p' );
+			const el3 = new ViewElement( 'p' );
+
+			el1.addClass( 'foo', 'bar' );
+			el2.addClass( 'bar', 'foo' );
+			el3.addClass( 'baz' );
+
+			expect( el1.isSimilar( el2 ) ).to.be.true;
+			expect( el1.isSimilar( el3 ) ).to.be.false;
+		} );
 	} );
 
 	describe( 'children manipulation methods', () => {