//! Mandatory test 2 (the core's half): a highlight's position survives re-rendering and re-import, and a changed //! edition degrades to "approximate" or "lost", never to other text. Font size and theme do not exist here: they //! never reach the tree (the app's test renders the same chapter under different settings). mod common; use common::*; use liber_format::position::{Point, anchor, offset_of, point_at, resolve}; use liber_format::{Limits, Resolved, open}; const CH0: &str = "

Prolog

Úvodní slovo.

"; const CH1: &str = "

Kapitola první

\

Bylo to v temné a bouřlivé noci, když 😀 robot poprvé promluvil.

\

Druhý odstavec s 1 poznámkou.

\ "; const CH2: &str = "

Kapitola druhá

Konec.

"; fn book(ch1: &str) -> Vec { Epub::new("R.U.R.").chapter("Prolog", CH0).chapter("Kapitola první", ch1).chapter("Kapitola druhá", CH2).build() } fn find16(text: &str, needle: &str) -> (usize, usize) { let b = text.find(needle).expect("needle"); let s = text[..b].encode_utf16().count(); (s, s + needle.encode_utf16().count()) } fn quote_at(doc: &liber_format::Document, r: &Resolved) -> String { match r { Resolved::Exact { chapter, start, end } | Resolved::Approximate { chapter, start, end } => { let t: Vec = doc.chapters[*chapter].text().encode_utf16().collect(); String::from_utf16(&t[*start..*end]).unwrap() } Resolved::Lost => String::new(), } } #[test] fn cfi_round_trips_on_every_text_offset() { let d = open(&book(CH1), None, &Limits::default()).unwrap(); let ch = &d.chapters[1]; let n = ch.text_len; for off in 0..=n { for end in [false, true] { let p = point_at(1, &ch.root, off, end); let parsed = Point::parse(&p.to_cfi()).unwrap(); assert_eq!(parsed, p); // Offsets inside a surrogate pair do not exist in the text; every other offset maps back. let t = ch.text(); if liber_format::text::byte_at_utf16(&t, off).is_some() { assert_eq!(offset_of(&ch.root, &parsed), Some(off), "offset {off} cfi {}", p.to_cfi()); } } } } #[test] fn cfi_shape() { let d = open(&book(CH1), None, &Limits::default()).unwrap(); let t = d.chapters[1].text(); let (s, e) = find16(&t, "temné a bouřlivé"); let a = anchor(&d, 1, s, e).unwrap(); // Chapter 1 → /6/4; root /4; the first

is the second element (after

): /4; is its first element: /2. assert_eq!(a.start, "epubcfi(/6/4!/4/4/2/1:0)"); assert_eq!(a.end, "epubcfi(/6/4!/4/4/2/1:16)"); assert_eq!(a.quote.exact, "temné a bouřlivé"); assert_eq!(a.quote.prefix, "Kapitola prvníBylo to v "); assert!(a.quote.suffix.starts_with(" noci, když 😀")); } #[test] fn a_highlight_survives_reimport_of_the_same_file() { let bytes = book(CH1); let d1 = open(&bytes, None, &Limits::default()).unwrap(); let t = d1.chapters[1].text(); let (s, e) = find16(&t, "noci, když 😀 robot"); let a = anchor(&d1, 1, s, e).unwrap(); for _ in 0..3 { let d2 = open(&bytes, None, &Limits::default()).unwrap(); assert_eq!(d2.chapters[1].xhtml, d1.chapters[1].xhtml, "the sanitizer is deterministic"); assert_eq!(resolve(&d2, &a), Resolved::Exact { chapter: 1, start: s, end: e }); } } #[test] fn a_highlight_across_elements() { let d = open(&book(CH1), None, &Limits::default()).unwrap(); let t = d.chapters[1].text(); let (s, e) = find16(&t, "v temné a bouřlivé noci"); let a = anchor(&d, 1, s, e).unwrap(); assert_ne!(Point::parse(&a.start).unwrap().steps, Point::parse(&a.end).unwrap().steps); let r = resolve(&d, &a); assert_eq!(r, Resolved::Exact { chapter: 1, start: s, end: e }); assert_eq!(quote_at(&d, &r), "v temné a bouřlivé noci"); } #[test] fn a_changed_edition_gives_approximate_at_the_same_words() { let d1 = open(&book(CH1), None, &Limits::default()).unwrap(); let t = d1.chapters[1].text(); let (s, e) = find16(&t, "robot poprvé promluvil"); let a = anchor(&d1, 1, s, e).unwrap(); // The new edition adds a paragraph before it and re-wraps the emphasis. let edited = CH1 .replace("

Bylo to", "

Nový úvodní odstavec.

Bylo to") .replace("temné a bouřlivé", "temné a bouřlivé"); let d2 = open(&book(&edited), None, &Limits::default()).unwrap(); let r = resolve(&d2, &a); assert!(matches!(r, Resolved::Approximate { chapter: 1, .. }), "{r:?}"); assert_eq!(quote_at(&d2, &r), "robot poprvé promluvil"); } #[test] fn a_moved_chapter_is_found_in_another_chapter() { let d1 = open(&book(CH1), None, &Limits::default()).unwrap(); let t = d1.chapters[1].text(); let (s, e) = find16(&t, "Druhý odstavec"); let a = anchor(&d1, 1, s, e).unwrap(); let moved = Epub::new("R.U.R.") .chapter("Kapitola první", CH1) .chapter("Prolog", CH0) .chapter("Kapitola druhá", CH2) .build(); let d2 = open(&moved, None, &Limits::default()).unwrap(); let r = resolve(&d2, &a); assert!(matches!(r, Resolved::Approximate { chapter: 0, .. }), "{r:?}"); assert_eq!(quote_at(&d2, &r), "Druhý odstavec"); } #[test] fn removed_text_is_lost_never_elsewhere() { let d1 = open(&book(CH1), None, &Limits::default()).unwrap(); let t = d1.chapters[1].text(); let (s, e) = find16(&t, "robot poprvé promluvil"); let a = anchor(&d1, 1, s, e).unwrap(); // Same length, same structure, different words: the CFI still resolves, but to other text. let reworded = CH1.replace("robot poprvé promluvil", "stroj zase mlčel dlouho"); let d2 = open(&book(&reworded), None, &Limits::default()).unwrap(); assert_eq!(resolve(&d2, &a), Resolved::Lost); } #[test] fn an_ambiguous_quote_is_lost() { let ch = "

ano ano

ano ano

"; let d1 = open(&Epub::new("A").chapter("A", ch).build(), None, &Limits::default()).unwrap(); let a = anchor(&d1, 0, 0, 3).unwrap(); // The same file: exact. assert!(matches!(resolve(&d1, &a), Resolved::Exact { .. })); // A new edition where the context no longer tells the occurrences apart. let d2 = open(&Epub::new("A").chapter("A", "

x

ano ano

ano ano

").build(), None, &Limits::default()) .unwrap(); let r = resolve(&d2, &a); assert!(r == Resolved::Lost || quote_at(&d2, &r) == "ano", "{r:?}"); } #[test] fn footnotes_and_links() { let d = open(&book(CH1), None, &Limits::default()).unwrap(); assert!(d.chapters[1].xhtml.contains("data-href=\"1#n1\""), "{}", d.chapters[1].xhtml); assert_eq!(d.footnotes.get("1#n1").map(|s| s.contains("Poznámka pod čarou")), Some(true)); }