aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/activation.rs
blob: 6fd1882c579af090c48fa3f4981dadf42fb3ea69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* 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::EventBinding::EventMethods;
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use dom::mouseevent::MouseEvent;
use dom::node::window_from_node;
use util::str::DOMString;

/// Trait for elements with defined activation behavior
pub trait Activatable {
    fn as_element(&self) -> ∈

    // Is this particular instance of the element activatable?
    fn is_instance_activatable(&self) -> bool;

    // https://html.spec.whatwg.org/multipage/#run-pre-click-activation-steps
    fn pre_click_activation(&self);

    // https://html.spec.whatwg.org/multipage/#run-canceled-activation-steps
    fn canceled_activation(&self);

    // https://html.spec.whatwg.org/multipage/#run-post-click-activation-steps
    fn activation_behavior(&self, event: &Event, target: &EventTarget);

    // https://html.spec.whatwg.org/multipage/#implicit-submission
    fn implicit_submission(&self, ctrlKey: bool, shiftKey: bool, altKey: bool, metaKey: bool);

    // https://html.spec.whatwg.org/multipage/#run-synthetic-click-activation-steps
    fn synthetic_click_activation(&self,
                                  ctrlKey: bool,
                                  shiftKey: bool,
                                  altKey: bool,
                                  metaKey: bool) {
        let element = self.as_element();
        // Step 1
        if element.click_in_progress() {
            return;
        }
        // Step 2
        element.set_click_in_progress(true);
        // Step 3
        self.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();
        let mouse = MouseEvent::new(win.r(),
                                    DOMString::from("click"),
                                    EventBubbles::DoesNotBubble,
                                    EventCancelable::NotCancelable,
                                    Some(win.r()),
                                    1,
                                    0,
                                    0,
                                    0,
                                    0,
                                    ctrlKey,
                                    shiftKey,
                                    altKey,
                                    metaKey,
                                    0,
                                    None);
        let event = mouse.upcast::<Event>();
        event.fire(target);

        // Step 5
        if event.DefaultPrevented() {
            self.canceled_activation();
        } else {
            // post click activation
            self.activation_behavior(event, target);
        }

        // Step 6
        element.set_click_in_progress(false);
    }
}