From 748d6614951312d52abc7d493e07acaacbbaa1b6 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 13 Dec 2021 00:04:05 -0500 Subject: [PATCH] Changeset: Fix off-by-one bug in `makeSplice` --- src/static/js/Changeset.js | 2 +- src/tests/frontend/specs/easysync-other.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index 81032b580..e3ae9d286 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -1493,7 +1493,7 @@ exports.identity = (N) => exports.pack(N, N, '', ''); exports.makeSplice = (orig, start, ndel, ins, attribs, pool) => { if (start < 0) throw new RangeError(`start index must be non-negative (is ${start})`); if (ndel < 0) throw new RangeError(`characters to delete must be non-negative (is ${ndel})`); - if (start >= orig.length) start = orig.length - 1; + if (start > orig.length) start = orig.length; if (ndel > orig.length - start) ndel = orig.length - start; const deleted = orig.substring(start, start + ndel); const assem = exports.smartOpAssembler(); diff --git a/src/tests/frontend/specs/easysync-other.js b/src/tests/frontend/specs/easysync-other.js index 856b7df62..af4580835 100644 --- a/src/tests/frontend/specs/easysync-other.js +++ b/src/tests/frontend/specs/easysync-other.js @@ -70,6 +70,13 @@ describe('easysync-other', function () { expect(t2).to.equal('a\nb\ncdef\n'); }); + it('makeSplice at the end', async function () { + const orig = '123'; + const ins = '456'; + expect(Changeset.applyToText(Changeset.makeSplice(orig, orig.length, 0, ins), orig)) + .to.equal(`${orig}${ins}`); + }); + it('testToSplices', async function () { const cs = Changeset.checkRep('Z:z>9*0=1=4-3+9=1|1-4-4+1*0+a$123456789abcdefghijk'); const correctSplices = [