From f7cd2330696500fe442f2bb4ed96098cf045e6f5 Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Sat, 30 Oct 2021 23:42:15 +0200 Subject: [PATCH] textLinesMutator: Fix insertions at the end of `lines` The new insertions are directly pushed to curSplice now instead of trying to incorporate an undefined line into the splice, which would result in an error: TypeError: Cannot read property 'substring' of undefined --- src/static/js/Changeset.js | 10 ++++++++-- .../frontend/specs/easysync-mutations.js | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index 7076910a6..95958c12c 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -983,11 +983,17 @@ class TextLinesMutator { this._curSplice.push(...newLines); this._curLine += newLines.length; } - } else { + } else if (!this.hasMore()) { // There are no additional lines. Although the line is put into splice, curLine is not - // increased because there may be more chars in the line (newline is not reached). + // increased because there may be more chars in the line (newline is not reached). We are + // inserting at the end of lines. curCol is 0 as curLine is not in splice. + this._curSplice.push(text); + this._curCol += text.length; + } else { + // insert text after curCol const sline = this._putCurLineInSplice(); if (!this._curSplice[sline]) { + // TODO should never happen now const err = new Error( 'curSplice[sline] not populated, actual curSplice contents is ' + `${JSON.stringify(this._curSplice)}. Possibly related to ` + diff --git a/src/tests/frontend/specs/easysync-mutations.js b/src/tests/frontend/specs/easysync-mutations.js index 5d9f6285d..27c4a1e5d 100644 --- a/src/tests/frontend/specs/easysync-mutations.js +++ b/src/tests/frontend/specs/easysync-mutations.js @@ -160,6 +160,25 @@ describe('easysync-mutations', function () { ['insert', 'z'], ], ['fuz']); + // #2836, #5214, #3560 regressions + runMutationTest(11, ['\n'], [ + ['remove', 1, 1, '\n'], + ['insert', 'c', 0], + ], ['c']); + + runMutationTest(12, ['\n'], [ + ['remove', 1, 1, '\n'], + ['insert', 'a\n', 1], + ['insert', 'c'], + ], ['a\n', 'c']); + + runMutationTest(13, ['\n', 'fun\n', '\n'], [ + ['remove', 1, 1, '\n'], + ['skip', 4, 1, false], + ['remove', 1, 1, '\n'], + ['insert', 'c'], + ], ['fun\n', 'c']); + it('mutatorHasMore', async function () { const lines = ['1\n', '2\n', '3\n', '4\n']; let mu;