use text_document::TextDocument;
fn char_pos_of(doc: &TextDocument, needle: &str) -> usize {
let text = doc.to_plain_text().expect("plain text");
let byte = text.find(needle).unwrap_or_else(|| {
panic!("{needle:?} not found in {text:?}");
});
text[..byte].chars().count()
}
#[test]
fn an_image_inserted_inside_a_blockquote_stays_in_the_blockquote() {
let doc = TextDocument::new();
doc.set_djot_sync("Before the quote.\n\n> quoted words here\n\nAfter the quote.\n")
.expect("import");
let pos = char_pos_of(&doc, "quoted") + "quoted".len();
doc.add_resource(
text_document::ResourceType::Image,
"pic.png",
"image/png",
b"fake",
)
.expect("resource");
doc.cursor_at(pos)
.insert_image("pic.png", "a picture", 32, 32)
.expect("insert");
let djot = doc.to_djot().expect("export");
let quoted_line = djot
.lines()
.find(|l| l.contains("quoted"))
.unwrap_or_else(|| panic!("no quoted line in {djot:?}"));
assert!(
quoted_line.starts_with('>'),
"the image must stay on the blockquote line, got {djot:?}"
);
assert!(
quoted_line.contains("pic.png"),
"the image landed outside the quoted block: {djot:?}"
);
}
fn assert_image_lands_where_text_does(source: &str, position: usize) {
let with_text = TextDocument::new();
with_text.set_djot_sync(source).expect("import");
with_text
.cursor_at(position)
.insert_text("\u{FFFC}")
.expect("insert text");
let with_image = TextDocument::new();
with_image.set_djot_sync(source).expect("import");
with_image
.add_resource(
text_document::ResourceType::Image,
"pic.png",
"image/png",
b"fake",
)
.expect("resource");
with_image
.cursor_at(position)
.insert_image("pic.png", "", 16, 16)
.expect("insert image");
assert_eq!(
with_image.to_plain_text().expect("plain text"),
with_text.to_plain_text().expect("plain text"),
"image and text disagree about position {position} in {source:?}"
);
}
#[test]
fn an_image_inserted_inside_a_table_cell_stays_in_the_cell() {
let source = "| alpha | beta |\n|---|---|\n| gamma | delta |\n";
let doc = TextDocument::new();
doc.set_djot_sync(source).expect("import");
let count = doc.character_count();
drop(doc);
for position in 0..=count {
assert_image_lands_where_text_does(source, position);
}
}
#[test]
fn an_image_inserted_anywhere_in_a_blockquote_agrees_with_text_insertion() {
let source = "Before.\n\n> quoted words here\n\nAfter.\n";
let doc = TextDocument::new();
doc.set_djot_sync(source).expect("import");
let count = doc.character_count();
drop(doc);
for position in 0..=count {
assert_image_lands_where_text_does(source, position);
}
}
#[test]
fn an_image_in_a_plain_paragraph_still_works() {
let doc = TextDocument::new();
doc.set_djot_sync("just a paragraph\n").expect("import");
let pos = char_pos_of(&doc, "just") + "just".len();
doc.add_resource(
text_document::ResourceType::Image,
"flat.png",
"image/png",
b"fake",
)
.expect("resource");
doc.cursor_at(pos)
.insert_image("flat.png", "", 8, 8)
.expect("insert");
let count = doc.character_count();
let cursor = doc.cursor_at(0);
cursor.set_position(count, text_document::MoveMode::KeepAnchor);
let text = cursor.selected_text().expect("selection");
assert_eq!(text.find('\u{FFFC}'), Some("just".len()));
}