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

Attribute elements have to have the same priority to be similar.

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

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

@@ -16,9 +16,7 @@ export const DEFAULT_PRIORITY = 10;
 
 /**
  * Attributes are elements which define document presentation. They are mostly elements like `<b>` or `<span>`.
- * Attributes can be broken and merged by the {@link engine.treeView.Writer}. Merging requires that attribute nodes are
- * {@link engine.treeView.Element#isSimilar similar} and have same priority. Setting different priorities on similar
- * nodes may prevent merging, eg. two `<abbr>` nodes next each other shouldn't be merged.
+ * Attributes can be broken and merged by the {@link engine.treeView.Writer}.
  *
  * Editing engine does not define fixed HTML DTD. This is why the type of the {@link engine.treeView.Element} need to
  * be defined by the feature developer. Creating an element you should use {@link engine.treeView.ContainerElement}
@@ -37,7 +35,9 @@ export default class AttributeElement extends Element {
 		super( name, attrs, children );
 
 		/**
-		 * Element priority.
+		 * Element priority. Attributes have to have the same priority to be
+		 * {@link engine.treeView.Element#isSimilar similar}. Setting different priorities on similar
+ 		 * nodes may prevent merging, eg. two `<abbr>` nodes next each other shouldn't be merged.
 		 *
 		 * @member {Number} engine.treeView.AttributeElement#priority
 		 */
@@ -59,4 +59,16 @@ export default class AttributeElement extends Element {
 
 		return cloned;
 	}
+
+	/**
+	 * Checks if this element is similar to other element.
+	 * Both elements should have the same name, attributes and priority to be considered as similar.
+	 * Two similar elements can contain different set of children nodes.
+	 *
+	 * @param {Element} otherElement
+	 * @returns {Boolean}
+	 */
+	isSimilar( otherElement ) {
+		return super.isSimilar( otherElement ) && this.priority == otherElement.priority;
+	}
 }

+ 3 - 8
packages/ckeditor5-engine/src/treeview/writer.js

@@ -173,7 +173,7 @@ import DocumentFragment from './documentfragment.js';
 
 	/**
 	 * Merges attribute nodes. It also merges text nodes if needed.
-	 * Only {@link engine.treeView.Element#isSimilar similar} `attribute` nodes, with same priority can be merged.
+	 * Only {@link engine.treeView.AttributeElement#isSimilar similar} `attribute` nodes can be merged.
 	 *
 	 * In following examples `<p>` is a container and `<b>` is an attribute node:
 	 *
@@ -219,11 +219,6 @@ import DocumentFragment from './documentfragment.js';
 		}
 		// When selection is between same nodes.
 		else if ( nodeBefore.isSimilar( nodeAfter ) ) {
-			// Do not merge same nodes with different priorities.
-			if ( !( nodeBefore instanceof AttributeElement ) || nodeBefore.priority !== nodeAfter.priority ) {
-				return Position.createFromPosition( position );
-			}
-
 			// Move all children nodes from node placed after selection and remove that node.
 			const count = nodeBefore.getChildCount();
 			nodeBefore.appendChildren( nodeAfter.getChildren() );
@@ -458,8 +453,8 @@ function unwrapChildren( writer, parent, startOffset, endOffset, attribute ) {
 	while ( i < endOffset ) {
 		const child = parent.getChild( i );
 
-		// If attributes are the similar and have same priority, then unwrap.
-		if (  child.isSimilar( attribute ) && child.priority == attribute.priority ) {
+		// If attributes are the similar, then unwrap.
+		if (  child.isSimilar( attribute ) ) {
 			const unwrapped = child.getChildren();
 			const count = child.getChildCount();
 

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

@@ -35,4 +35,25 @@ describe( 'AttributeElement', () => {
 			expect( clone.priority ).to.equal( el.priority );
 		} );
 	} );
+
+	describe( 'isSimilar', () => {
+		it( 'should return true if priorities are the same', () => {
+			const b1 = new AttributeElement( 'b' );
+			b1.priority = 7;
+
+			const b2 = new AttributeElement( 'b' );
+			b2.priority = 7;
+
+			expect( b1.isSimilar( b2 ) ).to.be.true;
+		} );
+
+		it( 'should return false if priorities are different', () => {
+			const b1 = new AttributeElement( 'b' );
+			b1.priority = 7;
+
+			const b2 = new AttributeElement( 'b' ); // default priority
+
+			expect( b1.isSimilar( b2 ) ).to.be.false;
+		} );
+	} );
 } );