Skip to content

Commit

Permalink
backport fix for #4576 encode spaces in mailto links as %20, in accor…
Browse files Browse the repository at this point in the history
…dance with RFC 3986, instead of using +
  • Loading branch information
mojavelinux committed May 16, 2024
1 parent 5cf3834 commit 1b1172a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ This project utilizes semantic versioning.
// tag::compact[]
== Unreleased

Compliance::

* Encode spaces in mailto links as %20, in accordance with RFC 3986, instead of using + (#4576)

Bug Fixes::

* Don't leave behind empty line inside skipped preprocessor conditional (#4580)
Expand Down
7 changes: 5 additions & 2 deletions lib/asciidoctor/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ def encode_uri_component str
})
)
end
elsif (CGI = ::CGI).respond_to? :escapeURIComponent
def encode_uri_component str
CGI.escapeURIComponent str
end
else
CGI = ::CGI
def encode_uri_component str
CGI.escape str
(CGI.escape str).gsub '+', '%20'
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
context 'URI Encoding' do
test 'should URI encode non-word characters generally' do
given = ' !*/%&?\\='
expect = '+%21%2A%2F%25%26%3F%5C%3D'
expect = '%20%21%2A%2F%25%26%3F%5C%3D'
assert_equal expect, (Asciidoctor::Helpers.encode_uri_component given)
end

Expand Down
12 changes: 6 additions & 6 deletions test/substitutions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -659,18 +659,18 @@
end

test 'a mailto macro with text and subject should be interpreted as a mailto link' do
para = block_from_string('mailto:doc.writer@asciidoc.org[Doc Writer, Pull request]')
assert_equal %q{<a href="mailto:doc.writer@asciidoc.org?subject=Pull+request">Doc Writer</a>}, para.sub_macros(para.source)
para = block_from_string 'mailto:doc.writer@asciidoc.org[Doc Writer, Pull request]'
assert_equal '<a href="mailto:doc.writer@asciidoc.org?subject=Pull%20request">Doc Writer</a>', para.sub_macros(para.source)
end

test 'a mailto macro with text, subject and body should be interpreted as a mailto link' do
para = block_from_string('mailto:doc.writer@asciidoc.org[Doc Writer, Pull request, Please accept my pull request]')
assert_equal %q{<a href="mailto:doc.writer@asciidoc.org?subject=Pull+request&amp;body=Please+accept+my+pull+request">Doc Writer</a>}, para.sub_macros(para.source)
para = block_from_string 'mailto:doc.writer@asciidoc.org[Doc Writer, Pull request, Please accept my pull request]'
assert_equal '<a href="mailto:doc.writer@asciidoc.org?subject=Pull%20request&amp;body=Please%20accept%20my%20pull%20request">Doc Writer</a>', para.sub_macros(para.source)
end

test 'a mailto macro with subject and body only should use e-mail as text' do
para = block_from_string('mailto:doc.writer@asciidoc.org[,Pull request,Please accept my pull request]')
assert_equal %q{<a href="mailto:doc.writer@asciidoc.org?subject=Pull+request&amp;body=Please+accept+my+pull+request">doc.writer@asciidoc.org</a>}, para.sub_macros(para.source)
para = block_from_string 'mailto:doc.writer@asciidoc.org[,Pull request,Please accept my pull request]'
assert_equal '<a href="mailto:doc.writer@asciidoc.org?subject=Pull%20request&amp;body=Please%20accept%20my%20pull%20request">doc.writer@asciidoc.org</a>', para.sub_macros(para.source)
end

test 'a mailto macro supports id and role attributes' do
Expand Down

0 comments on commit 1b1172a

Please sign in to comment.