mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Auto merge of #9787 - nox:event-invoke, r=Ms2ger
Refactor dispatch_event according to the spec (fixes #9178) Two new functions invoke and invoke_inner are introduced and some invariants documented. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.svg" height="40" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9787) <!-- Reviewable:end -->
This commit is contained in:
commit
7d4276b393
2 changed files with 108 additions and 72 deletions
|
@ -47,7 +47,9 @@ fn handle_event(window: Option<&Window>, listener: &CompiledEventListener,
|
||||||
listener.call_or_handle_event(current_target, event, Report);
|
listener.call_or_handle_event(current_target, event, Report);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dispatch_to_listeners(event: &Event, target: &EventTarget, chain: &[&EventTarget]) {
|
// See dispatch_event.
|
||||||
|
// https://dom.spec.whatwg.org/#concept-event-dispatch
|
||||||
|
fn dispatch_to_listeners(event: &Event, target: &EventTarget, event_path: &[&EventTarget]) {
|
||||||
assert!(!event.stop_propagation());
|
assert!(!event.stop_propagation());
|
||||||
assert!(!event.stop_immediate());
|
assert!(!event.stop_immediate());
|
||||||
|
|
||||||
|
@ -62,110 +64,90 @@ fn dispatch_to_listeners(event: &Event, target: &EventTarget, chain: &[&EventTar
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let type_ = event.type_();
|
// Step 5.
|
||||||
|
|
||||||
/* capturing */
|
|
||||||
event.set_phase(EventPhase::Capturing);
|
event.set_phase(EventPhase::Capturing);
|
||||||
for cur_target in chain.iter().rev() {
|
|
||||||
if let Some(listeners) = cur_target.get_listeners_for(&type_, ListenerPhase::Capturing) {
|
|
||||||
event.set_current_target(cur_target);
|
|
||||||
for listener in &listeners {
|
|
||||||
handle_event(window.r(), listener, *cur_target, event);
|
|
||||||
|
|
||||||
if event.stop_immediate() {
|
// Step 6.
|
||||||
return;
|
for object in event_path.iter().rev() {
|
||||||
}
|
invoke(window.r(), object, event, Some(ListenerPhase::Capturing));
|
||||||
}
|
|
||||||
|
|
||||||
if event.stop_propagation() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert!(!event.stop_propagation());
|
|
||||||
assert!(!event.stop_immediate());
|
|
||||||
|
|
||||||
/* at target */
|
|
||||||
event.set_phase(EventPhase::AtTarget);
|
|
||||||
event.set_current_target(target);
|
|
||||||
|
|
||||||
if let Some(listeners) = target.get_listeners(&type_) {
|
|
||||||
for listener in listeners {
|
|
||||||
handle_event(window.r(), &listener, target, event);
|
|
||||||
|
|
||||||
if event.stop_immediate() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if event.stop_propagation() {
|
if event.stop_propagation() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert!(!event.stop_propagation());
|
assert!(!event.stop_propagation());
|
||||||
assert!(!event.stop_immediate());
|
assert!(!event.stop_immediate());
|
||||||
|
|
||||||
/* bubbling */
|
// Step 7.
|
||||||
|
event.set_phase(EventPhase::AtTarget);
|
||||||
|
|
||||||
|
// Step 8.
|
||||||
|
invoke(window.r(), target, event, None);
|
||||||
|
if event.stop_propagation() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assert!(!event.stop_propagation());
|
||||||
|
assert!(!event.stop_immediate());
|
||||||
|
|
||||||
if !event.bubbles() {
|
if !event.bubbles() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Step 9.1.
|
||||||
event.set_phase(EventPhase::Bubbling);
|
event.set_phase(EventPhase::Bubbling);
|
||||||
for cur_target in chain {
|
|
||||||
if let Some(listeners) = cur_target.get_listeners_for(&type_, ListenerPhase::Bubbling) {
|
|
||||||
event.set_current_target(cur_target);
|
|
||||||
for listener in &listeners {
|
|
||||||
handle_event(window.r(), listener, *cur_target, event);
|
|
||||||
|
|
||||||
if event.stop_immediate() {
|
// Step 9.2.
|
||||||
return;
|
for object in event_path {
|
||||||
}
|
invoke(window.r(), object, event, Some(ListenerPhase::Bubbling));
|
||||||
}
|
if event.stop_propagation() {
|
||||||
|
return;
|
||||||
if event.stop_propagation() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See https://dom.spec.whatwg.org/#concept-event-dispatch for the full dispatch algorithm
|
// https://dom.spec.whatwg.org/#concept-event-dispatch
|
||||||
pub fn dispatch_event(target: &EventTarget, pseudo_target: Option<&EventTarget>,
|
pub fn dispatch_event(target: &EventTarget,
|
||||||
|
target_override: Option<&EventTarget>,
|
||||||
event: &Event) -> bool {
|
event: &Event) -> bool {
|
||||||
assert!(!event.dispatching());
|
assert!(!event.dispatching());
|
||||||
assert!(event.initialized());
|
assert!(event.initialized());
|
||||||
assert_eq!(event.phase(), EventPhase::None);
|
assert_eq!(event.phase(), EventPhase::None);
|
||||||
assert!(event.GetCurrentTarget().is_none());
|
assert!(event.GetCurrentTarget().is_none());
|
||||||
|
|
||||||
event.set_target(match pseudo_target {
|
// Step 2.
|
||||||
Some(pseudo_target) => pseudo_target,
|
event.set_target(target_override.unwrap_or(target));
|
||||||
None => target.clone(),
|
|
||||||
});
|
|
||||||
|
|
||||||
if event.stop_propagation() {
|
if event.stop_propagation() {
|
||||||
|
// If the event's stop propagation flag is set, we can skip everything because
|
||||||
|
// it prevents the calls of the invoke algorithm in the spec and we asserted
|
||||||
|
// at the beginning that steps 10-12 don't need to be executed.
|
||||||
return !event.DefaultPrevented();
|
return !event.DefaultPrevented();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Step 1. Postponed here for the reason stated above.
|
||||||
event.set_dispatching(true);
|
event.set_dispatching(true);
|
||||||
|
|
||||||
let mut chain: RootedVec<JS<EventTarget>> = RootedVec::new();
|
// Step 3. The "invoke" algorithm is only used on `target` separately,
|
||||||
|
// so we don't put it in the path.
|
||||||
|
let mut event_path: RootedVec<JS<EventTarget>> = RootedVec::new();
|
||||||
|
|
||||||
|
// Step 4.
|
||||||
if let Some(target_node) = target.downcast::<Node>() {
|
if let Some(target_node) = target.downcast::<Node>() {
|
||||||
for ancestor in target_node.ancestors() {
|
for ancestor in target_node.ancestors() {
|
||||||
chain.push(JS::from_ref(ancestor.upcast()));
|
event_path.push(JS::from_ref(ancestor.upcast()));
|
||||||
}
|
}
|
||||||
let top_most_ancestor_or_target =
|
let top_most_ancestor_or_target =
|
||||||
Root::from_ref(chain.r().last().cloned().unwrap_or(target));
|
Root::from_ref(event_path.r().last().cloned().unwrap_or(target));
|
||||||
if let Some(document) = Root::downcast::<Document>(top_most_ancestor_or_target) {
|
if let Some(document) = Root::downcast::<Document>(top_most_ancestor_or_target) {
|
||||||
if event.type_() != atom!("load") && document.browsing_context().is_some() {
|
if event.type_() != atom!("load") && document.browsing_context().is_some() {
|
||||||
chain.push(JS::from_ref(document.window().upcast()));
|
event_path.push(JS::from_ref(document.window().upcast()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch_to_listeners(event, target, chain.r());
|
// Steps 5-9. In a separate function to short-circuit various things easily.
|
||||||
|
dispatch_to_listeners(event, target, event_path.r());
|
||||||
|
|
||||||
/* default action */
|
// Default action.
|
||||||
let target = event.GetTarget();
|
let target = event.GetTarget();
|
||||||
match target {
|
match target {
|
||||||
Some(ref target) => {
|
Some(ref target) => {
|
||||||
|
@ -177,9 +159,67 @@ pub fn dispatch_event(target: &EventTarget, pseudo_target: Option<&EventTarget>,
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Step 10.
|
||||||
event.set_dispatching(false);
|
event.set_dispatching(false);
|
||||||
|
|
||||||
|
// Step 11.
|
||||||
event.set_phase(EventPhase::None);
|
event.set_phase(EventPhase::None);
|
||||||
|
|
||||||
|
// Step 12.
|
||||||
event.clear_current_target();
|
event.clear_current_target();
|
||||||
|
|
||||||
|
// Step 13.
|
||||||
!event.DefaultPrevented()
|
!event.DefaultPrevented()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://dom.spec.whatwg.org/#concept-event-listener-invoke
|
||||||
|
fn invoke(window: Option<&Window>,
|
||||||
|
object: &EventTarget,
|
||||||
|
event: &Event,
|
||||||
|
specific_listener_phase: Option<ListenerPhase>) {
|
||||||
|
// Step 1.
|
||||||
|
assert!(!event.stop_propagation());
|
||||||
|
|
||||||
|
// Steps 2-3.
|
||||||
|
let listeners = object.get_listeners_for(&event.type_(), specific_listener_phase);
|
||||||
|
|
||||||
|
// Step 4.
|
||||||
|
event.set_current_target(object);
|
||||||
|
|
||||||
|
// Step 5.
|
||||||
|
inner_invoke(window, object, event, &listeners);
|
||||||
|
|
||||||
|
// TODO: step 6.
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
|
||||||
|
fn inner_invoke(window: Option<&Window>,
|
||||||
|
object: &EventTarget,
|
||||||
|
event: &Event,
|
||||||
|
listeners: &[CompiledEventListener])
|
||||||
|
-> bool {
|
||||||
|
// Step 1.
|
||||||
|
let mut found = false;
|
||||||
|
|
||||||
|
// Step 2.
|
||||||
|
for listener in listeners {
|
||||||
|
// Steps 2.1 and 2.3-2.4 are not done because `listeners` contain only the
|
||||||
|
// relevant ones for this invoke call during the dispatch algorithm.
|
||||||
|
|
||||||
|
// Step 2.2.
|
||||||
|
found = true;
|
||||||
|
|
||||||
|
// TODO: step 2.5.
|
||||||
|
|
||||||
|
// Step 2.6.
|
||||||
|
handle_event(window, listener, object, event);
|
||||||
|
if event.stop_immediate() {
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: step 2.7.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 3.
|
||||||
|
found
|
||||||
|
}
|
||||||
|
|
|
@ -301,16 +301,12 @@ impl EventTarget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_listeners(&self, type_: &Atom) -> Option<Vec<CompiledEventListener>> {
|
pub fn get_listeners_for(&self,
|
||||||
self.handlers.borrow_mut().get_mut(type_).map(|listeners| {
|
type_: &Atom,
|
||||||
listeners.get_listeners(None, self, type_)
|
specific_phase: Option<ListenerPhase>)
|
||||||
})
|
-> Vec<CompiledEventListener> {
|
||||||
}
|
self.handlers.borrow_mut().get_mut(type_).map_or(vec![], |listeners| {
|
||||||
|
listeners.get_listeners(specific_phase, self, type_)
|
||||||
pub fn get_listeners_for(&self, type_: &Atom, desired_phase: ListenerPhase)
|
|
||||||
-> Option<Vec<CompiledEventListener>> {
|
|
||||||
self.handlers.borrow_mut().get_mut(type_).map(|listeners| {
|
|
||||||
listeners.get_listeners(Some(desired_phase), self, type_)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue