]>
git.proxmox.com Git - mirror_xterm.js.git/blob - addons/linkify/linkify.js
2 if (typeof define
== 'function') {
4 * Require.js is available
6 define(['../../src/xterm'], linkify
);
9 * Plain browser environment
17 * This module provides methods for convertings valid URL substrings
18 * into HTML anchor elements (links), inside a terminal view.
20 * @module xterm/addons/linkify/linkify
23 protocolClause
= '(https?:\\/\\/)',
24 domainBodyClause
= '([\\da-z\\.-]+)',
25 tldClause
= '([a-z\\.]{2,6})',
26 hostClause
= domainBodyClause
+ '\\.' + tldClause
,
27 pathClause
= '([\\/\\w\\.-]*)*\\/?',
28 bodyClause
= hostClause
+ pathClause
,
29 start
= '(?:^|\\s+)(',
31 urlClause
= start
+ protocolClause
+ '?' + bodyClause
+ end
,
32 urlRegex
= new RegExp(urlClause
);
35 * Converts all valid URLs found in the given terminal line into
36 * hyperlinks. The terminal line can be either the HTML element itself
37 * or the index of the termina line in the children of the terminal
40 * @param {Xterm} terminal - The terminal that owns the given line.
41 * @param {number|HTMLDivElement} line - The terminal line that should get
46 exports
.linkifyTerminalLine = function (terminal
, line
) {
47 if (typeof line
== 'number') {
48 line
= terminal
.rowContainer
.children
[line
];
49 } else if (! (line
instanceof HTMLDivElement
)) {
50 var message
= 'The "line" argument should be either a number';
51 message
+= ' or an HTMLDivElement';
53 throw new TypeError(message
);
56 var buffer
= document
.createElement('span'),
57 nodes
= line
.childNodes
;
59 console
.log(nodes
.length
, 'number of nodes');
60 for (var j
=0; j
<nodes
.length
; j
++) {
64 * Since we cannot access the TextNode's HTML representation
65 * from the instance itself, we assign its data as textContent
66 * to a dummy buffer span, in order to retrieve the TextNode's
67 * HTML representation from the buffer's innerHTML.
69 buffer
.textContent
= node
.data
;
71 var nodeHTML
= buffer
.innerHTML
;
74 * Apply function only on TextNodes
76 if (node
.nodeType
!= node
.TEXT_NODE
) {
81 var match
= node
.data
.match(urlRegex
);
84 * If no URL was found in the current text, return.
91 startsWithProtocol
= new RegExp('^' + protocolClause
),
92 urlHasProtocol
= url
.match(startsWithProtocol
),
93 href
= (urlHasProtocol
) ? url
: 'http://' + url
,
94 link
= '<a href="' + href
+ '" >' + url
+ '</a>',
95 newHTML
= nodeHTML
.replace(url
, link
);
97 line
.innerHTML
= line
.innerHTML
.replace(nodeHTML
, newHTML
);
101 * This event gets emitted when conversion of all URL susbtrings
102 * to HTML anchor elements (links) has finished, for a specific
103 * line of the current Xterm instance.
105 * @event linkify:line
107 terminal
.emit('linkify:line', line
);
112 * Converts all valid URLs found in the terminal view into hyperlinks.
114 * @param {Xterm} terminal - The terminal that should get "linkified".
116 * @emits linkify:line
118 exports
.linkify = function (terminal
) {
119 var rows
= terminal
.rowContainer
.children
;
121 for (var i
=0; i
<rows
.length
; i
++) {
124 exports
.linkifyTerminalLine(terminal
, line
);
128 * This event gets emitted when conversion of all URL substrings to
129 * HTML anchor elements (links) has finished for the current Xterm
134 terminal
.emit('linkify');
138 * Extend Xterm prototype.
142 * Converts all valid URLs found in the current terminal linte into
146 * @param {number|HTMLDivElement} line - The terminal line that should get
149 Xterm
.prototype.linkifyTerminalLine = function (line
) {
150 return exports
.linkifyTerminalLine(this, line
);
154 * Converts all valid URLs found in the current terminal into hyperlinks.
158 Xterm
.prototype.linkify = function () {
159 return exports
.linkify(this);