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

Fix issues with consuming delete updates beyond the end of the diff

James Allen 12 лет назад
Родитель
Сommit
22a806a200

+ 7 - 6
services/track-changes/app/coffee/DiffGenerator.coffee

@@ -94,19 +94,20 @@ module.exports = DiffGenerator =
 					consumedDiff.push DiffGenerator._slicePart part, 0, partOffset
 				if partOffset < length
 					remainingDiff.unshift DiffGenerator._slicePart part, partOffset
-				return {
-					consumedDiff: consumedDiff
-					remainingDiff: remainingDiff
-				}
+				break
 			else
 				position += length
 				consumedDiff.push part
-		throw new Error("Ran out of diff to consume. Offset is too small")
+
+		return {
+			consumedDiff: consumedDiff
+			remainingDiff: remainingDiff
+		}
 
 	_consumeDiffAffectedByDeleteOp: (remainingDiff, deleteOp, meta) ->
 		consumedDiff = []
 		remainingOp = deleteOp
-		while remainingOp
+		while remainingOp and remainingDiff.length > 0
 			{newPart, remainingDiff, remainingOp} = DiffGenerator._consumeDeletedPart remainingDiff, remainingOp, meta
 			consumedDiff.push newPart if newPart?
 		return {

+ 23 - 0
services/track-changes/test/unit/coffee/DiffGenerator/DiffGeneratorTests.coffee

@@ -311,4 +311,27 @@ describe "DiffGenerator", ->
 						)
 					).to.throw(@DiffGenerator.ConsistencyError)
 
+			describe "when the last update in the existing diff is a delete", ->
+				it "should insert the new update before the delete", ->
+					diff = @DiffGenerator.applyUpdateToDiff(
+						[ { u: "foo" }, { d: "bar", meta: @meta } ],
+						{ op: [{ p: 3, i: "baz" }], meta: @meta }
+					)
+					expect(diff).to.deep.equal([
+						{ u: "foo" }
+						{ i: "baz", meta: @meta }
+						{ d: "bar", meta: @meta }
+					])
+
+			describe "when the only update in the existing diff is a delete", ->
+				it "should insert the new update after the delete", ->
+					diff = @DiffGenerator.applyUpdateToDiff(
+						[ { d: "bar", meta: @meta } ],
+						{ op: [{ p: 0, i: "baz" }], meta: @meta }
+					)
+					expect(diff).to.deep.equal([
+						{ d: "bar", meta: @meta }
+						{ i: "baz", meta: @meta }
+					])
+