Преглед изворни кода

Added styles parsing to treeView.Element constructor.

Szymon Kupś пре 9 година
родитељ
комит
9a8bfc4ad2

+ 23 - 0
packages/ckeditor5-engine/src/treeview/element.js

@@ -79,6 +79,26 @@ export default class Element extends Node {
 		} else {
 			this._classes = new Set();
 		}
+
+		/**
+		 * Map of styles.
+		 *
+		 * @protected
+		 * @member {Set} core.treeView.Element#_styles
+		 */
+		this._styles = new Map();
+
+		if ( this._attrs.has( 'style' ) ) {
+			// TODO: Maybe parsing style string should be moved to utils?
+			const styleString = this._attrs.get( 'style' );
+			const regex = /\s*([^:;\s]+)\s*:\s*([^;]+)\s*(?=;|$)/g;
+			let matchStyle;
+
+			while ( ( matchStyle = regex.exec( styleString ) ) !== null ) {
+				this._styles.set( matchStyle[ 1 ], matchStyle[ 2 ].trim() );
+			}
+			this._attrs.delete( 'style' );
+		}
 	}
 
 	/**
@@ -98,6 +118,9 @@ export default class Element extends Node {
 		}
 
 		const cloned = new Element( this.name, this._attrs, childrenClone );
+
+		// Classes are cloned separately - it solution is faster than adding them back to attributes and
+		// parse once again in constructor.
 		cloned._classes = new Set( this._classes );
 
 		return cloned;

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

@@ -59,6 +59,19 @@ describe( 'Element', () => {
 			expect( el._classes.has( 'two' ) ).to.be.true;
 			expect( el._classes.has( 'three' ) ).to.be.true;
 		} );
+
+		it( 'should move style attribute to style map', () => {
+			const el = new ViewElement( 'p', { 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)' );
+		} );
 	} );
 
 	describe( 'clone', () => {