#![cfg(feature = "html")]
use crate::clipboard::{
ExportMimeTypeConfig, ExportMimeTypeFunction, GetClipboardDataConfigOverrides,
LEXICAL_MIME_TYPE, LexicalClipboardData, export_mime_type_from_selection,
get_clipboard_data_extension, get_clipboard_data_from_selection,
set_lexical_clipboard_data_transfer,
};
use crate::extension::{LexicalEditorWithDispose, build_editor_from_extensions};
use crate::{
DataTransfer, InitialEditorStateType, LexicalExtension, config_extension,
create_paragraph_node, create_text_node, define_extension, get_root, get_selection,
};
use std::cell::RefCell;
use std::rc::Rc;
const SEED_TEXT: &str = "hello world";
fn initial_editor_state() -> InitialEditorStateType {
InitialEditorStateType::Update(Rc::new(|_| {
let text = create_text_node(SEED_TEXT);
let paragraph = create_paragraph_node();
paragraph.append(std::slice::from_ref(&text))?;
get_root().append(&[paragraph])?;
text.select(Some(0), Some(SEED_TEXT.len()))?;
Ok(())
}))
}
fn build_seeded_editor(export_mime_type: Option<ExportMimeTypeConfig>) -> LexicalEditorWithDispose {
build_editor_from_extensions([define_extension(LexicalExtension {
initial_editor_state: Some(initial_editor_state()),
dependencies: vec![config_extension(
&get_clipboard_data_extension(),
[GetClipboardDataConfigOverrides { export_mime_type }],
)],
..LexicalExtension::<(), (), ()>::new("[test-root]", ())
})
.into()])
.unwrap()
}
fn stack(mime_type: &str, fns: Vec<ExportMimeTypeFunction>) -> Option<ExportMimeTypeConfig> {
Some(vec![(mime_type.to_owned(), fns)])
}
fn get<'a>(data: &'a LexicalClipboardData, mime_type: &str) -> Option<&'a str> {
data.iter()
.find(|(k, _)| k == mime_type)
.map(|(_, v)| v.as_str())
}
fn clipboard_data(editor: &LexicalEditorWithDispose) -> LexicalClipboardData {
editor
.read(|| get_clipboard_data_from_selection(get_selection().as_ref()).unwrap())
.unwrap()
}
#[test]
fn produces_default_entries() {
let editor = build_seeded_editor(None);
let data = clipboard_data(&editor);
assert_eq!(get(&data, "text/plain"), Some(SEED_TEXT));
assert!(get(&data, "text/html").unwrap().contains(SEED_TEXT));
let lexical_payload = get(&data, LEXICAL_MIME_TYPE).expect("a string");
let parsed = crate::json::parse(lexical_payload).unwrap();
let parsed = parsed.as_object().unwrap();
assert_eq!(
parsed.get("namespace").and_then(|n| n.as_str()),
Some(editor.get_namespace())
);
let nodes = parsed
.get("nodes")
.and_then(|n| n.as_array())
.expect("an array");
assert!(!nodes.is_empty());
editor.dispose();
}
#[test]
fn lets_an_override_replace_the_default_output() {
let editor = build_seeded_editor(stack(
"text/plain",
vec![Rc::new(|_, _| Ok(Some("OVERRIDDEN".to_owned())))],
));
let data = clipboard_data(&editor);
assert_eq!(get(&data, "text/plain"), Some("OVERRIDDEN"));
assert!(get(&data, "text/html").unwrap().contains(SEED_TEXT));
editor.dispose();
}
#[test]
fn falls_through_to_the_default_handler_when_the_override_calls_next() {
let editor = build_seeded_editor(stack(
"text/plain",
vec![Rc::new(|_, next| {
Ok(Some(format!("[wrapped] {}", next()?.unwrap_or_default())))
})],
));
let data = clipboard_data(&editor);
assert_eq!(
get(&data, "text/plain"),
Some(format!("[wrapped] {SEED_TEXT}").as_str())
);
editor.dispose();
}
#[test]
fn omits_a_mime_type_when_its_top_handler_returns_null() {
let editor = build_seeded_editor(stack("text/html", vec![Rc::new(|_, _| Ok(None))]));
let data = clipboard_data(&editor);
assert_eq!(get(&data, "text/html"), None);
assert_eq!(get(&data, "text/plain"), Some(SEED_TEXT));
assert!(get(&data, LEXICAL_MIME_TYPE).is_some());
editor.dispose();
}
fn my_format(text: &str) -> String {
let mut json = crate::json::Object::new();
json.insert("text", text.into());
json.insert("v", 1.0.into());
crate::json::stringify(&json.into()).unwrap()
}
#[test]
fn registers_a_brand_new_custom_mime_type() {
let editor = build_seeded_editor(stack(
"application/x-myformat",
vec![Rc::new(|selection, _| {
Ok(match selection {
Some(selection) => Some(my_format(&selection.get_text_content()?)),
None => None,
})
})],
));
let data = clipboard_data(&editor);
assert_eq!(
get(&data, "application/x-myformat"),
Some(my_format(SEED_TEXT).as_str())
);
assert_eq!(my_format(SEED_TEXT), r#"{"text":"hello world","v":1}"#);
assert_eq!(get(&data, "text/plain"), Some(SEED_TEXT));
assert!(get(&data, "text/html").unwrap().contains(SEED_TEXT));
editor.dispose();
}
#[test]
fn runs_higher_indexed_handlers_first_within_a_single_mime_stack() {
let calls = Rc::new(RefCell::new(Vec::<&str>::new()));
let (a, b) = (calls.clone(), calls.clone());
let editor = build_seeded_editor(stack(
"text/plain",
vec![
Rc::new(move |_, next| {
a.borrow_mut().push("A");
Ok(Some(format!("A({})", next()?.unwrap_or_default())))
}),
Rc::new(move |_, next| {
b.borrow_mut().push("B");
Ok(Some(format!("B({})", next()?.unwrap_or_default())))
}),
],
));
let data = clipboard_data(&editor);
assert_eq!(
get(&data, "text/plain"),
Some(format!("B(A({SEED_TEXT}))").as_str())
);
assert_eq!(*calls.borrow(), ["B", "A"]);
editor.dispose();
}
fn custom_payload() -> Option<ExportMimeTypeConfig> {
stack(
"application/x-myformat",
vec![Rc::new(|_, _| Ok(Some("custom-payload".to_owned())))],
)
}
fn export_mime_type(mime_type: &str) -> Option<String> {
export_mime_type_from_selection(mime_type, get_selection().as_ref()).unwrap()
}
#[test]
fn export_mime_type_returns_the_configured_stack_output() {
let editor = build_seeded_editor(custom_payload());
editor
.read(|| {
assert_eq!(export_mime_type("text/plain").as_deref(), Some(SEED_TEXT));
assert_eq!(
export_mime_type("application/x-myformat").as_deref(),
Some("custom-payload")
);
})
.unwrap();
editor.dispose();
}
#[test]
fn export_mime_type_returns_null_for_mime_types_with_no_handler() {
let editor = build_seeded_editor(None);
editor
.read(|| assert_eq!(export_mime_type("application/x-unknown"), None))
.unwrap();
editor.dispose();
}
#[test]
fn export_mime_type_uses_the_default_config_without_the_extension() {
let editor = build_editor_from_extensions([define_extension(LexicalExtension {
initial_editor_state: Some(initial_editor_state()),
..LexicalExtension::<(), (), ()>::new("[test-root]", ())
})
.into()])
.unwrap();
editor
.read(|| {
assert_eq!(export_mime_type("text/plain").as_deref(), Some(SEED_TEXT));
assert!(export_mime_type("text/html").unwrap().contains(SEED_TEXT));
})
.unwrap();
editor.dispose();
}
#[test]
fn set_lexical_clipboard_data_transfer_wires_custom_mime_types() {
let editor = build_seeded_editor(custom_payload());
let dt = DataTransfer::new();
let data = clipboard_data(&editor);
set_lexical_clipboard_data_transfer(&dt, &data);
assert_eq!(dt.get_data("text/plain"), SEED_TEXT);
assert!(dt.get_data("text/html").contains(SEED_TEXT));
assert_eq!(dt.get_data("application/x-myformat"), "custom-payload");
editor.dispose();
}