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