]> git.proxmox.com Git - mirror_xterm.js.git/blob - addons/linkify/linkify.js
Remove typings (not needed in TS 2+)
[mirror_xterm.js.git] / addons / linkify / linkify.js
1 (function (linkify) {
2 if (typeof exports === 'object' && typeof module === 'object') {
3 /*
4 * CommonJS environment
5 */
6 module.exports = linkify(require('../../dist/xterm'));
7 } else if (typeof define == 'function') {
8 /*
9 * Require.js is available
10 */
11 define(['../../dist/xterm'], linkify);
12 } else {
13 /*
14 * Plain browser environment
15 */
16 linkify(window.Terminal);
17 }
18 })(function (Xterm) {
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})',
33 ipClause = '((\\d{1,3}\\.){3}\\d{1,3})',
34 portClause = '(:\\d{1,5})',
35 hostClause = '((' + domainBodyClause + '\\.' + tldClause + ')|' + ipClause + ')' + portClause + '?',
36 pathClause = '(\\/[\\/\\w\\.-]*)*',
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
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.
57 * @param {string} target - Sets target="" attribute with value provided to links.
58 * Default doesn't set target attribute
59 * @emits linkify
60 * @emits linkify:line
61 */
62 exports.linkifyTerminalLine = function (terminal, line, lenient, target) {
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 }
71
72 if (typeof target === 'undefined') {
73 target = '';
74 } else {
75 target = 'target="' + target + '"';
76 }
77
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
102 var url = exports.findLinkMatch(node.data, lenient);
103
104 if (!url) {
105 continue;
106 }
107
108 var startsWithProtocol = new RegExp('^' + protocolClause),
109 urlHasProtocol = url.match(startsWithProtocol),
110 href = (urlHasProtocol) ? url : 'http://' + url,
111 link = '<a href="' + href + '" ' + target + '>' + url + '</a>',
112 newHTML = nodeHTML.replace(url, link);
113
114 line.innerHTML = line.innerHTML.replace(nodeHTML, newHTML);
115 }
116
117 /**
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.
121 *
122 * @event linkify:line
123 */
124 terminal.emit('linkify:line', line);
125 };
126
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 }
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.
148 * @param {string} target - Sets target="" attribute with value provided to links.
149 * Default doesn't set target attribute
150 * @emits linkify
151 * @emits linkify:line
152 */
153 exports.linkify = function (terminal, lenient, target) {
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
160 exports.linkifyTerminalLine(terminal, line, lenient, target);
161 }
162
163 /**
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.
167 *
168 * @event linkify
169 */
170 terminal.emit('linkify');
171 };
172
173 /**
174 * Extend Xterm prototype.
175 */
176
177 /**
178 * Converts all valid URLs found in the current terminal linte into
179 * hyperlinks.
180 *
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.
186 * @param {string} target - Sets target="" attribute with value provided to links.
187 * Default doesn't set target attribute
188 */
189 Xterm.prototype.linkifyTerminalLine = function (line, lenient, target) {
190 return exports.linkifyTerminalLine(this, line, lenient, target);
191 };
192
193 /**
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.
199 * @param {string} target - Sets target="" attribute with value provided to links.
200 * Default doesn't set target attribute
201 */
202 Xterm.prototype.linkify = function (lenient, target) {
203 return exports.linkify(this, lenient, target);
204 };
205
206 return exports;
207 });