Преглед на файлове

Merge pull request #109 from ckeditor/t/84b

T/84b AttributeDelta new tests and bugfix.
Piotr Jasiun преди 10 години
родител
ревизия
edd9f927d4

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/attributedelta.js

@@ -139,7 +139,7 @@ CKEDITOR.define( [
 
 		// Because position in the loop is not the iterator position (see let position comment), the last position in
 		// the while loop will be last but one position in the range. We need to check the last position manually.
-		if ( position != lastSplitPosition && valueBefore != value ) {
+		if ( position instanceof Position && position != lastSplitPosition && valueBefore != value ) {
 			addOperation();
 		}
 

+ 45 - 3
packages/ckeditor5-engine/tests/treemodel/delta/attributedelta.js

@@ -140,9 +140,9 @@ describe( 'Batch', () => {
 
 		function getRange( startIndex, endIndex ) {
 			return new Range(
-					Position.createFromParentAndOffset( root, startIndex ),
-					Position.createFromParentAndOffset( root, endIndex )
-				);
+				Position.createFromParentAndOffset( root, startIndex ),
+				Position.createFromParentAndOffset( root, endIndex )
+			);
 		}
 
 		function getChangesAttrsCount() {
@@ -218,6 +218,35 @@ describe( 'Batch', () => {
 				expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
 			} );
 
+			it( 'should not change elements attribute if range contains closing tag', () => {
+				let range = new Range(
+					new Position( root, [ 18, 1 ] ),
+					new Position( root, [ 21 ] )
+				);
+
+				batch.setAttr( 'a', 1, range );
+				expect( getOperationsCount() ).to.equal( 1 );
+				expect( getChangesAttrsCount() ).to.equal( 4 );
+				expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
+			} );
+
+			it( 'should not create an operation if the range contains only closing tag', () => {
+				let range = new Range(
+					new Position( root, [ 18, 3 ] ),
+					new Position( root, [ 19 ] )
+				);
+
+				batch.setAttr( 'a', 3, range );
+				expect( getOperationsCount() ).to.equal( 0 );
+				expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
+			} );
+
+			it( 'should not create an operation if is collapsed', () => {
+				batch.setAttr( 'a', 1, getRange( 3, 3 ) );
+				expect( getOperationsCount() ).to.equal( 0 );
+				expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
+			} );
+
 			it( 'should create a proper operations for the mixed range', () => {
 				batch.setAttr( 'a', 1, getRange( 0, 20 ) );
 				expect( getOperationsCount() ).to.equal( 5 );
@@ -285,6 +314,19 @@ describe( 'Batch', () => {
 				expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
 			} );
 
+			it( 'should not apply operation twice in the range contains opening and closing tags', () => {
+				batch.removeAttr( 'a', getRange( 18, 22 ) );
+				expect( getOperationsCount() ).to.equal( 1 );
+				expect( getChangesAttrsCount() ).to.equal( 1 );
+				expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
+			} );
+
+			it( 'should not create an operation if range is collapsed', () => {
+				batch.removeAttr( 'a', getRange( 3, 3 ) );
+				expect( getOperationsCount() ).to.equal( 0 );
+				expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
+			} );
+
 			it( 'should create a proper operations for the mixed range', () => {
 				batch.removeAttr( 'a', getRange( 3, 15 ) );
 				expect( getOperationsCount() ).to.equal( 2 );