use super::{OpenLink, RunMarks};
use crate::body::links::{Hyperlink, LinkMark, LinkTarget};
use crate::body::{Content, Paragraph, Run};
use crate::layout::RunSpan;
use crate::text_map::{StoryBookmark, StoryLink};
fn text(text: &str) -> Run {
Run {
content: vec![Content::Text(text.to_owned())],
..Run::default()
}
}
fn start(bookmark: &str) -> Run {
let link = Hyperlink {
anchor: Some(bookmark.to_owned()),
..Hyperlink::default()
};
Run {
links: vec![LinkMark::Start(Box::new(link))],
..Run::default()
}
}
fn end() -> Run {
Run {
links: vec![LinkMark::End],
..Run::default()
}
}
fn paragraph(runs: Vec<Run>) -> Paragraph {
Paragraph {
runs,
..Paragraph::empty()
}
}
fn spans(spans: &[(u32, u32, Option<u32>)]) -> Vec<RunSpan> {
(spans.iter())
.map(|&(start, end, run)| RunSpan { start, end, run })
.collect()
}
fn link(start: u32, end: u32, bookmark: &str) -> StoryLink {
StoryLink::new(start, end, LinkTarget::Bookmark(bookmark.to_owned()), None)
}
#[test]
fn places_a_link_over_the_glyphs_of_its_runs_and_a_bookmark_at_the_next_glyph() {
let bookmark = Run {
content: vec![Content::Bookmark("after".to_owned())],
..Run::default()
};
let runs = vec![
text("See "),
start("there"),
text("here"),
end(),
bookmark,
text(" now"),
];
let marks = RunMarks::of(¶graph(runs), &mut Vec::new());
let placed = marks.placed(
&spans(&[(0, 4, Some(0)), (4, 8, Some(2)), (8, 12, Some(5))]),
12,
);
assert_eq!(placed.0, [link(4, 8, "there")]);
assert_eq!(placed.1, [StoryBookmark::new("after".to_owned(), 8)]);
}
#[test]
fn gives_nested_links_outer_first_and_leaves_out_a_link_without_glyphs() {
let runs = vec![
start("entry"),
text("Entry\t"),
start("page"),
text("7"),
end(),
end(),
start("empty"),
end(),
];
let marks = RunMarks::of(¶graph(runs), &mut Vec::new());
let placed = marks.placed(&spans(&[(0, 5, Some(1)), (6, 7, Some(3))]), 7);
assert_eq!(placed.0, [link(0, 7, "entry"), link(6, 7, "page")]);
}
#[test]
fn runs_a_link_on_into_the_next_paragraph_to_its_end() {
let mut open: Vec<OpenLink> = Vec::new();
let first = RunMarks::of(
¶graph(vec![text("ab"), start("on"), text("cd")]),
&mut open,
);
assert_eq!(open.len(), 1);
let second = RunMarks::of(¶graph(vec![text("ef"), end(), text("gh")]), &mut open);
assert_eq!(open, Vec::<OpenLink>::new());
let line = spans(&[(0, 2, Some(0)), (2, 4, Some(2))]);
assert_eq!(first.placed(&line, 4).0, [link(2, 4, "on")]);
assert_eq!(second.placed(&line, 4).0, [link(0, 2, "on")]);
}
#[test]
fn places_a_bookmark_of_the_first_run_after_the_list_label_and_one_at_the_end_after_the_text() {
let bookmark = |name: &str| Run {
content: vec![Content::Bookmark(name.to_owned())],
..Run::default()
};
let runs = vec![bookmark("top"), text("abc"), bookmark("end")];
let marks = RunMarks::of(¶graph(runs), &mut Vec::new());
let placed = marks.placed(&spans(&[(0, 2, None), (2, 5, Some(1))]), 5);
assert_eq!(
placed.1,
[
StoryBookmark::new("top".to_owned(), 2),
StoryBookmark::new("end".to_owned(), 5)
]
);
assert_eq!(placed.0, Vec::<StoryLink>::new());
}