Auto merge of #24521 - tigleym:missing_cssom_attributes, r=jdm

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

<!-- Please describe your changes on the following line: -->
Implement MouseEvent's x/y and offsetX/offsetY attributes

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #24248 (GitHub issue number if applicable)

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
bors-servo 2019-11-05 02:43:19 -05:00 committed by GitHub
commit 28dbbda5bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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;