#![cfg(all(feature = "extension", feature = "rich-text"))]
use super::super::support::{
TestBlockableDecoratorNode, TestShadowRootNode, create_test_decorator_node_inline,
create_test_shadow_root_node, range_selection,
};
use crate::extension::{LexicalEditorWithDispose, build_editor_from_extensions};
use crate::selection_deletion::needs_block_cursor_beside;
use crate::{
LexicalError, LexicalExtension, NodeClass, NodeRef, PointType, UpdateOptions, Which,
create_paragraph_node, create_text_node, define_extension, get_root,
};
use std::cell::RefCell;
use std::rc::Rc;
fn ext() -> LexicalEditorWithDispose {
build_editor_from_extensions([define_extension(LexicalExtension {
dependencies: vec![crate::rich_text::rich_text_extension().into()],
nodes: vec![
NodeClass::of::<TestBlockableDecoratorNode>(),
NodeClass::of::<TestShadowRootNode>(),
],
..LexicalExtension::<(), (), ()>::new("[8939]", ())
})
.into()])
.unwrap()
}
fn update(
editor: &LexicalEditorWithDispose,
f: impl FnOnce() -> Result<(), LexicalError> + 'static,
) {
editor
.update(
f,
UpdateOptions {
discrete: true,
..UpdateOptions::default()
},
)
.unwrap();
}
fn block_decorator() -> NodeRef {
create_test_decorator_node_inline(false)
}
fn shadow_root(label: &str) -> Result<NodeRef, LexicalError> {
let paragraph = create_paragraph_node();
paragraph.append(&[create_text_node(label)])?;
let shadow = create_test_shadow_root_node();
shadow.append(&[paragraph])?;
Ok(shadow)
}
#[derive(Clone, Copy, PartialEq)]
enum Kind {
Decorator,
ShadowRoot,
}
fn make(kind: Kind, label: &str) -> Result<NodeRef, LexicalError> {
match kind {
Kind::Decorator => Ok(block_decorator()),
Kind::ShadowRoot => shadow_root(label),
}
}
fn set_root(children: &[NodeRef]) -> Result<(), LexicalError> {
let root = get_root();
root.clear()?;
root.append(children)
}
fn delete_character(editor: &LexicalEditorWithDispose, is_backward: bool) {
update(editor, move || {
range_selection().delete_character(is_backward)
});
}
fn anchor_key() -> String {
range_selection().range().unwrap().anchor.key
}
fn deletes_the_adjacent(kind: Kind, is_backward: bool) {
let editor = ext();
let keys = Rc::new(RefCell::new(Vec::<String>::new()));
let k = keys.clone();
update(&editor, move || {
let a = make(kind, "x")?;
let b = make(kind, "x")?;
k.borrow_mut()
.extend([a.get_key().to_owned(), b.get_key().to_owned()]);
set_root(&[a, b])?;
get_root().select(Some(1), Some(1))?;
Ok(())
});
let keys = keys.take();
editor
.read(|| {
let root = get_root();
assert!(needs_block_cursor_beside(
&root.get_child_at_index(0).unwrap()
));
assert!(needs_block_cursor_beside(
&root.get_child_at_index(1).unwrap()
));
})
.unwrap();
delete_character(&editor, is_backward);
editor
.read(|| {
assert_eq!(get_root().get_children_size(), 1);
assert_eq!(
get_root().get_child_at_index(0).unwrap().get_key(),
if is_backward { &keys[1] } else { &keys[0] }
);
})
.unwrap();
}
#[test]
fn deletes_the_adjacent_decorator_backward() {
deletes_the_adjacent(Kind::Decorator, true);
}
#[test]
fn deletes_the_adjacent_decorator_forward() {
deletes_the_adjacent(Kind::Decorator, false);
}
#[test]
fn deletes_the_adjacent_shadow_root_backward() {
deletes_the_adjacent(Kind::ShadowRoot, true);
}
#[test]
fn deletes_the_adjacent_shadow_root_forward() {
deletes_the_adjacent(Kind::ShadowRoot, false);
}
#[test]
fn backspace_after_the_last_shadow_root_deletes_it() {
let editor = ext();
update(&editor, || {
set_root(&[shadow_root("A")?, shadow_root("B")?])?;
get_root().select(Some(2), Some(2))?;
Ok(())
});
delete_character(&editor, true);
editor
.read(|| {
assert_eq!(get_root().get_children_size(), 1);
assert_eq!(get_root().get_text_content(), "A");
})
.unwrap();
}
fn deleting_the_only_node_leaves_the_root_editable(kind: Kind) {
let editor = ext();
update(&editor, move || {
set_root(&[make(kind, "A")?])?;
get_root().select(Some(1), Some(1))?;
Ok(())
});
delete_character(&editor, true);
editor
.read(|| {
let children = get_root().get_children();
assert_eq!(children.len(), 1);
let paragraph = &children[0];
assert_eq!(paragraph.get_type(), "paragraph");
assert!(paragraph.is_empty());
assert_eq!(anchor_key(), paragraph.get_key());
})
.unwrap();
update(&editor, || {
let selection = range_selection();
selection.insert_paragraph()?;
selection.insert_text("X")
});
editor
.read(|| {
let children = get_root().get_children();
assert!(children.iter().all(|c| c.get_type() == "paragraph"));
let texts: Vec<String> = children.iter().map(NodeRef::get_text_content).collect();
assert_eq!(texts, ["", "X"]);
})
.unwrap();
}
#[test]
fn deleting_the_only_decorator_leaves_the_root_editable() {
deleting_the_only_node_leaves_the_root_editable(Kind::Decorator);
}
#[test]
fn deleting_the_only_shadow_root_leaves_the_root_editable() {
deleting_the_only_node_leaves_the_root_editable(Kind::ShadowRoot);
}
#[test]
fn deleting_the_only_block_inside_a_shadow_root_keeps_it_editable() {
let editor = ext();
update(&editor, || {
let shadow = create_test_shadow_root_node();
shadow.append(&[block_decorator()])?;
set_root(std::slice::from_ref(&shadow))?;
shadow.select(Some(1), Some(1))?;
Ok(())
});
delete_character(&editor, true);
editor
.read(|| {
let shadow = get_root().get_first_child().unwrap();
assert_eq!(shadow.get_type(), "test_shadow_root");
assert_eq!(shadow.get_children_size(), 1);
let paragraph = shadow.get_first_child().unwrap();
assert_eq!(paragraph.get_type(), "paragraph");
assert!(paragraph.is_empty());
assert_eq!(anchor_key(), paragraph.get_key());
})
.unwrap();
}
#[test]
fn backspace_in_an_empty_block_after_a_shadow_root_removes_the_block() {
let editor = ext();
update(&editor, || {
let paragraph = create_paragraph_node();
set_root(&[shadow_root("A")?, paragraph.clone()])?;
paragraph.select(Some(0), Some(0))?;
Ok(())
});
delete_character(&editor, true);
editor
.read(|| {
assert_eq!(get_root().get_children_size(), 1);
assert_eq!(get_root().get_text_content(), "A");
})
.unwrap();
}
fn enter_over_all_of_a_shadow_root(element_points: bool) {
let editor = ext();
update(&editor, move || {
let shadow = create_test_shadow_root_node();
let paragraph = create_paragraph_node();
paragraph.append(&[create_text_node("inside")])?;
shadow.append(std::slice::from_ref(¶graph))?;
set_root(std::slice::from_ref(&shadow))?;
if element_points {
shadow.select(Some(0), Some(shadow.get_children_size()))?;
} else {
let selection = paragraph.select(Some(0), Some(0))?;
selection.point_set(
Which::Focus,
paragraph.get_first_child_or_throw()?.get_key(),
"inside".len(),
PointType::Text,
)?;
}
Ok(())
});
update(&editor, || {
range_selection().insert_paragraph()?;
Ok(())
});
editor
.read(|| {
let shadow = get_root().get_first_child().unwrap();
assert_eq!(shadow.get_type(), "test_shadow_root");
let paragraphs = shadow.get_children();
assert!(paragraphs.iter().all(|p| p.get_type() == "paragraph"));
assert_eq!(
paragraphs.iter().map(NodeRef::is_empty).collect::<Vec<_>>(),
[true, true]
);
assert_eq!(anchor_key(), paragraphs[1].get_key());
})
.unwrap();
}
#[test]
fn enter_over_all_of_a_shadow_root_with_element_points() {
enter_over_all_of_a_shadow_root(true);
}
#[test]
fn enter_over_all_of_a_shadow_root_with_text_points() {
enter_over_all_of_a_shadow_root(false);
}
#[test]
fn a_paragraph_does_not_need_a_block_cursor_beside_it() {
let editor = ext();
update(&editor, || {
let paragraph = create_paragraph_node();
paragraph.append(&[create_text_node("A")])?;
set_root(&[paragraph])
});
editor
.read(|| {
assert!(!needs_block_cursor_beside(
&get_root().get_child_at_index(0).unwrap()
));
})
.unwrap();
}