Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MouseEvent: fix pageX/Y and offsetX/Y during dispatch #3514

Merged
merged 6 commits into from Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 6 additions & 17 deletions lib/jsdom/living/events/MouseEvent-impl.js
Expand Up @@ -5,7 +5,6 @@ const EventModifierMixinImpl = require("./EventModifierMixin-impl").implementati
const UIEventImpl = require("./UIEvent-impl").implementation;

const MouseEventInit = require("../generated/MouseEventInit");
const { wrapperForImpl } = require("../generated/utils");

class MouseEventImpl extends UIEventImpl {
get x() {
Expand All @@ -15,29 +14,19 @@ class MouseEventImpl extends UIEventImpl {
return this.clientY;
}
get pageX() {
if (this._dispatchFlag) {
return 0;
}
const offset = wrapperForImpl(this.view)?.scrollX || 0;
return offset + this.clientX;
// TODO: consider dispatch flag and return page-relative event coordinate once layout is supported
return this.clientX; // TODO: add horizontal scroll offset once jsdom implements scrolling support
}
get pageY() {
if (this._dispatchFlag) {
return 0;
}
const offset = wrapperForImpl(this.view)?.scrollY || 0;
return offset + this.clientY;
// TODO: consider dispatch flag and return page-relative event coordinate once layout is supported
return this.clientY; // TODO: add vertical scroll offset once jsdom implements scrolling support
}
get offsetX() {
if (this._dispatchFlag) {
return 0;
}
// TODO: consider dispatch flag and return target-relative event coordinate once layout is supported
return this.pageX;
}
get offsetY() {
if (this._dispatchFlag) {
return 0;
}
// TODO: consider dispatch flag and return target-relative event coordinate once layout is supported
return this.pageY;
}

Expand Down
Expand Up @@ -91,19 +91,30 @@
}, "clicking using click() should produce an event object with the correct MouseEventInit values");

test(() => {
window.scrollX = 10;
window.scrollY = 20;
const e = new MouseEvent("click", {
clientX: 1, clientY: 2, view: window
});
assert_equals(e.x, 1);
assert_equals(e.y, 2);
assert_equals(e.pageX, 11);
assert_equals(e.pageY, 22);
assert_equals(e.offsetX, 11);
assert_equals(e.offsetY, 22);
assert_equals(e.pageX, 1);
assert_equals(e.pageY, 2);
assert_equals(e.offsetX, 1);
assert_equals(e.offsetY, 2);
}, "MouseEvent should provide the correct computed values when the dispatch flag is not set");

async_test(t => {
const element = document.documentElement; // dispatch against something at the origin (0,0) so it passes in browsers
element.addEventListener("click", t.step_func_done(e => {
assert_equals(e.pageX, 1);
assert_equals(e.pageY, 2);
assert_equals(e.offsetX, 1);
assert_equals(e.offsetY, 2);
}));
element.dispatchEvent(new MouseEvent("click", {
clientX: 1, clientY: 2, view: window
}));
jenseng marked this conversation as resolved.
Show resolved Hide resolved
}, "MouseEvent should provide the correct computed values when the dispatch flag is set");

test(() => {
const e = new MouseEvent("click", {
screenX: 1.5, screenY: 2.5, clientX: 3.5, clientY: 4.5
Expand All @@ -121,6 +132,5 @@
assert_equals(e.screenY, 2);
assert_equals(e.clientX, 3);
assert_equals(e.clientY, 4);

}, "MouseEvent.initMouseEvent should convert doubles to longs");
</script>