]> git.openstreetmap.org Git - rails.git/commitdiff
Merge pull request #2542 from boubascript/master
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 26 Feb 2020 16:09:27 +0000 (17:09 +0100)
committerGitHub <noreply@github.com>
Wed, 26 Feb 2020 16:09:27 +0000 (17:09 +0100)
Show mailto: links for email values in browse

.rubocop.yml
app/helpers/browse_tags_helper.rb
test/helpers/browse_tags_helper_test.rb

index 1ae01086a36590d17fcb82ec3578b495fedfa618..77f26d863bf5b41a23ea4ca47540ab36eb21ff7c 100644 (file)
@@ -29,6 +29,9 @@ Metrics/ClassLength:
   Exclude:
     - 'test/**/*'
 
+Metrics/ModuleLength:
+  Max: 150
+
 Naming/FileName:
   Exclude:
     - 'script/deliver-message'
index bbf6f3cf72ea6a79436a9add4df38aeed80a110e..4f73eb9d7c8018f932471ac71b98e6f8f22645df 100644 (file)
@@ -21,6 +21,8 @@ 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 email = email_link(key, value)
+      link_to(h(email[:email]), email[:url], :title => t("browse.tag_details.email_link", :email => email[:email]))
     elsif phones = telephone_links(key, value)
       # similarly, telephone_links() returns an array of phone numbers
       phones = phones.map do |p|
@@ -123,6 +125,25 @@ module BrowseTagsHelper
     nil
   end
 
+  def email_link(_key, value)
+    # Does the value look like an email? eg "someone@domain.tld"
+
+    #  Uses Ruby built-in regexp to validate email.
+    #  This will not catch certain valid emails containing comments, whitespace characters,
+    #  and quoted strings.
+    #    (see: https://github.com/ruby/ruby/blob/master/lib/uri/mailto.rb)
+
+    # remove any leading and trailing whitespace
+    email = value.strip
+
+    if email.match?(URI::MailTo::EMAIL_REGEXP)
+      # add 'mailto:'' prefix
+      return { :email => email, :url => "mailto:#{email}" }
+    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 ;
index a6390df38ca5bbe113f346bdb352cf99ae4c7de4..fb90eb51d8ffcacf773dadfe9449793971ff68e4 100644 (file)
@@ -221,6 +221,57 @@ class BrowseTagsHelperTest < ActionView::TestCase
     assert_nil link
   end
 
+  def test_email_link
+    email = email_link("foo", "Test")
+    assert_nil email
+
+    email = email_link("email", "123")
+    assert_nil email
+
+    email = email_link("email", "Abc.example.com")
+    assert_nil email
+
+    email = email_link("email", "a@b@c.com")
+    assert_nil email
+
+    email = email_link("email", "just\"not\"right@example.com")
+    assert_nil email
+
+    email = email_link("email", "123 abcdefg@space.com")
+    assert_nil email
+
+    email = email_link("email", "test@ abc")
+    assert_nil email
+
+    email = email_link("email", "using;semicolon@test.com")
+    assert_nil email
+
+    email = email_link("email", "x@example.com")
+    assert_equal "x@example.com", email[:email]
+    assert_equal "mailto:x@example.com", email[:url]
+
+    email = email_link("email", "other.email-with-hyphen@example.com")
+    assert_equal "other.email-with-hyphen@example.com", email[:email]
+    assert_equal "mailto:other.email-with-hyphen@example.com", email[:url]
+
+    email = email_link("email", "user.name+tag+sorting@example.com")
+    assert_equal "user.name+tag+sorting@example.com", email[:email]
+    assert_equal "mailto:user.name+tag+sorting@example.com", email[:url]
+
+    email = email_link("email", "dash-in@both-parts.com")
+    assert_equal "dash-in@both-parts.com", email[:email]
+    assert_equal "mailto:dash-in@both-parts.com", email[:url]
+
+    email = email_link("email", "example@s.example")
+    assert_equal "example@s.example", email[:email]
+    assert_equal "mailto:example@s.example", email[:url]
+
+    # Strips whitespace at ends
+    email = email_link("email", " test@email.com ")
+    assert_equal "test@email.com", email[:email]
+    assert_equal "mailto:test@email.com", email[:url]
+  end
+
   def test_telephone_links
     links = telephone_links("foo", "Test")
     assert_nil links