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 1 commit
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
13 changes: 1 addition & 12 deletions lib/jsdom/living/events/Event-impl.js
Expand Up @@ -22,7 +22,7 @@ class EventImpl {
}
}

this._target = null;
this.target = null;
this.currentTarget = null;
this.eventPhase = 0;

Expand All @@ -46,17 +46,6 @@ class EventImpl {
}
}

get target() {
return this._target;
}

set target(target) {
this._target = target;
this._receivedTarget();
}

_receivedTarget() {}

get srcElement() {
return this.target;
}
Expand Down
35 changes: 6 additions & 29 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,44 +14,22 @@ class MouseEventImpl extends UIEventImpl {
return this.clientY;
}
get pageX() {
if (this._dispatchFlag) {
return this._pageX;
}
const offset = wrapperForImpl(this.view)?.scrollX || 0;
return offset + this.clientX;
// TODO: consider dispatch flag and return page-relative event position once layout is supported
return this.clientX; // TODO: add horizontal scroll offset once jsdom implements scrolling support
}
get pageY() {
if (this._dispatchFlag) {
return this._pageY;
}
const offset = wrapperForImpl(this.view)?.scrollY || 0;
return offset + this.clientY;
// TODO: consider dispatch flag and return page-relative event position once layout is supported
return this.clientY; // TODO: add horizontal scroll offset once jsdom implements scrolling support
jenseng marked this conversation as resolved.
Show resolved Hide resolved
}
get offsetX() {
if (this._dispatchFlag) {
return this._offsetX;
}
// TODO: consider dispatch flag and return target-relative event position once layout is supported
return this.pageX;
}
get offsetY() {
if (this._dispatchFlag) {
return this._offsetY;
}
// TODO: consider dispatch flag and return target-relative event position once layout is supported
return this.pageY;
}

_receivedTarget() {
// calculate the event position when the target is set so that it's stable during dispatch
if (this.target) {
const { scrollX = 0, scrollY = 0 } = wrapperForImpl(this.view) ?? {};
this._pageX = this.clientX + scrollX;
this._pageY = this.clientY + scrollY;
const domRect = this.target.getBoundingClientRect?.() ?? { x: 0, y: 0 };
this._offsetX = this.clientX - domRect.x;
this._offsetY = this.clientY - domRect.y;
}
}

initMouseEvent(
type,
bubbles,
Expand Down
Expand Up @@ -103,7 +103,7 @@
}, "MouseEvent should provide the correct computed values when the dispatch flag is not set");

async_test(t => {
const element = document.querySelector("button");
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);
Expand Down