8
0
Эх сурвалжийг харах

Changed: AttributeDelta#range looks through all ranges to find first and last changed position.

Szymon Cofalik 9 жил өмнө
parent
commit
eb2780e882

+ 14 - 5
packages/ckeditor5-engine/src/model/delta/attributedelta.js

@@ -54,12 +54,21 @@ export default class AttributeDelta extends Delta {
 			return this._range;
 			return this._range;
 		}
 		}
 
 
-		// If it is not cached we will evaluate it and cache it.
-		let firstOperation = this.operations[ 0 ];
-		let lastOperation = this.operations[ this.operations.length - 1 ];
+		let start = null;
+		let end = null;
 
 
-		if ( firstOperation ) {
-			this._range = new Range( firstOperation.range.start, lastOperation.range.end );
+		for ( let operation of this.operations ) {
+			if ( start === null || start.isAfter( operation.range.start ) ) {
+				start = operation.range.start;
+			}
+
+			if ( end === null || end.isBefore( operation.range.end ) ) {
+				end = operation.range.end;
+			}
+		}
+
+		if ( start && end ) {
+			this._range = new Range( start, end );
 
 
 			return this._range;
 			return this._range;
 		}
 		}

+ 3 - 6
packages/ckeditor5-engine/tests/model/delta/attributedelta.js

@@ -440,12 +440,9 @@ describe( 'AttributeDelta', () => {
 			expect( delta.range ).to.be.null;
 			expect( delta.range ).to.be.null;
 		} );
 		} );
 
 
-		it( 'should be equal to the range on which delta operates', () => {
-			// Delta operates on range [ 1 ] to [ 6 ] but omits [ 4 ] - [ 5 ] for "a reason".
-			// Still the range should be from [ 1 ] to [ 6 ]. Delta may not apply anything on [ 4 ] - [ 5 ]
-			// because it already has proper attribute.
-			let rangeA = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
-			let rangeB = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
+		it( 'start and end should be equal to first and last changed position', () => {
+			let rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
+			let rangeB = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
 			let rangeC = new Range( new Position( root, [ 5 ] ), new Position( root, [ 6 ] ) );
 			let rangeC = new Range( new Position( root, [ 5 ] ), new Position( root, [ 6 ] ) );
 
 
 			delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
 			delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );