From 21a891274c318e953096a710f788a701d38ac968 Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Sun, 31 Oct 2021 00:17:48 +0200 Subject: [PATCH] textLinesMutator: Fix insertions with newlines at the end of a line In such cases the remaining part of the old line is directly pushed to the splice but we need to ensure it is not an empty string. Before this commit an empty string was pushed to the splice. --- src/static/js/Changeset.js | 5 +++-- src/tests/frontend/specs/easysync-mutations.js | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index c6a9b2712b19..606d1741fdae 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -973,8 +973,9 @@ class TextLinesMutator { this._curLine += newLines.length; // insert the remaining chars from the "old" line (e.g. the line we were in // when we started to insert new lines) - this._curSplice.push(theLine.substring(lineCol)); - this._curCol = 0; // TODO(doc) why is this not set to the length of last line? + const remaining = theLine.substring(lineCol); + if (remaining !== '') this._curSplice.push(remaining); + this._curCol = 0; } else { this._curSplice.push(...newLines); this._curLine += newLines.length; diff --git a/src/tests/frontend/specs/easysync-mutations.js b/src/tests/frontend/specs/easysync-mutations.js index 27c4a1e5d59c..3208858b9dae 100644 --- a/src/tests/frontend/specs/easysync-mutations.js +++ b/src/tests/frontend/specs/easysync-mutations.js @@ -179,6 +179,11 @@ describe('easysync-mutations', function () { ['insert', 'c'], ], ['fun\n', 'c']); + runMutationTest(14, ['\n'], [ + ['remove', 1, 1, '\n'], + ['insert', 'a'], + ['insert', 'c\n', 1], + ], ['ac\n']); it('mutatorHasMore', async function () { const lines = ['1\n', '2\n', '3\n', '4\n']; let mu;