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

AttributeElement clone priority.

Piotr Jasiun 9 лет назад
Родитель
Сommit
a87a50051d

+ 16 - 0
packages/ckeditor5-engine/src/treeview/attributeelement.js

@@ -43,4 +43,20 @@ export default class AttributeElement extends Element {
 		 */
 		this.priority = DEFAULT_PRIORITY;
 	}
+
+	/**
+	 * Clones provided element with priority.
+	 *
+	 * @param {Boolean} deep If set to `true` clones element and all its children recursively. When set to `false`,
+	 * element will be cloned without any children.
+	 * @returns {Element} Clone of this element.
+	 */
+	clone( deep ) {
+		const cloned = super.clone( deep );
+
+		// Clone priority too.
+		cloned.priority	= this.priority;
+
+		return cloned;
+	}
 }

+ 6 - 13
packages/ckeditor5-engine/src/treeview/writer.js

@@ -109,9 +109,6 @@ import DocumentFragment from './documentfragment.js';
 				// Break element.
 				const clonedNode = positionParent.clone();
 
-				// Clone priority.
-				clonedNode.priority = positionParent.priority;
-
 				// Insert cloned node to position's parent node.
 				positionParent.parent.insertChildren( offsetAfter, clonedNode );
 
@@ -211,21 +208,19 @@ import DocumentFragment from './documentfragment.js';
 			return position;
 		}
 
+		// When selection is between two text nodes.
 		if ( nodeBefore instanceof Text && nodeAfter instanceof Text ) {
-			// When selection is between two text nodes.
 			// Merge text data into first text node and remove second one.
 			const nodeBeforeLength = nodeBefore.data.length;
 			nodeBefore.data += nodeAfter.data;
 			positionParent.removeChildren( positionOffset );
 
 			return new Position( nodeBefore, nodeBeforeLength );
-		} else if ( nodeBefore.isSimilar( nodeAfter ) ) {
-			// When selection is between same nodes.
-			const nodeBeforePriority = nodeBefore.priority;
-			const nodeAfterPriority = nodeAfter.priority;
-
+		}
+		// When selection is between same nodes.
+		else if ( nodeBefore.isSimilar( nodeAfter ) ) {
 			// Do not merge same nodes with different priorities.
-			if ( nodeBeforePriority === undefined || nodeBeforePriority !== nodeAfterPriority ) {
+			if ( !( nodeBefore instanceof AttributeElement ) || nodeBefore.priority !== nodeAfter.priority ) {
 				return Position.createFromPosition( position );
 			}
 
@@ -536,13 +531,11 @@ function wrapChildren( writer, parent, startOffset, endOffset, attribute ) {
 		const child = parent.getChild( i );
 		const isText = child instanceof Text;
 		const isAttribute = child instanceof AttributeElement;
-		const priority = attribute.priority;
 
 		// Wrap text or attributes with higher or equal priority.
-		if ( isText || ( isAttribute && priority <= child.priority ) ) {
+		if ( isText || ( isAttribute && attribute.priority <= child.priority ) ) {
 			// Clone attribute.
 			const newAttribute = attribute.clone();
-			newAttribute.priority = priority;
 
 			// Wrap current node with new attribute;
 			child.remove();

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

@@ -22,4 +22,17 @@ describe( 'AttributeElement', () => {
 			expect( el ).to.have.property( 'priority' ).that.equals( DEFAULT_PRIORITY );
 		} );
 	} );
+
+	describe( 'clone', () => {
+		it( 'should clone element with priority', () => {
+			const el = new AttributeElement( 'b' );
+			el.priority = 7;
+
+			const clone = el.clone();
+
+			expect( clone ).to.not.equal( el );
+			expect( clone.name ).to.equal( el.name );
+			expect( clone.priority ).to.equal( el.priority );
+		} );
+	} );
 } );