Skip to content

Commit 282fc7f

Browse files
authored
Selection: prevent scrolling when focusing root (T1297029) (#141)
1 parent be57756 commit 282fc7f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

core/selection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Selection {
8686

8787
focus() {
8888
if (this.hasFocus()) return;
89-
this.root.focus();
89+
this.root.focus({ preventScroll: true });
9090
this.setRange(this.savedRange);
9191
}
9292

@@ -318,7 +318,7 @@ class Selection {
318318
const selection = document.getSelection();
319319
if (selection == null) return;
320320
if (startNode != null) {
321-
if (!this.hasFocus()) this.root.focus();
321+
if (!this.hasFocus()) this.root.focus({ preventScroll: true });
322322
const { native } = this.getNativeRange() || {};
323323
if (
324324
native == null

test/unit/core/selection.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ describe('Selection', function () {
4141
});
4242
});
4343

44+
describe('focusing should not trigger scroll', function () {
45+
beforeEach(function () {
46+
this.initialize(HTMLElement, '<div></div>');
47+
this.selection = this.initialize(Selection, '<p>0123</p>', this.container);
48+
spyOn(this.selection.root, 'focus').and.callThrough();
49+
});
50+
51+
it('on focus method call (T1297029)', function () {
52+
this.selection.focus();
53+
expect(this.selection.root.focus).toHaveBeenCalledOnceWith({ preventScroll: true });
54+
});
55+
56+
it('on setNativeRange method call', function () {
57+
this.selection.setNativeRange(this.container.firstChild, 0);
58+
expect(this.selection.root.focus).toHaveBeenCalledOnceWith({ preventScroll: true });
59+
});
60+
});
61+
4462
describe('getRange()', function () {
4563
it('empty document', function () {
4664
const selection = this.initialize(Selection, '');

0 commit comments

Comments
 (0)