Kaynağa Gözat

Tests: Add tests for Element.fromViewDefinition() method.

Maciej Gołaszewski 8 yıl önce
ebeveyn
işleme
e264c8944b

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

@@ -736,7 +736,9 @@ export default class Element extends Node {
 		const attributesObject = viewElementDefinition.attributes;
 
 		if ( attributesObject ) {
-			attributes.attribute = attributesObject;
+			for ( const key in attributesObject ) {
+				attributes[ key ] = attributesObject[ key ];
+			}
 		}
 
 		return new this( viewElementDefinition.name, attributes );

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

@@ -1028,4 +1028,58 @@ describe( 'Element', () => {
 			);
 		} );
 	} );
+
+	describe( 'fromViewDefinition()', () => {
+		it( 'should create element from definition without any attributes', () => {
+			const el = Element.fromViewDefinition( { name: 'p' } );
+
+			expect( el ).to.be.an.instanceof( Node );
+			expect( el ).to.have.property( 'name' ).that.equals( 'p' );
+			expect( el ).to.have.property( 'parent' ).that.is.null;
+			expect( count( el.getAttributeKeys() ) ).to.equal( 0 );
+		} );
+
+		it( 'should create element from definition with attributes as plain object', () => {
+			const el = Element.fromViewDefinition( { name: 'p', attributes: { foo: 'bar' } } );
+
+			expect( el ).to.have.property( 'name' ).that.equals( 'p' );
+			expect( count( el.getAttributeKeys() ) ).to.equal( 1 );
+			expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
+		} );
+
+		it( 'should create element from definition with classes as single string', () => {
+			const el = Element.fromViewDefinition( { name: 'p', attributes: { id: 'test' }, classes: 'foo-bar' } );
+
+			expect( el._attrs.has( 'class' ) ).to.be.false;
+			expect( el._attrs.has( 'id' ) ).to.be.true;
+			expect( el._classes.has( 'foo-bar' ) ).to.be.true;
+		} );
+
+		it( 'should create element from definition with classes set as array', () => {
+			const el = Element.fromViewDefinition( { name: 'p', attributes: { id: 'test' }, classes: [ 'one', 'two', 'three' ] } );
+
+			expect( el._attrs.has( 'class' ) ).to.be.false;
+			expect( el._attrs.has( 'id' ) ).to.be.true;
+			expect( el._classes.has( 'one' ) ).to.be.true;
+			expect( el._classes.has( 'two' ) ).to.be.true;
+			expect( el._classes.has( 'three' ) ).to.be.true;
+		} );
+
+		it( 'should create element from definition with styles object', () => {
+			const el = Element.fromViewDefinition( {
+				name: 'p',
+				attributes: { id: 'test' },
+				styles: { one: 'style1', two: 'style2', three: 'url(http://ckeditor.com)' }
+			} );
+
+			expect( el._attrs.has( 'style' ) ).to.be.false;
+			expect( el._attrs.has( 'id' ) ).to.be.true;
+			expect( el._styles.has( 'one' ) ).to.be.true;
+			expect( el._styles.get( 'one' ) ).to.equal( 'style1' );
+			expect( el._styles.has( 'two' ) ).to.be.true;
+			expect( el._styles.get( 'two' ) ).to.equal( 'style2' );
+			expect( el._styles.has( 'three' ) ).to.be.true;
+			expect( el._styles.get( 'three' ) ).to.equal( 'url(http://ckeditor.com)' );
+		} );
+	} );
 } );