use crate::{
CreateEditorArgs, LexicalError, PointType, UpdateOptions, Which, create_editor,
create_paragraph_node, create_range_selection, create_text_node, get_root, set_slot,
};
fn run(f: impl FnOnce() -> Result<(), LexicalError> + 'static) {
let editor = create_editor(CreateEditorArgs {
namespace: "caret-selection-test".into(),
..CreateEditorArgs::default()
});
editor
.update(
f,
UpdateOptions {
discrete: true,
..UpdateOptions::default()
},
)
.unwrap();
}
fn ends(backward: bool) -> (Which, Which) {
if backward {
(Which::Focus, Which::Anchor)
} else {
(Which::Anchor, Which::Focus)
}
}
#[test]
fn selected_text_ends_at_the_same_boundary_for_an_element_or_text_point() {
for backward in [false, true] {
run(move || {
let first = create_text_node("abcd");
let second = create_text_node("efgh");
second.set_format_type("italic");
let third = create_text_node("ijkl");
third.set_format_type("bold");
let paragraph = create_paragraph_node();
paragraph.append(&[first.clone(), second.clone(), third])?;
get_root().clear()?;
get_root().append(std::slice::from_ref(¶graph))?;
let selection = create_range_selection();
let (start, end) = ends(backward);
selection.point_set(start, first.get_key(), 1, PointType::Text)?;
selection.point_set(end, second.get_key(), 4, PointType::Text)?;
assert_eq!(selection.get_text_content()?, "bcdefgh");
selection.point_set(end, paragraph.get_key(), 2, PointType::Element)?;
assert_eq!(selection.get_text_content()?, "bcdefgh");
assert_eq!(paragraph.get_text_content(), "abcdefghijkl");
Ok(())
});
}
}
#[test]
fn equivalent_mixed_text_and_element_points_contain_no_selected_text() {
for backward in [false, true] {
run(move || {
let first = create_text_node("abcd");
let second = create_text_node("efgh");
second.set_format_type("italic");
let paragraph = create_paragraph_node();
paragraph.append(&[first.clone(), second])?;
get_root().clear()?;
get_root().append(std::slice::from_ref(¶graph))?;
let selection = create_range_selection();
let (start, end) = ends(backward);
selection.point_set(start, first.get_key(), 4, PointType::Text)?;
selection.point_set(end, paragraph.get_key(), 1, PointType::Element)?;
assert_eq!(selection.get_text_content()?, "");
assert_eq!(paragraph.get_text_content(), "abcdefgh");
Ok(())
});
}
}
#[test]
fn collapsed_element_point_beside_a_slot_host_contains_no_text() {
for offset in [0, 1] {
run(move || {
let root = get_root();
let host = create_paragraph_node();
root.clear()?;
root.append(std::slice::from_ref(&host))?;
let title = create_paragraph_node();
title.append(&[create_text_node("Title")])?;
set_slot(&host, "title", &title)?;
let selection = create_range_selection();
selection.point_set(Which::Anchor, root.get_key(), offset, PointType::Element)?;
selection.point_set(Which::Focus, root.get_key(), offset, PointType::Element)?;
assert_eq!(
selection.get_nodes()?,
std::slice::from_ref(&host),
"offset={offset}"
);
assert_eq!(selection.get_text_content()?, "");
assert_eq!(host.get_text_content(), "Title");
Ok(())
});
}
}