8
0
Просмотр исходного кода

Added viewElement.getIdentity().

Szymon Kupś 8 лет назад
Родитель
Сommit
45f6bc5c75

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

@@ -673,6 +673,13 @@ export default class Element extends Node {
 		yield* this._customProperties.entries();
 	}
 
+	getIdentity() {
+		const attributes = Array.from( this._attrs ).map( i => i[ 0 ] + '=' + i[ 1 ] ).sort();
+		const styles = Array.from( this._styles ).map( i => i[ 0 ] + '=' + i[ 1 ] ).sort();
+
+		return [ this.name, ...attributes, ...styles ].join( '|' );
+	}
+
 	/**
 	 * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
 	 *

+ 13 - 1
packages/ckeditor5-engine/src/view/writer.js

@@ -933,7 +933,7 @@ function wrapChildren( parent, startOffset, endOffset, attribute ) {
 		const isUI = child.is( 'uiElement' );
 
 		// Wrap text, empty elements, ui elements or attributes with higher or equal priority.
-		if ( isText || isEmpty || isUI || ( isAttribute && attribute.priority <= child.priority ) ) {
+		if ( isText || isEmpty || isUI || ( isAttribute && shouldABeOutsideB( attribute, child ) ) ) {
 			// Clone attribute.
 			const newAttribute = attribute.clone();
 
@@ -975,6 +975,18 @@ function wrapChildren( parent, startOffset, endOffset, attribute ) {
 	return Range.createFromParentsAndOffsets( parent, startOffset, parent, endOffset );
 }
 
+function shouldABeOutsideB( a, b ) {
+	if ( a.priority < b.priority ) {
+		return true;
+	} else if ( a.priority > b.priority ) {
+		return false;
+	}
+
+	// When priorities are equal.
+	console.log( a.getIdentity() );
+	return a.getIdentity() < b.getIdentity();
+}
+
 // Returns new position that is moved to near text node. Returns same position if there is no text node before of after
 // specified position.
 //

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

@@ -981,4 +981,30 @@ describe( 'Element', () => {
 			expect( properties[ 2 ][ 1 ] ).to.equal( 3 );
 		} );
 	} );
+
+	describe( 'getIdentity()', () => {
+		it( 'should return only name if no other attributes are present', () => {
+			const el = new Element( 'foo' );
+
+			expect( el.getIdentity() ).to.equal( 'foo' );
+		} );
+
+		it( 'should return attributes in sorted order', () => {
+			const el = new Element( 'foo', {
+				a: 1,
+				d: 4,
+				b: 3
+			} );
+
+			expect( el.getIdentity() ).to.equal( 'foo|a=1|b=3|d=4' );
+		} );
+
+		it( 'should return styles in sorted order', () => {
+			const el = new Element( 'foo', {
+				style: 'border: 1px solid red; background-color: red'
+			} );
+
+			expect( el.getIdentity() ).to.equal( 'foo|background-color=red|border=1px solid red' );
+		} );
+	} );
 } );