| 1 |
//! Tests for table of contents (sidebar). |
| 2 |
|
| 3 |
use crate::prelude::*; |
| 4 |
use anyhow::Result; |
| 5 |
use select::document::Document; |
| 6 |
use select::predicate::{Attr, Class, Name, Predicate}; |
| 7 |
|
| 8 |
const TOC_TOP_LEVEL: &[&str] = &[ |
| 9 |
"1. With Readme", |
| 10 |
"3. Deep Nest 1", |
| 11 |
"Prefix 1", |
| 12 |
"Prefix 2", |
| 13 |
"Suffix 1", |
| 14 |
"Suffix 2", |
| 15 |
]; |
| 16 |
const TOC_SECOND_LEVEL: &[&str] = &[ |
| 17 |
"1.1. Nested Index", |
| 18 |
"1.2. Nested two", |
| 19 |
"3.1. Deep Nest 2", |
| 20 |
"3.1.1. Deep Nest 3", |
| 21 |
]; |
| 22 |
|
| 23 |
/// Apply a series of predicates to some root predicate, where each |
| 24 |
/// successive predicate is the descendant of the last one. Similar to how you |
| 25 |
/// might do `ul.foo li a` in CSS to access all anchor tags in the `foo` list. |
| 26 |
macro_rules! descendants { |
| 27 |
($root:expr, $($child:expr),*) => { |
| 28 |
$root |
| 29 |
$( |
| 30 |
.descendant($child) |
| 31 |
)* |
| 32 |
}; |
| 33 |
} |
| 34 |
|
| 35 |
/// Read the TOC (`book/toc.js`) nested HTML and expose it as a DOM which we |
| 36 |
/// can search with the `select` crate |
| 37 |
fn toc_js_html() -> Document { |
| 38 |
let mut test = BookTest::from_dir("toc/basic_toc"); |
| 39 |
test.build(); |
| 40 |
let html = test.toc_js_html(); |
| 41 |
Document::from(html.as_str()) |
| 42 |
} |
| 43 |
|
| 44 |
/// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we |
| 45 |
/// can search with the `select` crate |
| 46 |
fn toc_fallback_html() -> Result<Document> { |
| 47 |
let mut test = BookTest::from_dir("toc/basic_toc"); |
| 48 |
test.build(); |
| 49 |
|
| 50 |
let toc_path = test.dir.join("book").join("toc.html"); |
| 51 |
let html = read_to_string(toc_path); |
| 52 |
Ok(Document::from(html.as_str())) |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn check_second_toc_level() { |
| 57 |
let doc = toc_js_html(); |
| 58 |
let mut should_be = Vec::from(TOC_SECOND_LEVEL); |
| 59 |
should_be.sort_unstable(); |
| 60 |
|
| 61 |
let pred = descendants!( |
| 62 |
Class("chapter"), |
| 63 |
Name("li"), |
| 64 |
Name("li"), |
| 65 |
Name("a").and(Class("toggle").not()) |
| 66 |
); |
| 67 |
|
| 68 |
let mut children_of_children: Vec<_> = doc |
| 69 |
.find(pred) |
| 70 |
.map(|elem| elem.text().trim().to_string()) |
| 71 |
.collect(); |
| 72 |
children_of_children.sort(); |
| 73 |
|
| 74 |
assert_eq!(children_of_children, should_be); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn check_first_toc_level() { |
| 79 |
let doc = toc_js_html(); |
| 80 |
let mut should_be = Vec::from(TOC_TOP_LEVEL); |
| 81 |
|
| 82 |
should_be.extend(TOC_SECOND_LEVEL); |
| 83 |
should_be.sort_unstable(); |
| 84 |
|
| 85 |
let pred = descendants!( |
| 86 |
Class("chapter"), |
| 87 |
Name("li"), |
| 88 |
Name("a").and(Class("toggle").not()) |
| 89 |
); |
| 90 |
|
| 91 |
let mut children: Vec<_> = doc |
| 92 |
.find(pred) |
| 93 |
.map(|elem| elem.text().trim().to_string()) |
| 94 |
.collect(); |
| 95 |
children.sort(); |
| 96 |
|
| 97 |
assert_eq!(children, should_be); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn check_spacers() { |
| 102 |
let doc = toc_js_html(); |
| 103 |
let should_be = 2; |
| 104 |
|
| 105 |
let num_spacers = doc |
| 106 |
.find(Class("chapter").descendant(Name("li").and(Class("spacer")))) |
| 107 |
.count(); |
| 108 |
assert_eq!(num_spacers, should_be); |
| 109 |
} |
| 110 |
|
| 111 |
// don't use target="_parent" in JS |
| 112 |
#[test] |
| 113 |
fn check_link_target_js() { |
| 114 |
let doc = toc_js_html(); |
| 115 |
|
| 116 |
let num_parent_links = doc |
| 117 |
.find( |
| 118 |
Class("chapter") |
| 119 |
.descendant(Name("li")) |
| 120 |
.descendant(Name("a").and(Attr("target", "_parent"))), |
| 121 |
) |
| 122 |
.count(); |
| 123 |
assert_eq!(num_parent_links, 0); |
| 124 |
} |
| 125 |
|
| 126 |
// don't use target="_parent" in IFRAME |
| 127 |
#[test] |
| 128 |
fn check_link_target_fallback() { |
| 129 |
let doc = toc_fallback_html().unwrap(); |
| 130 |
|
| 131 |
let num_parent_links = doc |
| 132 |
.find( |
| 133 |
Class("chapter") |
| 134 |
.descendant(Name("li")) |
| 135 |
.descendant(Name("a").and(Attr("target", "_parent"))), |
| 136 |
) |
| 137 |
.count(); |
| 138 |
assert_eq!( |
| 139 |
num_parent_links, |
| 140 |
TOC_TOP_LEVEL.len() + TOC_SECOND_LEVEL.len() |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
// Checks formatting of summary names with inline elements. |
| 145 |
#[test] |
| 146 |
fn summary_with_markdown_formatting() { |
| 147 |
BookTest::from_dir("toc/summary_with_markdown_formatting") |
| 148 |
.check_toc_js(str![[r#" |
| 149 |
<ol class="chapter"> |
| 150 |
<li class="chapter-item expanded "> |
| 151 |
<span class="chapter-link-wrapper"> |
| 152 |
<a href="formatted-summary.html"> |
| 153 |
<strong aria-hidden="true">1.</strong> Italic code *escape* `escape2`</a> |
| 154 |
</span> |
| 155 |
</li> |
| 156 |
<li class="chapter-item expanded "> |
| 157 |
<span class="chapter-link-wrapper"> |
| 158 |
<a href="soft.html"> |
| 159 |
<strong aria-hidden="true">2.</strong> Soft line break</a> |
| 160 |
</span> |
| 161 |
</li> |
| 162 |
<li class="chapter-item expanded "> |
| 163 |
<span class="chapter-link-wrapper"> |
| 164 |
<a href="escaped-tag.html"> |
| 165 |
<strong aria-hidden="true">3.</strong> <escaped tag></a> |
| 166 |
</span> |
| 167 |
</li> |
| 168 |
</ol> |
| 169 |
"#]]) |
| 170 |
.check_file( |
| 171 |
"src/formatted-summary.md", |
| 172 |
str![[r#" |
| 173 |
# Italic code *escape* `escape2` |
| 174 |
|
| 175 |
"#]], |
| 176 |
) |
| 177 |
.check_file( |
| 178 |
"src/soft.md", |
| 179 |
str![[r#" |
| 180 |
# Soft line break |
| 181 |
|
| 182 |
"#]], |
| 183 |
) |
| 184 |
.check_file( |
| 185 |
"src/escaped-tag.md", |
| 186 |
str![[r#" |
| 187 |
# <escaped tag> |
| 188 |
|
| 189 |
"#]], |
| 190 |
); |
| 191 |
} |