diff --git a/addons/linkify/linkify.js b/addons/linkify/linkify.js index 030c5b4a..92089929 100644 --- a/addons/linkify/linkify.js +++ b/addons/linkify/linkify.js @@ -12,14 +12,24 @@ } })(function (Xterm) { 'use strict'; + /** * This module provides methods for convertings valid URL substrings * into HTML anchor elements (links), inside a terminal view. * * @module xterm/addons/linkify/linkify */ - var exports = {}; - + var exports = {}, + protocolClause = '(https?:\\/\\/)', + domainBodyClause = '([\\da-z\\.-]+)', + tldClause = '([a-z\\.]{2,6})', + hostClause = domainBodyClause + '\\.' + tldClause, + pathClause = '([\\/\\w\\.-]*)*\\/?', + bodyClause = hostClause + pathClause, + start = '(?:^|\\s+)(', + end = ')($|\\s+)', + urlClause = start + protocolClause + '?' + bodyClause + end, + urlRegex = new RegExp(urlClause); /** * Converts all valid URLs found in the given terminal line into @@ -46,22 +56,45 @@ var buffer = document.createElement('span'), nodes = line.childNodes; + console.log(nodes.length, 'number of nodes'); for (var j=0; j' + url + '', - newData = node.data.replace(url, link); + var nodeHTML = buffer.innerHTML; - buffer.textContent = node.data; - line.innerHTML = line.innerHTML.replace(buffer.innerHTML, newData); - } + /* + * Apply function only on TextNodes + */ + if (node.nodeType != node.TEXT_NODE) { + continue; } + + + var match = node.data.match(urlRegex); + + /* + * If no URL was found in the current text, return. + */ + if (!match) { + continue; + } + + var url = match[1], + startsWithProtocol = new RegExp('^' + protocolClause), + urlHasProtocol = url.match(startsWithProtocol), + href = (urlHasProtocol) ? url : 'http://' + url, + link = '' + url + '', + newHTML = nodeHTML.replace(url, link); + + line.innerHTML = line.innerHTML.replace(nodeHTML, newHTML); } /**