Skip to content

Commit

Permalink
textLinesMutator: Fix insertions at the end of lines
Browse files Browse the repository at this point in the history
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
  • Loading branch information
webzwo0i committed Nov 29, 2021
1 parent 4a78bac commit 447f022
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/static/js/Changeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,11 +982,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 ` +
Expand Down
19 changes: 19 additions & 0 deletions src/tests/frontend/specs/easysync-mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 447f022

Please sign in to comment.