From 0e54a1078f20c9bcd867c5ab6697be2a7aaa07c4 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween <5454374+emcsween@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:09:53 -0400 Subject: [PATCH] Merge pull request #17931 from overleaf/em-invalid-ranges Throw error when constructing invalid ranges GitOrigin-RevId: 9451e0f8d35372610d08530048e7ee2ca1ff2052 --- libraries/overleaf-editor-core/lib/range.js | 6 ++++++ libraries/overleaf-editor-core/test/range.test.js | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/libraries/overleaf-editor-core/lib/range.js b/libraries/overleaf-editor-core/lib/range.js index 3fd83e88f2..4984f129c7 100644 --- a/libraries/overleaf-editor-core/lib/range.js +++ b/libraries/overleaf-editor-core/lib/range.js @@ -1,10 +1,16 @@ // @ts-check + +const OError = require('@overleaf/o-error') + class Range { /** * @param {number} pos * @param {number} length */ constructor(pos, length) { + if (pos < 0 || length < 0) { + throw new OError('Invalid range', { pos, length }) + } /** @readonly */ this.pos = pos /** @readonly */ diff --git a/libraries/overleaf-editor-core/test/range.test.js b/libraries/overleaf-editor-core/test/range.test.js index b9c6be6fe4..daad8fd6ed 100644 --- a/libraries/overleaf-editor-core/test/range.test.js +++ b/libraries/overleaf-editor-core/test/range.test.js @@ -30,6 +30,14 @@ describe('Range', function () { expect(range0length.isEmpty()).to.be.true }) + it('should not create a range with a negative position', function () { + expect(() => new Range(-1, 10)).to.throw + }) + + it('should not create a range with a negative length', function () { + expect(() => new Range(0, -2)).to.throw + }) + describe('overlaps', function () { it('same ranges should overlap', function () { const range1 = new Range(1, 3)