2 if (typeof exports
=== 'object' && typeof module
=== 'object') {
6 module
.exports
= linkify(require('../../src/xterm'));
7 } else if (typeof define
== 'function') {
9 * Require.js is available
11 define(['../../src/xterm'], linkify
);
14 * Plain browser environment
16 linkify(window
.Terminal
);
22 * This module provides methods for convertings valid URL substrings
23 * into HTML anchor elements (links), inside a terminal view.
25 * @module xterm/addons/linkify/linkify
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
);
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
52 * @param {Xterm} terminal - The terminal that owns the given line.
53 * @param {number|HTMLDivElement} line - The terminal line that should get
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
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';
69 throw new TypeError(message
);
72 if (typeof target
=== 'undefined') {
75 target
= 'target="' + target
+ '"';
78 var buffer
= document
.createElement('span'),
79 nodes
= line
.childNodes
;
81 for (var j
=0; j
<nodes
.length
; j
++) {
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.
91 buffer
.textContent
= node
.data
;
93 var nodeHTML
= buffer
.innerHTML
;
96 * Apply function only on TextNodes
98 if (node
.nodeType
!= node
.TEXT_NODE
) {
102 var url
= exports
.findLinkMatch(node
.data
, lenient
);
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
);
114 line
.innerHTML
= line
.innerHTML
.replace(nodeHTML
, newHTML
);
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.
122 * @event linkify:line
124 terminal
.emit('linkify:line', line
);
128 * Finds a link within a block of text.
130 * @param {string} text - The text to search .
131 * @param {boolean} lenient - Whether to use the lenient search.
132 * @return {string} A URL.
134 exports
.findLinkMatch = function (text
, lenient
) {
135 var match
= text
.match(lenient
? lenientUrlRegex
: strictUrlRegex
);
136 if (!match
|| match
.length
=== 0) {
143 * Converts all valid URLs found in the terminal view into hyperlinks.
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
151 * @emits linkify:line
153 exports
.linkify = function (terminal
, lenient
, target
) {
154 var rows
= terminal
.rowContainer
.children
;
156 lenient
= (typeof lenient
== "boolean") ? lenient
: true;
157 for (var i
=0; i
<rows
.length
; i
++) {
160 exports
.linkifyTerminalLine(terminal
, line
, lenient
, target
);
164 * This event gets emitted when conversion of all URL substrings to
165 * HTML anchor elements (links) has finished for the current Xterm
170 terminal
.emit('linkify');
174 * Extend Xterm prototype.
178 * Converts all valid URLs found in the current terminal linte into
182 * @param {number|HTMLDivElement} line - The terminal line that should get
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
189 Xterm
.prototype.linkifyTerminalLine = function (line
, lenient
, target
) {
190 return exports
.linkifyTerminalLine(this, line
, lenient
, target
);
194 * Converts all valid URLs found in the current terminal into hyperlinks.
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
202 Xterm
.prototype.linkify = function (lenient
, target
) {
203 return exports
.linkify(this, lenient
, target
);