浏览代码

Refactor: Rename `view.Element.fromViewDefinition()` to `view.Element.createFromDefinition()` and use methods for adding styles & classes.

Maciej Gołaszewski 8 年之前
父节点
当前提交
d65b8a99df

+ 2 - 2
packages/ckeditor5-engine/src/conversion/definition-based-converters.js

@@ -50,7 +50,7 @@ export function modelElementToViewContainerElement( definition, dispatchers ) {
 	buildModelConverter()
 		.for( ...dispatchers )
 		.fromElement( modelElement )
-		.toElement( () => ViewContainerElement.fromViewDefinition( viewDefinition ) );
+		.toElement( () => ViewContainerElement.createFromDefinition( viewDefinition ) );
 }
 
 /**
@@ -155,7 +155,7 @@ export function modelAttributeToViewAttributeElement( attributeName, definition,
 				return;
 			}
 
-			return AttributeElement.fromViewDefinition( viewDefinition );
+			return AttributeElement.createFromDefinition( viewDefinition );
 		} );
 }
 

+ 9 - 29
packages/ckeditor5-engine/src/view/element.js

@@ -717,42 +717,22 @@ export default class Element extends Node {
 	 * Creates element instance from provided viewElementDefinition.
 	 *
 	 * @param {module:engine/view/viewelementdefinition~ViewElementDefinition} viewElementDefinition
-	 * @returns {Element}
+	 * @returns {module:engine/view/element~Element}
 	 */
-	static fromViewDefinition( viewElementDefinition ) {
-		const attributes = {};
+	static createFromDefinition( viewElementDefinition ) {
+		const element = new this( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attributes ) );
 
-		const classes = viewElementDefinition.classes;
-
-		if ( classes ) {
-			attributes.class = Array.isArray( classes ) ? classes.join( ' ' ) : classes;
+		if ( viewElementDefinition.styles ) {
+			element.setStyle( viewElementDefinition.styles );
 		}
 
-		const stylesObject = viewElementDefinition.styles;
-
-		if ( stylesObject ) {
-			attributes.style = toStylesString( stylesObject );
-		}
-
-		const attributesObject = viewElementDefinition.attributes;
+		const classes = viewElementDefinition.classes;
 
-		if ( attributesObject ) {
-			for ( const key in attributesObject ) {
-				attributes[ key ] = attributesObject[ key ];
-			}
+		if ( classes ) {
+			element.addClass( ... typeof classes === 'string' ? [ classes ] : classes );
 		}
 
-		return new this( viewElementDefinition.name, attributes );
-
-		function toStylesString( stylesObject ) {
-			const styles = [];
-
-			for ( const key in stylesObject ) {
-				styles.push( key + ':' + stylesObject[ key ] );
-			}
-
-			return styles.join( ';' );
-		}
+		return element;
 	}
 
 	/**

+ 5 - 5
packages/ckeditor5-engine/tests/view/element.js

@@ -1045,7 +1045,7 @@ describe( 'Element', () => {
 
 	describe( 'fromViewDefinition()', () => {
 		it( 'should create element from definition without any attributes', () => {
-			const el = Element.fromViewDefinition( { name: 'p' } );
+			const el = Element.createFromDefinition( { name: 'p' } );
 
 			expect( el ).to.be.an.instanceof( Node );
 			expect( el ).to.have.property( 'name' ).that.equals( 'p' );
@@ -1054,7 +1054,7 @@ describe( 'Element', () => {
 		} );
 
 		it( 'should create element from definition with attributes as plain object', () => {
-			const el = Element.fromViewDefinition( { name: 'p', attributes: { foo: 'bar' } } );
+			const el = Element.createFromDefinition( { name: 'p', attributes: { foo: 'bar' } } );
 
 			expect( el ).to.have.property( 'name' ).that.equals( 'p' );
 			expect( count( el.getAttributeKeys() ) ).to.equal( 1 );
@@ -1062,7 +1062,7 @@ describe( 'Element', () => {
 		} );
 
 		it( 'should create element from definition with classes as single string', () => {
-			const el = Element.fromViewDefinition( { name: 'p', attributes: { id: 'test' }, classes: 'foo-bar' } );
+			const el = Element.createFromDefinition( { name: 'p', attributes: { id: 'test' }, classes: 'foo-bar' } );
 
 			expect( el._attrs.has( 'class' ) ).to.be.false;
 			expect( el._attrs.has( 'id' ) ).to.be.true;
@@ -1070,7 +1070,7 @@ describe( 'Element', () => {
 		} );
 
 		it( 'should create element from definition with classes set as array', () => {
-			const el = Element.fromViewDefinition( { name: 'p', attributes: { id: 'test' }, classes: [ 'one', 'two', 'three' ] } );
+			const el = Element.createFromDefinition( { 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;
@@ -1080,7 +1080,7 @@ describe( 'Element', () => {
 		} );
 
 		it( 'should create element from definition with styles object', () => {
-			const el = Element.fromViewDefinition( {
+			const el = Element.createFromDefinition( {
 				name: 'p',
 				attributes: { id: 'test' },
 				styles: { one: 'style1', two: 'style2', three: 'url(http://ckeditor.com)' }