Skip to content

Commit 5f2453e

Browse files
committed
Migrate check_link_target_js to BookTest
1 parent 20f71af commit 5f2453e

File tree

2 files changed

+16
-38
lines changed

2 files changed

+16
-38
lines changed

tests/rendered_output.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,6 @@ fn entry_ends_with(entry: &DirEntry, ending: &str) -> bool {
120120
entry.file_name().to_string_lossy().ends_with(ending)
121121
}
122122

123-
/// Read the TOC (`book/toc.js`) nested HTML and expose it as a DOM which we
124-
/// can search with the `select` crate
125-
fn toc_js_html() -> Result<Document> {
126-
let temp = DummyBook::new()
127-
.build()
128-
.with_context(|| "Couldn't create the dummy book")?;
129-
MDBook::load(temp.path())?
130-
.build()
131-
.with_context(|| "Book building failed")?;
132-
133-
let toc_path = temp.path().join("book").join("toc.js");
134-
let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?;
135-
for line in html.lines() {
136-
if let Some(left) = line.strip_prefix(" this.innerHTML = '") {
137-
if let Some(html) = left.strip_suffix("';") {
138-
return Ok(Document::from(html));
139-
}
140-
}
141-
}
142-
panic!("cannot find toc in file")
143-
}
144-
145123
/// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we
146124
/// can search with the `select` crate
147125
fn toc_fallback_html() -> Result<Document> {
@@ -157,21 +135,6 @@ fn toc_fallback_html() -> Result<Document> {
157135
Ok(Document::from(html.as_str()))
158136
}
159137

160-
// don't use target="_parent" in JS
161-
#[test]
162-
fn check_link_target_js() {
163-
let doc = toc_js_html().unwrap();
164-
165-
let num_parent_links = doc
166-
.find(
167-
Class("chapter")
168-
.descendant(Name("li"))
169-
.descendant(Name("a").and(Attr("target", "_parent"))),
170-
)
171-
.count();
172-
assert_eq!(num_parent_links, 0);
173-
}
174-
175138
// don't use target="_parent" in IFRAME
176139
#[test]
177140
fn check_link_target_fallback() {

tests/testsuite/toc.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::prelude::*;
44
use select::document::Document;
5-
use select::predicate::{Class, Name, Predicate};
5+
use select::predicate::{Attr, Class, Name, Predicate};
66

77
const TOC_TOP_LEVEL: &[&str] = &[
88
"1. With Readme",
@@ -95,3 +95,18 @@ fn check_spacers() {
9595
.count();
9696
assert_eq!(num_spacers, should_be);
9797
}
98+
99+
// don't use target="_parent" in JS
100+
#[test]
101+
fn check_link_target_js() {
102+
let doc = toc_js_html();
103+
104+
let num_parent_links = doc
105+
.find(
106+
Class("chapter")
107+
.descendant(Name("li"))
108+
.descendant(Name("a").and(Attr("target", "_parent"))),
109+
)
110+
.count();
111+
assert_eq!(num_parent_links, 0);
112+
}

0 commit comments

Comments
 (0)