lexical-ui 0.1.2

A Rust port of Lexical's model and headless packages, at parity with the JS implementation.
Documentation
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
//! Ports of `packages/lexical/src/__tests__/unit/HandleTextDrop.test.ts`.
//!
//! Upstream mocks `caretFromPoint` to return a DOM point in the mounted
//! editor. A Rust editor asks its root element for the caret at the drop
//! point ([`RootElement::caret_from_point`]), so the root element here
//! returns the caret each DOM point stands for: a DOM text node and offset
//! is a text point caret, and an element and child offset is
//! `$getChildCaretAtIndex(element, offset, 'next')`.

#![cfg(feature = "clipboard")]

use super::super::support::{create_test_editor, create_test_inline_element_node};
use crate::caret::{CaretDirection, PointCaret, get_text_point_caret};
use crate::caret_utils::get_child_caret_at_index;
use crate::clipboard::{
    get_clipboard_data_from_selection, handle_plain_text_drop, handle_rich_text_drop,
    set_lexical_clipboard_data_transfer, write_drag_source_to_data_transfer,
};
use crate::json::Value;
use crate::lexical_utils::set_node_key;
use crate::node::NodeBase;
use crate::{
    CreateEditorArgs, DataTransfer, DragEvent, Editor, LexicalError, LexicalNode, NodeClass,
    NodeRef, PointType, RootElement, RootElementRef, SelectionRef, UpdateOptions, Which,
    create_paragraph_node, create_range_selection, create_text_node, get_node_by_key, get_root,
    get_selection, set_selection,
};
use std::cell::RefCell;
use std::rc::Rc;

/// A DOM point upstream's `caretFromPoint` returns, by the Lexical node
/// rendered there.
#[derive(Clone)]
enum DropPoint {
    /// The DOM text of a text node, and an offset in it.
    Text(String, usize),
    /// The DOM element of an element node, and a child offset.
    Element(String, usize),
}

impl DropPoint {
    fn caret(&self) -> PointCaret {
        match self {
            DropPoint::Text(key, offset) => {
                let text = get_node_by_key(key).expect("the text node");
                get_text_point_caret(&text, CaretDirection::Next, *offset).unwrap()
            }
            DropPoint::Element(key, offset) => {
                let element = get_node_by_key(key).expect("the element");
                get_child_caret_at_index(&element, *offset, CaretDirection::Next)
            }
        }
    }
}

thread_local! {
    /// `caretFromPointState.current`.
    static CARET_FROM_POINT: RefCell<Option<DropPoint>> = const { RefCell::new(None) };
}

/// `setCaretFromPoint(node, offset)`.
fn set_caret_from_point(point: DropPoint) {
    CARET_FROM_POINT.with(|c| *c.borrow_mut() = Some(point));
}

/// The mounted root element, whose `caretFromPoint` is the mocked one.
struct DropRootElement;

impl RootElement for DropRootElement {
    fn caret_from_point(&self, _x: f64, _y: f64) -> Option<PointCaret> {
        CARET_FROM_POINT
            .with(|c| c.borrow().clone())
            .map(|point| point.caret())
    }
}

/// `TestDecoratorNode` (`test_decorator`) as upstream's test utilities
/// define it, with the `createDOM` and `exportDOM` the clipboard's HTML
/// export of a selection reaches (the shared test node has neither).
#[derive(Clone, Debug, Default)]
struct TestDecoratorNode {
    base: NodeBase,
}

impl LexicalNode for TestDecoratorNode {
    fn get_type(&self) -> &'static str {
        "test_decorator"
    }
    fn base(&self) -> &NodeBase {
        &self.base
    }
    fn base_mut(&mut self) -> &mut NodeBase {
        &mut self.base
    }
    fn is_decorator(&self) -> bool {
        true
    }
    fn get_text_content(&self) -> String {
        "Hello world".to_owned()
    }
    fn decorate(&self, _editor: &Editor) -> Option<Rc<dyn std::any::Any>> {
        Some(Rc::new("Hello world"))
    }
    #[cfg(feature = "html")]
    fn create_dom(
        &self,
        _this: &NodeRef,
        _config: &crate::EditorConfig,
        _editor: &Editor,
    ) -> Result<crate::dom::DomNode, LexicalError> {
        Ok(crate::lexical_dom::get_document().create_element("span"))
    }
    #[cfg(feature = "html")]
    fn export_dom(
        &self,
        _this: &NodeRef,
        _editor: &Editor,
    ) -> Result<crate::lexical_dom::DomExportOutput, LexicalError> {
        Ok(crate::lexical_dom::DomExportOutput {
            element: Some(crate::lexical_dom::get_document().create_element("test-decorator")),
            ..Default::default()
        })
    }
}

/// `$createTestDecoratorNode()`.
fn create_test_decorator_node() -> NodeRef {
    set_node_key(Box::new(TestDecoratorNode::default()))
}

/// `initializeUnitTest`'s editor, mounted on a root element whose
/// `caretFromPoint` resolves nothing until a test sets a point
/// (`beforeEach`).
fn drop_editor() -> Editor {
    CARET_FROM_POINT.with(|c| *c.borrow_mut() = None);
    let editor = create_test_editor(CreateEditorArgs {
        nodes: vec![NodeClass::of::<TestDecoratorNode>()],
        ..CreateEditorArgs::default()
    });
    editor
        .set_root_element(Some(RootElementRef(Rc::new(DropRootElement))))
        .unwrap();
    editor
}

/// `createDropEvent()`: a `drop` event at (0, 0) carrying a new
/// `DataTransfer`. Upstream spies on `preventDefault`; here the event's
/// `default_prevented` says whether it was called.
fn create_drop_event() -> (DragEvent, Rc<DataTransfer>) {
    let data_transfer = Rc::new(DataTransfer::new());
    (
        DragEvent::new("drop", Some(data_transfer.clone())),
        data_transfer,
    )
}

/// `$markActiveSelectionAsDragSource(dataTransfer, editor)`.
fn mark_active_selection_as_drag_source(data_transfer: &DataTransfer, editor: &Editor) {
    if get_selection()
        .and_then(|s| s.range())
        .is_some_and(|r| !r.is_collapsed())
    {
        write_drag_source_to_data_transfer(data_transfer, editor);
    }
}

/// `setLexicalClipboardDataTransfer(dataTransfer,
/// $getClipboardDataFromSelection())` and
/// `$writeDragSourceToDataTransfer(dataTransfer, editor)`, as DRAGSTART
/// writes them.
fn write_drag_start(data_transfer: &DataTransfer, editor: &Editor) -> Result<(), LexicalError> {
    set_lexical_clipboard_data_transfer(
        data_transfer,
        &get_clipboard_data_from_selection(get_selection().as_ref())?,
    );
    write_drag_source_to_data_transfer(data_transfer, editor);
    Ok(())
}

/// `await editor.update(fn)`: a committed update.
fn update(editor: &Editor, f: impl FnOnce() -> Result<(), LexicalError> + 'static) {
    editor
        .update(
            f,
            UpdateOptions {
                discrete: true,
                ..UpdateOptions::default()
            },
        )
        .unwrap();
}

/// `$setSelection` of a range selection from `(anchor key, offset)` to
/// `(focus key, offset)` in text nodes.
fn select_text(anchor: (&str, usize), focus: (&str, usize)) -> Result<(), LexicalError> {
    let selection = create_range_selection();
    selection.point_set(Which::Anchor, anchor.0, anchor.1, PointType::Text)?;
    selection.point_set(Which::Focus, focus.0, focus.1, PointType::Text)?;
    set_selection(Some(selection));
    Ok(())
}

fn root_text(editor: &Editor) -> String {
    editor.read(|| get_root().get_text_content()).unwrap()
}

/// A paragraph holding `Hello foo world`, with `foo` selected from
/// `anchor` to `focus`; the text node's key.
fn hello_foo_world(editor: &Editor, anchor: usize, focus: usize) -> String {
    let key = Rc::new(RefCell::new(String::new()));
    let k = key.clone();
    update(editor, move || {
        let paragraph = create_paragraph_node();
        let text = create_text_node("Hello foo world");
        paragraph.append(std::slice::from_ref(&text))?;
        get_root().clear()?;
        get_root().append(&[paragraph])?;
        *k.borrow_mut() = text.get_key().to_owned();
        select_text((text.get_key(), anchor), (text.get_key(), focus))
    });
    key.take()
}

/// Drops `text/plain` data `plain` at `point` with the drag marker, as
/// `$handleRichTextDrop`; whether it was handled and the default prevented.
fn drop_plain_text(editor: &Editor, point: DropPoint, plain: &'static str) -> (bool, bool) {
    let editor2 = editor.clone();
    let out = Rc::new(RefCell::new((false, false)));
    let o = out.clone();
    update(editor, move || {
        set_caret_from_point(point);
        let (event, data_transfer) = create_drop_event();
        data_transfer.set_data("text/plain", plain);
        mark_active_selection_as_drag_source(&data_transfer, &editor2);
        let handled = handle_rich_text_drop(&event, &editor2)?;
        *o.borrow_mut() = (handled, event.default_prevented());
        Ok(())
    });
    out.take()
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > moves a selected word later within the same TextNode
#[test]
fn moves_a_selected_word_later_within_the_same_text_node() {
    let editor = drop_editor();
    let text_key = hello_foo_world(&editor, 6, 9);
    // Drop position at offset 15 ("Hello foo world|"), past the source's end.
    let (handled, prevented) = drop_plain_text(&editor, DropPoint::Text(text_key, 15), "foo");
    assert!(handled);
    assert!(prevented);
    assert_eq!(root_text(&editor), "Hello  worldfoo");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > moves a selected word earlier within the same TextNode
#[test]
fn moves_a_selected_word_earlier_within_the_same_text_node() {
    let editor = drop_editor();
    let text_key = hello_foo_world(&editor, 6, 9);
    // Drop position at offset 0 (before the source).
    drop_plain_text(&editor, DropPoint::Text(text_key, 0), "foo");
    assert_eq!(root_text(&editor), "fooHello  world");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > no-op when the drop point is inside the source range
#[test]
fn no_op_when_the_drop_point_is_inside_the_source_range() {
    let editor = drop_editor();
    let text_key = hello_foo_world(&editor, 6, 9);
    // Drop inside the selected "foo" range.
    let (handled, prevented) = drop_plain_text(&editor, DropPoint::Text(text_key, 7), "foo");
    assert!(handled);
    assert!(prevented);
    assert_eq!(root_text(&editor), "Hello foo world");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > keeps the dragged text when dropped on the end edge of the source range
#[test]
fn keeps_the_dragged_text_when_dropped_on_the_end_edge_of_the_source_range() {
    let editor = drop_editor();
    let keys = Rc::new(RefCell::new(Vec::<String>::new()));
    let k = keys.clone();
    update(&editor, move || {
        let bold = create_text_node("Hello");
        bold.toggle_format("bold");
        let rest = create_text_node(" world");
        let paragraph = create_paragraph_node();
        paragraph.append(&[bold.clone(), rest.clone()])?;
        get_root().clear()?;
        get_root().append(&[paragraph])?;
        k.borrow_mut()
            .extend([bold.get_key().to_owned(), rest.get_key().to_owned()]);
        bold.select(Some(0), Some(5))?;
        Ok(())
    });
    let keys = keys.take();
    // Drop at the end of "Hello", then at the start of " world".
    for (key, offset) in [(&keys[0], 5), (&keys[1], 0)] {
        let (handled, _) = drop_plain_text(&editor, DropPoint::Text(key.clone(), offset), "Hello");
        assert!(handled);
        assert_eq!(root_text(&editor), "Hello world");
    }
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > moves a selection across TextNodes in the same block
#[test]
fn moves_a_selection_across_text_nodes_in_the_same_block() {
    let editor = drop_editor();
    let dest_key = Rc::new(RefCell::new(String::new()));
    let d = dest_key.clone();
    update(&editor, move || {
        let paragraph = create_paragraph_node();
        let source = create_text_node("source ");
        source.toggle_format("bold");
        let dest = create_text_node("destination");
        paragraph.append(&[source.clone(), dest.clone()])?;
        get_root().clear()?;
        get_root().append(&[paragraph])?;
        *d.borrow_mut() = dest.get_key().to_owned();
        select_text((source.get_key(), 0), (source.get_key(), 6))
    });
    // Drop just after the "t" of "destination" (offset 4 into "destination").
    drop_plain_text(&editor, DropPoint::Text(dest_key.take(), 4), "source");
    // " " from the source TextNode is preserved where it was, and "source"
    // is inserted into the destination TextNode at offset 4.
    assert_eq!(root_text(&editor), " destsourceination");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > returns false for an external drag (no marker), letting the browser handle it
#[test]
fn returns_false_for_an_external_drag() {
    let editor = drop_editor();
    update(&editor, || {
        let paragraph = create_paragraph_node();
        paragraph.append(&[create_text_node("Hello world")])?;
        get_root().clear()?;
        get_root().append(&[paragraph])
    });
    let out = Rc::new(RefCell::new((true, true)));
    let o = out.clone();
    let editor2 = editor.clone();
    update(&editor, move || {
        let (event, data_transfer) = create_drop_event();
        data_transfer.set_data("text/plain", "brave ");
        // No drag-source marker — this represents an external drag. The
        // handler should bail so the browser's native drag-drop flow (which
        // fires beforeinput insertFromDrop on the destination) takes over.
        let handled = handle_rich_text_drop(&event, &editor2)?;
        *o.borrow_mut() = (handled, event.default_prevented());
        Ok(())
    });
    let (handled, prevented) = out.take();
    assert!(!handled);
    assert!(!prevented);
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > returns false when caretFromPoint cannot resolve a location
#[test]
fn returns_false_when_caret_from_point_cannot_resolve_a_location() {
    let editor = drop_editor();
    update(&editor, || {
        let paragraph = create_paragraph_node();
        paragraph.append(&[create_text_node("Hello")])?;
        get_root().clear()?;
        get_root().append(&[paragraph])
    });
    let out = Rc::new(RefCell::new((true, true)));
    let o = out.clone();
    let editor2 = editor.clone();
    update(&editor, move || {
        let (event, _) = create_drop_event();
        let handled = handle_rich_text_drop(&event, &editor2)?;
        *o.borrow_mut() = (handled, event.default_prevented());
        Ok(())
    });
    let (handled, prevented) = out.take();
    assert!(!handled);
    assert!(!prevented);
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > handles a backward (right-to-left) source selection
#[test]
fn handles_a_backward_source_selection() {
    let editor = drop_editor();
    // Backward selection: anchor > focus in "foo".
    let text_key = hello_foo_world(&editor, 9, 6);
    editor
        .read(|| {
            let current = get_selection().filter(SelectionRef::is_range);
            assert!(
                current.is_some_and(|r| r.is_backward().unwrap()),
                "expected backward selection"
            );
        })
        .unwrap();
    drop_plain_text(&editor, DropPoint::Text(text_key, 15), "foo");
    assert_eq!(root_text(&editor), "Hello  worldfoo");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > preserves DecoratorNodes in the source when moved to a new location
#[test]
fn preserves_decorator_nodes_in_the_source_when_moved_to_a_new_location() {
    let editor = drop_editor();
    let target_key = Rc::new(RefCell::new(String::new()));
    let t = target_key.clone();
    update(&editor, move || {
        let root = get_root();
        root.clear()?;
        // Source paragraph: "before" + DecoratorNode + "after"
        let source_paragraph = create_paragraph_node();
        let before_text = create_text_node("before");
        let decorator = create_test_decorator_node();
        let after_text = create_text_node("after");
        source_paragraph.append(&[before_text.clone(), decorator, after_text.clone()])?;
        // Target paragraph (drop destination)
        let target_paragraph = create_paragraph_node();
        let target_text = create_text_node("target");
        target_paragraph.append(std::slice::from_ref(&target_text))?;
        *t.borrow_mut() = target_text.get_key().to_owned();
        root.append(&[source_paragraph, target_paragraph])?;
        // Select from start of "before" to end of "after" — covers the decorator.
        select_text((before_text.get_key(), 0), (after_text.get_key(), 5))
    });
    // Populate the DataTransfer the same way DRAGSTART does — with Lexical's
    // own serialization so custom nodes survive the drop, plus a drag-source
    // marker so the handler treats it as a same-editor move.
    let target_key = target_key.take();
    let editor2 = editor.clone();
    update(&editor, move || {
        let selection = get_selection().filter(SelectionRef::is_range);
        assert!(selection.is_some(), "expected range selection");
        set_caret_from_point(DropPoint::Text(target_key, 6)); // drop at end of "target"
        let (event, data_transfer) = create_drop_event();
        set_lexical_clipboard_data_transfer(
            &data_transfer,
            &get_clipboard_data_from_selection(selection.as_ref())?,
        );
        write_drag_source_to_data_transfer(&data_transfer, &editor2);
        handle_rich_text_drop(&event, &editor2)?;
        Ok(())
    });
    editor
        .read(|| {
            let top_level_children = get_root().get_children();
            // Two paragraphs at the top level: the (now-empty) source and the
            // target (now containing the moved content).
            assert_eq!(top_level_children.len(), 2);
            let decorators_of = |node: &NodeRef| {
                node.get_children()
                    .iter()
                    .filter(|child| child.is_decorator())
                    .count()
            };
            // Exactly one decorator in the whole tree (preserved, not duplicated).
            assert_eq!(
                top_level_children.iter().map(decorators_of).sum::<usize>(),
                1
            );
            // The decorator must live under the target paragraph, not the source.
            let source_paragraph = &top_level_children[0];
            let target_paragraph = &top_level_children[1];
            assert!(source_paragraph.is_element(), "expected source paragraph");
            assert!(target_paragraph.is_element(), "expected target paragraph");
            assert_eq!(decorators_of(source_paragraph), 0);
            assert_eq!(decorators_of(target_paragraph), 1);
            // TestDecoratorNode.getTextContent() is 'Hello world', so the
            // decorator appears between "before" and "after" in textContent.
            assert_eq!(source_paragraph.get_text_content(), "");
            assert_eq!(
                target_paragraph.get_text_content(),
                "targetbeforeHello worldafter"
            );
        })
        .unwrap();
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleTextDrop > no-op when drop is inside a multi-node source range
#[test]
fn no_op_when_drop_is_inside_a_multi_node_source_range() {
    let editor = drop_editor();
    let inner_key = Rc::new(RefCell::new(String::new()));
    let i = inner_key.clone();
    update(&editor, move || {
        let paragraph = create_paragraph_node();
        let t1 = create_text_node("one");
        let t2 = create_text_node("two");
        t2.toggle_format("bold");
        let t3 = create_text_node("three");
        paragraph.append(&[t1.clone(), t2.clone(), t3.clone()])?;
        get_root().clear()?;
        get_root().append(&[paragraph])?;
        *i.borrow_mut() = t2.get_key().to_owned();
        // Selection spans t1..t3 (all three text nodes).
        select_text((t1.get_key(), 0), (t3.get_key(), 5))
    });
    // Drop INSIDE the "two" text node, which is strictly between the
    // source's anchor and focus.
    let (handled, prevented) =
        drop_plain_text(&editor, DropPoint::Text(inner_key.take(), 1), "onetwothree");
    assert!(handled);
    assert!(prevented);
    assert_eq!(root_text(&editor), "onetwothree");
}

// Drop boundaries (`describe.each` over the rich and plain text handlers,
// then `backward`).

type DropHandler = fn(&DragEvent, &Editor) -> Result<bool, LexicalError>;

/// Selects the whole of `source`, forward or backward
/// (`source.select(backward ? 5 : 0, backward ? 0 : 5)`).
fn select_whole(source: &NodeRef, backward: bool) -> Result<(), LexicalError> {
    let (anchor, focus) = if backward { (5, 0) } else { (0, 5) };
    source.select(Some(anchor), Some(focus))?;
    Ok(())
}

/// The selection `start..end` in text nodes, made backward by setting
/// `start` on the focus.
fn select_range(
    start: (&str, usize),
    end: (&str, usize),
    backward: bool,
) -> Result<(), LexicalError> {
    if backward {
        select_text(end, start)
    } else {
        select_text(start, end)
    }
}

/// Drops the selection's own clipboard data at `point`, with the drag
/// marker; whether it was handled and the default prevented.
fn drop_selection(editor: &Editor, handler: DropHandler, point: DropPoint) -> (bool, bool) {
    let editor2 = editor.clone();
    let out = Rc::new(RefCell::new((false, false)));
    let o = out.clone();
    update(editor, move || {
        set_caret_from_point(point);
        let (event, data_transfer) = create_drop_event();
        write_drag_start(&data_transfer, &editor2)?;
        let handled = handler(&event, &editor2)?;
        *o.borrow_mut() = (handled, event.default_prevented());
        Ok(())
    });
    out.take()
}

/// Drops at `point` and expects the drop to be taken (and, where upstream
/// checks it, its default prevented) but the editor state, the selection
/// and the nodes `keys` to be unchanged.
fn expect_drop_keeps_state(
    editor: &Editor,
    handler: DropHandler,
    point: DropPoint,
    keys: Vec<String>,
    expect_prevented: bool,
) {
    let original: Value = editor.get_editor_state().to_json(false);
    let selection = editor
        .read(|| get_selection().unwrap().clone_selection())
        .unwrap();
    let (handled, prevented) = drop_selection(editor, handler, point);
    assert!(handled);
    if expect_prevented {
        assert!(prevented);
    }
    assert_eq!(editor.get_editor_state().to_json(false), original);
    editor
        .read(|| {
            assert!(get_selection().unwrap().get().is(&selection.get()));
            for key in &keys {
                assert_eq!(get_node_by_key(key).map(|n| n.is_attached()), Some(true));
            }
        })
        .unwrap();
}

/// `keeps a token unchanged at %s`.
fn keeps_a_token_unchanged(handler: DropHandler, backward: bool, position: &str) {
    let editor = drop_editor();
    let keys = Rc::new(RefCell::new(Vec::<String>::new()));
    let k = keys.clone();
    update(&editor, move || {
        let before = create_text_node("before ");
        let source = create_text_node("Hello");
        source.set_mode("token")?;
        let after = create_text_node(" world");
        let paragraph = create_paragraph_node();
        paragraph.append(&[before.clone(), source.clone(), after.clone()])?;
        get_root().clear()?;
        get_root().append(std::slice::from_ref(&paragraph))?;
        k.borrow_mut()
            .extend([&paragraph, &before, &source, &after].map(|n| n.get_key().to_owned()));
        select_whole(&source, backward)
    });
    let keys = keys.take();
    let [paragraph, before, source, after] = [0, 1, 2, 3].map(|i| keys[i].clone());
    let point = match position {
        "element end" => DropPoint::Element(paragraph, 2),
        "element start" => DropPoint::Element(paragraph, 1),
        "next start" => DropPoint::Text(after, 0),
        "previous end" => DropPoint::Text(before, 7),
        "text end" => DropPoint::Text(source, 5),
        "text middle" => DropPoint::Text(source, 2),
        "text start" => DropPoint::Text(source, 0),
        _ => unreachable!(),
    };
    expect_drop_keeps_state(&editor, handler, point, keys, true);
}

/// `keeps nested inline wrappers unchanged at %s`.
fn keeps_nested_inline_wrappers_unchanged(handler: DropHandler, backward: bool, position: &str) {
    let editor = drop_editor();
    let keys = Rc::new(RefCell::new(Vec::<String>::new()));
    let k = keys.clone();
    update(&editor, move || {
        let before = create_text_node("before ");
        let source = create_text_node("Hello");
        let after = create_text_node(" world");
        let inner = create_test_inline_element_node();
        inner.append(std::slice::from_ref(&source))?;
        let outer = create_test_inline_element_node();
        outer.append(std::slice::from_ref(&inner))?;
        let paragraph = create_paragraph_node();
        paragraph.append(&[before.clone(), outer.clone(), after.clone()])?;
        get_root().clear()?;
        get_root().append(std::slice::from_ref(&paragraph))?;
        k.borrow_mut()
            .extend([&before, &after, &paragraph, &inner, &outer].map(|n| n.get_key().to_owned()));
        select_whole(&source, backward)
    });
    let keys = keys.take();
    let [before, after, paragraph] = [0, 1, 2].map(|i| keys[i].clone());
    let point = match position {
        "element end" => DropPoint::Element(paragraph, 2),
        "element start" => DropPoint::Element(paragraph, 1),
        "next start" => DropPoint::Text(after, 0),
        "previous end" => DropPoint::Text(before, 7),
        _ => unreachable!(),
    };
    expect_drop_keeps_state(&editor, handler, point, keys[3..].to_vec(), false);
}

/// `keeps a decorator in a multi-node range at the adjacent end edge`.
fn keeps_a_decorator_at_the_adjacent_end_edge(handler: DropHandler, backward: bool) {
    let editor = drop_editor();
    let keys = Rc::new(RefCell::new(Vec::<String>::new()));
    let k = keys.clone();
    update(&editor, move || {
        let first = create_text_node("before");
        let decorator = create_test_decorator_node();
        let last = create_text_node("after");
        last.set_mode("token")?;
        let after = create_text_node(" rest");
        k.borrow_mut()
            .extend([after.get_key().to_owned(), decorator.get_key().to_owned()]);
        let paragraph = create_paragraph_node();
        paragraph.append(&[first.clone(), decorator, last.clone(), after])?;
        get_root().clear()?;
        get_root().append(&[paragraph])?;
        select_range((first.get_key(), 0), (last.get_key(), 5), backward)
    });
    let keys = keys.take();
    expect_drop_keeps_state(
        &editor,
        handler,
        DropPoint::Text(keys[0].clone(), 0),
        vec![keys[1].clone()],
        false,
    );
}

/// `keeps a %s destination after a cross-paragraph deletion`.
fn keeps_a_destination_after_a_cross_paragraph_deletion(
    handler: DropHandler,
    backward: bool,
    position: &str,
) {
    let editor = drop_editor();
    let keys = Rc::new(RefCell::new(Vec::<String>::new()));
    let k = keys.clone();
    update(&editor, move || {
        let first = create_text_node("abc");
        let last = create_text_node("def");
        let paragraph = create_paragraph_node();
        paragraph.append(std::slice::from_ref(&last))?;
        k.borrow_mut()
            .extend([last.get_key().to_owned(), paragraph.get_key().to_owned()]);
        let first_paragraph = create_paragraph_node();
        first_paragraph.append(std::slice::from_ref(&first))?;
        get_root().clear()?;
        get_root().append(&[first_paragraph, paragraph])?;
        select_range((first.get_key(), 1), (last.get_key(), 2), backward)
    });
    let keys = keys.take();
    let point = match position {
        "paragraph" => DropPoint::Element(keys[1].clone(), 1),
        "root" => DropPoint::Element("root".to_owned(), 2),
        "text" => DropPoint::Text(keys[0].clone(), 3),
        _ => unreachable!(),
    };
    let (handled, prevented) = drop_selection(&editor, handler, point);
    assert!(handled);
    assert!(prevented);
    let is_rich = std::ptr::fn_addr_eq(handler, handle_rich_text_drop as DropHandler);
    editor
        .read(|| {
            // The unselected f must precede the moved bc/de, even when its
            // former paragraph was the destination caret's origin.
            assert_eq!(get_root().get_text_content().replace('\n', ""), "afbcde");
            assert!(get_selection().unwrap().range().unwrap().is_collapsed());
            if position != "root" {
                let children: Vec<String> = get_root()
                    .get_children()
                    .iter()
                    .map(NodeRef::get_text_content)
                    .collect();
                if is_rich {
                    assert_eq!(children, ["afbc", "de"]);
                } else {
                    assert_eq!(children, ["afbc\nde"]);
                }
            }
        })
        .unwrap();
}

// $handleRichTextDrop across editors

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::$handleRichTextDrop across editors > cancelled cross-editor drag leaves both editors untouched
#[test]
fn cancelled_cross_editor_drag_leaves_both_editors_untouched() {
    let source_editor = drop_editor();
    let dest_editor = drop_editor();
    update(&source_editor, || {
        let paragraph = create_paragraph_node();
        paragraph.append(&[create_text_node("source-content")])?;
        get_root().clear()?;
        get_root().append(std::slice::from_ref(&paragraph))?;
        let text = paragraph.get_first_child_or_throw()?;
        select_text((text.get_key(), 0), (text.get_key(), 6))
    });
    update(&dest_editor, || {
        let paragraph = create_paragraph_node();
        paragraph.append(&[create_text_node("destination")])?;
        get_root().clear()?;
        get_root().append(&[paragraph])
    });
    // No drop event ever fires — there is no DataTransfer to read, so the
    // marker-based design has nothing to act on. Both editors are unchanged.
    assert_eq!(root_text(&source_editor), "source-content");
    assert_eq!(root_text(&dest_editor), "destination");
}

// The drop boundary cases, one test per upstream case.

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a token unchanged at text start
#[test]
fn rich_backward_false_keeps_a_token_unchanged_at_text_start() {
    keeps_a_token_unchanged(handle_rich_text_drop, false, "text start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a token unchanged at text middle
#[test]
fn rich_backward_false_keeps_a_token_unchanged_at_text_middle() {
    keeps_a_token_unchanged(handle_rich_text_drop, false, "text middle");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a token unchanged at text end
#[test]
fn rich_backward_false_keeps_a_token_unchanged_at_text_end() {
    keeps_a_token_unchanged(handle_rich_text_drop, false, "text end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a token unchanged at previous end
#[test]
fn rich_backward_false_keeps_a_token_unchanged_at_previous_end() {
    keeps_a_token_unchanged(handle_rich_text_drop, false, "previous end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a token unchanged at next start
#[test]
fn rich_backward_false_keeps_a_token_unchanged_at_next_start() {
    keeps_a_token_unchanged(handle_rich_text_drop, false, "next start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a token unchanged at element start
#[test]
fn rich_backward_false_keeps_a_token_unchanged_at_element_start() {
    keeps_a_token_unchanged(handle_rich_text_drop, false, "element start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a token unchanged at element end
#[test]
fn rich_backward_false_keeps_a_token_unchanged_at_element_end() {
    keeps_a_token_unchanged(handle_rich_text_drop, false, "element end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps nested inline wrappers unchanged at previous end
#[test]
fn rich_backward_false_keeps_nested_inline_wrappers_unchanged_at_previous_end() {
    keeps_nested_inline_wrappers_unchanged(handle_rich_text_drop, false, "previous end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps nested inline wrappers unchanged at next start
#[test]
fn rich_backward_false_keeps_nested_inline_wrappers_unchanged_at_next_start() {
    keeps_nested_inline_wrappers_unchanged(handle_rich_text_drop, false, "next start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps nested inline wrappers unchanged at element start
#[test]
fn rich_backward_false_keeps_nested_inline_wrappers_unchanged_at_element_start() {
    keeps_nested_inline_wrappers_unchanged(handle_rich_text_drop, false, "element start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps nested inline wrappers unchanged at element end
#[test]
fn rich_backward_false_keeps_nested_inline_wrappers_unchanged_at_element_end() {
    keeps_nested_inline_wrappers_unchanged(handle_rich_text_drop, false, "element end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a decorator in a multi-node range at the adjacent end edge
#[test]
fn rich_backward_false_keeps_a_decorator_in_a_multi_node_range_at_the_adjacent_end_edge() {
    keeps_a_decorator_at_the_adjacent_end_edge(handle_rich_text_drop, false);
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a text destination after a cross-paragraph deletion
#[test]
fn rich_backward_false_keeps_a_text_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_rich_text_drop, false, "text");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a paragraph destination after a cross-paragraph deletion
#[test]
fn rich_backward_false_keeps_a_paragraph_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_rich_text_drop, false, "paragraph");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=false > keeps a root destination after a cross-paragraph deletion
#[test]
fn rich_backward_false_keeps_a_root_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_rich_text_drop, false, "root");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a token unchanged at text start
#[test]
fn rich_backward_true_keeps_a_token_unchanged_at_text_start() {
    keeps_a_token_unchanged(handle_rich_text_drop, true, "text start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a token unchanged at text middle
#[test]
fn rich_backward_true_keeps_a_token_unchanged_at_text_middle() {
    keeps_a_token_unchanged(handle_rich_text_drop, true, "text middle");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a token unchanged at text end
#[test]
fn rich_backward_true_keeps_a_token_unchanged_at_text_end() {
    keeps_a_token_unchanged(handle_rich_text_drop, true, "text end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a token unchanged at previous end
#[test]
fn rich_backward_true_keeps_a_token_unchanged_at_previous_end() {
    keeps_a_token_unchanged(handle_rich_text_drop, true, "previous end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a token unchanged at next start
#[test]
fn rich_backward_true_keeps_a_token_unchanged_at_next_start() {
    keeps_a_token_unchanged(handle_rich_text_drop, true, "next start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a token unchanged at element start
#[test]
fn rich_backward_true_keeps_a_token_unchanged_at_element_start() {
    keeps_a_token_unchanged(handle_rich_text_drop, true, "element start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a token unchanged at element end
#[test]
fn rich_backward_true_keeps_a_token_unchanged_at_element_end() {
    keeps_a_token_unchanged(handle_rich_text_drop, true, "element end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps nested inline wrappers unchanged at previous end
#[test]
fn rich_backward_true_keeps_nested_inline_wrappers_unchanged_at_previous_end() {
    keeps_nested_inline_wrappers_unchanged(handle_rich_text_drop, true, "previous end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps nested inline wrappers unchanged at next start
#[test]
fn rich_backward_true_keeps_nested_inline_wrappers_unchanged_at_next_start() {
    keeps_nested_inline_wrappers_unchanged(handle_rich_text_drop, true, "next start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps nested inline wrappers unchanged at element start
#[test]
fn rich_backward_true_keeps_nested_inline_wrappers_unchanged_at_element_start() {
    keeps_nested_inline_wrappers_unchanged(handle_rich_text_drop, true, "element start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps nested inline wrappers unchanged at element end
#[test]
fn rich_backward_true_keeps_nested_inline_wrappers_unchanged_at_element_end() {
    keeps_nested_inline_wrappers_unchanged(handle_rich_text_drop, true, "element end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a decorator in a multi-node range at the adjacent end edge
#[test]
fn rich_backward_true_keeps_a_decorator_in_a_multi_node_range_at_the_adjacent_end_edge() {
    keeps_a_decorator_at_the_adjacent_end_edge(handle_rich_text_drop, true);
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a text destination after a cross-paragraph deletion
#[test]
fn rich_backward_true_keeps_a_text_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_rich_text_drop, true, "text");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a paragraph destination after a cross-paragraph deletion
#[test]
fn rich_backward_true_keeps_a_paragraph_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_rich_text_drop, true, "paragraph");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::rich text drop boundaries > backward=true > keeps a root destination after a cross-paragraph deletion
#[test]
fn rich_backward_true_keeps_a_root_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_rich_text_drop, true, "root");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a token unchanged at text start
#[test]
fn plain_backward_false_keeps_a_token_unchanged_at_text_start() {
    keeps_a_token_unchanged(handle_plain_text_drop, false, "text start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a token unchanged at text middle
#[test]
fn plain_backward_false_keeps_a_token_unchanged_at_text_middle() {
    keeps_a_token_unchanged(handle_plain_text_drop, false, "text middle");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a token unchanged at text end
#[test]
fn plain_backward_false_keeps_a_token_unchanged_at_text_end() {
    keeps_a_token_unchanged(handle_plain_text_drop, false, "text end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a token unchanged at previous end
#[test]
fn plain_backward_false_keeps_a_token_unchanged_at_previous_end() {
    keeps_a_token_unchanged(handle_plain_text_drop, false, "previous end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a token unchanged at next start
#[test]
fn plain_backward_false_keeps_a_token_unchanged_at_next_start() {
    keeps_a_token_unchanged(handle_plain_text_drop, false, "next start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a token unchanged at element start
#[test]
fn plain_backward_false_keeps_a_token_unchanged_at_element_start() {
    keeps_a_token_unchanged(handle_plain_text_drop, false, "element start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a token unchanged at element end
#[test]
fn plain_backward_false_keeps_a_token_unchanged_at_element_end() {
    keeps_a_token_unchanged(handle_plain_text_drop, false, "element end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps nested inline wrappers unchanged at previous end
#[test]
fn plain_backward_false_keeps_nested_inline_wrappers_unchanged_at_previous_end() {
    keeps_nested_inline_wrappers_unchanged(handle_plain_text_drop, false, "previous end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps nested inline wrappers unchanged at next start
#[test]
fn plain_backward_false_keeps_nested_inline_wrappers_unchanged_at_next_start() {
    keeps_nested_inline_wrappers_unchanged(handle_plain_text_drop, false, "next start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps nested inline wrappers unchanged at element start
#[test]
fn plain_backward_false_keeps_nested_inline_wrappers_unchanged_at_element_start() {
    keeps_nested_inline_wrappers_unchanged(handle_plain_text_drop, false, "element start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps nested inline wrappers unchanged at element end
#[test]
fn plain_backward_false_keeps_nested_inline_wrappers_unchanged_at_element_end() {
    keeps_nested_inline_wrappers_unchanged(handle_plain_text_drop, false, "element end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a decorator in a multi-node range at the adjacent end edge
#[test]
fn plain_backward_false_keeps_a_decorator_in_a_multi_node_range_at_the_adjacent_end_edge() {
    keeps_a_decorator_at_the_adjacent_end_edge(handle_plain_text_drop, false);
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a text destination after a cross-paragraph deletion
#[test]
fn plain_backward_false_keeps_a_text_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_plain_text_drop, false, "text");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a paragraph destination after a cross-paragraph deletion
#[test]
fn plain_backward_false_keeps_a_paragraph_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(
        handle_plain_text_drop,
        false,
        "paragraph",
    );
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=false > keeps a root destination after a cross-paragraph deletion
#[test]
fn plain_backward_false_keeps_a_root_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_plain_text_drop, false, "root");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a token unchanged at text start
#[test]
fn plain_backward_true_keeps_a_token_unchanged_at_text_start() {
    keeps_a_token_unchanged(handle_plain_text_drop, true, "text start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a token unchanged at text middle
#[test]
fn plain_backward_true_keeps_a_token_unchanged_at_text_middle() {
    keeps_a_token_unchanged(handle_plain_text_drop, true, "text middle");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a token unchanged at text end
#[test]
fn plain_backward_true_keeps_a_token_unchanged_at_text_end() {
    keeps_a_token_unchanged(handle_plain_text_drop, true, "text end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a token unchanged at previous end
#[test]
fn plain_backward_true_keeps_a_token_unchanged_at_previous_end() {
    keeps_a_token_unchanged(handle_plain_text_drop, true, "previous end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a token unchanged at next start
#[test]
fn plain_backward_true_keeps_a_token_unchanged_at_next_start() {
    keeps_a_token_unchanged(handle_plain_text_drop, true, "next start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a token unchanged at element start
#[test]
fn plain_backward_true_keeps_a_token_unchanged_at_element_start() {
    keeps_a_token_unchanged(handle_plain_text_drop, true, "element start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a token unchanged at element end
#[test]
fn plain_backward_true_keeps_a_token_unchanged_at_element_end() {
    keeps_a_token_unchanged(handle_plain_text_drop, true, "element end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps nested inline wrappers unchanged at previous end
#[test]
fn plain_backward_true_keeps_nested_inline_wrappers_unchanged_at_previous_end() {
    keeps_nested_inline_wrappers_unchanged(handle_plain_text_drop, true, "previous end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps nested inline wrappers unchanged at next start
#[test]
fn plain_backward_true_keeps_nested_inline_wrappers_unchanged_at_next_start() {
    keeps_nested_inline_wrappers_unchanged(handle_plain_text_drop, true, "next start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps nested inline wrappers unchanged at element start
#[test]
fn plain_backward_true_keeps_nested_inline_wrappers_unchanged_at_element_start() {
    keeps_nested_inline_wrappers_unchanged(handle_plain_text_drop, true, "element start");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps nested inline wrappers unchanged at element end
#[test]
fn plain_backward_true_keeps_nested_inline_wrappers_unchanged_at_element_end() {
    keeps_nested_inline_wrappers_unchanged(handle_plain_text_drop, true, "element end");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a decorator in a multi-node range at the adjacent end edge
#[test]
fn plain_backward_true_keeps_a_decorator_in_a_multi_node_range_at_the_adjacent_end_edge() {
    keeps_a_decorator_at_the_adjacent_end_edge(handle_plain_text_drop, true);
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a text destination after a cross-paragraph deletion
#[test]
fn plain_backward_true_keeps_a_text_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_plain_text_drop, true, "text");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a paragraph destination after a cross-paragraph deletion
#[test]
fn plain_backward_true_keeps_a_paragraph_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_plain_text_drop, true, "paragraph");
}

// upstream: lexical/src/__tests__/unit/HandleTextDrop.test.ts::plain text drop boundaries > backward=true > keeps a root destination after a cross-paragraph deletion
#[test]
fn plain_backward_true_keeps_a_root_destination_after_a_cross_paragraph_deletion() {
    keeps_a_destination_after_a_cross_paragraph_deletion(handle_plain_text_drop, true, "root");
}