Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/rdoc/markup/to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def initialize(pipe: false, output_decoration: true)

UNSAFE_URL_SCHEME_REGEXP = /\A(?:javascript|vbscript|data|file):/i

##
# Splits the part of an rdoc-image link after "rdoc-image:" into the
# image URL and the alt text. A port number after the host is part of
# the URL.

IMAGE_URL_AND_ALT_REGEXP = %r{\A((?:[A-Za-z][A-Za-z0-9+.\-]*://[^/:?#]*(?::\d+(?=[/?#:]|\z))?)?[^:]*)(?::(.*))?\z}m

# :nodoc:
URL_CHARACTERS_REGEXP_STR = /[A-Za-z0-9\-._~:\/\?#\[\]@!$&'\(\)*+,;%=]/.source

Expand Down Expand Up @@ -203,7 +210,8 @@ def handle_RDOCLINK(url) # :nodoc:
# Split the string after "rdoc-image:" into url and alt.
# "path/to/image.jpg:alt text" => ["path/to/image.jpg", "alt text"]
# "http://example.com/path/to/image.jpg:alt text" => ["http://example.com/path/to/image.jpg", "alt text"]
url, alt = $'.split(/:(?!\/)/, 2)
# "http://example.com:8080/image.jpg:alt text" => ["http://example.com:8080/image.jpg", "alt text"]
url, alt = $'.match(IMAGE_URL_AND_ALT_REGEXP).captures
if alt && !alt.empty?
%[<img src="#{CGI.escapeHTML(url)}" alt="#{CGI.escapeHTML(alt)}">]
else
Expand Down
16 changes: 16 additions & 0 deletions test/rdoc/markup/to_html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,22 @@ def test_convert_TIDYLINK_image_external
assert_equal expected, result
end

def test_convert_TIDYLINK_image_external_with_port
result =
@to.convert '{rdoc-image:http://example.com:8080/path/to/image.jpg:alt text}[http://example.com]'

expected =
"\n<p><a href=\"http://example.com\"><img src=\"http://example.com:8080/path/to/image.jpg\" alt=\"alt text\"></a></p>\n"

assert_equal expected, result
end

def test_convert_RDOCLINK_image_external_with_port_without_alt
result = @to.convert 'rdoc-image:http://localhost:8080/image.png'

assert_equal "\n<p><img src=\"http://localhost:8080/image.png\"></p>\n", result
end

def test_convert_TIDYLINK_image_external_with_alt
result =
@to.convert '{rdoc-image:http://example.com/path/to/image.jpg:alt text}[http://example.com]'
Expand Down