From f132ad3099cda25014594e4e00c3c39510d6fba4 Mon Sep 17 00:00:00 2001 From: aditj Date: Wed, 6 Mar 2019 23:31:03 +0530 Subject: [PATCH] Changed the Firing a Synthetic MouseEvent Algorithm --- components/script/dom/activation.rs | 39 ++++++---------------- components/script/dom/document.rs | 15 ++------- components/script/dom/event.rs | 7 +++- components/script/dom/htmlbuttonelement.rs | 4 +-- components/script/dom/htmlelement.rs | 11 ++---- components/script/dom/htmlinputelement.rs | 4 +-- components/script/dom/htmllabelelement.rs | 11 ++---- components/script/dom/mouseevent.rs | 1 + 8 files changed, 28 insertions(+), 64 deletions(-) diff --git a/components/script/dom/activation.rs b/components/script/dom/activation.rs index b838ac4156a4..2f960ab535d1 100644 --- a/components/script/dom/activation.rs +++ b/components/script/dom/activation.rs @@ -48,43 +48,28 @@ pub trait Activatable { } } -/// Whether an activation was initiated via the click() method -#[derive(PartialEq)] -pub enum ActivationSource { - FromClick, - NotFromClick, -} - -// https://html.spec.whatwg.org/multipage/#run-synthetic-click-activation-steps +//https://html.spec.whatwg.org/multipage/#fire-a-synthetic-mouse-event pub fn synthetic_click_activation( element: &Element, ctrl_key: bool, shift_key: bool, alt_key: bool, meta_key: bool, - source: ActivationSource, + not_trusted: bool, ) { - // Step 1 - if element.click_in_progress() { - return; - } - // Step 2 - element.set_click_in_progress(true); - // Step 3 - let activatable = element.as_maybe_activatable(); - if let Some(a) = activatable { - a.pre_click_activation(); - } - // Step 4 + // https://html.spec.whatwg.org/multipage/#fire-a-synthetic-mouse-event let win = window_from_node(element); let target = element.upcast::(); + //Step 1 let mouse = MouseEvent::new( &win, + //Step 2 DOMString::from("click"), - EventBubbles::DoesNotBubble, - EventCancelable::NotCancelable, + //Step 3 & 4 + EventBubbles::Bubbles, + EventCancelable::Cancelable, Some(&win), 1, 0, @@ -99,13 +84,13 @@ pub fn synthetic_click_activation( None, None, ); + //Step 5 let event = mouse.upcast::(); - if source == ActivationSource::FromClick { + if not_trusted { event.set_trusted(false); } + //Step 9 target.dispatch_event(event); - - // Step 5 if let Some(a) = activatable { if event.DefaultPrevented() { a.canceled_activation(); @@ -114,7 +99,5 @@ pub fn synthetic_click_activation( a.activation_behavior(event, target); } } - - // Step 6 element.set_click_in_progress(false); } diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 48e43f805908..cceb955229f0 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -3,7 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use crate::document_loader::{DocumentLoader, LoadType}; -use crate::dom::activation::{synthetic_click_activation, ActivationSource}; +use crate::dom::activation::{synthetic_click_activation}; use crate::dom::attr::Attr; use crate::dom::beforeunloadevent::BeforeUnloadEvent; use crate::dom::bindings::callback::ExceptionHandling; @@ -1443,10 +1443,6 @@ impl Document { let msg = EmbedderMsg::Keyboard(keyboard_event.clone()); self.send_to_embedder(msg); - // This behavior is unspecced - // We are supposed to dispatch synthetic click activation for Space and/or Return, - // however *when* we do it is up to us. - // Here, we're dispatching it after the key event so the script has a chance to cancel it // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27337 match keyboard_event.key { Key::Character(ref letter) @@ -1454,14 +1450,7 @@ impl Document { { let maybe_elem = target.downcast::(); if let Some(el) = maybe_elem { - synthetic_click_activation( - el, - false, - false, - false, - false, - ActivationSource::NotFromClick, - ) + synthetic_click_activation(el, false, false, false, false, false) } } Key::Enter if keyboard_event.state == KeyState::Up => { diff --git a/components/script/dom/event.rs b/components/script/dom/event.rs index 61b9b14877ef..f2c14ed1c7f2 100644 --- a/components/script/dom/event.rs +++ b/components/script/dom/event.rs @@ -144,6 +144,7 @@ impl Event { target: &EventTarget, target_override: Option<&EventTarget>, ) -> EventStatus { + let is_activation_event = false; assert!(!self.dispatching()); assert!(self.initialized()); assert_eq!(self.phase.get(), EventPhase::None); @@ -169,6 +170,9 @@ impl Event { // Step 3-4. let path = self.construct_event_path(&target); rooted_vec!(let event_path <- path.into_iter()); + if self.type_() == atom!("click") { + is_activation_event = true; + } // Steps 5-9. In a separate function to short-circuit various things easily. dispatch_to_listeners(self, target, event_path.r()); @@ -203,7 +207,6 @@ impl Event { // https://dom.spec.whatwg.org/#concept-event-dispatch Steps 10-12. fn clear_dispatching_flags(&self) { assert!(self.dispatching.get()); - self.dispatching.set(false); self.stop_propagation.set(false); self.stop_immediate.set(false); @@ -483,6 +486,8 @@ fn dispatch_to_listeners(event: &Event, target: &EventTarget, event_path: &[&Eve return; } } + + //Step 9.6.1 } // https://dom.spec.whatwg.org/#concept-event-listener-invoke diff --git a/components/script/dom/htmlbuttonelement.rs b/components/script/dom/htmlbuttonelement.rs index 96b51b472c45..b2fdf3d090b2 100755 --- a/components/script/dom/htmlbuttonelement.rs +++ b/components/script/dom/htmlbuttonelement.rs @@ -2,7 +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::activation::{synthetic_click_activation, Activatable, ActivationSource}; +use crate::dom::activation::{synthetic_click_activation, Activatable}; use crate::dom::attr::Attr; use crate::dom::bindings::codegen::Bindings::HTMLButtonElementBinding; use crate::dom::bindings::codegen::Bindings::HTMLButtonElementBinding::HTMLButtonElementMethods; @@ -343,7 +343,7 @@ impl Activatable for HTMLButtonElement { shift_key, alt_key, meta_key, - ActivationSource::NotFromClick, + false, ) }); } diff --git a/components/script/dom/htmlelement.rs b/components/script/dom/htmlelement.rs index 50786f8aac99..5d19d52e9aa4 100644 --- a/components/script/dom/htmlelement.rs +++ b/components/script/dom/htmlelement.rs @@ -2,7 +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::activation::{synthetic_click_activation, ActivationSource}; +use crate::dom::activation::{synthetic_click_activation}; use crate::dom::attr::Attr; use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods; use crate::dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull; @@ -361,14 +361,7 @@ impl HTMLElementMethods for HTMLElement { // https://html.spec.whatwg.org/multipage/#dom-click fn Click(&self) { if !self.upcast::().disabled_state() { - synthetic_click_activation( - self.upcast::(), - false, - false, - false, - false, - ActivationSource::FromClick, - ) + synthetic_click_activation(self.upcast::(), false, false, false, false, true) } } diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs index 4541c38038e0..8ca78652e923 100755 --- a/components/script/dom/htmlinputelement.rs +++ b/components/script/dom/htmlinputelement.rs @@ -2,7 +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::activation::{synthetic_click_activation, Activatable, ActivationSource}; +use crate::dom::activation::{synthetic_click_activation, Activatable}; use crate::dom::attr::Attr; use crate::dom::bindings::cell::DomRefCell; use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods; @@ -1759,7 +1759,7 @@ impl Activatable for HTMLInputElement { shift_key, alt_key, meta_key, - ActivationSource::NotFromClick, + false, ) } }, diff --git a/components/script/dom/htmllabelelement.rs b/components/script/dom/htmllabelelement.rs index 3c9106a589a7..33e4654edb49 100644 --- a/components/script/dom/htmllabelelement.rs +++ b/components/script/dom/htmllabelelement.rs @@ -2,7 +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::activation::{synthetic_click_activation, Activatable, ActivationSource}; +use crate::dom::activation::{synthetic_click_activation, Activatable}; use crate::dom::attr::Attr; use crate::dom::bindings::codegen::Bindings::HTMLLabelElementBinding; use crate::dom::bindings::codegen::Bindings::HTMLLabelElementBinding::HTMLLabelElementMethods; @@ -73,14 +73,7 @@ impl Activatable for HTMLLabelElement { fn activation_behavior(&self, _event: &Event, _target: &EventTarget) { if let Some(e) = self.GetControl() { let elem = e.upcast::(); - synthetic_click_activation( - elem, - false, - false, - false, - false, - ActivationSource::NotFromClick, - ); + synthetic_click_activation(elem, false, false, false, false, false); } } diff --git a/components/script/dom/mouseevent.rs b/components/script/dom/mouseevent.rs index e9710128e0d1..7a53e2fca808 100644 --- a/components/script/dom/mouseevent.rs +++ b/components/script/dom/mouseevent.rs @@ -34,6 +34,7 @@ pub struct MouseEvent { button: Cell, related_target: MutNullableDom, point_in_target: Cell>>, + // TODO Composed Flag (Step 8 of https://html.spec.whatwg.org/multipage/#fire-a-synthetic-mouse-event) } impl MouseEvent {