diff --git a/services/track-changes/app/coffee/DiffGenerator.coffee b/services/track-changes/app/coffee/DiffGenerator.coffee index 7f6749dbd4..9593f42f36 100644 --- a/services/track-changes/app/coffee/DiffGenerator.coffee +++ b/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 { diff --git a/services/track-changes/test/unit/coffee/DiffGenerator/DiffGeneratorTests.coffee b/services/track-changes/test/unit/coffee/DiffGenerator/DiffGeneratorTests.coffee index 848086dc2e..3263d84dda 100644 --- a/services/track-changes/test/unit/coffee/DiffGenerator/DiffGeneratorTests.coffee +++ b/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 } + ]) +