8
0
Pārlūkot izejas kodu

Modified attribute methods in core.treeView.Element to work properly with classes set.

Szymon Kupś 9 gadi atpakaļ
vecāks
revīzija
6080074c05

+ 31 - 4
packages/ckeditor5-engine/src/treeview/element.js

@@ -157,16 +157,33 @@ export default class Element extends Node {
 	 * @returns {Iterator.<String>} Keys for attributes.
 	 */
 	getAttributeKeys() {
-		return this._attrs.keys();
+		const iterator = this._attrs.keys();
+
+		if ( this._classes.size > 0 ) {
+			return ( function*() {
+				yield 'class';
+				yield* iterator;
+			}() );
+		}
+
+		return iterator;
 	}
 
 	/**
-	 * Gets attribute by key.
+	 * Gets attribute by key. If attribute is not present - returns undefined.
 	 *
 	 * @param {String} key Attribute key.
-	 * @returns {String} Attribute value.
+	 * @returns {String|undefined} Attribute value.
 	 */
 	getAttribute( key ) {
+		if ( key == 'class' ) {
+			if ( this._classes.size > 0 ) {
+				return [ ...this._classes ].join( ' ' );
+			}
+
+			return undefined;
+		}
+
 		return this._attrs.get( key );
 	}
 
@@ -177,6 +194,10 @@ export default class Element extends Node {
 	 * @returns {Boolean} `true` if attribute with the specified key exists in the element, false otherwise.
 	 */
 	hasAttribute( key ) {
+		if ( key == 'class' ) {
+			return this._classes.size  > 0;
+		}
+
 		return this._attrs.has( key );
 	}
 
@@ -190,7 +211,13 @@ export default class Element extends Node {
 	setAttribute( key, value ) {
 		this._fireChange( 'ATTRIBUTES', this );
 
-		this._attrs.set( key, value );
+		if ( key == 'class' ) {
+			const classArray = value.split( /\s+/ );
+			this._classes.clear();
+			this.addClass( ...classArray );
+		} else {
+			this._attrs.set( key, value );
+		}
 	}
 
 	/**

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

@@ -260,6 +260,33 @@ describe( 'Element', () => {
 			el = new ViewElement( 'p' );
 		} );
 
+		describe( 'setAttribute', () => {
+			it( 'should set attribute', () => {
+				el.setAttribute( 'foo', 'bar' );
+
+				expect( el._attrs.has( 'foo' ) ).to.be.true;
+				expect( el._attrs.get( 'foo' ) ).to.equal( 'bar' );
+			} );
+
+			it( 'should set class', () => {
+				el.setAttribute( 'class', 'foo bar' );
+
+				expect( el._attrs.has( 'class' ) ).to.be.false;
+				expect( el._classes.has( 'foo' )  ).to.be.true;
+				expect( el._classes.has( 'bar' )  ).to.be.true;
+			} );
+
+			it( 'should replace all existing classes', () => {
+				el.setAttribute( 'class', 'foo bar baz' );
+				el.setAttribute( 'class', 'qux' );
+
+				expect( el._classes.has( 'foo' )  ).to.be.false;
+				expect( el._classes.has( 'bar' )  ).to.be.false;
+				expect( el._classes.has( 'baz' )  ).to.be.false;
+				expect( el._classes.has( 'qux' )  ).to.be.true;
+			} );
+		} );
+
 		describe( 'getAttribute', () => {
 			it( 'should return attribute', () => {
 				el.setAttribute( 'foo', 'bar' );
@@ -276,6 +303,12 @@ describe( 'Element', () => {
 				expect( el.hasAttribute( 'foo' ) ).to.be.true;
 				expect( el.hasAttribute( 'bom' ) ).to.be.false;
 			} );
+
+			it( 'should return true if element has class attribute', () => {
+				expect( el.hasAttribute( 'class' ) ).to.be.false;
+				el.addClass( 'foo' );
+				expect( el.hasAttribute( 'class' ) ).to.be.true;
+			} );
 		} );
 
 		describe( 'getAttributeKeys', () => {
@@ -293,6 +326,18 @@ describe( 'Element', () => {
 
 				expect( i ).to.equal( 2 );
 			} );
+
+			it( 'should return class key', () => {
+				el.addClass( 'foo' );
+				el.setAttribute( 'bar', true );
+				const expected = [ 'class', 'bar' ];
+				let i = 0;
+
+				for ( let key of el.getAttributeKeys() ) {
+					expect( key ).to.equal( expected[ i ] );
+					i++;
+				}
+			} );
 		} );
 
 		describe( 'removeAttribute', () => {