Sfoglia il codice sorgente

Changed treeView.Writer.mergeAttributes method to remove empty attributes.

Szymon Kupś 9 anni fa
parent
commit
9178211065

+ 14 - 0
packages/ckeditor5-engine/src/treeview/writer.js

@@ -204,6 +204,11 @@ import isIterable from '../../utils/isiterable.js';
 	 *		<p><b>foo</b>[]<b>bar</b> -> <p><b>foo{}bar</b></b>
 	 *		<p><b foo="bar">a</b>[]<b foo="baz">b</b> -> <p><b foo="bar">a</b>[]<b foo="baz">b</b>
 	 *
+	 * It will also take care about empty attributes when merging:
+	 *
+	 *		<p><b>[]</b></p> -> <p>[]</p>
+	 *		<p><b>foo</b><i>[]</i><b>bar</b></p> -> <p><b>foo{}bar</b></p>
+	 *
 	 * @see engine.treeView.AttributeElement
 	 * @see engine.treeView.ContainerElement
 	 * @param {engine.treeView.Position} position Merge position.
@@ -218,6 +223,15 @@ import isIterable from '../../utils/isiterable.js';
 			return position;
 		}
 
+		// When inside empty attribute - remove it.
+		if ( positionParent instanceof AttributeElement && positionParent.getChildCount() === 0 ) {
+			const parent = positionParent.parent;
+			const offset = positionParent.getIndex();
+			positionParent.remove();
+
+			return this.mergeAttributes( new Position( parent, offset ) );
+		}
+
 		const nodeBefore = positionParent.getChild( positionOffset - 1 );
 		const nodeAfter = positionParent.getChild( positionOffset );
 

+ 23 - 2
packages/ckeditor5-engine/tests/treeview/writer/mergeattributes.js

@@ -79,8 +79,8 @@ describe( 'Writer', () => {
 
 		it( 'should merge when placed between similar attribute nodes', () => {
 			test(
-				'<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:1 foo="bar"></attribute:b:1></container:p>',
-				'<container:p><attribute:b:1 foo="bar">[]</attribute:b:1></container:p>'
+				'<container:p><attribute:b:1 foo="bar">baz</attribute:b:1>[]<attribute:b:1 foo="bar">qux</attribute:b:1></container:p>',
+				'<container:p><attribute:b:1 foo="bar">baz{}qux</attribute:b:1></container:p>'
 			);
 		} );
 
@@ -104,5 +104,26 @@ describe( 'Writer', () => {
 				'<container:p><attribute:b:1 foo="bar">foo{}bar</attribute:b:1></container:p>'
 			);
 		} );
+
+		it( 'should remove empty attributes after merge #1', () => {
+			test(
+				'<container:p><attribute:b>[]</attribute:b></container:p>',
+				'<container:p>[]</container:p>'
+			);
+		} );
+
+		it( 'should remove empty attributes after merge #2', () => {
+			test(
+				'<container:p><attribute:b>foo</attribute:b><attribute:i>[]</attribute:i><attribute:b>bar</attribute:b></container:p>',
+				'<container:p><attribute:b:10>foo{}bar</attribute:b:10></container:p>'
+			);
+		} );
+
+		it( 'should remove empty attributes after merge #3', () => {
+			test(
+				'<container:p><attribute:b></attribute:b><attribute:i>[]</attribute:i><attribute:b></attribute:b></container:p>',
+				'<container:p>[]</container:p>'
+			);
+		} );
 	} );
 } );