[geary/cherry-pick-753a67f4] Merge branch 'links' into 'mainline'



commit f333201abb0aa77a688afd3e3e0992a028c82c60
Author: Michael Gratton <mike vee net>
Date:   Sat Oct 26 05:31:55 2019 +0000

    Merge branch 'links' into 'mainline'
    
    Don't linkify text near a URL that looks like a URL with a bad protocol
    
    See merge request GNOME/geary!339
    
    (cherry picked from commit 753a67f40af50176df0a6db782303373174efbe7)
    
    feed8922 Don't linkify text near a URL that looks like a URL with a bad protocol

 test/js/composer-page-state-test.vala |  4 +++
 ui/composer-web-view.js               | 55 ++++++++++++++++++-----------------
 2 files changed, 33 insertions(+), 26 deletions(-)
---
diff --git a/test/js/composer-page-state-test.vala b/test/js/composer-page-state-test.vala
index 8c1fcf08..480823a7 100644
--- a/test/js/composer-page-state-test.vala
+++ b/test/js/composer-page-state-test.vala
@@ -154,6 +154,8 @@ http://example1.com
 <a href="blarg">http://example5.com</a>
 
 unknown://example6.com
+
+I can send email through smtp.gmail.com:587 or through https://www.gmail.com/
 """);
 
         string expected = """
@@ -166,6 +168,8 @@ unknown://example6.com
 <a href="blarg">http://example5.com</a>
 
 unknown://example6.com
+
+I can send email through smtp.gmail.com:587 or through <a 
href="https://www.gmail.com/";>https://www.gmail.com/</a>
 """;
 
         try {
diff --git a/ui/composer-web-view.js b/ui/composer-web-view.js
index a848bb1a..0f7ad798 100644
--- a/ui/composer-web-view.js
+++ b/ui/composer-web-view.js
@@ -571,33 +571,36 @@ ComposerPageState.htmlToText = function(root) {
 // Linkifies "plain text" link
 ComposerPageState.linkify = function(node) {
     if (node.nodeType == Node.TEXT_NODE) {
-        // Examine text node for something that looks like a URL
-        let input = node.nodeValue;
-        if (input != null) {
-            let output = input.replace(ComposerPageState.URL_REGEX, function(url) {
-                if (url.match(ComposerPageState.PROTOCOL_REGEX) != null) {
-                    url = "\x01" + url + "\x01";
+        while (node.nodeValue) {
+            // Examine text node for something that looks like a URL
+            let urlRegex = new RegExp(ComposerPageState.URL_REGEX);
+            let url;
+            do {
+                let urlMatch = urlRegex.exec(node.nodeValue);
+                if (!urlMatch) {
+                    return;
                 }
-                return url;
-            });
-
-            if (input != output) {
-                // We got one!  Now split the text and swap in a new anchor.
-                let parent = node.parentNode;
-                let sibling = node.nextSibling;
-                for (let part of output.split("\x01")) {
-                    let newNode = null;
-                    if (part.match(ComposerPageState.URL_REGEX) != null) {
-                        newNode = document.createElement("A");
-                        newNode.href = part;
-                        newNode.innerText = part;
-                    } else {
-                        newNode = document.createTextNode(part);
-                    }
-                    parent.insertBefore(newNode, sibling);
-                }
-                parent.removeChild(node);
-            }
+                url = urlMatch[0];
+            } while (!url.match(ComposerPageState.PROTOCOL_REGEX));
+
+            // We got one! Now split the text and swap in a new anchor.
+            let before = node.nodeValue.substring(0, urlRegex.lastIndex - url.length);
+            let after = node.nodeValue.substring(urlRegex.lastIndex);
+
+            let beforeNode = document.createTextNode(before);
+            let linkNode = document.createElement("A");
+            linkNode.href = url;
+            linkNode.textContent = url;
+            let afterNode = document.createTextNode(after);
+
+            let parentNode = node.parentNode;
+            parentNode.insertBefore(beforeNode, node);
+            parentNode.insertBefore(linkNode, node);
+            parentNode.insertBefore(afterNode, node);
+            parentNode.removeChild(node);
+
+            // Keep searching for URLs after this one
+            node = afterNode;
         }
     } else {
         // Recurse


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]