diff --git a/services/track-changes/app/coffee/UpdateCompressor.coffee b/services/track-changes/app/coffee/UpdateCompressor.coffee index 95e3f58e86..7f606b29d5 100644 --- a/services/track-changes/app/coffee/UpdateCompressor.coffee +++ b/services/track-changes/app/coffee/UpdateCompressor.coffee @@ -133,6 +133,7 @@ module.exports = UpdateCompressor = else if firstOp.i? and secondOp.d? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length) offset = secondOp.p - firstOp.p insertedText = firstOp.i.slice(offset, offset + secondOp.d.length) + # Only trim the insert when the delete is fully contained within in it if insertedText == secondOp.d insert = strRemove(firstOp.i, offset, secondOp.d.length) return [ @@ -146,7 +147,7 @@ module.exports = UpdateCompressor = v: secondUpdate.v ] else - # This shouldn't be possible! + # This will only happen if the delete extends outside the insert return [firstUpdate, secondUpdate] else diff --git a/services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee b/services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee index be3bec5350..4486219b68 100644 --- a/services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee +++ b/services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee @@ -263,6 +263,26 @@ describe "UpdateCompressor", -> v: 43 }] + it "should not combine updates with overlap beyond the end", -> + expect(@UpdateCompressor.compressUpdates [{ + op: { p: 3, i: "foobar" } + meta: ts: @ts1, user_id: @user_id + v: 42 + }, { + op: { p: 6, d: "bardle" } + meta: ts: @ts2, user_id: @user_id + v: 43 + }]) + .to.deep.equal [{ + op: { p: 3, i: "foobar" } + meta: start_ts: @ts1, end_ts: @ts1, user_id: @user_id + v: 42 + }, { + op: { p: 6, d: "bardle" } + meta: start_ts: @ts2, end_ts: @ts2, user_id: @user_id + v: 43 + }] + describe "noop - insert", -> it "should leave them untouched", -> expect(@UpdateCompressor.compressUpdates [{