mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Auto merge of #9529 - paulrouget:focusEvents, r=asajeffrey
Implement focus, blur, focusin and focusout events Based on https://github.com/servo/servo/pull/7985 Fixes https://github.com/servo/servo/issues/7981 <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.svg" height="40" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9529) <!-- Reviewable:end -->
This commit is contained in:
commit
d8ffa3d0b8
8 changed files with 232 additions and 20 deletions
|
@ -38,6 +38,7 @@ use dom::domimplementation::DOMImplementation;
|
|||
use dom::element::{Element, ElementCreator};
|
||||
use dom::event::{Event, EventBubbles, EventCancelable};
|
||||
use dom::eventtarget::EventTarget;
|
||||
use dom::focusevent::FocusEvent;
|
||||
use dom::htmlanchorelement::HTMLAnchorElement;
|
||||
use dom::htmlappletelement::HTMLAppletElement;
|
||||
use dom::htmlareaelement::HTMLAreaElement;
|
||||
|
@ -609,17 +610,21 @@ impl Document {
|
|||
/// Reassign the focus context to the element that last requested focus during this
|
||||
/// transaction, or none if no elements requested it.
|
||||
pub fn commit_focus_transaction(&self, focus_type: FocusType) {
|
||||
// TODO: dispatch blur, focus, focusout, and focusin events
|
||||
|
||||
if let Some(ref elem) = self.focused.get() {
|
||||
let node = elem.upcast::<Node>();
|
||||
elem.set_focus_state(false);
|
||||
// FIXME: pass appropriate relatedTarget
|
||||
self.fire_focus_event(FocusEventType::Blur, node, None);
|
||||
}
|
||||
|
||||
self.focused.set(self.possibly_focused.get().r());
|
||||
|
||||
if let Some(ref elem) = self.focused.get() {
|
||||
elem.set_focus_state(true);
|
||||
|
||||
let node = elem.upcast::<Node>();
|
||||
// FIXME: pass appropriate relatedTarget
|
||||
self.fire_focus_event(FocusEventType::Focus, node, None);
|
||||
// Update the focus state for all elements in the focus chain.
|
||||
// https://html.spec.whatwg.org/multipage/#focus-chain
|
||||
if focus_type == FocusType::Element {
|
||||
|
@ -1451,6 +1456,25 @@ impl Document {
|
|||
pub fn get_dom_complete(&self) -> u64 {
|
||||
self.dom_complete.get()
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#fire-a-focus-event
|
||||
fn fire_focus_event(&self, focus_event_type: FocusEventType, node: &Node, relatedTarget: Option<&EventTarget>) {
|
||||
let (event_name, does_bubble) = match focus_event_type {
|
||||
FocusEventType::Focus => (DOMString::from("focus"), EventBubbles::DoesNotBubble),
|
||||
FocusEventType::Blur => (DOMString::from("blur"), EventBubbles::DoesNotBubble),
|
||||
};
|
||||
let event = FocusEvent::new(&self.window,
|
||||
event_name,
|
||||
does_bubble,
|
||||
EventCancelable::NotCancelable,
|
||||
Some(&self.window),
|
||||
0i32,
|
||||
relatedTarget);
|
||||
let event = event.upcast::<Event>();
|
||||
event.set_trusted(true);
|
||||
let target = node.upcast();
|
||||
event.fire(target);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, HeapSizeOf)]
|
||||
|
@ -2579,3 +2603,9 @@ pub enum FocusType {
|
|||
Element, // The first focus message - focus the element itself
|
||||
Parent, // Focusing a parent element (an iframe)
|
||||
}
|
||||
|
||||
/// Focus events
|
||||
pub enum FocusEventType {
|
||||
Focus, // Element gained focus. Doesn't bubble.
|
||||
Blur, // Element lost focus. Doesn't bubble.
|
||||
}
|
||||
|
|
84
components/script/dom/focusevent.rs
Normal file
84
components/script/dom/focusevent.rs
Normal file
|
@ -0,0 +1,84 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::bindings::codegen::Bindings::FocusEventBinding;
|
||||
use dom::bindings::codegen::Bindings::FocusEventBinding::FocusEventMethods;
|
||||
use dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods;
|
||||
use dom::bindings::error::Fallible;
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::bindings::js::{JS, MutNullableHeap, Root, RootedReference};
|
||||
use dom::bindings::reflector::reflect_dom_object;
|
||||
use dom::event::{EventBubbles, EventCancelable};
|
||||
use dom::eventtarget::EventTarget;
|
||||
use dom::uievent::UIEvent;
|
||||
use dom::window::Window;
|
||||
use std::default::Default;
|
||||
use util::str::DOMString;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct FocusEvent {
|
||||
uievent: UIEvent,
|
||||
related_target: MutNullableHeap<JS<EventTarget>>,
|
||||
}
|
||||
|
||||
impl FocusEvent {
|
||||
fn new_inherited() -> FocusEvent {
|
||||
FocusEvent {
|
||||
uievent: UIEvent::new_inherited(),
|
||||
related_target: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(window: &Window,
|
||||
type_: DOMString,
|
||||
can_bubble: EventBubbles,
|
||||
cancelable: EventCancelable,
|
||||
view: Option<&Window>,
|
||||
detail: i32,
|
||||
related_target: Option<&EventTarget>) -> Root<FocusEvent> {
|
||||
let event = box FocusEvent::new_inherited();
|
||||
let ev = reflect_dom_object(event, GlobalRef::Window(window), FocusEventBinding::Wrap);
|
||||
ev.upcast::<UIEvent>().InitUIEvent(type_,
|
||||
can_bubble == EventBubbles::Bubbles,
|
||||
cancelable == EventCancelable::Cancelable,
|
||||
view, detail);
|
||||
ev.related_target.set(related_target);
|
||||
ev
|
||||
}
|
||||
|
||||
pub fn Constructor(global: GlobalRef,
|
||||
type_: DOMString,
|
||||
init: &FocusEventBinding::FocusEventInit) -> Fallible<Root<FocusEvent>> {
|
||||
let bubbles = if init.parent.parent.bubbles {
|
||||
EventBubbles::Bubbles
|
||||
} else {
|
||||
EventBubbles::DoesNotBubble
|
||||
};
|
||||
let cancelable = if init.parent.parent.cancelable {
|
||||
EventCancelable::Cancelable
|
||||
} else {
|
||||
EventCancelable::NotCancelable
|
||||
};
|
||||
let event = FocusEvent::new(global.as_window(), type_,
|
||||
bubbles,
|
||||
cancelable,
|
||||
init.parent.view.r(),
|
||||
init.parent.detail,
|
||||
init.relatedTarget.r());
|
||||
Ok(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusEventMethods for FocusEvent {
|
||||
// https://w3c.github.io/uievents/#widl-FocusEvent-relatedTarget
|
||||
fn GetRelatedTarget(&self) -> Option<Root<EventTarget>> {
|
||||
self.related_target.get()
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-event-istrusted
|
||||
fn IsTrusted(&self) -> bool {
|
||||
self.uievent.IsTrusted()
|
||||
}
|
||||
}
|
|
@ -253,6 +253,7 @@ pub mod eventtarget;
|
|||
pub mod file;
|
||||
pub mod filelist;
|
||||
pub mod filereader;
|
||||
pub mod focusevent;
|
||||
pub mod formdata;
|
||||
pub mod htmlanchorelement;
|
||||
pub mod htmlappletelement;
|
||||
|
|
14
components/script/dom/webidls/FocusEvent.webidl
Normal file
14
components/script/dom/webidls/FocusEvent.webidl
Normal file
|
@ -0,0 +1,14 @@
|
|||
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// https://w3c.github.io/uievents/#interface-FocusEvent
|
||||
[Constructor(DOMString typeArg, optional FocusEventInit focusEventInitDict)]
|
||||
interface FocusEvent : UIEvent {
|
||||
readonly attribute EventTarget? relatedTarget;
|
||||
};
|
||||
|
||||
dictionary FocusEventInit : UIEventInit {
|
||||
EventTarget? relatedTarget = null;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue