8
0
فهرست منبع

Make styles.hasProperty() check to be in line with styles.getProperty().

Maciej Gołaszewski 6 سال پیش
والد
کامیت
2455d25ac4

+ 1 - 1
packages/ckeditor5-engine/src/view/element.js

@@ -332,7 +332,7 @@ export default class Element extends Node {
 		}
 
 		// Check if styles are the same.
-		for ( const property of Object.keys( this._styles._styles ) ) {
+		for ( const property of this._styles.getStyleNames() ) {
 			if (
 				!otherElement._styles.hasProperty( property ) ||
 				otherElement._styles.getInlineProperty( property ) !== this._styles.getInlineProperty( property )

+ 18 - 4
packages/ckeditor5-engine/src/view/styles.js

@@ -229,10 +229,24 @@ export default class Styles {
 	 * @param {String} name
 	 * @returns {Boolean}
 	 */
-	hasProperty( name ) {
-		// TODO: this behavior is not in sync with getProperty.
-		// TODO: also what to do on `border` vs `border-top` vs `border-color`...
-		return Array.from( this.getStyleNames() ).includes( name );
+	hasProperty( propertyName ) {
+		const normalized = this.converter.getNormalized( propertyName, this._styles );
+
+		if ( !normalized ) {
+			// Try return styles set directly - values that are not parsed.
+			return this._styles[ propertyName ] !== undefined;
+		}
+
+		if ( isObject( normalized ) ) {
+			const styles = this.converter.getReducedForm( propertyName, normalized );
+
+			const propertyDescriptor = styles.find( ( [ property ] ) => property === propertyName );
+
+			// Only return a value if it is set;
+			return Array.isArray( propertyDescriptor );
+		} else {
+			return true;
+		}
 	}
 
 	/**

+ 7 - 3
packages/ckeditor5-engine/tests/view/styles.js

@@ -135,7 +135,11 @@ describe( 'Styles', () => {
 	} );
 
 	describe( 'hasProperty()', () => {
-		it( 'should return false if property is not set', () => {
+		it( 'should return false if normalized property is not set', () => {
+			expect( styles.hasProperty( 'bar' ) ).to.be.false;
+		} );
+
+		it( 'should return false if normalized property is not set', () => {
 			expect( styles.hasProperty( 'foo' ) ).to.be.false;
 		} );
 
@@ -146,9 +150,9 @@ describe( 'Styles', () => {
 		} );
 
 		it( 'should return true if property is set', () => {
-			styles.setStyle( 'color:deeppink' );
+			styles.setStyle( 'bar:deeppink' );
 
-			expect( styles.hasProperty( 'color' ) ).to.be.true;
+			expect( styles.hasProperty( 'bar' ) ).to.be.true;
 		} );
 
 		it( 'should return true if normalized shorthanded property is set', () => {