]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/Linkifier.ts
Allow links to be registered before terminal is attached to DOM
[mirror_xterm.js.git] / src / Linkifier.ts
CommitLineData
2207d356 1/**
55cb43d7 2 * @license MIT
2207d356 3 */
55cb43d7 4
6198556e 5import { LinkMatcherOptions } from './Interfaces';
7ac4f1a9 6import { LinkMatcher, LinkMatcherHandler, LinkMatcherValidationCallback } from './Types';
6198556e
DI
7
8const INVALID_LINK_CLASS = 'xterm-invalid-link';
2207d356 9
2207d356
DI
10const protocolClause = '(https?:\\/\\/)';
11const domainCharacterSet = '[\\da-z\\.-]+';
12const negatedDomainCharacterSet = '[^\\da-z\\.-]+';
13const domainBodyClause = '(' + domainCharacterSet + ')';
14const tldClause = '([a-z\\.]{2,6})';
15const ipClause = '((\\d{1,3}\\.){3}\\d{1,3})';
28d4ec77 16const localHostClause = '(localhost)';
2207d356 17const portClause = '(:\\d{1,5})';
28d4ec77 18const hostClause = '((' + domainBodyClause + '\\.' + tldClause + ')|' + ipClause + '|' + localHostClause + ')' + portClause + '?';
b68180b9
DI
19const pathClause = '(\\/[\\/\\w\\.\\-%~]*)*';
20const queryStringHashFragmentCharacterSet = '[0-9\\w\\[\\]\\(\\)\\/\\?\\!#@$%&\'*+,:;~\\=\\.\\-]*';
9aae4396
DI
21const queryStringClause = '(\\?' + queryStringHashFragmentCharacterSet + ')?';
22const hashFragmentClause = '(#' + queryStringHashFragmentCharacterSet + ')?';
7279ee0f 23const negatedPathCharacterSet = '[^\\/\\w\\.\\-%]+';
9aae4396 24const bodyClause = hostClause + pathClause + queryStringClause + hashFragmentClause;
2207d356
DI
25const start = '(?:^|' + negatedDomainCharacterSet + ')(';
26const end = ')($|' + negatedPathCharacterSet + ')';
f7bc0fba 27const strictUrlRegex = new RegExp(start + protocolClause + bodyClause + end);
2207d356 28
55cb43d7
DI
29/**
30 * The ID of the built in http(s) link matcher.
31 */
7167b06b
DI
32const HYPERTEXT_LINK_MATCHER_ID = 0;
33
f7bc0fba
DI
34/**
35 * The Linkifier applies links to rows shortly after they have been refreshed.
36 */
2207d356 37export class Linkifier {
15d79143
DI
38 /**
39 * The time to wait after a row is changed before it is linkified. This prevents
40 * the costly operation of searching every row multiple times, pntentially a
41 * huge aount of times.
42 */
43 protected static TIME_BEFORE_LINKIFY = 200;
44
7ac4f1a9
DI
45 protected _linkMatchers: LinkMatcher[];
46
26ebc3d9 47 private _document: Document;
2207d356
DI
48 private _rows: HTMLElement[];
49 private _rowTimeoutIds: number[];
5183332f 50 private _nextLinkMatcherId = HYPERTEXT_LINK_MATCHER_ID;
2207d356 51
b0624cad 52 constructor() {
2207d356 53 this._rowTimeoutIds = [];
7167b06b 54 this._linkMatchers = [];
012051c1 55 this.registerLinkMatcher(strictUrlRegex, null, { matchIndex: 1 });
2207d356
DI
56 }
57
b0624cad
DI
58 /**
59 * Attaches the linkifier to the DOM, enabling linkification.
60 * @param document The document object.
61 * @param rows The array of rows to apply links to.
62 */
63 public attachToDom(document: Document, rows: HTMLElement[]) {
64 this._document = document;
65 this._rows = rows;
66 }
67
2207d356
DI
68 /**
69 * Queues a row for linkification.
70 * @param {number} rowIndex The index of the row to linkify.
71 */
72 public linkifyRow(rowIndex: number): void {
b0624cad
DI
73 // Don't attempt linkify if not yet attached to DOM
74 if (!this._document) {
75 return;
76 }
77
2207d356
DI
78 const timeoutId = this._rowTimeoutIds[rowIndex];
79 if (timeoutId) {
80 clearTimeout(timeoutId);
81 }
15d79143 82 this._rowTimeoutIds[rowIndex] = setTimeout(this._linkifyRow.bind(this, rowIndex), Linkifier.TIME_BEFORE_LINKIFY);
2207d356
DI
83 }
84
7167b06b 85 /**
3bf31aa4
DI
86 * Attaches a handler for hypertext links, overriding default <a> behavior
87 * for standard http(s) links.
7167b06b
DI
88 * @param {LinkHandler} handler The handler to use, this can be cleared with
89 * null.
90 */
4c99c032 91 public attachHypertextLinkHandler(handler: LinkMatcherHandler): void {
7167b06b
DI
92 this._linkMatchers[HYPERTEXT_LINK_MATCHER_ID].handler = handler;
93 }
94
95 /**
96 * Registers a link matcher, allowing custom link patterns to be matched and
97 * handled.
3b62aa44 98 * @param {RegExp} regex The regular expression to search for, specifically
1ee774d0
DI
99 * this searches the textContent of the rows. You will want to use \s to match
100 * a space ' ' character for example.
7167b06b 101 * @param {LinkHandler} handler The callback when the link is called.
6198556e 102 * @param {LinkMatcherOptions} [options] Options for the link matcher.
7167b06b
DI
103 * @return {number} The ID of the new matcher, this can be used to deregister.
104 */
4c99c032 105 public registerLinkMatcher(regex: RegExp, handler: LinkMatcherHandler, options: LinkMatcherOptions = {}): number {
5183332f 106 if (this._nextLinkMatcherId !== HYPERTEXT_LINK_MATCHER_ID && !handler) {
aafb5333 107 throw new Error('handler must be defined');
7167b06b
DI
108 }
109 const matcher: LinkMatcher = {
5183332f 110 id: this._nextLinkMatcherId++,
7167b06b 111 regex,
c8bb3216 112 handler,
6198556e 113 matchIndex: options.matchIndex,
7ac4f1a9
DI
114 validationCallback: options.validationCallback,
115 priority: options.priority || 0
7167b06b 116 };
7ac4f1a9 117 this._addLinkMatcherToList(matcher);
7167b06b
DI
118 return matcher.id;
119 }
120
7ac4f1a9
DI
121 /**
122 * Inserts a link matcher to the list in the correct position based on the
123 * priority of each link matcher. New link matchers of equal priority are
124 * considered after older link matchers.
125 * @param matcher The link matcher to be added.
126 */
127 private _addLinkMatcherToList(matcher: LinkMatcher): void {
128 if (this._linkMatchers.length === 0) {
129 this._linkMatchers.push(matcher);
130 return;
131 }
132
133 for (let i = this._linkMatchers.length - 1; i >= 0; i--) {
78d5fc95 134 if (matcher.priority <= this._linkMatchers[i].priority) {
7ac4f1a9
DI
135 this._linkMatchers.splice(i + 1, 0, matcher);
136 return;
137 }
138 }
139
78d5fc95 140 this._linkMatchers.splice(0, 0, matcher);
7ac4f1a9
DI
141 }
142
7167b06b
DI
143 /**
144 * Deregisters a link matcher if it has been registered.
145 * @param {number} matcherId The link matcher's ID (returned after register)
1c030f57 146 * @return {boolean} Whether a link matcher was found and deregistered.
7167b06b 147 */
1c030f57 148 public deregisterLinkMatcher(matcherId: number): boolean {
7167b06b
DI
149 // ID 0 is the hypertext link matcher which cannot be deregistered
150 for (let i = 1; i < this._linkMatchers.length; i++) {
151 if (this._linkMatchers[i].id === matcherId) {
152 this._linkMatchers.splice(i, 1);
1c030f57 153 return true;
7167b06b
DI
154 }
155 }
1c030f57 156 return false;
2207d356
DI
157 }
158
159 /**
160 * Linkifies a row.
161 * @param {number} rowIndex The index of the row to linkify.
162 */
163 private _linkifyRow(rowIndex: number): void {
c4f43184
DI
164 const row = this._rows[rowIndex];
165 if (!row) {
166 return;
167 }
168 const text = row.textContent;
e6fc80c1 169 for (let i = 0; i < this._linkMatchers.length; i++) {
7167b06b 170 const matcher = this._linkMatchers[i];
1ee774d0 171 const uri = this._findLinkMatch(text, matcher.regex, matcher.matchIndex);
7167b06b 172 if (uri) {
235ae2fb 173 const linkElement = this._doLinkifyRow(rowIndex, uri, matcher.handler, matcher.id === HYPERTEXT_LINK_MATCHER_ID);
6198556e 174 // Fire validation callback
dcffaf1c 175 if (linkElement && matcher.validationCallback) {
6198556e
DI
176 matcher.validationCallback(uri, isValid => {
177 if (!isValid) {
178 linkElement.classList.add(INVALID_LINK_CLASS);
179 }
180 });
181 }
7167b06b
DI
182 // Only allow a single LinkMatcher to trigger on any given row.
183 return;
184 }
a489037e 185 }
7167b06b 186 }
a489037e 187
7167b06b
DI
188 /**
189 * Linkifies a row given a specific handler.
190 * @param {number} rowIndex The index of the row to linkify.
191 * @param {string} uri The uri that has been found.
192 * @param {handler} handler The handler to trigger when the link is triggered.
6198556e 193 * @return The link element if it was added, otherwise undefined.
7167b06b 194 */
235ae2fb 195 private _doLinkifyRow(rowIndex: number, uri: string, handler: LinkMatcherHandler, isHttpLinkMatcher: boolean): HTMLElement {
a489037e
DI
196 // Iterate over nodes as we want to consider text nodes
197 const nodes = this._rows[rowIndex].childNodes;
198 for (let i = 0; i < nodes.length; i++) {
199 const node = nodes[i];
200 const searchIndex = node.textContent.indexOf(uri);
201 if (searchIndex >= 0) {
235ae2fb 202 const linkElement = this._createAnchorElement(uri, handler, isHttpLinkMatcher);
99a27021 203 if (node.textContent.length === uri.length) {
a489037e 204 // Matches entire string
26ebc3d9
DI
205
206 if (node.nodeType === 3 /*Node.TEXT_NODE*/) {
a489037e
DI
207 this._replaceNode(node, linkElement);
208 } else {
a489037e 209 const element = (<HTMLElement>node);
c8bb3216
DI
210 if (element.nodeName === 'A') {
211 // This row has already been linkified
212 return;
213 }
a489037e
DI
214 element.innerHTML = '';
215 element.appendChild(linkElement);
216 }
217 } else {
218 // Matches part of string
a489037e
DI
219 this._replaceNodeSubstringWithNode(node, linkElement, uri, searchIndex);
220 }
6198556e 221 return linkElement;
a489037e 222 }
2207d356
DI
223 }
224 }
225
226 /**
1ee774d0
DI
227 * Finds a link match in a piece of text.
228 * @param {string} text The text to search.
c8bb3216 229 * @param {number} matchIndex The regex match index of the link.
a489037e 230 * @return {string} The matching URI or null if not found.
2207d356 231 */
1ee774d0
DI
232 private _findLinkMatch(text: string, regex: RegExp, matchIndex?: number): string {
233 const match = text.match(regex);
2207d356
DI
234 if (!match || match.length === 0) {
235 return null;
236 }
c8bb3216 237 return match[typeof matchIndex !== 'number' ? 0 : matchIndex];
2207d356 238 }
a489037e
DI
239
240 /**
241 * Creates a link anchor element.
242 * @param {string} uri The uri of the link.
243 * @return {HTMLAnchorElement} The link.
244 */
1f518b0b 245 private _createAnchorElement(uri: string, handler: LinkMatcherHandler, isHypertextLinkHandler: boolean): HTMLAnchorElement {
26ebc3d9 246 const element = this._document.createElement('a');
a489037e 247 element.textContent = uri;
adde8ab2 248 element.draggable = false;
1f518b0b 249 if (isHypertextLinkHandler) {
a489037e 250 element.href = uri;
0f3ee21d
DI
251 // Force link on another tab so work is not lost
252 element.target = '_blank';
aafb5333
DI
253 element.addEventListener('click', (event: MouseEvent) => {
254 if (handler) {
255 return handler(event, uri);
c7b4c2be
DI
256 }
257 });
1f518b0b
DI
258 } else {
259 element.addEventListener('click', (event: MouseEvent) => {
260 // Don't execute the handler if the link is flagged as invalid
261 if (element.classList.contains(INVALID_LINK_CLASS)) {
262 return;
263 }
264 return handler(event, uri);
265 });
a489037e
DI
266 }
267 return element;
268 }
269
270 /**
271 * Replace a node with 1 or more other nodes.
272 * @param {Node} oldNode The node to replace.
273 * @param {Node[]} newNodes The new nodes to insert in order.
274 */
275 private _replaceNode(oldNode: Node, ...newNodes: Node[]): void {
276 const parent = oldNode.parentNode;
277 for (let i = 0; i < newNodes.length; i++) {
278 parent.insertBefore(newNodes[i], oldNode);
279 }
280 parent.removeChild(oldNode);
281 }
282
283 /**
284 * Replace a substring within a node with a new node.
0f3ee21d
DI
285 * @param {Node} targetNode The target node; either a text node or a <span>
286 * containing a single text node.
a489037e
DI
287 * @param {Node} newNode The new node to insert.
288 * @param {string} substring The substring to replace.
289 * @param {number} substringIndex The index of the substring within the string.
290 */
291 private _replaceNodeSubstringWithNode(targetNode: Node, newNode: Node, substring: string, substringIndex: number): void {
292 let node = targetNode;
26ebc3d9 293 if (node.nodeType !== 3/*Node.TEXT_NODE*/) {
a489037e
DI
294 node = node.childNodes[0];
295 }
0f3ee21d
DI
296
297 // The targetNode will be either a text node or a <span>. The text node
298 // (targetNode or its only-child) needs to be replaced with newNode plus new
299 // text nodes potentially on either side.
a489037e
DI
300 if (node.childNodes.length === 0 && node.nodeType !== Node.TEXT_NODE) {
301 throw new Error('targetNode must be a text node or only contain a single text node');
302 }
303
304 const fullText = node.textContent;
305
306 if (substringIndex === 0) {
307 // Replace with <newNode><textnode>
a489037e 308 const rightText = fullText.substring(substring.length);
26ebc3d9 309 const rightTextNode = this._document.createTextNode(rightText);
a489037e
DI
310 this._replaceNode(node, newNode, rightTextNode);
311 } else if (substringIndex === targetNode.textContent.length - substring.length) {
312 // Replace with <textnode><newNode>
a489037e 313 const leftText = fullText.substring(0, substringIndex);
26ebc3d9 314 const leftTextNode = this._document.createTextNode(leftText);
a489037e
DI
315 this._replaceNode(node, leftTextNode, newNode);
316 } else {
317 // Replace with <textnode><newNode><textnode>
a489037e 318 const leftText = fullText.substring(0, substringIndex);
26ebc3d9 319 const leftTextNode = this._document.createTextNode(leftText);
a489037e 320 const rightText = fullText.substring(substringIndex + substring.length);
26ebc3d9 321 const rightTextNode = this._document.createTextNode(rightText);
a489037e
DI
322 this._replaceNode(node, leftTextNode, newNode, rightTextNode);
323 }
324 }
2207d356 325}