]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/Linkifier.ts
Merge pull request #557 from Tyriar/CharMeasure_aria_hidden
[mirror_xterm.js.git] / src / Linkifier.ts
1 /**
2 * @license MIT
3 */
4
5 export type LinkHandler = (uri: string) => void;
6
7 type LinkMatcher = {id: number, regex: RegExp, matchIndex?: number, handler: LinkHandler};
8
9 const protocolClause = '(https?:\\/\\/)';
10 const domainCharacterSet = '[\\da-z\\.-]+';
11 const negatedDomainCharacterSet = '[^\\da-z\\.-]+';
12 const domainBodyClause = '(' + domainCharacterSet + ')';
13 const tldClause = '([a-z\\.]{2,6})';
14 const ipClause = '((\\d{1,3}\\.){3}\\d{1,3})';
15 const portClause = '(:\\d{1,5})';
16 const hostClause = '((' + domainBodyClause + '\\.' + tldClause + ')|' + ipClause + ')' + portClause + '?';
17 const pathClause = '(\\/[\\/\\w\\.-]*)*';
18 const queryStringClause = '(\\?[\\w\\[\\]\\(\\)\\/\\?\\!#@$&\'*+,:;]*)?';
19 const negatedPathCharacterSet = '[^\\/\\w\\.-]+';
20 const bodyClause = hostClause + pathClause + queryStringClause;
21 const start = '(?:^|' + negatedDomainCharacterSet + ')(';
22 const end = ')($|' + negatedPathCharacterSet + ')';
23 const strictUrlRegex = new RegExp(start + protocolClause + bodyClause + end);
24
25 /**
26 * The ID of the built in http(s) link matcher.
27 */
28 const HYPERTEXT_LINK_MATCHER_ID = 0;
29
30 /**
31 * The time to wait after a row is changed before it is linkified. This prevents
32 * the costly operation of searching every row multiple times, pntentially a
33 * huge aount of times.
34 */
35 const TIME_BEFORE_LINKIFY = 200;
36
37 /**
38 * The Linkifier applies links to rows shortly after they have been refreshed.
39 */
40 export class Linkifier {
41 private _rows: HTMLElement[];
42 private _rowTimeoutIds: number[];
43 private _linkMatchers: LinkMatcher[];
44 private _nextLinkMatcherId = HYPERTEXT_LINK_MATCHER_ID;
45
46 constructor(rows: HTMLElement[]) {
47 this._rows = rows;
48 this._rowTimeoutIds = [];
49 this._linkMatchers = [];
50 this.registerLinkMatcher(strictUrlRegex, null, 1);
51 }
52
53 /**
54 * Queues a row for linkification.
55 * @param {number} rowIndex The index of the row to linkify.
56 */
57 public linkifyRow(rowIndex: number): void {
58 const timeoutId = this._rowTimeoutIds[rowIndex];
59 if (timeoutId) {
60 clearTimeout(timeoutId);
61 }
62 this._rowTimeoutIds[rowIndex] = setTimeout(this._linkifyRow.bind(this, rowIndex), TIME_BEFORE_LINKIFY);
63 }
64
65 /**
66 * Attaches a handler for hypertext links, overriding default <a> behavior
67 * for standard http(s) links.
68 * @param {LinkHandler} handler The handler to use, this can be cleared with
69 * null.
70 */
71 public attachHypertextLinkHandler(handler: LinkHandler): void {
72 this._linkMatchers[HYPERTEXT_LINK_MATCHER_ID].handler = handler;
73 }
74
75 /**
76 * Registers a link matcher, allowing custom link patterns to be matched and
77 * handled.
78 * @param {RegExp} regex The regular expression to search for, specifically
79 * this searches the textContent of the rows. You will want to use \s to match
80 * a space ' ' character for example.
81 * @param {LinkHandler} handler The callback when the link is called.
82 * @param {number} matchIndex The index of the link from the regex.match(text)
83 * call. This defaults to 0 (for regular expressions without capture groups).
84 * @return {number} The ID of the new matcher, this can be used to deregister.
85 */
86 public registerLinkMatcher(regex: RegExp, handler: LinkHandler, matchIndex?: number): number {
87 if (this._nextLinkMatcherId !== HYPERTEXT_LINK_MATCHER_ID && !handler) {
88 throw new Error('handler cannot be falsy');
89 }
90 const matcher: LinkMatcher = {
91 id: this._nextLinkMatcherId++,
92 regex,
93 handler,
94 matchIndex
95 };
96 this._linkMatchers.push(matcher);
97 return matcher.id;
98 }
99
100 /**
101 * Deregisters a link matcher if it has been registered.
102 * @param {number} matcherId The link matcher's ID (returned after register)
103 * @return {boolean} Whether a link matcher was found and deregistered.
104 */
105 public deregisterLinkMatcher(matcherId: number): boolean {
106 // ID 0 is the hypertext link matcher which cannot be deregistered
107 for (let i = 1; i < this._linkMatchers.length; i++) {
108 if (this._linkMatchers[i].id === matcherId) {
109 this._linkMatchers.splice(i, 1);
110 return true;
111 }
112 }
113 return false;
114 }
115
116 /**
117 * Linkifies a row.
118 * @param {number} rowIndex The index of the row to linkify.
119 */
120 private _linkifyRow(rowIndex: number): void {
121 const row = this._rows[rowIndex];
122 if (!row) {
123 return;
124 }
125 const text = row.textContent;
126 for (let i = 0; i < this._linkMatchers.length; i++) {
127 const matcher = this._linkMatchers[i];
128 const uri = this._findLinkMatch(text, matcher.regex, matcher.matchIndex);
129 if (uri) {
130 this._doLinkifyRow(rowIndex, uri, matcher.handler);
131 // Only allow a single LinkMatcher to trigger on any given row.
132 return;
133 }
134 }
135 }
136
137 /**
138 * Linkifies a row given a specific handler.
139 * @param {number} rowIndex The index of the row to linkify.
140 * @param {string} uri The uri that has been found.
141 * @param {handler} handler The handler to trigger when the link is triggered.
142 */
143 private _doLinkifyRow(rowIndex: number, uri: string, handler?: LinkHandler): void {
144 // Iterate over nodes as we want to consider text nodes
145 const nodes = this._rows[rowIndex].childNodes;
146 for (let i = 0; i < nodes.length; i++) {
147 const node = nodes[i];
148 const searchIndex = node.textContent.indexOf(uri);
149 if (searchIndex >= 0) {
150 const linkElement = this._createAnchorElement(uri, handler);
151 if (node.textContent.trim().length === uri.length) {
152 // Matches entire string
153 if (node.nodeType === Node.TEXT_NODE) {
154 this._replaceNode(node, linkElement);
155 } else {
156 const element = (<HTMLElement>node);
157 if (element.nodeName === 'A') {
158 // This row has already been linkified
159 return;
160 }
161 element.innerHTML = '';
162 element.appendChild(linkElement);
163 }
164 } else {
165 // Matches part of string
166 this._replaceNodeSubstringWithNode(node, linkElement, uri, searchIndex);
167 }
168 }
169 }
170 }
171
172 /**
173 * Finds a link match in a piece of text.
174 * @param {string} text The text to search.
175 * @param {number} matchIndex The regex match index of the link.
176 * @return {string} The matching URI or null if not found.
177 */
178 private _findLinkMatch(text: string, regex: RegExp, matchIndex?: number): string {
179 const match = text.match(regex);
180 if (!match || match.length === 0) {
181 return null;
182 }
183 return match[typeof matchIndex !== 'number' ? 0 : matchIndex];
184 }
185
186 /**
187 * Creates a link anchor element.
188 * @param {string} uri The uri of the link.
189 * @return {HTMLAnchorElement} The link.
190 */
191 private _createAnchorElement(uri: string, handler: LinkHandler): HTMLAnchorElement {
192 const element = document.createElement('a');
193 element.textContent = uri;
194 if (handler) {
195 element.addEventListener('click', () => handler(uri));
196 } else {
197 element.href = uri;
198 // Force link on another tab so work is not lost
199 element.target = '_blank';
200 }
201 return element;
202 }
203
204 /**
205 * Replace a node with 1 or more other nodes.
206 * @param {Node} oldNode The node to replace.
207 * @param {Node[]} newNodes The new nodes to insert in order.
208 */
209 private _replaceNode(oldNode: Node, ...newNodes: Node[]): void {
210 const parent = oldNode.parentNode;
211 for (let i = 0; i < newNodes.length; i++) {
212 parent.insertBefore(newNodes[i], oldNode);
213 }
214 parent.removeChild(oldNode);
215 }
216
217 /**
218 * Replace a substring within a node with a new node.
219 * @param {Node} targetNode The target node; either a text node or a <span>
220 * containing a single text node.
221 * @param {Node} newNode The new node to insert.
222 * @param {string} substring The substring to replace.
223 * @param {number} substringIndex The index of the substring within the string.
224 */
225 private _replaceNodeSubstringWithNode(targetNode: Node, newNode: Node, substring: string, substringIndex: number): void {
226 let node = targetNode;
227 if (node.nodeType !== Node.TEXT_NODE) {
228 node = node.childNodes[0];
229 }
230
231 // The targetNode will be either a text node or a <span>. The text node
232 // (targetNode or its only-child) needs to be replaced with newNode plus new
233 // text nodes potentially on either side.
234 if (node.childNodes.length === 0 && node.nodeType !== Node.TEXT_NODE) {
235 throw new Error('targetNode must be a text node or only contain a single text node');
236 }
237
238 const fullText = node.textContent;
239
240 if (substringIndex === 0) {
241 // Replace with <newNode><textnode>
242 const rightText = fullText.substring(substring.length);
243 const rightTextNode = document.createTextNode(rightText);
244 this._replaceNode(node, newNode, rightTextNode);
245 } else if (substringIndex === targetNode.textContent.length - substring.length) {
246 // Replace with <textnode><newNode>
247 const leftText = fullText.substring(0, substringIndex);
248 const leftTextNode = document.createTextNode(leftText);
249 this._replaceNode(node, leftTextNode, newNode);
250 } else {
251 // Replace with <textnode><newNode><textnode>
252 const leftText = fullText.substring(0, substringIndex);
253 const leftTextNode = document.createTextNode(leftText);
254 const rightText = fullText.substring(substringIndex + substring.length);
255 const rightTextNode = document.createTextNode(rightText);
256 this._replaceNode(node, leftTextNode, newNode, rightTextNode);
257 }
258 }
259 }