Skip to content

Commit 34e5ef2

Browse files
committed
Don't include empty class attribute.
1 parent b141297 commit 34e5ef2

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,12 @@ fn insert_link_into_header(
832832
id_counter: &mut HashMap<String, usize>,
833833
) -> String {
834834
let id = id.unwrap_or_else(|| utils::unique_id_from_content(content, id_counter));
835-
let classes = classes.unwrap_or("".to_string());
835+
let classes = classes
836+
.map(|s| format!(" class=\"{s}\""))
837+
.unwrap_or_default();
836838

837839
format!(
838-
r##"<h{level} id="{id}" class="{classes}"><a class="header" href="#{id}">{text}</a></h{level}>"##,
840+
r##"<h{level} id="{id}"{classes}><a class="header" href="#{id}">{text}</a></h{level}>"##,
839841
level = level,
840842
id = id,
841843
text = content,
@@ -1014,28 +1016,39 @@ mod tests {
10141016
let inputs = vec![
10151017
(
10161018
"blah blah <h1>Foo</h1>",
1017-
r##"blah blah <h1 id="foo" class=""><a class="header" href="#foo">Foo</a></h1>"##,
1019+
r##"blah blah <h1 id="foo"><a class="header" href="#foo">Foo</a></h1>"##,
10181020
),
10191021
(
10201022
"<h1>Foo</h1>",
1021-
r##"<h1 id="foo" class=""><a class="header" href="#foo">Foo</a></h1>"##,
1023+
r##"<h1 id="foo"><a class="header" href="#foo">Foo</a></h1>"##,
10221024
),
10231025
(
10241026
"<h3>Foo^bar</h3>",
1025-
r##"<h3 id="foobar" class=""><a class="header" href="#foobar">Foo^bar</a></h3>"##,
1027+
r##"<h3 id="foobar"><a class="header" href="#foobar">Foo^bar</a></h3>"##,
10261028
),
10271029
(
10281030
"<h4></h4>",
1029-
r##"<h4 id="" class=""><a class="header" href="#"></a></h4>"##,
1031+
r##"<h4 id=""><a class="header" href="#"></a></h4>"##,
10301032
),
10311033
(
10321034
"<h4><em>Hï</em></h4>",
1033-
r##"<h4 id="hï" class=""><a class="header" href="#hï"><em>Hï</em></a></h4>"##,
1035+
r##"<h4 id="hï"><a class="header" href="#hï"><em>Hï</em></a></h4>"##,
10341036
),
10351037
(
10361038
"<h1>Foo</h1><h3>Foo</h3>",
1037-
r##"<h1 id="foo" class=""><a class="header" href="#foo">Foo</a></h1><h3 id="foo-1" class=""><a class="header" href="#foo-1">Foo</a></h3>"##,
1039+
r##"<h1 id="foo"><a class="header" href="#foo">Foo</a></h1><h3 id="foo-1"><a class="header" href="#foo-1">Foo</a></h3>"##,
10381040
),
1041+
// id only
1042+
(
1043+
r##"<h1 id="foobar">Foo</h1>"##,
1044+
r##"<h1 id="foobar"><a class="header" href="#foobar">Foo</a></h1>"##,
1045+
),
1046+
// class only
1047+
(
1048+
r##"<h1 class="class1 class2">Foo</h1>"##,
1049+
r##"<h1 id="foo" class="class1 class2"><a class="header" href="#foo">Foo</a></h1>"##,
1050+
),
1051+
// both id and class
10391052
(
10401053
r##"<h1 id="foobar" class="class1 class2">Foo</h1>"##,
10411054
r##"<h1 id="foobar" class="class1 class2"><a class="header" href="#foobar">Foo</a></h1>"##,

tests/rendered_output.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ fn check_correct_cross_links_in_nested_dir() {
105105

106106
assert_contains_strings(
107107
first.join("index.html"),
108-
&[r##"<h2 id="some-section" class=""><a class="header" href="#some-section">"##],
108+
&[r##"<h2 id="some-section"><a class="header" href="#some-section">"##],
109109
);
110110

111111
assert_contains_strings(
112112
first.join("nested.html"),
113-
&[r##"<h2 id="some-section" class=""><a class="header" href="#some-section">"##],
113+
&[r##"<h2 id="some-section"><a class="header" href="#some-section">"##],
114114
);
115115
}
116116

@@ -393,7 +393,7 @@ fn able_to_include_files_in_chapters() {
393393
let includes = temp.path().join("book/first/includes.html");
394394

395395
let summary_strings = &[
396-
r##"<h1 id="summary" class=""><a class="header" href="#summary">Summary</a></h1>"##,
396+
r##"<h1 id="summary"><a class="header" href="#summary">Summary</a></h1>"##,
397397
">First Chapter</a>",
398398
];
399399
assert_contains_strings(&includes, summary_strings);

0 commit comments

Comments
 (0)