Browse Source

Added custom properties to view.Element.

Szymon Kupś 9 years ago
parent
commit
ae5c0ebd1c

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

@@ -645,7 +645,7 @@ export default class Element extends Node {
 	 *
 	 * @returns {Iterable.<*>}
 	 */
-	*getCustomProperties( ) {
+	*getCustomProperties() {
 		yield* this._customProperties.entries();
 	}
 }

+ 61 - 0
packages/ckeditor5-engine/tests/view/element.js

@@ -157,6 +157,18 @@ describe( 'Element', () => {
 			expect( clone._styles.has( 'font-size' ) ).to.be.true;
 			expect( clone._styles.get( 'font-size' ) ).to.equal( '12px' );
 		} );
+
+		it( 'should clone custom properties', () => {
+			const el = new Element( 'p' );
+			const symbol = Symbol( 'custom' );
+			el.setCustomProperty( 'foo', 'bar' );
+			el.setCustomProperty( symbol, 'baz' );
+
+			const cloned = el.clone();
+
+			expect( cloned.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
+			expect( cloned.getCustomProperty( symbol ) ).to.equal( 'baz' );
+		} );
 	} );
 
 	describe( 'isSimilar', () => {
@@ -817,4 +829,53 @@ describe( 'Element', () => {
 			} ) ).to.be.null;
 		} );
 	} );
+
+	describe( 'custom properties', () => {
+		it( 'should allow to set and get custom properties', () => {
+			const el = new Element( 'p' );
+			el.setCustomProperty( 'foo', 'bar' );
+
+			expect( el.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
+		} );
+
+		it( 'should allow to add symbol property', () => {
+			const el = new Element( 'p' );
+			const symbol = Symbol( 'custom' );
+			el.setCustomProperty( symbol, 'bar' );
+
+			expect( el.getCustomProperty( symbol ) ).to.equal( 'bar' );
+		} );
+
+		it( 'should allow to remove custom property', () => {
+			const el = new Element( 'foo' );
+			const symbol = Symbol( 'quix' );
+			el.setCustomProperty( 'bar', 'baz' );
+			el.setCustomProperty( symbol, 'test' );
+
+			expect( el.getCustomProperty( 'bar' ) ).to.equal( 'baz' );
+			expect( el.getCustomProperty( symbol ) ).to.equal( 'test' );
+
+			el.removeCustomProperty( 'bar' );
+			el.removeCustomProperty( symbol );
+
+			expect( el.getCustomProperty( 'bar' ) ).to.be.undefined;
+			expect( el.getCustomProperty( symbol ) ).to.be.undefined;
+		} );
+
+		it( 'should allow to iterate over custom properties', () => {
+			const el = new Element( 'p' );
+			el.setCustomProperty( 'foo', 1 );
+			el.setCustomProperty( 'bar', 2 );
+			el.setCustomProperty( 'baz', 3 );
+
+			const properties = [ ...el.getCustomProperties() ];
+
+			expect( properties[0][0] ).to.equal( 'foo' );
+			expect( properties[0][1] ).to.equal( 1 );
+			expect( properties[1][0] ).to.equal( 'bar' );
+			expect( properties[1][1] ).to.equal( 2 );
+			expect( properties[2][0] ).to.equal( 'baz' );
+			expect( properties[2][1] ).to.equal( 3 );
+		} );
+	} );
 } );