Skip to content

Commit 5061fe7

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 2a7cd0f commit 5061fe7

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
@@ -979,11 +979,17 @@ class TextLinesMutator {
979979
this._curSplice.push(...newLines);
980980
this._curLine += newLines.length;
981981
}
982-
} else {
982+
} else if (!this.hasMore()) {
983983
// There are no additional lines. Although the line is put into splice, curLine is not
984-
// increased because there may be more chars in the line (newline is not reached).
984+
// increased because there may be more chars in the line (newline is not reached). We are
985+
// inserting at the end of lines. curCol is 0 as curLine is not in splice.
986+
this._curSplice.push(text);
987+
this._curCol += text.length;
988+
} else {
989+
// insert text after curCol
985990
const sline = this._putCurLineInSplice();
986991
if (!this._curSplice[sline]) {
992+
// TODO should never happen now
987993
const err = new Error(
988994
'curSplice[sline] not populated, actual curSplice contents is ' +
989995
`${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)