Skip to content

Commit

Permalink
fix "Jquery scrollTop causes "Uncaught object "[object Object]" was t…
Browse files Browse the repository at this point in the history
…hrown." (close #2045) (#2049)
  • Loading branch information
miherlosev committed Jun 14, 2019
1 parent 3de4468 commit 18f8fd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/client/sandbox/native-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class NativeMethods {
runInContext: any;
runInNewContext: any;
runInThisContext: any;
scrollTo: any;

constructor (doc?: Document, win?: Window) {
win = win || window;
Expand Down Expand Up @@ -939,6 +940,8 @@ class NativeMethods {
this.tokenListToggle = win.DOMTokenList.prototype.toggle;
this.tokenListContains = win.DOMTokenList.prototype.contains;

this.scrollTo = win.scrollTo;

this.refreshClasses(win);
}

Expand Down
4 changes: 2 additions & 2 deletions src/client/utils/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export function setScrollLeft (el, value) {
const win = domUtils.findDocument(el).defaultView;
const scrollTop = getScrollTop(el);

win.scrollTo(value, scrollTop);
nativeMethods.scrollTo.call(win, value, scrollTop);
}
else
el.scrollLeft = value;
Expand All @@ -276,7 +276,7 @@ export function setScrollTop (el, value) {
const win = domUtils.findDocument(el).defaultView;
const scrollLeft = getScrollLeft(el);

win.scrollTo(scrollLeft, value);
nativeMethods.scrollTo.call(win, scrollLeft, value);
}
else
el.scrollTop = value;
Expand Down
20 changes: 20 additions & 0 deletions test/client/fixtures/utils/style-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ test('setScrollLeft, setScrollTop', function () {
$div.remove();
});

test('should use the native "window.scrollTo" function internally', function () {
var storedScrollToFn = window.scrollTo;

window.scrollTo = function () {
throw new Error('An error in the "scrollTo" function');
};

try {
styleUtils.setScrollTop(window, 0);
styleUtils.setScrollLeft(window, 0);

ok(true);
}
catch (e) {
ok(false, e.toString());
}

window.scrollTo = storedScrollToFn;
});

test('getInnerWidth', function () {
strictEqual(styleUtils.getInnerWidth(null), null);
strictEqual($(window).innerWidth(), styleUtils.getInnerWidth(window));
Expand Down

0 comments on commit 18f8fd4

Please sign in to comment.