Skip to content

Commit 8ea5033

Browse files
webzwo0irhansen
authored andcommitted
- fix handling of insertions when there is nothing left in the lines
array. - add some more test cases
1 parent 7b0dd47 commit 8ea5033

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/node/easysync_tests.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,28 @@ const runTests = () => {
218218
], ['banana\n', 'cabbage\n', 'duffle\n']);
219219

220220
// #2836 regressions
221-
runMutationTest(8, ['\n'], [
221+
runMutationTest(8, ['\n', 'foo\n', '\n'], [
222+
['remove', 1, 1, '\n'],
223+
['skip', 4, 1, false],
224+
['remove', 1, 1, '\n'],
225+
['insert', 'c'],
226+
], ['foo\n', 'c']);
227+
runMutationTest(9, ['\n', 'foo\n', '\n'], [
228+
['remove', 1, 1, '\n'],
229+
['skip', 3, 0, false],
230+
['remove', 2, 2, '\n\n'],
231+
['insert', 'c'],
232+
], ['fooc']);
233+
runMutationTest(10, ['\n'], [
222234
['remove', 1, 1, '\n'],
223235
['insert', 'c', 0],
224-
], ['c']);
225-
runMutationTest(9, ['\n'], [
236+
], ['c']); // TODO find out if c must have a newline because of unknown constraints
237+
runMutationTest(11, ['\n'], [
226238
['remove', 1, 1, '\n'],
227239
['insert', 'a'],
228240
['insert', 'c\n', 1],
229241
], ['ac\n']);
230-
runMutationTest(10, ['\n'], [
242+
runMutationTest(12, ['\n'], [
231243
['remove', 1, 1, '\n'],
232244
['insert', 'a\n', 1],
233245
['insert', 'c'],

src/static/js/Changeset.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,14 @@ exports.textLinesMutator = (lines) => {
758758
curSplice[1] += L - 1;
759759
const sline = curSplice.length - 1;
760760
removed = curSplice[sline].substring(curCol) + removed;
761-
curSplice[sline] = curSplice[sline].substring(0, curCol) +
762-
lines_get(curSplice[0] + curSplice[1]);
763-
curSplice[1] += 1;
761+
cosnt line = lines_get(curSplice[0] + curSplice[1]);
762+
// if no line follows the splice
763+
if (!line) {
764+
curSplice[sline] = curSplice[sline].substring(0, curCol);
765+
} else {
766+
curSplice[sline] = curSplice[sline].substring(0, curCol) + line;
767+
curSplice[1] += 1;
768+
}
764769
}
765770
} else {
766771
removed = nextKLinesText(L);
@@ -813,21 +818,46 @@ exports.textLinesMutator = (lines) => {
813818
const sline = curSplice.length - 1;
814819
const theLine = curSplice[sline];
815820
const lineCol = curCol;
821+
// insert the first new line
816822
curSplice[sline] = theLine.substring(0, lineCol) + newLines[0];
817823
curLine++;
818824
newLines.splice(0, 1);
825+
// insert the remaining new lines
819826
Array.prototype.push.apply(curSplice, newLines);
820827
curLine += newLines.length;
821-
curSplice.push(theLine.substring(lineCol));
822-
curCol = 0;
828+
// insert the remaining chars from the "old" line (e.g. the line we were in
829+
// when we started to insert new lines)
830+
// if nothing is left we don't push an empty string
831+
if (theLine.substring(lineCol)) {
832+
curSplice.push(theLine.substring(lineCol));
833+
}
834+
curCol = 0; // TODO(doc) why is this not set to the length of last line?
823835
} else {
824836
Array.prototype.push.apply(curSplice, newLines);
825837
curLine += newLines.length;
826838
}
827839
} else {
828-
const sline = putCurLineInSplice();
829-
if (!curSplice[sline]) {
830-
console.error('curSplice[sline] not populated, actual curSplice contents is ', curSplice, '. Possibly related to https://github.com/ether/etherpad-lite/issues/2802');
840+
// there are no additional lines
841+
if (lines_get(curSplice[0] + curSplice[1]) === undefined) {
842+
// find out if there is a line in splice that is not finished processing
843+
// if yes, we can add our text to it
844+
if (isCurLineInSplice()) {
845+
var sline = curSplice.length - 1;
846+
curSplice[sline] = curSplice[sline].substring(0, curCol) + text + curSplice[sline].substring(curCol);
847+
curCol += text.length;
848+
}
849+
// if no, we need to add the text in a new line
850+
else {
851+
Array.prototype.push.apply(curSplice, [text]);
852+
curCol += text.length;
853+
}
854+
} else {
855+
// although the line is put into splice, curLine is not increased, because
856+
// there may be more chars in the line (newline is not reached)
857+
const sline = putCurLineInSplice();
858+
if (!curSplice[sline]) {
859+
console.error('curSplice[sline] not populated, actual curSplice contents is ', curSplice, '. Possibly related to https://github.com/ether/etherpad-lite/issues/2802');
860+
}
831861
}
832862
curSplice[sline] = curSplice[sline].substring(0, curCol) + text +
833863
curSplice[sline].substring(curCol);

0 commit comments

Comments
 (0)