From e8878e5ce835d28fadece9d4e0384fb30e60d3ba Mon Sep 17 00:00:00 2001
From: Ben Gotow <bengotow@gmail.com>
Date: Wed, 25 Nov 2015 18:29:59 -0800
Subject: [PATCH] fix(autolinker): Add title attr to all links in emails to
 display href

Fixes GitHub Issues #479 and #335
---
 .../lib/plugins/autolinker-extension.coffee   |  6 ++++
 .../spec/autolinker-extension-spec.coffee     | 28 +++++++++++++++++++
 2 files changed, 34 insertions(+)
 create mode 100644 internal_packages/message-list/spec/autolinker-extension-spec.coffee

diff --git a/internal_packages/message-list/lib/plugins/autolinker-extension.coffee b/internal_packages/message-list/lib/plugins/autolinker-extension.coffee
index 358dc0949..6c2f5060d 100644
--- a/internal_packages/message-list/lib/plugins/autolinker-extension.coffee
+++ b/internal_packages/message-list/lib/plugins/autolinker-extension.coffee
@@ -7,4 +7,10 @@ class AutolinkerExtension extends MessageStoreExtension
     # Apply the autolinker pass to make emails and links clickable
     message.body = Autolinker.link(message.body, {twitter: false})
 
+    # Ensure that the hrefs in the email always have alt text so you can't hide
+    # the target of links
+    # https://regex101.com/r/cH0qM7/1
+    message.body = message.body.replace /href[ ]*=[ ]*?['"]([^'"]*)(['"]+)/gi, (match, url, quoteCharacter) =>
+      return "#{match} title=#{quoteCharacter}#{url}#{quoteCharacter} "
+
 module.exports = AutolinkerExtension
diff --git a/internal_packages/message-list/spec/autolinker-extension-spec.coffee b/internal_packages/message-list/spec/autolinker-extension-spec.coffee
new file mode 100644
index 000000000..f4202aa16
--- /dev/null
+++ b/internal_packages/message-list/spec/autolinker-extension-spec.coffee
@@ -0,0 +1,28 @@
+Autolinker = require 'autolinker'
+AutolinkerExtension = require '../lib/plugins/autolinker-extension'
+
+describe "AutolinkerExtension", ->
+  beforeEach ->
+    spyOn(Autolinker, 'link').andCallFake (txt) => txt
+
+  it "should call through to Autolinker", ->
+    AutolinkerExtension.formatMessageBody({body:'body'})
+    expect(Autolinker.link).toHaveBeenCalledWith('body', {twitter: false})
+
+  it "should add a title to everything with an href", ->
+    message =
+      body: """
+        <a href="apple.com">hello world!</a>
+        <a href = "http://apple.com">hello world!</a>
+        <a href ='http://apple.com'>hello world!</a>
+        <a href ='mailto://'>hello world!</a>
+      """
+    expected =
+      body: """
+        <a href="apple.com" title="apple.com" >hello world!</a>
+        <a href = "http://apple.com" title="http://apple.com" >hello world!</a>
+        <a href ='http://apple.com' title='http://apple.com' >hello world!</a>
+        <a href ='mailto://' title='mailto://' >hello world!</a>
+      """
+    AutolinkerExtension.formatMessageBody(message)
+    expect(message.body).toEqual(expected.body)