Implement MouseEvent's x/y and offsetX/offsetY attributes

This commit is contained in:
Micah Tigley 2019-10-18 16:04:18 -04:00
parent 1d450ba1f3
commit 38b91c3501
6 changed files with 74 additions and 29 deletions

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::dom::bindings::codegen::Bindings::EventBinding::EventBinding::EventMethods;
use crate::dom::bindings::codegen::Bindings::MouseEventBinding;
use crate::dom::bindings::codegen::Bindings::MouseEventBinding::MouseEventMethods;
use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods;
@ -12,6 +13,7 @@ use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::eventtarget::EventTarget;
use crate::dom::node::Node;
use crate::dom::uievent::UIEvent;
use crate::dom::window::Window;
use dom_struct::dom_struct;
@ -29,6 +31,10 @@ pub struct MouseEvent {
client_y: Cell<i32>,
page_x: Cell<i32>,
page_y: Cell<i32>,
x: Cell<i32>,
y: Cell<i32>,
offset_x: Cell<i32>,
offset_y: Cell<i32>,
ctrl_key: Cell<bool>,
shift_key: Cell<bool>,
alt_key: Cell<bool>,
@ -49,6 +55,10 @@ impl MouseEvent {
client_y: Cell::new(0),
page_x: Cell::new(0),
page_y: Cell::new(0),
x: Cell::new(0),
y: Cell::new(0),
offset_x: Cell::new(0),
offset_y: Cell::new(0),
ctrl_key: Cell::new(false),
shift_key: Cell::new(false),
alt_key: Cell::new(false),
@ -192,6 +202,56 @@ impl MouseEventMethods for MouseEvent {
}
}
// https://drafts.csswg.org/cssom-view/#dom-mouseevent-x
fn X(&self) -> i32 {
self.client_x.get()
}
// https://drafts.csswg.org/cssom-view/#dom-mouseevent-y
fn Y(&self) -> i32 {
self.client_y.get()
}
// https://drafts.csswg.org/cssom-view/#dom-mouseevent-offsetx
fn OffsetX(&self) -> i32 {
let event = self.upcast::<Event>();
if event.dispatching() {
match event.GetTarget() {
Some(target) => {
if let Some(node) = target.downcast::<Node>() {
let rect = node.client_rect();
self.client_x.get() - rect.origin.x
} else {
self.offset_x.get()
}
},
None => self.offset_x.get(),
}
} else {
self.PageX()
}
}
// https://drafts.csswg.org/cssom-view/#dom-mouseevent-offsety
fn OffsetY(&self) -> i32 {
let event = self.upcast::<Event>();
if event.dispatching() {
match event.GetTarget() {
Some(target) => {
if let Some(node) = target.downcast::<Node>() {
let rect = node.client_rect();
self.client_y.get() - rect.origin.y
} else {
self.offset_y.get()
}
},
None => self.offset_y.get(),
}
} else {
self.PageY()
}
}
// https://w3c.github.io/uievents/#widl-MouseEvent-ctrlKey
fn CtrlKey(&self) -> bool {
self.ctrl_key.get()

View file

@ -12,6 +12,10 @@ interface MouseEvent : UIEvent {
readonly attribute long clientY;
readonly attribute long pageX;
readonly attribute long pageY;
readonly attribute long x;
readonly attribute long y;
readonly attribute long offsetX;
readonly attribute long offsetY;
readonly attribute boolean ctrlKey;
readonly attribute boolean shiftKey;
readonly attribute boolean altKey;

View file

@ -616088,7 +616088,7 @@
"testharness"
],
"css/cssom-view/mouseEvent.html": [
"7e194b8909317806ff80e300cd480b31680a397e",
"d50959729239599ff057a71143553b4a53f88975",
"testharness"
],
"css/cssom-view/negativeMargins.html": [

View file

@ -128,12 +128,6 @@
[Range interface: operation getClientRects()]
expected: FAIL
[MouseEvent interface: attribute y]
expected: FAIL
[MouseEvent interface: attribute x]
expected: FAIL
[Element interface: calling scrollIntoView([object Object\],[object Object\]) on document.createElement("img") with too few arguments must throw TypeError]
expected: FAIL
@ -251,12 +245,6 @@
[Element interface: calling convertRectFromNode(DOMRectReadOnly, GeometryNode, ConvertCoordinateOptions) on document.createElement("div") with too few arguments must throw TypeError]
expected: FAIL
[MouseEvent interface: attribute offsetX]
expected: FAIL
[MouseEvent interface: attribute offsetY]
expected: FAIL
[CSSPseudoElement interface: operation convertRectFromNode(DOMRectReadOnly, GeometryNode, ConvertCoordinateOptions)]
expected: FAIL
@ -298,15 +286,3 @@
[Partial dictionary MouseEventInit: member names are unique]
expected: FAIL
[MouseEvent interface: new MouseEvent("foo") must inherit property "offsetY" with the proper type]
expected: FAIL
[MouseEvent interface: new MouseEvent("foo") must inherit property "y" with the proper type]
expected: FAIL
[MouseEvent interface: new MouseEvent("foo") must inherit property "x" with the proper type]
expected: FAIL
[MouseEvent interface: new MouseEvent("foo") must inherit property "offsetX" with the proper type]
expected: FAIL

View file

@ -1,4 +0,0 @@
[mouseEvent.html]
[MouseEvent's x and y must be equal to clientX and clientY.]
expected: FAIL

View file

@ -29,5 +29,14 @@ test(function () {
assert_equals(mouseEvent2.pageX, 10);
assert_equals(mouseEvent2.pageY, 5020);
}, 'MouseEvent\'s pageX and pageY attributes should be the sum of the scroll offset and clientX/clientY');
test(function () {
var mouseEvent = new MouseEvent('mousedown', {clientX: 10, clientY: 20});
assert_equals(mouseEvent.offsetX, mouseEvent.pageX);
assert_equals(mouseEvent.offsetY, mouseEvent.pageY);
scrollBy(0, 5000);
assert_equals(mouseEvent.offsetX, mouseEvent.pageX);
assert_equals(mouseEvent.offsetY, mouseEvent.pageY);
}, 'MouseEvent\'s offsetX/offsetY attributes should be the same value as its pageX/pageY attributes.');
</script>
</head>