]> git.proxmox.com Git - mirror_xterm.js.git/blame - addons/linkify/linkify.js
Move to TypeScript build
[mirror_xterm.js.git] / addons / linkify / linkify.js
CommitLineData
b60903a2 1(function (linkify) {
fff56eea
PK
2 if (typeof exports === 'object' && typeof module === 'object') {
3 /*
4 * CommonJS environment
5 */
441009d3 6 module.exports = linkify(require('../../build/xterm'));
fff56eea
PK
7 } else if (typeof define == 'function') {
8 /*
9 * Require.js is available
10 */
441009d3 11 define(['../../build/xterm'], linkify);
fff56eea
PK
12 } else {
13 /*
dc67c945
PK
14 * Plain browser environment
15 */
ed1a31d1 16 linkify(window.Terminal);
fff56eea 17 }
b60903a2 18})(function (Xterm) {
fff56eea
PK
19 'use strict';
20
21 /**
22 * This module provides methods for convertings valid URL substrings
23 * into HTML anchor elements (links), inside a terminal view.
24 *
25 * @module xterm/addons/linkify/linkify
26 */
27 var exports = {},
28 protocolClause = '(https?:\\/\\/)',
29 domainCharacterSet = '[\\da-z\\.-]+',
30 negatedDomainCharacterSet = '[^\\da-z\\.-]+',
31 domainBodyClause = '(' + domainCharacterSet + ')',
32 tldClause = '([a-z\\.]{2,6})',
23cfa3d8
DI
33 ipClause = '((\\d{1,3}\\.){3}\\d{1,3})',
34 portClause = '(:\\d{1,5})',
9c3b1105 35 hostClause = '((' + domainBodyClause + '\\.' + tldClause + ')|' + ipClause + ')' + portClause + '?',
23cfa3d8 36 pathClause = '(\\/[\\/\\w\\.-]*)*',
fff56eea
PK
37 negatedPathCharacterSet = '[^\\/\\w\\.-]+',
38 bodyClause = hostClause + pathClause,
39 start = '(?:^|' + negatedDomainCharacterSet + ')(',
40 end = ')($|' + negatedPathCharacterSet + ')',
41 lenientUrlClause = start + protocolClause + '?' + bodyClause + end,
42 strictUrlClause = start + protocolClause + bodyClause + end,
43 lenientUrlRegex = new RegExp(lenientUrlClause),
44 strictUrlRegex = new RegExp(strictUrlClause);
45
fff56eea
PK
46 /**
47 * Converts all valid URLs found in the given terminal line into
48 * hyperlinks. The terminal line can be either the HTML element itself
49 * or the index of the termina line in the children of the terminal
50 * rows container.
51 *
52 * @param {Xterm} terminal - The terminal that owns the given line.
53 * @param {number|HTMLDivElement} line - The terminal line that should get
54 * "linkified".
55 * @param {boolean} lenient - The regex type that will be used to identify links. If lenient is
56 * false, the regex requires a protocol clause. Defaults to true.
9604674c
TJ
57 * @param {string} target - Sets target="" attribute with value provided to links.
58 * Default doesn't set target attribute
fff56eea
PK
59 * @emits linkify
60 * @emits linkify:line
61 */
d9294779 62 exports.linkifyTerminalLine = function (terminal, line, lenient, target) {
fff56eea
PK
63 if (typeof line == 'number') {
64 line = terminal.rowContainer.children[line];
65 } else if (! (line instanceof HTMLDivElement)) {
66 var message = 'The "line" argument should be either a number';
67 message += ' or an HTMLDivElement';
68
69 throw new TypeError(message);
70 }
b1058b9f 71
64d77d24
TJ
72 if (typeof target === 'undefined') {
73 target = '';
74 } else {
75 target = 'target="' + target + '"';
76 }
d9294779 77
fff56eea
PK
78 var buffer = document.createElement('span'),
79 nodes = line.childNodes;
80
81 for (var j=0; j<nodes.length; j++) {
82 var node = nodes[j],
83 match;
84
85 /**
86 * Since we cannot access the TextNode's HTML representation
87 * from the instance itself, we assign its data as textContent
88 * to a dummy buffer span, in order to retrieve the TextNode's
89 * HTML representation from the buffer's innerHTML.
90 */
91 buffer.textContent = node.data;
92
93 var nodeHTML = buffer.innerHTML;
94
95 /**
96 * Apply function only on TextNodes
97 */
98 if (node.nodeType != node.TEXT_NODE) {
99 continue;
100 }
101
f2f0f460 102 var url = exports.findLinkMatch(node.data, lenient);
fff56eea 103
f2f0f460 104 if (!url) {
fff56eea
PK
105 continue;
106 }
107
f2f0f460 108 var startsWithProtocol = new RegExp('^' + protocolClause),
fff56eea
PK
109 urlHasProtocol = url.match(startsWithProtocol),
110 href = (urlHasProtocol) ? url : 'http://' + url,
9604674c 111 link = '<a href="' + href + '" ' + target + '>' + url + '</a>',
fff56eea
PK
112 newHTML = nodeHTML.replace(url, link);
113
114 line.innerHTML = line.innerHTML.replace(nodeHTML, newHTML);
115 }
c8a497eb
PK
116
117 /**
fff56eea
PK
118 * This event gets emitted when conversion of all URL susbtrings
119 * to HTML anchor elements (links) has finished, for a specific
120 * line of the current Xterm instance.
c8a497eb 121 *
fff56eea 122 * @event linkify:line
c8a497eb 123 */
fff56eea
PK
124 terminal.emit('linkify:line', line);
125 };
126
f2f0f460
DI
127 /**
128 * Finds a link within a block of text.
129 *
130 * @param {string} text - The text to search .
131 * @param {boolean} lenient - Whether to use the lenient search.
132 * @return {string} A URL.
133 */
134 exports.findLinkMatch = function (text, lenient) {
135 var match = text.match(lenient ? lenientUrlRegex : strictUrlRegex);
136 if (!match || match.length === 0) {
137 return null;
138 }
139 return match[1];
140 }
fff56eea
PK
141
142 /**
143 * Converts all valid URLs found in the terminal view into hyperlinks.
144 *
145 * @param {Xterm} terminal - The terminal that should get "linkified".
146 * @param {boolean} lenient - The regex type that will be used to identify links. If lenient is
147 * false, the regex requires a protocol clause. Defaults to true.
9604674c
TJ
148 * @param {string} target - Sets target="" attribute with value provided to links.
149 * Default doesn't set target attribute
fff56eea
PK
150 * @emits linkify
151 * @emits linkify:line
152 */
d9294779 153 exports.linkify = function (terminal, lenient, target) {
fff56eea
PK
154 var rows = terminal.rowContainer.children;
155
156 lenient = (typeof lenient == "boolean") ? lenient : true;
157 for (var i=0; i<rows.length; i++) {
158 var line = rows[i];
159
d9294779 160 exports.linkifyTerminalLine(terminal, line, lenient, target);
fff56eea 161 }
c8a497eb
PK
162
163 /**
fff56eea
PK
164 * This event gets emitted when conversion of all URL substrings to
165 * HTML anchor elements (links) has finished for the current Xterm
166 * instance's view.
c8a497eb 167 *
fff56eea 168 * @event linkify
c8a497eb 169 */
fff56eea
PK
170 terminal.emit('linkify');
171 };
172
173 /**
174 * Extend Xterm prototype.
175 */
176
177 /**
dc67c945
PK
178 * Converts all valid URLs found in the current terminal linte into
179 * hyperlinks.
fff56eea 180 *
dc67c945
PK
181 * @memberof Xterm
182 * @param {number|HTMLDivElement} line - The terminal line that should get
183 * "linkified".
184 * @param {boolean} lenient - The regex type that will be used to identify links. If lenient is
185 * false, the regex requires a protocol clause. Defaults to true.
9604674c
TJ
186 * @param {string} target - Sets target="" attribute with value provided to links.
187 * Default doesn't set target attribute
fff56eea 188 */
d9294779
TJ
189 Xterm.prototype.linkifyTerminalLine = function (line, lenient, target) {
190 return exports.linkifyTerminalLine(this, line, lenient, target);
fff56eea
PK
191 };
192
193 /**
dc67c945
PK
194 * Converts all valid URLs found in the current terminal into hyperlinks.
195 *
196 * @memberof Xterm
197 * @param {boolean} lenient - The regex type that will be used to identify links. If lenient is
198 * false, the regex requires a protocol clause. Defaults to true.
9604674c
TJ
199 * @param {string} target - Sets target="" attribute with value provided to links.
200 * Default doesn't set target attribute
fff56eea 201 */
d9294779
TJ
202 Xterm.prototype.linkify = function (lenient, target) {
203 return exports.linkify(this, lenient, target);
dc67c945 204 };
c8a497eb 205
fff56eea 206 return exports;
b1058b9f 207});