Skip to content

Commit

Permalink
MouseEvent: fix {page,offset}{X,Y} during dispatch
Browse files Browse the repository at this point in the history
Ensure that pageX and pageY reflect the coordinate relative to the origin of the document, i.e. clientX/clientY in the absence of scrolling support, during dispatch.

Also fix the target-relative coordinates, offsetX and offsetY, during dispatch. These will typically be (0, 0), e.g. in a synthetic click or a dispatched MouseEvent with default coordinates, but it will mirror clientX/clientY if those are set.

Lastly, remove some scrollX/scrollY logic for values computed outside of dispatch. While it logically followed the spec, scrolling is not implemented and it is unsafe to base the calculation on those replaceable properties.
  • Loading branch information
jenseng committed Apr 16, 2023
1 parent 31cfdd4 commit 12a24a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
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
}));
}, "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>

0 comments on commit 12a24a9

Please sign in to comment.