diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb
index 6477f98910..6baab3ff4e 100644
--- a/lib/rdoc/markup/to_html.rb
+++ b/lib/rdoc/markup/to_html.rb
@@ -300,9 +300,10 @@ def handle_TIDYLINK(label_part, url)
if label_part.size == 1 && String === label_part[0]
raw_label = label_part[0]
+ in_tidylink_label = @in_tidylink_label
@in_tidylink_label = true
special = apply_tidylink_label_special_handling(raw_label, url)
- @in_tidylink_label = false
+ @in_tidylink_label = in_tidylink_label
if special
tag = gen_url(CGI.escapeHTML(url), special)
@@ -317,9 +318,10 @@ def handle_TIDYLINK(label_part, url)
open_tag, close_tag = tag.split(/(?=<\/a>)/, 2)
valid_tag = open_tag && close_tag
emit_inline(open_tag) if valid_tag
+ in_tidylink_label = @in_tidylink_label
@in_tidylink_label = true
traverse_inline_nodes(label_part)
- @in_tidylink_label = false
+ @in_tidylink_label = in_tidylink_label
emit_inline(close_tag) if valid_tag
end
@@ -544,7 +546,12 @@ def accept_heading(heading)
if @pipe
@res << to_html(heading.text)
else
- @res << "#{to_html(heading.text)}"
+ # The heading text is the label of the heading's own link, so it must
+ # not contain links itself (nested tags are invalid HTML).
+ @in_tidylink_label = true
+ text = to_html(heading.text)
+ @in_tidylink_label = false
+ @res << "#{text}"
end
@res << "\n"
diff --git a/test/rdoc/markup/to_html_crossref_test.rb b/test/rdoc/markup/to_html_crossref_test.rb
index 70242bb37f..83c0f56457 100644
--- a/test/rdoc/markup/to_html_crossref_test.rb
+++ b/test/rdoc/markup/to_html_crossref_test.rb
@@ -32,6 +32,21 @@ def test_convert_CROSSREF
assert_equal para("# :stopdoc::"), result
end
+ def test_convert_CROSSREF_in_heading
+ result = @to.convert "= C1 and EXAMPLE.md\n\nC1"
+
+ assert_equal "\n" \
+ "\n
C1"), result
+ end
+
+ def test_convert_CROSSREF_in_heading_after_tidylink
+ result = @to.convert "= {Foo}[https://example.com] C1"
+
+ assert_include result, 'Foo'
+ assert_not_include result, 'C1.html'
+ end
+
def test_convert_CROSSREF_backslash_in_tt
formatter = RDoc::Markup::ToHtmlCrossref.new('C9.html', @c9_b)