lexical-ui 0.1.3

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
//! Ports of
//! `packages/lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts`.
//!
//! Upstream guards against a parent cycle (an update that never returns)
//! by counting `getParent` calls; a Rust method can't be patched, so the
//! property tests run each document's update on a thread of its own and
//! count one that hasn't returned after [`HANG`] as never returning.

#![cfg(all(feature = "extension", feature = "rich-text"))]

use crate::extension::{
    LexicalEditorWithDispose, build_editor_from_extensions, create_horizontal_rule_node,
    horizontal_rule_extension, is_horizontal_rule_node,
};
use crate::nodes::element::ElementData;
use crate::rich_text::{QuoteNode, create_quote_node};
use crate::selection::wrap_nodes;
use crate::upstream_tests::support::{panic_message, update};
use crate::{
    LexicalExtension, LexicalNode, NodeBase, NodeClass, NodeRef, PointType, UpdateOptions, Which,
    create_paragraph_node, create_range_selection, create_text_node, define_extension, get_root,
    set_selection,
};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::time::Duration;

/// How long a document's update may take before it counts as never
/// returning (one takes milliseconds). The thread of one that never
/// returns spins until the test binary exits; with `direct_text`, 3 of the
/// 500 documents do, as upstream's comment says of JS.
const HANG: Duration = Duration::from_secs(3);

/// `BoxNode` (`test_wrap_box`): a plain block element that holds blocks,
/// with no declared structure.
#[derive(Clone, Debug, Default)]
struct BoxNode {
    base: NodeBase,
    element: ElementData,
}

impl LexicalNode for BoxNode {
    fn get_type(&self) -> &'static str {
        "test_wrap_box"
    }
    fn base(&self) -> &NodeBase {
        &self.base
    }
    fn base_mut(&mut self) -> &mut NodeBase {
        &mut self.base
    }
    fn element(&self) -> Option<&ElementData> {
        Some(&self.element)
    }
    fn element_mut(&mut self) -> Option<&mut ElementData> {
        Some(&mut self.element)
    }
}

fn build() -> LexicalEditorWithDispose {
    build_editor_from_extensions([define_extension(LexicalExtension {
        dependencies: vec![horizontal_rule_extension().into()],
        nodes: vec![NodeClass::of::<BoxNode>(), NodeClass::of::<QuoteNode>()],
        ..LexicalExtension::<(), (), ()>::new("wrap-block-leaf", ())
    })
    .into()])
    .unwrap()
}

fn element_of(element: NodeRef, children: &[NodeRef]) -> NodeRef {
    element.append(children).unwrap();
    element
}

fn box_of(children: &[NodeRef]) -> NodeRef {
    element_of(crate::create::<BoxNode>(), children)
}

fn paragraph_of(text: &NodeRef) -> NodeRef {
    element_of(create_paragraph_node(), std::slice::from_ref(text))
}

fn quote() -> NodeRef {
    create_quote_node(false)
}

/// `outline(editor)`: the texts and rules in document order, and the
/// quotes' texts.
fn outline(editor: &LexicalEditorWithDispose) -> (Vec<String>, Vec<String>) {
    editor
        .read(|| {
            fn walk(
                node: &NodeRef,
                in_quote: bool,
                all: &mut Vec<String>,
                quoted: &mut Vec<String>,
            ) {
                if node.is_text() {
                    all.push(node.get_text_content());
                    if in_quote {
                        quoted.push(node.get_text_content());
                    }
                } else if is_horizontal_rule_node(node) {
                    all.push("<hr>".to_owned());
                    if in_quote {
                        quoted.push("<hr>".to_owned());
                    }
                } else if node.is_element() {
                    let quote = in_quote || node.get_type() == "quote";
                    for child in node.get_children() {
                        walk(&child, quote, all, quoted);
                    }
                }
            }
            let (mut all, mut quoted) = (Vec::new(), Vec::new());
            walk(&get_root(), false, &mut all, &mut quoted);
            (all, quoted)
        })
        .unwrap()
}

fn strings(values: &[&str]) -> Vec<String> {
    values.iter().map(|s| (*s).to_owned()).collect()
}

/// The JS LCG in doubles (the product exceeds 2^53 and rounds as in JS).
struct Random(Cell<f64>);

impl Random {
    fn next(&self) -> f64 {
        let seed = (self.0.get() * 1_103_515_245.0 + 12345.0) % 2_147_483_648.0;
        self.0.set(seed);
        seed / 2_147_483_648.0
    }

    fn pick(&self, count: usize) -> usize {
        (self.next() * count as f64).floor() as usize
    }
}

/// `$children(depth)`.
fn random_children(
    random: &Random,
    n: &Cell<usize>,
    direct_text: bool,
    depth: usize,
) -> Vec<NodeRef> {
    let mut out = Vec::new();
    let count = 1 + random.pick(4);
    for _ in 0..count {
        let r = random.next();
        if r < 0.25 {
            out.push(create_horizontal_rule_node());
        } else if r < 0.4 && depth < 2 {
            out.push(box_of(&random_children(random, n, direct_text, depth + 1)));
        } else if direct_text && r < 0.55 && depth > 0 {
            // Text directly in a box, next to its blocks.
            out.push(create_text_node(&format!("t{}", n.get())));
            n.set(n.get() + 1);
        } else {
            out.push(paragraph_of(&create_text_node(&format!("t{}", n.get()))));
            n.set(n.get() + 1);
        }
    }
    out
}

/// One document of `wrapFailures`: builds it from `seed`, sends the seed
/// after its random picks on `seed_out`, and returns the failure, if any.
fn wrap_run(
    run: usize,
    seed: f64,
    direct_text: bool,
    seed_out: std::sync::mpsc::Sender<f64>,
) -> Option<String> {
    let editor = build();
    let before = Rc::new(RefCell::new(Vec::<String>::new()));
    let before_in = before.clone();
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        editor.update(
            move || {
                let random = Random(Cell::new(seed));
                let n = Cell::new(0);
                let children = random_children(&random, &n, direct_text, 0);
                get_root().clear()?;
                get_root().append(&children)?;
                *before_in.borrow_mut() = get_root()
                    .get_all_text_nodes()
                    .iter()
                    .map(NodeRef::get_text_content)
                    .collect();
                let mut points: Vec<(String, usize, PointType)> = Vec::new();
                fn collect(node: &NodeRef, points: &mut Vec<(String, usize, PointType)>) {
                    if node.is_text() {
                        for o in 0..=node.get_text_content_size() {
                            points.push((node.get_key().to_owned(), o, PointType::Text));
                        }
                    } else if node.is_element() {
                        for o in 0..=node.get_children_size() {
                            points.push((node.get_key().to_owned(), o, PointType::Element));
                        }
                        for child in node.get_children() {
                            collect(&child, points);
                        }
                    }
                }
                collect(&get_root(), &mut points);
                let a = points[random.pick(points.len())].clone();
                let b = points[random.pick(points.len())].clone();
                let _ = seed_out.send(random.0.get());
                let selection = create_range_selection();
                selection.point_set(Which::Anchor, &a.0, a.1, a.2)?;
                selection.point_set(Which::Focus, &b.0, b.1, b.2)?;
                set_selection(Some(selection.clone()));
                wrap_nodes(&selection, quote, None)
            },
            UpdateOptions {
                discrete: true,
                ..UpdateOptions::default()
            },
        )
    }));
    let error = match result {
        Ok(Ok(())) => None,
        Ok(Err(error)) => Some(error.to_string()),
        Err(payload) => Some(panic_message(&*payload)),
    };
    let after: Vec<String> = outline(&editor)
        .0
        .into_iter()
        .filter(|t| t != "<hr>")
        .collect();
    // Adjacent texts may merge: compare the text in order.
    let before = before.borrow().concat();
    editor.dispose();
    if error.is_some() || after.concat() != before {
        Some(format!(
            "run {run}: {}",
            error.unwrap_or_else(|| after.join(","))
        ))
    } else {
        None
    }
}

/// `wrapFailures(directText)`: random documents of paragraphs, rules
/// (adjacent ones too), boxes (nested up to twice) and, with
/// `direct_text`, text directly in a box, and random ranges between text
/// and element points: the documents where `wrap_nodes` fails (or never
/// returns) or does not keep every text in order.
fn wrap_failures(direct_text: bool) -> Vec<String> {
    let mut seed = 9638.0;
    let mut failures = Vec::new();
    for run in 0..500 {
        let (seed_in, seed_out) = std::sync::mpsc::channel();
        let (done_in, done_out) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let _ = done_in.send(wrap_run(run, seed, direct_text, seed_in));
        });
        seed = seed_out.recv().expect("the document was built");
        match done_out.recv_timeout(HANG) {
            Ok(failure) => failures.extend(failure),
            Err(_) => failures.push(format!("run {run}: never returned")),
        }
    }
    failures
}

/// Runs `setup` in an update, then wraps from the `[anchor, focus]` it
/// returns.
fn wrap(
    editor: &LexicalEditorWithDispose,
    setup: impl FnOnce() -> [(NodeRef, usize); 2] + 'static,
) {
    update(editor, move || {
        let [anchor, focus] = setup();
        let selection = create_range_selection();
        for (which, (node, offset)) in [(Which::Anchor, anchor), (Which::Focus, focus)] {
            let point_type = if node.is_element() {
                PointType::Element
            } else {
                PointType::Text
            };
            selection.point_set(which, node.get_key(), offset, point_type)?;
        }
        set_selection(Some(selection.clone()));
        wrap_nodes(&selection, quote, None)
    });
}

fn set_root(children: &[NodeRef]) {
    get_root().clear().unwrap();
    get_root().append(children).unwrap();
}

// upstream: lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts::$wrapNodes with a block leaf in an element that is not a block container (#9638) > a range across a horizontal rule in a box keeps every paragraph
#[test]
fn a_range_across_a_horizontal_rule_in_a_box_keeps_every_paragraph() {
    let editor = build();
    wrap(&editor, || {
        let bb = create_text_node("bb");
        let cc = create_text_node("cc");
        set_root(&[box_of(&[
            paragraph_of(&create_text_node("aa")),
            paragraph_of(&bb),
            create_horizontal_rule_node(),
            paragraph_of(&cc),
        ])]);
        [(bb, 1), (cc, 2)]
    });
    assert_eq!(
        outline(&editor),
        (
            strings(&["aa", "bb", "<hr>", "cc"]),
            strings(&["bb", "<hr>", "cc"])
        )
    );
    // The rule stays in the box, next to the paragraphs.
    let types = editor
        .read(|| {
            get_root()
                .get_children()
                .iter()
                .map(NodeRef::get_type)
                .collect::<Vec<_>>()
        })
        .unwrap();
    assert_eq!(types, ["test_wrap_box"]);
    editor.dispose();
}

// upstream: lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts::$wrapNodes with a block leaf in an element that is not a block container (#9638) > a range that starts at the rule
#[test]
fn a_range_that_starts_at_the_rule() {
    let editor = build();
    wrap(&editor, || {
        let cc = create_text_node("cc");
        let b = box_of(&[
            paragraph_of(&create_text_node("aa")),
            create_horizontal_rule_node(),
            paragraph_of(&cc),
        ]);
        set_root(std::slice::from_ref(&b));
        [(b, 1), (cc, 2)]
    });
    assert_eq!(
        outline(&editor),
        (strings(&["aa", "<hr>", "cc"]), strings(&["<hr>", "cc"]))
    );
    editor.dispose();
}

/// `a box with text and two rules in it, from the second rule (...)`.
fn a_box_with_text_and_two_rules_in_it_from_the_second_rule(backward: bool) {
    let editor = build();
    wrap(&editor, move || {
        let cc = create_text_node("cc");
        let b = box_of(&[
            create_text_node("aa"),
            create_horizontal_rule_node(),
            create_horizontal_rule_node(),
            cc.clone(),
        ]);
        set_root(std::slice::from_ref(&b));
        let (from, to) = ((b, 2), (cc, 2));
        if backward { [to, from] } else { [from, to] }
    });
    assert_eq!(outline(&editor).0, strings(&["aa", "<hr>", "<hr>", "cc"]));
    editor.dispose();
}

// upstream: lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts::$wrapNodes with a block leaf in an element that is not a block container (#9638) > a box with text and two rules in it, from the second rule (forward)
#[test]
fn a_box_with_text_and_two_rules_in_it_from_the_second_rule_forward() {
    a_box_with_text_and_two_rules_in_it_from_the_second_rule(false);
}

// upstream: lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts::$wrapNodes with a block leaf in an element that is not a block container (#9638) > a box with text and two rules in it, from the second rule (backward)
#[test]
fn a_box_with_text_and_two_rules_in_it_from_the_second_rule_backward() {
    a_box_with_text_and_two_rules_in_it_from_the_second_rule(true);
}

// upstream: lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts::$wrapNodes with a block leaf in an element that is not a block container (#9638) > control: the same range without the rule
#[test]
fn control_the_same_range_without_the_rule() {
    let editor = build();
    wrap(&editor, || {
        let bb = create_text_node("bb");
        let cc = create_text_node("cc");
        set_root(&[box_of(&[
            paragraph_of(&create_text_node("aa")),
            paragraph_of(&bb),
            paragraph_of(&cc),
        ])]);
        [(bb, 1), (cc, 2)]
    });
    assert_eq!(
        outline(&editor),
        (strings(&["aa", "bb", "cc"]), strings(&["bb", "cc"]))
    );
    editor.dispose();
}

// upstream: lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts::$wrapNodes with a block leaf in an element that is not a block container (#9638) > control: a rule at the root
#[test]
fn control_a_rule_at_the_root() {
    let editor = build();
    wrap(&editor, || {
        let bb = create_text_node("bb");
        let cc = create_text_node("cc");
        set_root(&[
            paragraph_of(&create_text_node("aa")),
            paragraph_of(&bb),
            create_horizontal_rule_node(),
            paragraph_of(&cc),
        ]);
        [(bb, 1), (cc, 2)]
    });
    assert_eq!(
        outline(&editor),
        (
            strings(&["aa", "bb", "<hr>", "cc"]),
            strings(&["bb", "<hr>", "cc"])
        )
    );
    editor.dispose();
}

// upstream: lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts::$wrapNodes with a block leaf in an element that is not a block container (#9638) > property: random box, rule and paragraph mixes
#[test]
fn property_random_box_rule_and_paragraph_mixes() {
    assert_eq!(wrap_failures(false), Vec::<String>::new());
}

// With text directly in a box next to its blocks, $wrapNodes reorders text
// or never returns in some documents on main too (#9665): pinned until that
// is fixed (upstream's `test.fails`).
// upstream: lexical-selection/src/__tests__/unit/WrapNodesBlockLeaf.test.ts::$wrapNodes with a block leaf in an element that is not a block container (#9638) > property: with text directly in a box (#9665)
#[test]
fn property_with_text_directly_in_a_box() {
    assert_ne!(wrap_failures(true), Vec::<String>::new());
}