Skip to content

Commit f7cd233

Browse files
webzwo0irhansen
authored andcommitted
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 535331a commit f7cd233

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
@@ -983,11 +983,17 @@ class TextLinesMutator {
983983
this._curSplice.push(...newLines);
984984
this._curLine += newLines.length;
985985
}
986-
} else {
986+
} else if (!this.hasMore()) {
987987
// There are no additional lines. Although the line is put into splice, curLine is not
988-
// increased because there may be more chars in the line (newline is not reached).
988+
// increased because there may be more chars in the line (newline is not reached). We are
989+
// inserting at the end of lines. curCol is 0 as curLine is not in splice.
990+
this._curSplice.push(text);
991+
this._curCol += text.length;
992+
} else {
993+
// insert text after curCol
989994
const sline = this._putCurLineInSplice();
990995
if (!this._curSplice[sline]) {
996+
// TODO should never happen now
991997
const err = new Error(
992998
'curSplice[sline] not populated, actual curSplice contents is ' +
993999
`${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)