From 5f2453e4463f224a8418ec50d502867552bb4c0d Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 22 Apr 2025 11:21:01 -0700 Subject: [PATCH] Migrate check_link_target_js to BookTest --- tests/rendered_output.rs | 37 ------------------------------------- tests/testsuite/toc.rs | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 38 deletions(-) diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index 9b8d3579..f27c436e 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -120,28 +120,6 @@ fn entry_ends_with(entry: &DirEntry, ending: &str) -> bool { entry.file_name().to_string_lossy().ends_with(ending) } -/// Read the TOC (`book/toc.js`) nested HTML and expose it as a DOM which we -/// can search with the `select` crate -fn toc_js_html() -> Result { - let temp = DummyBook::new() - .build() - .with_context(|| "Couldn't create the dummy book")?; - MDBook::load(temp.path())? - .build() - .with_context(|| "Book building failed")?; - - let toc_path = temp.path().join("book").join("toc.js"); - let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?; - for line in html.lines() { - if let Some(left) = line.strip_prefix(" this.innerHTML = '") { - if let Some(html) = left.strip_suffix("';") { - return Ok(Document::from(html)); - } - } - } - panic!("cannot find toc in file") -} - /// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we /// can search with the `select` crate fn toc_fallback_html() -> Result { @@ -157,21 +135,6 @@ fn toc_fallback_html() -> Result { Ok(Document::from(html.as_str())) } -// don't use target="_parent" in JS -#[test] -fn check_link_target_js() { - let doc = toc_js_html().unwrap(); - - let num_parent_links = doc - .find( - Class("chapter") - .descendant(Name("li")) - .descendant(Name("a").and(Attr("target", "_parent"))), - ) - .count(); - assert_eq!(num_parent_links, 0); -} - // don't use target="_parent" in IFRAME #[test] fn check_link_target_fallback() { diff --git a/tests/testsuite/toc.rs b/tests/testsuite/toc.rs index 3cc32e77..4d4743a8 100644 --- a/tests/testsuite/toc.rs +++ b/tests/testsuite/toc.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use select::document::Document; -use select::predicate::{Class, Name, Predicate}; +use select::predicate::{Attr, Class, Name, Predicate}; const TOC_TOP_LEVEL: &[&str] = &[ "1. With Readme", @@ -95,3 +95,18 @@ fn check_spacers() { .count(); assert_eq!(num_spacers, should_be); } + +// don't use target="_parent" in JS +#[test] +fn check_link_target_js() { + let doc = toc_js_html(); + + let num_parent_links = doc + .find( + Class("chapter") + .descendant(Name("li")) + .descendant(Name("a").and(Attr("target", "_parent"))), + ) + .count(); + assert_eq!(num_parent_links, 0); +}