Sfoglia il codice sorgente

Other: Move static method `createFromDefinition` from `view:Element` to definition-based-converters as a private method.

Maciej Gołaszewski 8 anni fa
parent
commit
5f0788569c

+ 23 - 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.createFromDefinition( targetView ) );
+		.toElement( () => createViewElementFromDefinition( targetView, ViewContainerElement ) );
 }
 
 /**
@@ -155,7 +155,7 @@ export function modelAttributeToViewAttributeElement( attributeName, definition,
 				return;
 			}
 
-			return AttributeElement.createFromDefinition( targetView );
+			return createViewElementFromDefinition( targetView, AttributeElement );
 		} );
 }
 
@@ -269,6 +269,27 @@ function prepareViewConverter( dispatchers, viewDefinitions ) {
 	return converter;
 }
 
+// Creates view element instance from provided viewElementDefinition and class.
+//
+// @param {module:engine/view/viewelementdefinition~ViewElementDefinition} viewElementDefinition
+// @param {Function} ViewElementClass
+// @returns {module:engine/view/element~Element}
+function createViewElementFromDefinition( viewElementDefinition, ViewElementClass ) {
+	const element = new ViewElementClass( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attribute ) );
+
+	if ( viewElementDefinition.style ) {
+		element.setStyle( viewElementDefinition.style );
+	}
+
+	const classes = viewElementDefinition.class;
+
+	if ( classes ) {
+		element.addClass( ... typeof classes === 'string' ? [ classes ] : classes );
+	}
+
+	return element;
+}
+
 /**
  * Defines conversion details. It is used in configuration based converters:
  * - {@link module:engine/conversion/definition-based-converters~modelAttributeToViewAttributeElement}

+ 0 - 22
packages/ckeditor5-engine/src/view/element.js

@@ -713,28 +713,6 @@ export default class Element extends Node {
 			( attributes == '' ? '' : ` ${ attributes }` );
 	}
 
-	/**
-	 * Creates element instance from provided viewElementDefinition.
-	 *
-	 * @param {module:engine/view/viewelementdefinition~ViewElementDefinition} viewElementDefinition
-	 * @returns {module:engine/view/element~Element}
-	 */
-	static createFromDefinition( viewElementDefinition ) {
-		const element = new this( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attribute ) );
-
-		if ( viewElementDefinition.style ) {
-			element.setStyle( viewElementDefinition.style );
-		}
-
-		const classes = viewElementDefinition.class;
-
-		if ( classes ) {
-			element.addClass( ... typeof classes === 'string' ? [ classes ] : classes );
-		}
-
-		return element;
-	}
-
 	/**
 	 * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
 	 *

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

@@ -1042,58 +1042,4 @@ describe( 'Element', () => {
 			);
 		} );
 	} );
-
-	describe( 'createFromDefinition()', () => {
-		it( 'should create element from definition without any attributes', () => {
-			const el = Element.createFromDefinition( { 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.createFromDefinition( { name: 'p', attribute: { 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.createFromDefinition( { name: 'p', attribute: { id: 'test' }, class: '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.createFromDefinition( { name: 'p', attribute: { id: 'test' }, class: [ '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 style object', () => {
-			const el = Element.createFromDefinition( {
-				name: 'p',
-				attribute: { id: 'test' },
-				style: { 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)' );
-		} );
-	} );
 } );