servo-script 0.7.0

A component of the servo web-engine.
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/* 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 https://mozilla.org/MPL/2.0/. */

//! Common handling of keyboard input and state management for text input controls

use std::default::Default;
use std::ops::Range;

use app_units::Au;
use embedder_traits::{
    EditingAction, EditingDirection, EditingMotion, EmbedderMsg, ModifySelection, MouseButton,
    ScriptToEmbedderChan,
};
use script_bindings::codegen::GenericBindings::UIEventBinding::UIEventMethods;
use script_bindings::match_domstring_ascii;
use script_bindings::root::Dom;
use script_bindings::trace::CustomTraceable;
use script_traits::MouseButtons;
use servo_base::generic_channel::GenericCallback;
use servo_base::id::WebViewId;
use servo_base::text::{AssumeUnder4GB, RangeAny, Utf8CodeUnits, Utf16CodeUnits, Utf32CodeUnits};
use servo_base::{Rope, RopeIndex, RopeMovement, RopeSlice};

use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::str::DOMString;
use crate::dom::compositionevent::CompositionEvent;
use crate::dom::event::Event;
use crate::dom::inputevent::HitTestResult;
use crate::dom::mouseevent::MouseEvent;
use crate::dom::text_control::TextControlElement;
use crate::dom::types::{HTMLInputElement, HTMLTextAreaElement, UIEvent};
use crate::dom::{Element, NodeTraits};
use crate::drag::drag_gesture::{DragGesture, DragHandler};

/// A trait which abstracts access to the embedder's clipboard in order to allow unit
/// testing clipboard-dependent parts of `script`.
pub trait ClipboardProvider {
    /// Get the text content of the clipboard.
    fn get_text(&mut self) -> Result<String, String>;
    /// Set the text content of the clipboard.
    fn set_text(&mut self, _: String);
}

#[derive(MallocSizeOf)]
pub(crate) struct EmbedderClipboardProvider {
    pub embedder_sender: ScriptToEmbedderChan,
    pub webview_id: WebViewId,
}

impl ClipboardProvider for EmbedderClipboardProvider {
    fn get_text(&mut self) -> Result<String, String> {
        let (callback, rx) = GenericCallback::new_blocking().unwrap();
        self.embedder_sender
            .send(EmbedderMsg::GetClipboardText(self.webview_id, callback))
            .unwrap();
        rx.recv().unwrap()
    }
    fn set_text(&mut self, s: String) {
        self.embedder_sender
            .send(EmbedderMsg::SetClipboardText(self.webview_id, s))
            .unwrap();
    }
}

#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
pub enum SelectionDirection {
    Forward,
    Backward,
    None,
}

impl From<DOMString> for SelectionDirection {
    fn from(direction: DOMString) -> SelectionDirection {
        match_domstring_ascii!(direction,
            "forward" => SelectionDirection::Forward,
            "backward" => SelectionDirection::Backward,
            _ => SelectionDirection::None,
        )
    }
}

impl From<SelectionDirection> for DOMString {
    fn from(direction: SelectionDirection) -> DOMString {
        match direction {
            SelectionDirection::Forward => DOMString::from_static("forward"),
            SelectionDirection::Backward => DOMString::from_static("backward"),
            SelectionDirection::None => DOMString::from_static("none"),
        }
    }
}

#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
pub enum Lines {
    Single,
    Multiple,
}

impl Lines {
    fn normalize(&self, contents: impl Into<String>) -> String {
        let contents = contents.into().replace("\r\n", "\n");
        match self {
            Self::Multiple => {
                // https://html.spec.whatwg.org/multipage/#textarea-line-break-normalisation-transformation
                contents.replace("\r", "\n")
            },
            // https://infra.spec.whatwg.org/#strip-newlines
            //
            // Browsers generally seem to convert newlines to spaces, so we do the same.
            Lines::Single => contents.replace(['\r', '\n'], " "),
        }
    }
}

#[derive(Clone, Copy, PartialEq)]
pub(crate) struct SelectionState {
    start: RopeIndex,
    end: RopeIndex,
    direction: SelectionDirection,
}

/// Encapsulated state for handling keyboard input in a single or multiline text input control.
#[derive(JSTraceable, MallocSizeOf)]
pub struct TextInput<T: ClipboardProvider> {
    #[no_trace]
    rope: Rope,

    /// The type of [`TextInput`] this is. When in multi-line mode, the [`TextInput`] will
    /// automatically split all inserted text into lines and incorporate them into
    /// the [`Self::rope`]. When in single line mode, the inserted text will be stripped of
    /// newlines.
    mode: Lines,

    /// Current cursor input point
    #[no_trace]
    edit_point: RopeIndex,

    /// The current selection goes from the selection_origin until the edit_point. Note that the
    /// selection_origin may be after the edit_point, in the case of a backward selection.
    #[no_trace]
    selection_origin: Option<RopeIndex>,
    selection_direction: SelectionDirection,

    #[ignore_malloc_size_of = "Can't easily measure this generic type"]
    clipboard_provider: T,

    /// The maximum number of UTF-16 code units this text input is allowed to hold.
    ///
    /// <https://html.spec.whatwg.org/multipage/#attr-fe-maxlength>
    max_length: Option<Utf16CodeUnits>,
    min_length: Option<Utf16CodeUnits>,

    /// Was last change made by set_content?
    was_last_change_by_set_content: bool,

    #[no_trace]
    pub(crate) previous_selection_range: Range<RopeIndex>,
    #[no_trace]
    pub(crate) selection_for_layout: Option<RangeAny<Utf32CodeUnits>>,
}

#[derive(Clone, Copy, PartialEq)]
pub enum IsComposing {
    Composing,
    NotComposing,
}

impl From<IsComposing> for bool {
    fn from(is_composing: IsComposing) -> Self {
        match is_composing {
            IsComposing::Composing => true,
            IsComposing::NotComposing => false,
        }
    }
}

/// <https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes>
#[derive(Clone, Copy, PartialEq)]
pub enum InputEventType {
    InsertText,
    InsertLineBreak,
    InsertFromPaste,
    InsertCompositionText,
    DeleteByCut,
    DeleteContentBackward,
    DeleteContentForward,
    Nothing,
}

impl InputEventType {
    pub(crate) fn as_str(&self) -> &str {
        match *self {
            Self::InsertText => "insertText",
            Self::InsertLineBreak => "insertLineBreak",
            Self::InsertFromPaste => "insertFromPaste",
            Self::InsertCompositionText => "insertCompositionText",
            Self::DeleteByCut => "deleteByCut",
            Self::DeleteContentBackward => "deleteContentBackward",
            Self::DeleteContentForward => "deleteContentForward",
            Self::Nothing => "",
        }
    }
}

/// Resulting action to be taken by the owner of a text input that is handling an event.
#[derive(PartialEq)]
pub enum KeyReaction {
    TriggerDefaultAction,
    DispatchInput(Option<String>, IsComposing, InputEventType),
    RedrawSelection,
    Nothing,
}

impl<T: ClipboardProvider> TextInput<T> {
    /// Instantiate a new text input control
    pub fn new(lines: Lines, initial: DOMString, clipboard_provider: T) -> TextInput<T> {
        Self {
            rope: Rope::new(initial),
            mode: lines,
            edit_point: Default::default(),
            selection_origin: None,
            clipboard_provider,
            max_length: Default::default(),
            min_length: Default::default(),
            selection_direction: SelectionDirection::None,
            was_last_change_by_set_content: true,
            previous_selection_range: Default::default(),
            selection_for_layout: None,
        }
    }

    pub fn edit_point(&self) -> RopeIndex {
        self.edit_point
    }

    pub fn selection_origin(&self) -> Option<RopeIndex> {
        self.selection_origin
    }

    /// The selection origin, or the edit point if there is no selection. Note that the selection
    /// origin may be after the edit point, in the case of a backward selection.
    pub fn selection_origin_or_edit_point(&self) -> RopeIndex {
        self.selection_origin.unwrap_or(self.edit_point)
    }

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

    pub fn set_max_length(&mut self, length: Option<Utf16CodeUnits>) {
        self.max_length = length;
    }

    pub fn set_min_length(&mut self, length: Option<Utf16CodeUnits>) {
        self.min_length = length;
    }

    /// Was last edit made by set_content?
    pub(crate) fn was_last_change_by_set_content(&self) -> bool {
        self.was_last_change_by_set_content
    }

    /// If there is an uncollapsed selection, delete it, otherwise do nothing. Returns
    /// true if any text was deleted.
    pub(crate) fn delete_selection(&mut self) -> bool {
        if self.selection_start() == self.selection_end() {
            return false;
        }
        self.replace_selection(&DOMString::new());
        true
    }

    /// If there is an uncollapsed selection, delete it. Otherwise delete the given [`unit`]
    /// worth of text in [`direction`] Remove a character at the current editing point
    ///
    /// Returns true if any text was deleted.
    pub fn delete_unit_or_selection(
        &mut self,
        unit: RopeMovement,
        direction: EditingDirection,
    ) -> bool {
        if !self.has_uncollapsed_selection() {
            let amount = match direction {
                EditingDirection::Forward => 1,
                EditingDirection::Backward => -1,
            };
            self.modify_selection(amount, unit);
        }
        self.delete_selection()
    }

    /// Insert a string at the current editing point or replace the selection if
    /// one exists.
    pub fn insert<S: Into<String>>(&mut self, string: S) {
        if self.selection_origin.is_none() {
            self.selection_origin = Some(self.edit_point);
        }
        self.replace_selection(&DOMString::from(string.into()));
    }

    /// The start of the selection (or the edit point, if there is no selection). Always less than
    /// or equal to selection_end(), regardless of the selection direction.
    pub fn selection_start(&self) -> RopeIndex {
        match self.selection_direction {
            SelectionDirection::None | SelectionDirection::Forward => {
                self.selection_origin_or_edit_point()
            },
            SelectionDirection::Backward => self.edit_point,
        }
    }

    pub(crate) fn selection_start_utf16(&self) -> Utf16CodeUnits {
        self.rope.index_to_utf16_offset(self.selection_start())
    }

    /// The byte offset of the selection_start()
    fn selection_start_offset(&self) -> Utf8CodeUnits {
        self.rope.index_to_utf8_offset(self.selection_start())
    }

    /// The end of the selection (or the edit point, if there is no selection). Always greater
    /// than or equal to selection_start(), regardless of the selection direction.
    pub fn selection_end(&self) -> RopeIndex {
        match self.selection_direction {
            SelectionDirection::None | SelectionDirection::Forward => self.edit_point,
            SelectionDirection::Backward => self.selection_origin_or_edit_point(),
        }
    }

    pub(crate) fn selection_end_utf16(&self) -> Utf16CodeUnits {
        self.rope.index_to_utf16_offset(self.selection_end())
    }

    /// Whether or not there is an active uncollapsed selection. This means that the
    /// selection origin is set and it differs from the edit point.
    #[inline]
    pub(crate) fn has_uncollapsed_selection(&self) -> bool {
        self.selection_origin
            .is_some_and(|selection_origin| selection_origin != self.edit_point)
    }

    /// Return the selection range as UTF-32 offsets from the start of the content.
    ///
    /// If there is no selection, returns an empty range at the edit point.
    ///
    /// If the start or/and end of the range is at the start/end of the text,
    /// return a `RangeAny` unbounded on that side.
    pub(crate) fn sorted_selection_character_offsets_range(&self) -> RangeAny<Utf32CodeUnits> {
        let rope = &self.rope;
        let start = self.selection_start();
        let end = self.selection_end();
        let start = (start != rope.first_index()).then(|| rope.index_to_character_offset(start));
        // TODO: `TextInputWidgetShadowTree::update` has a hack with a "\u{200B}" to force
        // the text to be non-empty, so `rope.last_index()` is untrustworthy.
        // For now, use a bounded end unconditionally instead.
        // let end = (end != rope.last_index()).then(|| rope.index_to_character_offset(end));
        let end = Some(rope.index_to_character_offset(end));
        RangeAny::new(start, end)
    }

    /// The state of the current selection. Can be used to compare whether selection state has changed.
    pub(crate) fn selection_state(&self) -> SelectionState {
        SelectionState {
            start: self.selection_start(),
            end: self.selection_end(),
            direction: self.selection_direction,
        }
    }

    // Check that the selection is valid.
    fn assert_ok_selection(&self) {
        debug!(
            "edit_point: {:?}, selection_origin: {:?}, direction: {:?}",
            self.edit_point, self.selection_origin, self.selection_direction
        );

        debug_assert_eq!(self.edit_point, self.rope.normalize_index(self.edit_point));
        if let Some(selection_origin) = self.selection_origin {
            debug_assert_eq!(
                selection_origin,
                self.rope.normalize_index(selection_origin)
            );
            match self.selection_direction {
                SelectionDirection::None | SelectionDirection::Forward => {
                    debug_assert!(selection_origin <= self.edit_point)
                },
                SelectionDirection::Backward => debug_assert!(self.edit_point <= selection_origin),
            }
        }
    }

    fn selection_slice(&self) -> RopeSlice<'_> {
        self.rope
            .slice(Some(self.selection_start()), Some(self.selection_end()))
    }

    pub(crate) fn selection_content(&self) -> Option<String> {
        let text: String = self.selection_slice().into();
        if text.is_empty() {
            return None;
        }
        Some(text)
    }

    /// The length of the selected text in UTF-16 code units.
    fn selection_utf16_len(&self) -> Utf16CodeUnits {
        self.selection_slice().len_utf16()
    }

    /// Replace the current selection with the given [`DOMString`]. If the [`Rope`] is in
    /// single line mode this *will* strip newlines, as opposed to [`Self::set_content`],
    /// which does not.
    pub fn replace_selection(&mut self, insert: &DOMString) {
        let string_to_insert = if let Some(max_length) = self.max_length {
            let utf16_length_without_selection =
                self.len_utf16().saturating_sub(self.selection_utf16_len());
            let utf16_length_that_can_be_inserted =
                max_length.saturating_sub(utf16_length_without_selection);
            // TODO: ensure that DOMString’s are under 4 GiB?
            let last_char_index = usize::from(
                utf16_length_that_can_be_inserted
                    .to_utf8_code_units_in(AssumeUnder4GB, &insert.str()),
            );
            &insert.str()[..last_char_index]
        } else {
            &insert.str()
        };
        let string_to_insert = self.mode.normalize(string_to_insert);

        let start = self.selection_start();
        let end = self.selection_end();
        let end_index_of_insertion = self.rope.replace_range(start..end, string_to_insert);

        self.was_last_change_by_set_content = false;
        self.clear_selection();
        self.edit_point = end_index_of_insertion;
    }

    pub fn modify_edit_point(&mut self, amount: isize, movement: RopeMovement) {
        if amount == 0 {
            return;
        }

        // When moving by lines or if we do not have a selection, we do actually move
        // the edit point from its position.
        if matches!(movement, RopeMovement::Line) || !self.has_uncollapsed_selection() {
            self.clear_selection();
            self.edit_point = self.rope.move_by(self.edit_point, movement, amount);
            return;
        }

        // If there's a selection and we are moving by words or characters, we just collapse
        // the selection in the direction of the motion.
        let new_edit_point = if amount > 0 {
            self.selection_end()
        } else {
            self.selection_start()
        };
        self.clear_selection();
        self.edit_point = new_edit_point;
    }

    pub fn modify_selection(&mut self, amount: isize, movement: RopeMovement) {
        let old_edit_point = self.edit_point;
        self.edit_point = self.rope.move_by(old_edit_point, movement, amount);

        if self.selection_origin.is_none() {
            self.selection_origin = Some(old_edit_point);
        }
        self.update_selection_direction();
    }

    pub fn modify_selection_or_edit_point(
        &mut self,
        amount: isize,
        movement: RopeMovement,
        update_selection: ModifySelection,
    ) {
        match update_selection {
            ModifySelection::Yes => self.modify_selection(amount, movement),
            ModifySelection::No => self.modify_edit_point(amount, movement),
        }
        self.assert_ok_selection();
    }

    /// Update the field selection_direction.
    ///
    /// When the edit_point (or focus) is before the selection_origin (or anchor)
    /// you have a backward selection. Otherwise you have a forward selection.
    fn update_selection_direction(&mut self) {
        debug!(
            "edit_point: {:?}, selection_origin: {:?}",
            self.edit_point, self.selection_origin
        );
        self.selection_direction = if Some(self.edit_point) < self.selection_origin {
            SelectionDirection::Backward
        } else {
            SelectionDirection::Forward
        }
    }

    /// Deal with a newline input.
    pub fn handle_return(&mut self) -> KeyReaction {
        match self.mode {
            Lines::Multiple => {
                self.insert('\n');
                KeyReaction::DispatchInput(
                    None,
                    IsComposing::NotComposing,
                    InputEventType::InsertLineBreak,
                )
            },
            Lines::Single => KeyReaction::TriggerDefaultAction,
        }
    }

    /// Select all text in the input control.
    pub fn select_all(&mut self) {
        self.selection_origin = Some(RopeIndex::default());
        self.edit_point = self.rope.last_index();
        self.selection_direction = SelectionDirection::Forward;
        self.assert_ok_selection();
    }

    /// Remove the current selection.
    pub fn clear_selection(&mut self) {
        self.selection_origin = None;
        self.selection_direction = SelectionDirection::None;
    }

    /// Remove the current selection and set the edit point to the end of the content.
    pub(crate) fn clear_selection_to_end(&mut self) {
        self.clear_selection();
        self.edit_point = self.rope.last_index();
    }

    pub(crate) fn clear_selection_to_start(&mut self) {
        self.clear_selection();
        self.edit_point = Default::default();
    }

    /// Process a given `EditingAction` and return an action for the caller to execute.
    ///
    /// This is public so that it can be used in external unit tests.
    pub fn perform_editing_action(&mut self, action: EditingAction) -> KeyReaction {
        match action {
            EditingAction::MoveCursor(direction, motion, selection) => {
                let movement = match motion {
                    EditingMotion::Character => RopeMovement::Character,
                    EditingMotion::Grapheme => RopeMovement::Grapheme,
                    EditingMotion::Word => RopeMovement::Word,
                    EditingMotion::Line => RopeMovement::Line,
                    EditingMotion::LineStartOrEnd => RopeMovement::LineStartOrEnd,
                    EditingMotion::Page => RopeMovement::Line,
                    EditingMotion::DocumentStartOrEnd => RopeMovement::RopeStartOrEnd,
                };
                let amount = match (direction, motion) {
                    // TODO: This should really be based on the size of the text area.
                    (EditingDirection::Forward, EditingMotion::Page) => 28,
                    (EditingDirection::Backward, EditingMotion::Page) => -28,
                    (EditingDirection::Forward, _) => 1,
                    (EditingDirection::Backward, _) => -1,
                };
                self.modify_selection_or_edit_point(amount, movement, selection);
                KeyReaction::RedrawSelection
            },
            EditingAction::InsertNewline | EditingAction::InsertParagraph => self.handle_return(),
            EditingAction::InsertText(text) => {
                self.insert(&text);
                KeyReaction::DispatchInput(
                    Some(text),
                    IsComposing::NotComposing,
                    InputEventType::InsertText,
                )
            },
            EditingAction::Delete => {
                if self.delete_unit_or_selection(RopeMovement::Grapheme, EditingDirection::Forward)
                {
                    KeyReaction::DispatchInput(
                        None,
                        IsComposing::NotComposing,
                        InputEventType::DeleteContentForward,
                    )
                } else {
                    KeyReaction::Nothing
                }
            },
            EditingAction::Backspace(motion) => {
                let movement = match motion {
                    EditingMotion::Character => RopeMovement::Character,
                    EditingMotion::Grapheme => RopeMovement::Grapheme,
                    EditingMotion::Word => RopeMovement::Word,
                    EditingMotion::Line => RopeMovement::Line,
                    EditingMotion::LineStartOrEnd => RopeMovement::LineStartOrEnd,
                    EditingMotion::Page => return KeyReaction::Nothing,
                    EditingMotion::DocumentStartOrEnd => RopeMovement::RopeStartOrEnd,
                };
                if self.delete_unit_or_selection(movement, EditingDirection::Backward) {
                    KeyReaction::DispatchInput(
                        None,
                        IsComposing::NotComposing,
                        InputEventType::DeleteContentBackward,
                    )
                } else {
                    KeyReaction::Nothing
                }
            },
            EditingAction::SelectAll | EditingAction::Clipboard(..) => KeyReaction::Nothing,
        }
    }

    pub(crate) fn handle_compositionend(&mut self, event: &CompositionEvent) -> KeyReaction {
        let insertion = event.data().str();
        if insertion.is_empty() {
            self.clear_selection();
            return KeyReaction::RedrawSelection;
        }

        self.insert(insertion.to_string());
        KeyReaction::DispatchInput(
            Some(insertion.to_string()),
            IsComposing::NotComposing,
            InputEventType::InsertCompositionText,
        )
    }

    pub(crate) fn handle_compositionupdate(&mut self, event: &CompositionEvent) -> KeyReaction {
        let insertion = event.data().str();
        if insertion.is_empty() {
            return KeyReaction::Nothing;
        }

        let start = self.selection_start_offset();
        let insertion = insertion.to_string();
        self.insert(insertion.clone());
        self.set_selection_range_utf8(
            start,
            start + event.data().len_utf8(),
            SelectionDirection::Forward,
        );
        KeyReaction::DispatchInput(
            Some(insertion),
            IsComposing::Composing,
            InputEventType::InsertCompositionText,
        )
    }

    fn edit_point_for_hit_test_result(&self, hit_test_result: &HitTestResult) -> RopeIndex {
        hit_test_result
            .dom_position_for_selection
            .as_ref()
            .map(|(_, character_offset)| {
                self.rope.move_by(
                    Default::default(),
                    RopeMovement::Character,
                    character_offset.0 as isize,
                )
            })
            .unwrap_or_else(|| self.rope.last_index())
    }

    fn drag_moved(&mut self, element: &impl TextControlElement, hit_test_result: &HitTestResult) {
        let point_in_viewport = hit_test_result.point_in_frame.map(Au::from_f32_px);
        let element = element.as_element();
        self.edit_point = element
            .owner_window()
            .text_index_query_on_node_for_event(element.upcast(), point_in_viewport)
            .map(|(_, character_offset)| {
                self.rope.move_by(
                    Default::default(),
                    RopeMovement::Character,
                    character_offset.0 as isize,
                )
            })
            .unwrap_or_else(|| self.rope.last_index());

        self.update_selection_direction();
    }

    /// Handle a "mousedown" event that happened on this [`TextInput`], belonging to the
    /// given [`Node`].
    ///
    /// Returns `true` if the [`TextInput`] changed at all or `false` otherwise.
    pub(crate) fn handle_mousedown_event(
        &mut self,
        element: &Element,
        mouse_event: &MouseEvent,
        hit_test_result: &HitTestResult,
    ) -> bool {
        assert_eq!(mouse_event.upcast::<Event>().type_(), atom!("mousedown"));

        let button = mouse_event.button();
        let selection_changed = match mouse_event.upcast::<UIEvent>().Detail() {
            3 if button == MouseButton::Primary => {
                let word_boundaries = self.rope.line_boundaries(self.edit_point);
                self.edit_point = word_boundaries.end;
                self.selection_origin = Some(word_boundaries.start);
                self.update_selection_direction();
                true
            },
            2 if button == MouseButton::Primary => {
                let word_boundaries = self.rope.relevant_word_boundaries(self.edit_point);
                self.edit_point = word_boundaries.end;
                self.selection_origin = Some(word_boundaries.start);
                self.update_selection_direction();
                true
            },
            1 if matches!(button, MouseButton::Primary | MouseButton::Auxiliary) => {
                self.clear_selection();
                self.edit_point = self.edit_point_for_hit_test_result(hit_test_result);
                self.selection_origin = Some(self.edit_point);
                self.update_selection_direction();
                true
            },
            _ => {
                // We currently don't do anything for higher click counts, but some platforms do.
                // We should re-examine this when implementing support for platform-specific editing
                // behaviors.
                false
            },
        };

        if selection_changed && mouse_event.buttons().contains(MouseButtons::Primary) {
            element
                .owner_document()
                .event_handler()
                .install_drag_gesture(DragGesture::new(DragHandler::TextInputSelection(
                    TextInputSelectionDragHandler(Dom::from_ref(element)),
                )));
        }

        selection_changed
    }

    /// Whether the content is empty.
    pub(crate) fn is_empty(&self) -> bool {
        self.rope.is_empty()
    }

    /// The total number of code units required to encode the content in utf16.
    pub(crate) fn len_utf16(&self) -> Utf16CodeUnits {
        self.rope.len_utf16()
    }

    /// Get the current contents of the text input. Multiple lines are joined by \n.
    pub fn get_content(&self) -> DOMString {
        self.rope.contents().into()
    }

    /// Set the current contents of the text input. If this is control supports multiple lines,
    /// any \n encountered will be stripped and force a new logical line.
    ///
    /// Note that when the [`Rope`] is in single line mode, this will **not** strip newlines.
    /// Newline stripping only happens for incremental updates to the [`Rope`] as `<input>`
    /// elements currently need to store unsanitized values while being created.
    pub fn set_content(&mut self, content: DOMString) {
        self.rope = Rope::new(content.str().replace("\r\n", "\n").replace("\r", "\n"));
        self.was_last_change_by_set_content = true;

        self.edit_point = self.rope.normalize_index(self.edit_point());
        self.selection_origin = self
            .selection_origin
            .map(|selection_origin| self.rope.normalize_index(selection_origin));
    }

    pub fn set_selection_range_utf16(
        &mut self,
        start: Utf16CodeUnits,
        end: Utf16CodeUnits,
        direction: SelectionDirection,
    ) {
        self.set_selection_range_utf8(
            self.rope.utf16_offset_to_utf8_offset(start),
            self.rope.utf16_offset_to_utf8_offset(end),
            direction,
        );
    }

    pub fn set_selection_range_utf8(
        &mut self,
        mut start: Utf8CodeUnits,
        mut end: Utf8CodeUnits,
        direction: SelectionDirection,
    ) {
        let text_end = self.get_content().len_utf8();
        if end > text_end {
            end = text_end;
        }
        if start > end {
            start = end;
        }

        self.selection_direction = direction;

        match direction {
            SelectionDirection::None | SelectionDirection::Forward => {
                self.selection_origin = Some(self.rope.utf8_offset_to_rope_index(start));
                self.edit_point = self.rope.utf8_offset_to_rope_index(end);
            },
            SelectionDirection::Backward => {
                self.selection_origin = Some(self.rope.utf8_offset_to_rope_index(end));
                self.edit_point = self.rope.utf8_offset_to_rope_index(start);
            },
        }

        self.assert_ok_selection();
    }
}

#[derive(JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub(crate) struct TextInputSelectionDragHandler(Dom<Element>);

impl TextInputSelectionDragHandler {
    pub(crate) fn still_connected(&self) -> bool {
        self.0.is_connected()
    }

    /// Process a mouse move event on this [`TextInputSelectionDragHandler`].
    ///
    /// Returns `true` if the drag should continue and `false` otherwise.
    pub(crate) fn moved(&self, hit_test_result: &HitTestResult) -> bool {
        if !self.0.is_connected() {
            return false;
        }

        if let Some(input) = self.0.downcast::<HTMLInputElement>() {
            input.text_input_mut().drag_moved(input, hit_test_result);
            input.maybe_update_shared_selection();
            true
        } else if let Some(text_area) = self.0.downcast::<HTMLTextAreaElement>() {
            text_area
                .text_input_mut()
                .drag_moved(text_area, hit_test_result);
            text_area.maybe_update_shared_selection();
            true
        } else {
            false
        }
    }
}