diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index 6477f98910..6d93052ab1 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -526,12 +526,16 @@ def accept_blank_line(blank_line) def accept_heading(heading) level = [6, heading.level].min - label = deduplicate_heading_id(heading.label(@code_object)) - legacy_label = deduplicate_heading_id(heading.legacy_label(@code_object)) + raw_label = heading.label(@code_object) + raw_legacy_label = heading.legacy_label(@code_object) + label = deduplicate_heading_id(raw_label) + # No legacy anchor is needed when the heading's own id already uses + # the legacy format (see RDoc::Text.to_anchor). + legacy_label = deduplicate_heading_id(raw_legacy_label) unless raw_legacy_label == raw_label # Add legacy anchor before the heading for backward compatibility. # This allows old links with label- prefix to still work. - if @output_decoration && !@pipe + if @output_decoration && !@pipe && legacy_label @res << "\n" end diff --git a/lib/rdoc/text.rb b/lib/rdoc/text.rb index 9fb074d2da..22d46d9cdd 100644 --- a/lib/rdoc/text.rb +++ b/lib/rdoc/text.rb @@ -3,6 +3,7 @@ ## # For RDoc::Text#to_html +require 'cgi/escape' require 'strscan' module RDoc @@ -210,13 +211,21 @@ def wrap(txt, line_len = 76) # - Remove characters that aren't alphanumeric, space, or hyphen # - Replace spaces with hyphens # + # If nothing is left, for example because +text+ only contains symbols + # or non-ASCII characters, falls back to the legacy label format so the + # anchor is never empty. + # # Examples: # "Hello World" -> "hello-world" # "Foo::Bar" -> "foobar" # "What's New?" -> "whats-new" + # "$=" -> "label-24-3D" module_function def to_anchor(text) - text.downcase.gsub(/[^a-z0-9 \-]/, '').gsub(' ', '-') + anchor = text.downcase.gsub(/[^a-z0-9 \-]/, '').gsub(' ', '-') + return anchor unless anchor.empty? + + "label-#{CGI.escape(text).gsub('%', '-').sub(/^-/, '')}" end ## diff --git a/test/rdoc/markup/to_html_test.rb b/test/rdoc/markup/to_html_test.rb index 6d0107602c..bfe9d6315e 100644 --- a/test/rdoc/markup/to_html_test.rb +++ b/test/rdoc/markup/to_html_test.rb @@ -384,6 +384,26 @@ def test_accept_heading_duplicate_punctuation_collision assert_match(/

/, result) end + def test_accept_heading_symbols_only + @to.start_accepting + + @to.accept_heading @RM::Heading.new(3, '$=') + + assert_equal "\n

$=

\n", @to.res.join + end + + def test_accept_heading_symbols_only_duplicate + @to.start_accepting + + @to.accept_heading @RM::Heading.new(3, '$=') + @to.accept_heading @RM::Heading.new(3, '$=') + + result = @to.res.join + assert_match(/

/, result) + assert_match(/

/, result) + refute_match(/id=""/, result) + end + def test_accept_heading_three_duplicates @to.start_accepting diff --git a/test/rdoc/rdoc_text_test.rb b/test/rdoc/rdoc_text_test.rb index 0123146021..f1eabd2f43 100644 --- a/test/rdoc/rdoc_text_test.rb +++ b/test/rdoc/rdoc_text_test.rb @@ -105,6 +105,16 @@ def test_flush_left_encoding assert_equal Encoding::US_ASCII, result.encoding end + def test_to_anchor + assert_equal 'hello-world', to_anchor('Hello World') + assert_equal 'whats-new', to_anchor("What's New?") + end + + def test_to_anchor_without_alphanumerics + assert_equal 'label-24-3D', to_anchor('$=') + assert_equal 'label-E6-97-A5-E6-9C-AC-E8-AA-9E', to_anchor('日本語') + end + def test_markup_string out = markup('hi').gsub("\n", '')