From c42722d57dbfdb42dcdfa74d3bcdf6f6308cbb45 Mon Sep 17 00:00:00 2001 From: Boubacar Date: Sun, 16 Feb 2020 21:46:48 -0500 Subject: [PATCH] Add email link helper to create mailto: links for email values in objects --- app/helpers/browse_tags_helper.rb | 27 ++++++++++ test/helpers/browse_tags_helper_test.rb | 72 +++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/app/helpers/browse_tags_helper.rb b/app/helpers/browse_tags_helper.rb index bbf6f3cf7..2a1496f13 100644 --- a/app/helpers/browse_tags_helper.rb +++ b/app/helpers/browse_tags_helper.rb @@ -21,6 +21,12 @@ module BrowseTagsHelper link_to h(wmc[:title]), wmc[:url], :title => t("browse.tag_details.wikimedia_commons_link", :page => wmc[:title]) elsif url = wiki_link("tag", "#{key}=#{value}") link_to h(value), url, :title => t("browse.tag_details.wiki_link.tag", :key => key, :value => value) + elsif emails = email_links(key, value) + # similarly, email_links() returns an array of emails + emails = emails.map do |e| + link_to(h(e[:email]), e[:url], :title => t("browse.tag_details.email_link", :email => e[:email])) + end + safe_join(emails, "; ") elsif phones = telephone_links(key, value) # similarly, telephone_links() returns an array of phone numbers phones = phones.map do |p| @@ -123,6 +129,27 @@ module BrowseTagsHelper nil end + def email_links(_key, value) + # Does value look like an email? eg "someone@domain.tld" + # or a list of alternate emails separated by ; + + # Uses WHATWG implementation of email validation, which follows RFC 1123 + # but is a willful violation of RFC 5322. + # (see: https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address) + if value.match?(%r{^\s*[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\s* + (;\s*[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\s*)*$ + }x) + return value.split(";").map do |email| + # remove any leading or trailing whitespace if present + email = email.strip + + # add 'mailto:'' prefix + { :email => email, :url => "mailto:#{email}" } + end + end + nil + end + def telephone_links(_key, value) # Does it look like a global phone number? eg "+1 (234) 567-8901 " # or a list of alternate numbers separated by ; diff --git a/test/helpers/browse_tags_helper_test.rb b/test/helpers/browse_tags_helper_test.rb index a6390df38..3239f2211 100644 --- a/test/helpers/browse_tags_helper_test.rb +++ b/test/helpers/browse_tags_helper_test.rb @@ -221,6 +221,78 @@ class BrowseTagsHelperTest < ActionView::TestCase assert_nil link end + def test_email_links + links = email_links("foo", "Test") + assert_nil links + + links = email_links("email", "123") + assert_nil links + + links = email_links("email", "Abc.example.com") + assert_nil links + + links = email_links("email", "a@b@c.com") + assert_nil links + + links = email_links("email", "123 abcdefg@space.com") + assert_nil links + + links = email_links("email", "test@ abc") + assert_nil links + + links = email_links("email", "just\"not\"right@example.com") + assert_nil links + + # If multiple emails are listed, all must be valid + links = email_links("email", "very.common@test.com; a@b@c.com") + assert_nil links + + links = email_links("email", "x@example.com") + assert_equal 1, links.length + assert_equal "x@example.com", links[0][:email] + assert_equal "mailto:x@example.com", links[0][:url] + + links = email_links("email", "other.email-with-hyphen@example.com") + assert_equal 1, links.length + assert_equal "other.email-with-hyphen@example.com", links[0][:email] + assert_equal "mailto:other.email-with-hyphen@example.com", links[0][:url] + + links = email_links("email", "user.name+tag+sorting@example.com") + assert_equal 1, links.length + assert_equal "user.name+tag+sorting@example.com", links[0][:email] + assert_equal "mailto:user.name+tag+sorting@example.com", links[0][:url] + + links = email_links("email", "dash-in@both-parts.com") + assert_equal 1, links.length + assert_equal "dash-in@both-parts.com", links[0][:email] + assert_equal "mailto:dash-in@both-parts.com", links[0][:url] + + links = email_links("email", " test@email.com ") + assert_equal 1, links.length + assert_equal "test@email.com", links[0][:email] + assert_equal "mailto:test@email.com", links[0][:url] + + links = email_links("email", "example@s.example") + assert_equal 1, links.length + assert_equal "example@s.example", links[0][:email] + assert_equal "mailto:example@s.example", links[0][:url] + + # Multiple valid phone numbers separated by ; + links = email_links("email", "test@email.com; example@s.example") + assert_equal 2, links.length + assert_equal "test@email.com", links[0][:email] + assert_equal "mailto:test@email.com", links[0][:url] + assert_equal "example@s.example", links[1][:email] + assert_equal "mailto:example@s.example", links[1][:url] + + links = email_links("email", "x@example.com ; dash-in@both-parts.com ") + assert_equal 2, links.length + assert_equal "x@example.com", links[0][:email] + assert_equal "mailto:x@example.com", links[0][:url] + assert_equal "dash-in@both-parts.com", links[1][:email] + assert_equal "mailto:dash-in@both-parts.com", links[1][:url] + end + def test_telephone_links links = telephone_links("foo", "Test") assert_nil links -- 2.39.5