keyboard_types/
webdriver.rs

1//! Keyboard related WebDriver functionality.
2//!
3//! The low-level `dispatch_keydown` and `dispatch_keyup` API
4//! creates keyboard events from WebDriver codes. It is used
5//! in the *Perform Actions* API.
6//!
7//! ```rust
8//! # extern crate keyboard_types;
9//! # use keyboard_types::*;
10//! # use keyboard_types::webdriver::*;
11//! let mut state = KeyInputState::new();
12//! let mut keyboard_event = state.dispatch_keydown('a');
13//! assert_eq!(keyboard_event.state, KeyState::Down);
14//! assert_eq!(keyboard_event.key, Key::Character("a".to_owned()));
15//! assert_eq!(keyboard_event.code, Code::KeyA);
16//!
17//! // The `\u{E029}` code is the WebDriver id for the Numpad divide key.
18//! keyboard_event = state.dispatch_keydown('\u{E050}');
19//! assert_eq!(keyboard_event.key, Key::Shift);
20//! assert_eq!(keyboard_event.code, Code::ShiftRight);
21//! assert_eq!(keyboard_event.location, Location::Right);
22//!
23//! keyboard_event = state.dispatch_keyup('\u{E050}').expect("key is released");
24//! keyboard_event = state.dispatch_keyup('a').expect("key is released");
25//! ```
26//!
27//! The higher level `send_keys` function is used for the *Element Send Keys*
28//! WebDriver API. It accepts a string and returns a sequence of `KeyboardEvent`
29//! and `CompositionEvent` values.
30//!
31//! ```rust
32//! # extern crate keyboard_types;
33//! # use keyboard_types::*;
34//! # use keyboard_types::webdriver::*;
35//! let events = send_keys("Hello world!\u{E006}");
36//! println!("{:#?}", events);
37//!
38//! let events = send_keys("A\u{0308}");
39//! println!("{:#?}", events);
40//! ```
41//!
42//! Specification: <https://w3c.github.io/webdriver/>
43
44use std::collections::HashSet;
45
46use unicode_segmentation::UnicodeSegmentation;
47
48use crate::first_char;
49use crate::{Code, Key, KeyState, KeyboardEvent, Location, Modifiers};
50use crate::{CompositionEvent, CompositionState};
51
52// Spec: <https://w3c.github.io/webdriver/#keyboard-actions>
53// normalised (sic) as in british spelling
54fn normalised_key_value(raw_key: char) -> Key {
55    match raw_key {
56        '\u{E000}' => Key::Unidentified,
57        '\u{E001}' => Key::Cancel,
58        '\u{E002}' => Key::Help,
59        '\u{E003}' => Key::Backspace,
60        '\u{E004}' => Key::Tab,
61        '\u{E005}' => Key::Clear,
62        // FIXME: spec says "Return"
63        '\u{E006}' => Key::Enter,
64        '\u{E007}' => Key::Enter,
65        '\u{E008}' => Key::Shift,
66        '\u{E009}' => Key::Control,
67        '\u{E00A}' => Key::Alt,
68        '\u{E00B}' => Key::Pause,
69        '\u{E00C}' => Key::Escape,
70        '\u{E00D}' => Key::Character(" ".to_string()),
71        '\u{E00E}' => Key::PageUp,
72        '\u{E00F}' => Key::PageDown,
73        '\u{E010}' => Key::End,
74        '\u{E011}' => Key::Home,
75        '\u{E012}' => Key::ArrowLeft,
76        '\u{E013}' => Key::ArrowUp,
77        '\u{E014}' => Key::ArrowRight,
78        '\u{E015}' => Key::ArrowDown,
79        '\u{E016}' => Key::Insert,
80        '\u{E017}' => Key::Delete,
81        '\u{E018}' => Key::Character(";".to_string()),
82        '\u{E019}' => Key::Character("=".to_string()),
83        '\u{E01A}' => Key::Character("0".to_string()),
84        '\u{E01B}' => Key::Character("1".to_string()),
85        '\u{E01C}' => Key::Character("2".to_string()),
86        '\u{E01D}' => Key::Character("3".to_string()),
87        '\u{E01E}' => Key::Character("4".to_string()),
88        '\u{E01F}' => Key::Character("5".to_string()),
89        '\u{E020}' => Key::Character("6".to_string()),
90        '\u{E021}' => Key::Character("7".to_string()),
91        '\u{E022}' => Key::Character("8".to_string()),
92        '\u{E023}' => Key::Character("9".to_string()),
93        '\u{E024}' => Key::Character("*".to_string()),
94        '\u{E025}' => Key::Character("+".to_string()),
95        '\u{E026}' => Key::Character(",".to_string()),
96        '\u{E027}' => Key::Character("-".to_string()),
97        '\u{E028}' => Key::Character(".".to_string()),
98        '\u{E029}' => Key::Character("/".to_string()),
99        '\u{E031}' => Key::F1,
100        '\u{E032}' => Key::F2,
101        '\u{E033}' => Key::F3,
102        '\u{E034}' => Key::F4,
103        '\u{E035}' => Key::F5,
104        '\u{E036}' => Key::F6,
105        '\u{E037}' => Key::F7,
106        '\u{E038}' => Key::F8,
107        '\u{E039}' => Key::F9,
108        '\u{E03A}' => Key::F10,
109        '\u{E03B}' => Key::F11,
110        '\u{E03C}' => Key::F12,
111        '\u{E03D}' => Key::Meta,
112        '\u{E040}' => Key::ZenkakuHankaku,
113        '\u{E050}' => Key::Shift,
114        '\u{E051}' => Key::Control,
115        '\u{E052}' => Key::Alt,
116        '\u{E053}' => Key::Meta,
117        '\u{E054}' => Key::PageUp,
118        '\u{E055}' => Key::PageDown,
119        '\u{E056}' => Key::End,
120        '\u{E057}' => Key::Home,
121        '\u{E058}' => Key::ArrowLeft,
122        '\u{E059}' => Key::ArrowUp,
123        '\u{E05A}' => Key::ArrowRight,
124        '\u{E05B}' => Key::ArrowDown,
125        '\u{E05C}' => Key::Insert,
126        '\u{E05D}' => Key::Delete,
127        _ => Key::Character(raw_key.to_string()),
128    }
129}
130
131/// Spec: <https://w3c.github.io/webdriver/#dfn-code>
132fn code(raw_key: char) -> Code {
133    match raw_key {
134        '`' | '~' => Code::Backquote,
135        '\\' | '|' => Code::Backslash,
136        '\u{E003}' => Code::Backspace,
137        '[' | '{' => Code::BracketLeft,
138        ']' | '}' => Code::BracketRight,
139        ',' | '<' => Code::Comma,
140        '0' | ')' => Code::Digit0,
141        '1' | '!' => Code::Digit1,
142        '2' | '@' => Code::Digit2,
143        '3' | '#' => Code::Digit3,
144        '4' | '$' => Code::Digit4,
145        '5' | '%' => Code::Digit5,
146        '6' | '^' => Code::Digit6,
147        '7' | '&' => Code::Digit7,
148        '8' | '*' => Code::Digit8,
149        '9' | '(' => Code::Digit9,
150        '=' | '+' => Code::Equal,
151        // FIXME: spec has '<' | '>' => Code::IntlBackslash,
152        'a' | 'A' => Code::KeyA,
153        'b' | 'B' => Code::KeyB,
154        'c' | 'C' => Code::KeyC,
155        'd' | 'D' => Code::KeyD,
156        'e' | 'E' => Code::KeyE,
157        'f' | 'F' => Code::KeyF,
158        'g' | 'G' => Code::KeyG,
159        'h' | 'H' => Code::KeyH,
160        'i' | 'I' => Code::KeyI,
161        'j' | 'J' => Code::KeyJ,
162        'k' | 'K' => Code::KeyK,
163        'l' | 'L' => Code::KeyL,
164        'm' | 'M' => Code::KeyM,
165        'n' | 'N' => Code::KeyN,
166        'o' | 'O' => Code::KeyO,
167        'p' | 'P' => Code::KeyP,
168        'q' | 'Q' => Code::KeyQ,
169        'r' | 'R' => Code::KeyR,
170        's' | 'S' => Code::KeyS,
171        't' | 'T' => Code::KeyT,
172        'u' | 'U' => Code::KeyU,
173        'v' | 'V' => Code::KeyV,
174        'w' | 'W' => Code::KeyW,
175        'x' | 'X' => Code::KeyX,
176        'y' | 'Y' => Code::KeyY,
177        'z' | 'Z' => Code::KeyZ,
178        '-' | '_' => Code::Minus,
179        '.' | '>' => Code::Period,
180        '\'' | '"' => Code::Quote,
181        ';' | ':' => Code::Semicolon,
182        '/' | '?' => Code::Slash,
183        '\u{E00A}' => Code::AltLeft,
184        '\u{E052}' => Code::AltRight,
185        '\u{E009}' => Code::ControlLeft,
186        '\u{E051}' => Code::ControlRight,
187        '\u{E006}' => Code::Enter,
188        // FIXME: spec says "OSLeft"
189        '\u{E03D}' => Code::MetaLeft,
190        // FIXME: spec says "OSRight"
191        '\u{E053}' => Code::MetaRight,
192        '\u{E008}' => Code::ShiftLeft,
193        '\u{E050}' => Code::ShiftRight,
194        ' ' | '\u{E00D}' => Code::Space,
195        '\u{E004}' => Code::Tab,
196        '\u{E017}' => Code::Delete,
197        '\u{E010}' => Code::End,
198        '\u{E002}' => Code::Help,
199        '\u{E011}' => Code::Home,
200        '\u{E016}' => Code::Insert,
201        // FIXME: spec says '\u{E01E}' => Code::PageDown, which is Numpad 4
202        '\u{E00F}' => Code::PageDown,
203        // FIXME: spec says '\u{E01F}' => Code::PageUp, which is Numpad 5
204        '\u{E00E}' => Code::PageUp,
205        '\u{E015}' => Code::ArrowDown,
206        '\u{E012}' => Code::ArrowLeft,
207        '\u{E014}' => Code::ArrowRight,
208        '\u{E013}' => Code::ArrowUp,
209        '\u{E00C}' => Code::Escape,
210        '\u{E031}' => Code::F1,
211        '\u{E032}' => Code::F2,
212        '\u{E033}' => Code::F3,
213        '\u{E034}' => Code::F4,
214        '\u{E035}' => Code::F5,
215        '\u{E036}' => Code::F6,
216        '\u{E037}' => Code::F7,
217        '\u{E038}' => Code::F8,
218        '\u{E039}' => Code::F9,
219        '\u{E03A}' => Code::F10,
220        '\u{E03B}' => Code::F11,
221        '\u{E03C}' => Code::F12,
222        '\u{E01A}' | '\u{E05C}' => Code::Numpad0,
223        '\u{E01B}' | '\u{E056}' => Code::Numpad1,
224        '\u{E01C}' | '\u{E05B}' => Code::Numpad2,
225        '\u{E01D}' | '\u{E055}' => Code::Numpad3,
226        '\u{E01E}' | '\u{E058}' => Code::Numpad4,
227        '\u{E01F}' => Code::Numpad5,
228        '\u{E020}' | '\u{E05A}' => Code::Numpad6,
229        '\u{E021}' | '\u{E057}' => Code::Numpad7,
230        '\u{E022}' | '\u{E059}' => Code::Numpad8,
231        '\u{E023}' | '\u{E054}' => Code::Numpad9,
232        // FIXME: spec says uE024
233        '\u{E025}' => Code::NumpadAdd,
234        '\u{E026}' => Code::NumpadComma,
235        '\u{E028}' | '\u{E05D}' => Code::NumpadDecimal,
236        '\u{E029}' => Code::NumpadDivide,
237        '\u{E007}' => Code::NumpadEnter,
238        '\u{E024}' => Code::NumpadMultiply,
239        // FIXME: spec says uE026
240        '\u{E027}' => Code::NumpadSubtract,
241        _ => Code::Unidentified,
242    }
243}
244
245fn is_shifted_character(raw_key: char) -> bool {
246    matches!(
247        raw_key,
248        '~' | '|'
249            | '{'
250            | '}'
251            | '<'
252            | ')'
253            | '!'
254            | '@'
255            | '#'
256            | '$'
257            | '%'
258            | '^'
259            | '&'
260            | '*'
261            | '('
262            | '+'
263            | '>'
264            | '_'
265            | '\"'
266            | ':'
267            | '?'
268            | '\u{E00D}'
269            | '\u{E05C}'
270            | '\u{E056}'
271            | '\u{E05B}'
272            | '\u{E055}'
273            | '\u{E058}'
274            | '\u{E05A}'
275            | '\u{E057}'
276            | '\u{E059}'
277            | '\u{E054}'
278            | '\u{E05D}'
279            | 'A'..='Z'
280    )
281}
282
283fn key_location(raw_key: char) -> Location {
284    match raw_key {
285        '\u{E007}'..='\u{E00A}' => Location::Left,
286        '\u{E01A}'..='\u{E029}' => Location::Numpad,
287        '\u{E03D}' => Location::Left,
288        '\u{E050}'..='\u{E053}' => Location::Right,
289        '\u{E054}'..='\u{E05D}' => Location::Numpad,
290        _ => Location::Standard,
291    }
292}
293
294fn get_modifier(key: &Key) -> Modifiers {
295    match key {
296        Key::Alt => Modifiers::ALT,
297        Key::Shift => Modifiers::SHIFT,
298        Key::Control => Modifiers::CONTROL,
299        Key::Meta => Modifiers::META,
300        _ => Modifiers::empty(),
301    }
302}
303
304/// Store pressed keys and modifiers.
305///
306/// Spec: <https://w3c.github.io/webdriver/#dfn-key-input-state>
307#[derive(Clone, Debug, Default)]
308#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
309pub struct KeyInputState {
310    pressed: HashSet<Key>,
311    modifiers: Modifiers,
312}
313
314impl KeyInputState {
315    /// New state without any keys or modifiers pressed.
316    ///
317    /// Same as the default value.
318    pub fn new() -> KeyInputState {
319        KeyInputState::default()
320    }
321
322    /// Get a keyboard-keydown event from a WebDriver key value.
323    ///
324    /// Stores that the key is pressed in the state object.
325    ///
326    /// The input cancel list is not implemented here but can be emulated
327    /// by adding the `raw_key` value with a `keyUp` action to a list
328    /// before executing this function.
329    ///
330    /// Specification: <https://w3c.github.io/webdriver/#dfn-dispatch-a-keydown-action>
331    pub fn dispatch_keydown(&mut self, raw_key: char) -> KeyboardEvent {
332        let key = normalised_key_value(raw_key);
333        let repeat = self.pressed.contains(&key);
334        let code = code(raw_key);
335        let location = key_location(raw_key);
336        self.modifiers.insert(get_modifier(&key));
337        self.pressed.insert(key.clone());
338        KeyboardEvent {
339            state: KeyState::Down,
340            key,
341            code,
342            location,
343            modifiers: self.modifiers,
344            repeat,
345            is_composing: false,
346        }
347    }
348
349    /// Get a keyboard-keyup event from a WebDriver key value.
350    ///
351    /// Updates state. Returns `None` if the key is not listed as pressed.
352    ///
353    /// Specification: <https://w3c.github.io/webdriver/#dfn-dispatch-a-keyup-action>
354    pub fn dispatch_keyup(&mut self, raw_key: char) -> Option<KeyboardEvent> {
355        let key = normalised_key_value(raw_key);
356        if !self.pressed.contains(&key) {
357            return None;
358        }
359        let code = code(raw_key);
360        let location = key_location(raw_key);
361        self.modifiers.remove(get_modifier(&key));
362        self.pressed.remove(&key);
363        Some(KeyboardEvent {
364            state: KeyState::Up,
365            key,
366            code,
367            location,
368            modifiers: self.modifiers,
369            repeat: false,
370            is_composing: false,
371        })
372    }
373
374    fn clear(&mut self, undo_actions: &mut HashSet<char>, result: &mut Vec<Event>) {
375        let mut actions: Vec<_> = undo_actions.drain().collect();
376        actions.sort_unstable();
377        for action in actions {
378            result.push(self.dispatch_keyup(action).unwrap().into())
379        }
380        assert!(undo_actions.is_empty());
381    }
382
383    fn dispatch_typeable(&mut self, text: &mut String, result: &mut Vec<Event>) {
384        for character in text.chars() {
385            let shifted = self.modifiers.contains(Modifiers::SHIFT);
386            if is_shifted_character(character) && !shifted {
387                // dispatch left shift down
388                result.push(self.dispatch_keydown('\u{E008}').into());
389            }
390            if !is_shifted_character(character) && shifted {
391                // dispatch left shift up
392                result.push(self.dispatch_keyup('\u{E008}').unwrap().into());
393            }
394            result.push(self.dispatch_keydown(character).into());
395            result.push(self.dispatch_keyup(character).unwrap().into());
396        }
397        text.clear();
398    }
399}
400
401/// Either a `KeyboardEvent` or a `CompositionEvent`.
402///
403/// Returned by the `send_keys` function.
404#[derive(Clone, Eq, PartialEq, Hash, Debug)]
405#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
406pub enum Event {
407    Keyboard(KeyboardEvent),
408    Composition(CompositionEvent),
409}
410
411impl From<KeyboardEvent> for Event {
412    fn from(v: KeyboardEvent) -> Event {
413        Event::Keyboard(v)
414    }
415}
416
417impl From<CompositionEvent> for Event {
418    fn from(v: CompositionEvent) -> Event {
419        Event::Composition(v)
420    }
421}
422
423/// Compute the events resulting from a WebDriver *Element Send Keys* command.
424///
425/// Spec: <https://w3c.github.io/webdriver/#element-send-keys>
426pub fn send_keys(text: &str) -> Vec<Event> {
427    fn is_modifier(text: &str) -> bool {
428        if text.chars().count() != 1 {
429            return false;
430        }
431        // values from <https://www.w3.org/TR/uievents-key/#keys-modifier>
432        matches!(
433            normalised_key_value(first_char(text)),
434            Key::Alt
435                | Key::AltGraph
436                | Key::CapsLock
437                | Key::Control
438                | Key::Fn
439                | Key::FnLock
440                | Key::Meta
441                | Key::NumLock
442                | Key::ScrollLock
443                | Key::Shift
444                | Key::Symbol
445                | Key::SymbolLock
446                | Key::Hyper
447                | Key::Super
448        )
449    }
450
451    /// Spec: <https://w3c.github.io/webdriver/#dfn-typeable>
452    fn is_typeable(text: &str) -> bool {
453        text.chars().count() == 1
454    }
455
456    let mut result = Vec::new();
457    let mut typeable_text = String::new();
458    let mut state = KeyInputState::new();
459    let mut undo_actions = HashSet::new();
460    for cluster in UnicodeSegmentation::graphemes(text, true) {
461        match cluster {
462            "\u{E000}" => {
463                state.dispatch_typeable(&mut typeable_text, &mut result);
464                state.clear(&mut undo_actions, &mut result);
465            }
466            s if is_modifier(s) => {
467                state.dispatch_typeable(&mut typeable_text, &mut result);
468                let raw_modifier = first_char(s);
469                result.push(state.dispatch_keydown(raw_modifier).into());
470                undo_actions.insert(raw_modifier);
471            }
472            s if is_typeable(s) => typeable_text.push_str(s),
473            s => {
474                state.dispatch_typeable(&mut typeable_text, &mut result);
475                // FIXME: Spec says undefined instead of empty string
476                result.push(
477                    CompositionEvent {
478                        state: CompositionState::Start,
479                        data: String::new(),
480                    }
481                    .into(),
482                );
483                result.push(
484                    CompositionEvent {
485                        state: CompositionState::Update,
486                        data: s.to_owned(),
487                    }
488                    .into(),
489                );
490                result.push(
491                    CompositionEvent {
492                        state: CompositionState::End,
493                        data: s.to_owned(),
494                    }
495                    .into(),
496                );
497            }
498        }
499    }
500    state.dispatch_typeable(&mut typeable_text, &mut result);
501    state.clear(&mut undo_actions, &mut result);
502    result
503}