diff --git a/core/selection.js b/core/selection.js index 494a3270ca..20636d24e2 100644 --- a/core/selection.js +++ b/core/selection.js @@ -79,6 +79,9 @@ class Selection { } getBounds(index, length = 0) { + let scrollLength = this.scroll.length(); + index = Math.min(index, scrollLength - 1); + length = Math.min(index + length, scrollLength - 1) - index; let bounds, node, [leaf, offset] = this.scroll.leaf(index); if (leaf == null) return null; [node, offset] = leaf.position(offset, true); @@ -192,6 +195,7 @@ class Selection { scrollIntoView(range = this.lastRange) { if (range == null) return; let bounds = this.getBounds(range.index, range.length); + if (bounds == null) return; if (this.root.offsetHeight < bounds.bottom) { let [line, offset] = this.scroll.line(range.index + range.length); this.root.scrollTop = line.domNode.offsetTop + line.domNode.offsetHeight; diff --git a/test/unit/core/selection.js b/test/unit/core/selection.js index a9f5965d60..cec122ae58 100644 --- a/test/unit/core/selection.js +++ b/test/unit/core/selection.js @@ -488,5 +488,15 @@ describe('Selection', function() { expect(this.bounds.height).toBeApproximately(32, 1); expect(this.bounds.top).toBeApproximately(this.reference.top, 1); }); + + it('beyond document', function() { + let selection = this.initialize(Selection, '
0123
'); + expect(() => { + this.bounds = selection.getBounds(10, 0); + }).not.toThrow(); + expect(() => { + this.bounds = selection.getBounds(0, 10); + }).not.toThrow(); + }); }); });