Skip to content

Commit 6da879d

Browse files
committed
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
1 parent 666b245 commit 6da879d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/static/js/Changeset.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,17 @@ class TextLinesMutator {
982982
this._curSplice.push(...newLines);
983983
this._curLine += newLines.length;
984984
}
985-
} else {
985+
} else if (!this.hasMore()) {
986986
// There are no additional lines. Although the line is put into splice, curLine is not
987-
// increased because there may be more chars in the line (newline is not reached).
987+
// increased because there may be more chars in the line (newline is not reached). We are
988+
// inserting at the end of lines. curCol is 0 as curLine is not in splice.
989+
this._curSplice.push(text);
990+
this._curCol += text.length;
991+
} else {
992+
// insert text after curCol
988993
const sline = this._putCurLineInSplice();
989994
if (!this._curSplice[sline]) {
995+
// TODO should never happen now
990996
const err = new Error(
991997
'curSplice[sline] not populated, actual curSplice contents is ' +
992998
`${JSON.stringify(this._curSplice)}. Possibly related to ` +

src/tests/frontend/specs/easysync-mutations.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,25 @@ describe('easysync-mutations', function () {
160160
['insert', 'z'],
161161
], ['fuz']);
162162

163+
// #2836, #5214, #3560 regressions
164+
runMutationTest(11, ['\n'], [
165+
['remove', 1, 1, '\n'],
166+
['insert', 'c', 0],
167+
], ['c']);
168+
169+
runMutationTest(12, ['\n'], [
170+
['remove', 1, 1, '\n'],
171+
['insert', 'a\n', 1],
172+
['insert', 'c'],
173+
], ['a\n', 'c']);
174+
175+
runMutationTest(13, ['\n', 'fun\n', '\n'], [
176+
['remove', 1, 1, '\n'],
177+
['skip', 4, 1, false],
178+
['remove', 1, 1, '\n'],
179+
['insert', 'c'],
180+
], ['fun\n', 'c']);
181+
163182
it('mutatorHasMore', async function () {
164183
const lines = ['1\n', '2\n', '3\n', '4\n'];
165184
let mu;

0 commit comments

Comments
 (0)