diff --git a/core/selection.js b/core/selection.js index 7c01202353..69aeb66331 100644 --- a/core/selection.js +++ b/core/selection.js @@ -195,10 +195,10 @@ class Selection { let bounds = this.getBounds(range.index, range.length); if (bounds == null) return; if (this.root.offsetHeight < bounds.bottom) { - let [line, ] = this.scroll.line(range.index + range.length); + let [line, ] = this.scroll.line(Math.min(range.index + range.length, this.scroll.length()-1)); this.root.scrollTop = line.domNode.offsetTop + line.domNode.offsetHeight - this.root.offsetHeight; } else if (bounds.top < 0) { - let [line, ] = this.scroll.line(range.index); + let [line, ] = this.scroll.line(Math.min(range.index, this.scroll.length()-1)); this.root.scrollTop = line.domNode.offsetTop; } }