Ver Fonte

Addded styles checking to core.treeView.Element#isSimilar.

Szymon Kupś há 9 anos atrás
pai
commit
78b7d363a5

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

@@ -366,13 +366,14 @@ export default class Element extends Node {
 		}
 
 		// Check number of attributes and classes.
-		if ( this._attrs.size !== otherElement._attrs.size || this._classes.size !== otherElement._classes.size ) {
+		if ( this._attrs.size !== otherElement._attrs.size || this._classes.size !== otherElement._classes.size ||
+			this._styles.size !== otherElement._styles.size ) {
 			return false;
 		}
 
 		// 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 ) ) {
+		for ( let [ key, value ] of this._attrs ) {
+			if ( !otherElement._attrs.has( key ) || otherElement._attrs.get( key ) !== value ) {
 				return false;
 			}
 		}
@@ -384,6 +385,13 @@ export default class Element extends Node {
 			}
 		}
 
+		// Check if styles are the same.
+		for ( let [ property, value ] of this._styles ) {
+			if ( !otherElement._styles.has( property ) || otherElement._styles.get( property ) !== value ) {
+				return false;
+			}
+		}
+
 		return true;
 	}
 

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

@@ -197,6 +197,26 @@ describe( 'Element', () => {
 			expect( el1.isSimilar( el3 ) ).to.be.false;
 			expect( el1.isSimilar( el4 ) ).to.be.false;
 		} );
+
+		it( 'should compare styles attribute', () => {
+			const el1 = new ViewElement( 'p' );
+			const el2 = new ViewElement( 'p' );
+			const el3 = new ViewElement( 'p' );
+			const el4 = new ViewElement( 'p' );
+
+			el1.setStyle( 'color', 'red' );
+			el1.setStyle( 'top', '10px' );
+			el2.setStyle( 'top', '20px' );
+			el3.setStyle( 'top', '10px' );
+			el3.setStyle( 'color', 'red' );
+			el4.setStyle( 'color', 'blue' );
+			el4.setStyle( 'top', '10px' );
+
+			expect( el1.isSimilar( el2 ) ).to.be.false;
+			expect( el1.isSimilar( el3 ) ).to.be.true;
+			expect( el2.isSimilar( el3 ) ).to.be.false;
+			expect( el3.isSimilar( el4 ) ).to.be.false;
+		} );
 	} );
 
 	describe( 'children manipulation methods', () => {