Skip to content

Commit bd045fe

Browse files
authored
Escape unlinked cross-references in HTML snippets (#1833)
## Description `RDoc::Markup::ToHtmlSnippet` doesn't link cross-references, but its `handle_regexp_CROSSREF` returned the matched text unchanged. `apply_regexp_handling` treats a handler's return value as already-converted HTML, so a CROSSREF match such as `#<` in `[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>]` was emitted with a raw `<`. The resulting search snippets (Aliki search results, Darkfish `search_index.js`) contained `#<Encoding:UTF-8&gt;`, which the browser parses as a tag. The handler now escapes the text with `convert_string`, the same way `ToHtml#handle_regexp_SUPPRESSED_CROSSREF` does. On current master the regular page output (`ToHtmlCrossref`) already escapes this case, so the snippet formatter was the remaining path; I checked by generating aliki and darkfish docs for the example from the issue, before and after. Closes #1743
1 parent 5e0ea99 commit bd045fe

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎lib/rdoc/markup/to_html_snippet.rb‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ def start_accepting
128128
end
129129

130130
##
131-
# Removes escaping from the cross-references in +target+
131+
# Removes escaping from the cross-references in +target+ and escapes HTML
132+
# characters, since snippets don't link cross-references.
132133

133134
def handle_regexp_CROSSREF(text)
134-
text.sub(/\A\\/, '')
135+
convert_string(text.delete_prefix('\\'))
135136
end
136137

137138
##

‎test/rdoc/markup/to_html_snippet_test.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,13 @@ def test_convert_TIDYLINK_rdoc_label
650650
assert_equal 3, @to.characters
651651
end
652652

653+
def test_convert_CROSSREF_escapes_html
654+
result = @to.convert 'See [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>] and Foo::<bar>'
655+
656+
expected = "<p>See [#&lt;Encoding:ISO-8859-1&gt;, #&lt;Encoding:UTF-8&gt;] and Foo::&lt;bar&gt;\n"
657+
assert_equal expected, result
658+
end
659+
653660
def test_handle_regexp_HYPERLINK_link
654661
target = 'link:README.txt'
655662

0 commit comments

Comments
 (0)