Commit feed8922 authored by Alex Henrie's avatar Alex Henrie

Don't linkify text near a URL that looks like a URL with a bad protocol

parent d0773078
Pipeline #125416 passed with stages
in 27 minutes and 48 seconds
...@@ -234,6 +234,8 @@ http://example1.com ...@@ -234,6 +234,8 @@ http://example1.com
<a href="blarg">http://example5.com</a> <a href="blarg">http://example5.com</a>
unknown://example6.com unknown://example6.com
I can send email through smtp.gmail.com:587 or through https://www.gmail.com/
"""); """);
string expected = """ string expected = """
...@@ -246,6 +248,8 @@ unknown://example6.com ...@@ -246,6 +248,8 @@ unknown://example6.com
<a href="blarg">http://example5.com</a> <a href="blarg">http://example5.com</a>
unknown://example6.com 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 { try {
......
...@@ -576,33 +576,36 @@ ComposerPageState.htmlToText = function(root, blacklist = []) { ...@@ -576,33 +576,36 @@ ComposerPageState.htmlToText = function(root, blacklist = []) {
// Linkifies "plain text" link // Linkifies "plain text" link
ComposerPageState.linkify = function(node) { ComposerPageState.linkify = function(node) {
if (node.nodeType == Node.TEXT_NODE) { if (node.nodeType == Node.TEXT_NODE) {
// Examine text node for something that looks like a URL while (node.nodeValue) {
let input = node.nodeValue; // Examine text node for something that looks like a URL
if (input != null) { let urlRegex = new RegExp(ComposerPageState.URL_REGEX);
let output = input.replace(ComposerPageState.URL_REGEX, function(url) { let url;
if (url.match(ComposerPageState.PROTOCOL_REGEX) != null) { do {
url = "\x01" + url + "\x01"; let urlMatch = urlRegex.exec(node.nodeValue);
if (!urlMatch) {
return;
} }
return url; url = urlMatch[0];
}); } while (!url.match(ComposerPageState.PROTOCOL_REGEX));
if (input != output) { // We got one! Now split the text and swap in a new anchor.
// We got one! Now split the text and swap in a new anchor. let before = node.nodeValue.substring(0, urlRegex.lastIndex - url.length);
let parent = node.parentNode; let after = node.nodeValue.substring(urlRegex.lastIndex);
let sibling = node.nextSibling;
for (let part of output.split("\x01")) { let beforeNode = document.createTextNode(before);
let newNode = null; let linkNode = document.createElement("A");
if (part.match(ComposerPageState.URL_REGEX) != null) { linkNode.href = url;
newNode = document.createElement("A"); linkNode.textContent = url;
newNode.href = part; let afterNode = document.createTextNode(after);
newNode.innerText = part;
} else { let parentNode = node.parentNode;
newNode = document.createTextNode(part); parentNode.insertBefore(beforeNode, node);
} parentNode.insertBefore(linkNode, node);
parent.insertBefore(newNode, sibling); parentNode.insertBefore(afterNode, node);
} parentNode.removeChild(node);
parent.removeChild(node);
} // Keep searching for URLs after this one
node = afterNode;
} }
} else { } else {
// Recurse // Recurse
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment