aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/textcontrol.rs
blob: 0b8870b1996a7dab98072a7a479f00e8ba29f4ae (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
/* 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::cell::DomRefCell;
use dom::bindings::conversions::DerivedFrom;
use dom::bindings::str::DOMString;
use dom::event::{EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use dom::node::{Node, NodeDamage, window_from_node};
use script_traits::ScriptToConstellationChan;
use textinput::{SelectionDirection, TextInput};

pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
    fn textinput(&self) -> &DomRefCell<TextInput<ScriptToConstellationChan>>;

    // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
    fn dom_selection_start(&self) -> u32 {
        self.textinput().borrow().get_selection_start()
    }

    // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
    fn set_dom_selection_start(&self, start: u32) {
        self.set_selection_range(start, self.dom_selection_end(), self.selection_direction());
    }

    // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend
    fn dom_selection_end(&self) -> u32 {
        self.textinput().borrow().get_absolute_insertion_point() as u32
    }

    // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend
    fn set_dom_selection_end(&self, end: u32) {
        self.set_selection_range(self.dom_selection_start(), end, self.selection_direction());
    }

    // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection
    fn dom_selection_direction(&self) -> DOMString {
        DOMString::from(self.selection_direction())
    }

    // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection
    fn set_dom_selection_direction(&self, direction: DOMString) {
        self.textinput().borrow_mut().selection_direction = SelectionDirection::from(direction);
    }

    // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange
    fn set_dom_selection_range(&self, start: u32, end: u32, direction: Option<DOMString>) {
        // Step 4
        let direction = direction.map_or(SelectionDirection::None, |d| SelectionDirection::from(d));

        self.set_selection_range(start, end, direction);
    }

    fn selection_direction(&self) -> SelectionDirection {
        self.textinput().borrow().selection_direction
    }

    // https://html.spec.whatwg.org/multipage/#set-the-selection-range
    fn set_selection_range(&self, start: u32, end: u32, direction: SelectionDirection) {
        // Step 5
        self.textinput().borrow_mut().selection_direction = direction;

        // Step 3
        self.textinput().borrow_mut().set_selection_range(start, end);

        // Step 6
        let window = window_from_node(self);
        let _ = window.user_interaction_task_source().queue_event(
            &self.upcast::<EventTarget>(),
            atom!("select"),
            EventBubbles::Bubbles,
            EventCancelable::NotCancelable,
            &window);

        self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
    }
}