Skip to content

Commit

Permalink
fix #728
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Jun 8, 2016
1 parent 8e1ad43 commit e1f89a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions test/unit/core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<p>0123</p>');
expect(() => {
this.bounds = selection.getBounds(10, 0);
}).not.toThrow();
expect(() => {
this.bounds = selection.getBounds(0, 10);
}).not.toThrow();
});
});
});

0 comments on commit e1f89a8

Please sign in to comment.