]>
Commit | Line | Data |
---|---|---|
2207d356 | 1 | /** |
55cb43d7 | 2 | * @license MIT |
2207d356 | 3 | */ |
55cb43d7 DI |
4 | |
5 | export type LinkHandler = (uri: string) => void; | |
6 | ||
7 | type LinkMatcher = {id: number, regex: RegExp, handler: LinkHandler}; | |
2207d356 | 8 | |
2207d356 DI |
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 negatedPathCharacterSet = '[^\\/\\w\\.-]+'; | |
19 | const bodyClause = hostClause + pathClause; | |
20 | const start = '(?:^|' + negatedDomainCharacterSet + ')('; | |
21 | const end = ')($|' + negatedPathCharacterSet + ')'; | |
f7bc0fba | 22 | const strictUrlRegex = new RegExp(start + protocolClause + bodyClause + end); |
2207d356 | 23 | |
55cb43d7 DI |
24 | /** |
25 | * The ID of the built in http(s) link matcher. | |
26 | */ | |
7167b06b DI |
27 | const HYPERTEXT_LINK_MATCHER_ID = 0; |
28 | ||
55cb43d7 DI |
29 | /** |
30 | * The time to wait after a row is changed before it is linkified. This prevents | |
31 | * the costly operation of searching every row multiple times, pntentially a | |
32 | * huge aount of times. | |
33 | */ | |
34 | const TIME_BEFORE_LINKIFY = 200; | |
35 | ||
f7bc0fba DI |
36 | /** |
37 | * The Linkifier applies links to rows shortly after they have been refreshed. | |
38 | */ | |
2207d356 | 39 | export class Linkifier { |
7167b06b DI |
40 | private static _nextLinkMatcherId = HYPERTEXT_LINK_MATCHER_ID; |
41 | ||
2207d356 DI |
42 | private _rows: HTMLElement[]; |
43 | private _rowTimeoutIds: number[]; | |
7167b06b | 44 | private _linkMatchers: LinkMatcher[]; |
2207d356 DI |
45 | |
46 | constructor(rows: HTMLElement[]) { | |
47 | this._rows = rows; | |
48 | this._rowTimeoutIds = []; | |
7167b06b DI |
49 | this._linkMatchers = []; |
50 | this.registerLinkMatcher(strictUrlRegex, null); | |
2207d356 DI |
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 | ||
7167b06b | 65 | /** |
3bf31aa4 DI |
66 | * Attaches a handler for hypertext links, overriding default <a> behavior |
67 | * for standard http(s) links. | |
7167b06b DI |
68 | * @param {LinkHandler} handler The handler to use, this can be cleared with |
69 | * null. | |
70 | */ | |
0f3ee21d | 71 | public attachHypertextLinkHandler(handler: LinkHandler): void { |
7167b06b DI |
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 the search for. | |
79 | * @param {LinkHandler} handler The callback when the link is called. | |
80 | * @return {number} The ID of the new matcher, this can be used to deregister. | |
81 | */ | |
82 | public registerLinkMatcher(regex: RegExp, handler: LinkHandler): number { | |
83 | if (Linkifier._nextLinkMatcherId !== HYPERTEXT_LINK_MATCHER_ID && !handler) { | |
84 | throw new Error('handler cannot be falsy'); | |
85 | } | |
86 | const matcher: LinkMatcher = { | |
87 | id: Linkifier._nextLinkMatcherId++, | |
88 | regex, | |
89 | handler | |
90 | }; | |
91 | this._linkMatchers.push(matcher); | |
92 | return matcher.id; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Deregisters a link matcher if it has been registered. | |
97 | * @param {number} matcherId The link matcher's ID (returned after register) | |
98 | */ | |
99 | public deregisterLinkMatcher(matcherId: number): void { | |
100 | // ID 0 is the hypertext link matcher which cannot be deregistered | |
101 | for (let i = 1; i < this._linkMatchers.length; i++) { | |
102 | if (this._linkMatchers[i].id === matcherId) { | |
103 | this._linkMatchers.splice(i, 1); | |
104 | } | |
105 | } | |
2207d356 DI |
106 | } |
107 | ||
108 | /** | |
109 | * Linkifies a row. | |
110 | * @param {number} rowIndex The index of the row to linkify. | |
111 | */ | |
112 | private _linkifyRow(rowIndex: number): void { | |
113 | const rowHtml = this._rows[rowIndex].innerHTML; | |
7167b06b DI |
114 | for (let i = 0; i < this._linkMatchers.length; i++) { |
115 | const matcher = this._linkMatchers[i]; | |
116 | const uri = this._findLinkMatch(rowHtml, matcher.regex); | |
117 | if (uri) { | |
118 | this._doLinkifyRow(rowIndex, uri, matcher.handler); | |
119 | // Only allow a single LinkMatcher to trigger on any given row. | |
120 | return; | |
121 | } | |
a489037e | 122 | } |
7167b06b | 123 | } |
a489037e | 124 | |
7167b06b DI |
125 | /** |
126 | * Linkifies a row given a specific handler. | |
127 | * @param {number} rowIndex The index of the row to linkify. | |
128 | * @param {string} uri The uri that has been found. | |
129 | * @param {handler} handler The handler to trigger when the link is triggered. | |
130 | */ | |
131 | private _doLinkifyRow(rowIndex: number, uri: string, handler?: LinkHandler): void { | |
a489037e DI |
132 | // Iterate over nodes as we want to consider text nodes |
133 | const nodes = this._rows[rowIndex].childNodes; | |
134 | for (let i = 0; i < nodes.length; i++) { | |
135 | const node = nodes[i]; | |
136 | const searchIndex = node.textContent.indexOf(uri); | |
137 | if (searchIndex >= 0) { | |
138 | if (node.childNodes.length > 0) { | |
139 | // This row has already been linkified | |
140 | return; | |
141 | } | |
142 | ||
7167b06b | 143 | const linkElement = this._createAnchorElement(uri, handler); |
a489037e DI |
144 | if (node.textContent.trim().length === uri.length) { |
145 | // Matches entire string | |
a489037e | 146 | if (node.nodeType === Node.TEXT_NODE) { |
a489037e DI |
147 | this._replaceNode(node, linkElement); |
148 | } else { | |
a489037e DI |
149 | const element = (<HTMLElement>node); |
150 | element.innerHTML = ''; | |
151 | element.appendChild(linkElement); | |
152 | } | |
153 | } else { | |
154 | // Matches part of string | |
a489037e DI |
155 | this._replaceNodeSubstringWithNode(node, linkElement, uri, searchIndex); |
156 | } | |
157 | } | |
2207d356 DI |
158 | } |
159 | } | |
160 | ||
161 | /** | |
162 | * Finds a link match in a piece of HTML. | |
163 | * @param {string} html The HTML to search. | |
a489037e | 164 | * @return {string} The matching URI or null if not found. |
2207d356 | 165 | */ |
7167b06b DI |
166 | private _findLinkMatch(html: string, regex: RegExp): string { |
167 | const match = html.match(regex); | |
2207d356 DI |
168 | if (!match || match.length === 0) { |
169 | return null; | |
170 | } | |
171 | return match[1]; | |
172 | } | |
a489037e DI |
173 | |
174 | /** | |
175 | * Creates a link anchor element. | |
176 | * @param {string} uri The uri of the link. | |
177 | * @return {HTMLAnchorElement} The link. | |
178 | */ | |
7167b06b | 179 | private _createAnchorElement(uri: string, handler: LinkHandler): HTMLAnchorElement { |
a489037e DI |
180 | const element = document.createElement('a'); |
181 | element.textContent = uri; | |
7167b06b DI |
182 | if (handler) { |
183 | element.addEventListener('click', () => handler(uri)); | |
a489037e DI |
184 | } else { |
185 | element.href = uri; | |
0f3ee21d DI |
186 | // Force link on another tab so work is not lost |
187 | element.target = '_blank'; | |
a489037e DI |
188 | } |
189 | return element; | |
190 | } | |
191 | ||
192 | /** | |
193 | * Replace a node with 1 or more other nodes. | |
194 | * @param {Node} oldNode The node to replace. | |
195 | * @param {Node[]} newNodes The new nodes to insert in order. | |
196 | */ | |
197 | private _replaceNode(oldNode: Node, ...newNodes: Node[]): void { | |
198 | const parent = oldNode.parentNode; | |
199 | for (let i = 0; i < newNodes.length; i++) { | |
200 | parent.insertBefore(newNodes[i], oldNode); | |
201 | } | |
202 | parent.removeChild(oldNode); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Replace a substring within a node with a new node. | |
0f3ee21d DI |
207 | * @param {Node} targetNode The target node; either a text node or a <span> |
208 | * containing a single text node. | |
a489037e DI |
209 | * @param {Node} newNode The new node to insert. |
210 | * @param {string} substring The substring to replace. | |
211 | * @param {number} substringIndex The index of the substring within the string. | |
212 | */ | |
213 | private _replaceNodeSubstringWithNode(targetNode: Node, newNode: Node, substring: string, substringIndex: number): void { | |
214 | let node = targetNode; | |
215 | if (node.nodeType !== Node.TEXT_NODE) { | |
216 | node = node.childNodes[0]; | |
217 | } | |
0f3ee21d DI |
218 | |
219 | // The targetNode will be either a text node or a <span>. The text node | |
220 | // (targetNode or its only-child) needs to be replaced with newNode plus new | |
221 | // text nodes potentially on either side. | |
a489037e DI |
222 | if (node.childNodes.length === 0 && node.nodeType !== Node.TEXT_NODE) { |
223 | throw new Error('targetNode must be a text node or only contain a single text node'); | |
224 | } | |
225 | ||
226 | const fullText = node.textContent; | |
227 | ||
228 | if (substringIndex === 0) { | |
229 | // Replace with <newNode><textnode> | |
a489037e DI |
230 | const rightText = fullText.substring(substring.length); |
231 | const rightTextNode = document.createTextNode(rightText); | |
232 | this._replaceNode(node, newNode, rightTextNode); | |
233 | } else if (substringIndex === targetNode.textContent.length - substring.length) { | |
234 | // Replace with <textnode><newNode> | |
a489037e DI |
235 | const leftText = fullText.substring(0, substringIndex); |
236 | const leftTextNode = document.createTextNode(leftText); | |
237 | this._replaceNode(node, leftTextNode, newNode); | |
238 | } else { | |
239 | // Replace with <textnode><newNode><textnode> | |
a489037e DI |
240 | const leftText = fullText.substring(0, substringIndex); |
241 | const leftTextNode = document.createTextNode(leftText); | |
242 | const rightText = fullText.substring(substringIndex + substring.length); | |
243 | const rightTextNode = document.createTextNode(rightText); | |
244 | this._replaceNode(node, leftTextNode, newNode, rightTextNode); | |
245 | } | |
246 | } | |
2207d356 | 247 | } |