Skip to content

Commit

Permalink
Add CSSOM View extensions to MouseEvent
Browse files Browse the repository at this point in the history
Namely, x, y, pageX, pageY, offsetX, offsetY. Also updates screenX and screenY to not always coerce to integers.

Co-authored-by: Vinícius Francisco Xavier <viniciusfranciscoxavier@gmail.com>
  • Loading branch information
jenseng and ViniciusFXavier committed Jan 10, 2023
1 parent a7c8545 commit eb82a27
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
34 changes: 34 additions & 0 deletions lib/jsdom/living/events/MouseEvent-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,42 @@ 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() {
return this.clientX;
}
get y() {
return this.clientY;
}
get pageX() {
if (this._dispatchFlag) {
return 0;
}
const offset = wrapperForImpl(this.view)?.scrollX || 0;
return offset + this.clientX;
}
get pageY() {
if (this._dispatchFlag) {
return 0;
}
const offset = wrapperForImpl(this.view)?.scrollY || 0;
return offset + this.clientY;
}
get offsetX() {
if (this._dispatchFlag) {
return 0;
}
return this.pageX;
}
get offsetY() {
if (this._dispatchFlag) {
return 0;
}
return this.pageY;
}

initMouseEvent(
type,
bubbles,
Expand Down
25 changes: 24 additions & 1 deletion lib/jsdom/living/events/MouseEvent.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ interface MouseEvent : UIEvent {
readonly attribute long screenY;
readonly attribute long clientX;
readonly attribute long clientY;

readonly attribute boolean ctrlKey;
readonly attribute boolean shiftKey;
readonly attribute boolean altKey;
Expand Down Expand Up @@ -52,3 +51,27 @@ partial interface MouseEvent {
optional short buttonArg = 0,
optional EventTarget? relatedTargetArg = null);
};

// https://drafts.csswg.org/cssom-view/#extensions-to-the-mouseevent-interface
// Adds attributes and changes existing ones to doubles
partial interface MouseEvent {
readonly attribute double screenX;
readonly attribute double screenY;
readonly attribute double pageX;
readonly attribute double pageY;
readonly attribute double clientX;
readonly attribute double clientY;
readonly attribute double x;
readonly attribute double y;
readonly attribute double offsetX;
readonly attribute double offsetY;
};

// https://drafts.csswg.org/cssom-view/#extensions-to-the-mouseevent-interface
// Changes existing coordinate entries to doubles
partial dictionary MouseEventInit {
double screenX = 0.0;
double screenY = 0.0;
double clientX = 0.0;
double clientY = 0.0;
};
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
assert_equals(e.screenY, 0);
assert_equals(e.clientX, 0);
assert_equals(e.clientY, 0);
assert_equals(e.x, 0);
assert_equals(e.y, 0);
assert_equals(e.pageX, 0);
assert_equals(e.pageY, 0);
assert_equals(e.offsetX, 0);
assert_equals(e.offsetY, 0);

assert_equals(e.button, 0);
assert_equals(e.buttons, 0);
Expand All @@ -84,4 +90,37 @@

}, "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);
}, "MouseEvent should provide the correct computed values when the dispatch flag is not set");

test(() => {
const e = new MouseEvent("click", {
screenX: 1.5, screenY: 2.5, clientX: 3.5, clientY: 4.5
});
assert_equals(e.screenX, 1.5);
assert_equals(e.screenY, 2.5);
assert_equals(e.clientX, 3.5);
assert_equals(e.clientY, 4.5);
}, "MouseEvent.constructor should preserve doubles");

test(() => {
const e = new MouseEvent("click");
e.initMouseEvent("click", true, true, null, null, 1.5, 2.5, 3.5, 4.5);
assert_equals(e.screenX, 1);
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 eb82a27

Please sign in to comment.