44class HTML ::Pipeline ::TableOfContentsFilterTest < Test ::Unit ::TestCase
55 TocFilter = HTML ::Pipeline ::TableOfContentsFilter
66
7+ TocPipeline =
8+ HTML ::Pipeline . new [
9+ HTML ::Pipeline ::TableOfContentsFilter
10+ ]
11+
12+ def toc
13+ result = { }
14+ TocPipeline . call ( @orig , { } , result )
15+ result [ :toc ]
16+ end
17+
718 def test_anchors_are_added_properly
819 orig = %(<h1>Ice cube</h1><p>Will swarm on any motherfucker in a blue uniform</p>)
920 assert_includes '<a name=' , TocFilter . call ( orig ) . to_s
1021 end
1122
23+ def test_toc_list_added_properly
24+ @orig = %(<h1>Ice cube</h1><p>Will swarm on any motherfucker in a blue uniform</p>)
25+ assert_includes %Q{<ul class="section-nav">\n <li><a href="} , toc
26+ end
27+
1228 def test_anchors_have_sane_names
1329 orig = %(<h1>Dr Dre</h1><h1>Ice Cube</h1><h1>Eazy-E</h1><h1>MC Ren</h1>)
1430 result = TocFilter . call ( orig ) . to_s
@@ -19,6 +35,14 @@ def test_anchors_have_sane_names
1935 assert_includes '"mc-ren"' , result
2036 end
2137
38+ def test_toc_hrefs_have_sane_values
39+ @orig = %(<h1>Dr Dre</h1><h1>Ice Cube</h1><h1>Eazy-E</h1><h1>MC Ren</h1>)
40+ assert_includes '"#dr-dre"' , toc
41+ assert_includes '"#ice-cube"' , toc
42+ assert_includes '"#eazy-e"' , toc
43+ assert_includes '"#mc-ren"' , toc
44+ end
45+
2246 def test_dupe_headers_have_unique_trailing_identifiers
2347 orig = %(<h1>Straight Outta Compton</h1>
2448 <h2>Dopeman</h2>
@@ -31,6 +55,16 @@ def test_dupe_headers_have_unique_trailing_identifiers
3155 assert_includes '"dopeman-1"' , result
3256 end
3357
58+ def test_dupe_headers_have_unique_toc_anchors
59+ @orig = %(<h1>Straight Outta Compton</h1>
60+ <h2>Dopeman</h2>
61+ <h3>Express Yourself</h3>
62+ <h1>Dopeman</h1>)
63+
64+ assert_includes '"#dopeman"' , toc
65+ assert_includes '"#dopeman-1"' , toc
66+ end
67+
3468 def test_all_header_tags_are_found_when_adding_anchors
3569 orig = %(<h1>"Funky President" by James Brown</h1>
3670 <h2>"It's My Thing" by Marva Whitney</h2>
@@ -41,18 +75,47 @@ def test_all_header_tags_are_found_when_adding_anchors
4175 <h7>"Be Thankful for What You Got" by William DeVaughn</h7>)
4276
4377 doc = TocFilter . call ( orig )
78+
4479 assert_equal 6 , doc . search ( 'a' ) . size
4580 end
4681
47- def test_anchors_with_utf8_characters
48- orig = %(<h1>日本語</h1>
49- <h1>Русский</h1)
82+ def test_toc_is_complete
83+ @orig = %(<h1>"Funky President" by James Brown</h1>
84+ <h2>"It's My Thing" by Marva Whitney</h2>
85+ <h3>"Boogie Back" by Roy Ayers</h3>
86+ <h4>"Feel Good" by Fancy</h4>
87+ <h5>"Funky Drummer" by James Brown</h5>
88+ <h6>"Ruthless Villain" by Eazy-E</h6>
89+ <h7>"Be Thankful for What You Got" by William DeVaughn</h7>)
5090
51- rendered_h1s = TocFilter . call ( orig ) . search ( 'h1' ) . map ( & :to_s )
91+ expected = %Q{<ul class="section-nav"> \n <li><a href="#funky-president-by-james-brown">"Funky President" by James Brown</a></li> \n <li><a href="#its-my-thing-by-marva-whitney">"It's My Thing" by Marva Whitney</a></li> \n <li><a href="#boogie-back-by-roy-ayers">"Boogie Back" by Roy Ayers</a></li> \n <li><a href="#feel-good-by-fancy">"Feel Good" by Fancy</a></li> \n <li><a href="#funky-drummer-by-james-brown">"Funky Drummer" by James Brown</a></li> \n <li><a href="#ruthless-villain-by-eazy-e">"Ruthless Villain" by Eazy-E</a></li> \n </ul>}
5292
53- assert_equal "<h1>\n <a name=\" %E6%97%A5%E6%9C%AC%E8%AA%9E\" class=\" anchor\" href=\" #%E6%97%A5%E6%9C%AC%E8%AA%9E\" ><span class=\" octicon octicon-link\" ></span></a>日本語</h1>" ,
54- rendered_h1s [ 0 ]
55- assert_equal "<h1>\n <a name=\" %D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9\" class=\" anchor\" href=\" #%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9\" ><span class=\" octicon octicon-link\" ></span></a>Русский</h1>" ,
56- rendered_h1s [ 1 ]
57- end if RUBY_VERSION > "1.9" # not sure how to make this work on 1.8.7
93+ assert_equal expected , toc
94+ end
95+
96+ if RUBY_VERSION > "1.9" # not sure how to make this work on 1.8.7
97+
98+ def test_anchors_with_utf8_characters
99+ orig = %(<h1>日本語</h1>
100+ <h1>Русский</h1)
101+
102+ rendered_h1s = TocFilter . call ( orig ) . search ( 'h1' ) . map ( &:to_s )
103+
104+ assert_equal "<h1>\n <a name=\" %E6%97%A5%E6%9C%AC%E8%AA%9E\" class=\" anchor\" href=\" #%E6%97%A5%E6%9C%AC%E8%AA%9E\" ><span class=\" octicon octicon-link\" ></span></a>日本語</h1>" ,
105+ rendered_h1s [ 0 ]
106+ assert_equal "<h1>\n <a name=\" %D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9\" class=\" anchor\" href=\" #%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9\" ><span class=\" octicon octicon-link\" ></span></a>Русский</h1>" ,
107+ rendered_h1s [ 1 ]
108+ end
109+
110+ def test_toc_with_utf8_characters
111+ @orig = %(<h1>日本語</h1>
112+ <h1>Русский</h1)
113+
114+ rendered_toc = Nokogiri ::HTML ::DocumentFragment . parse ( toc ) . to_s
115+
116+ expected = %Q{<ul class="section-nav">\n <li><a href="#%E6%97%A5%E6%9C%AC%E8%AA%9E">日本語</a></li>\n <li><a href="#%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9">Русский</a></li>\n </ul>}
117+
118+ assert_equal expected , rendered_toc
119+ end
120+ end
58121end
0 commit comments