@@ -143,6 +143,7 @@ Autolinker.prototype = {
143
143
* For example: A url like 'http://www.yahoo.com/some/long/path/to/a/file' truncated to 25 characters might look
144
144
* something like this: 'yahoo.com/some/long/pat..'
145
145
*/
146
+ truncate : undefined ,
146
147
147
148
/**
148
149
* @cfg {String} className
@@ -192,6 +193,7 @@ Autolinker.prototype = {
192
193
* The HtmlParser instance used to skip over HTML tags, while finding text nodes to process. This is lazily instantiated
193
194
* in the {@link #getHtmlParser} method.
194
195
*/
196
+ htmlParser : undefined ,
195
197
196
198
/**
197
199
* @private
@@ -200,6 +202,7 @@ Autolinker.prototype = {
200
202
* The MatchParser instance used to find URL/email/Twitter matches in the text nodes of an input string passed to
201
203
* {@link #link}. This is lazily instantiated in the {@link #getMatchParser} method.
202
204
*/
205
+ matchParser : undefined ,
203
206
204
207
/**
205
208
* @private
@@ -208,6 +211,7 @@ Autolinker.prototype = {
208
211
* The AnchorTagBuilder instance used to build the URL/email/Twitter replacement anchor tags. This is lazily instantiated
209
212
* in the {@link #getTagBuilder} method.
210
213
*/
214
+ tagBuilder : undefined ,
211
215
212
216
213
217
/**
@@ -225,34 +229,37 @@ Autolinker.prototype = {
225
229
* @return {String } The HTML, with URLs/emails/Twitter handles automatically linked.
226
230
*/
227
231
link : function ( textOrHtml ) {
228
- var me = this , // for closure
229
- htmlParser = this . getHtmlParser ( ) ,
232
+ var htmlParser = this . getHtmlParser ( ) ,
233
+ htmlNodes = htmlParser . parse ( textOrHtml ) ,
230
234
htmlCharacterEntitiesRegex = this . htmlCharacterEntitiesRegex ,
231
235
anchorTagStackCount = 0 , // used to only process text around anchor tags, and any inner text/html they may have
232
236
resultHtml = [ ] ;
233
237
234
- htmlParser . parse ( textOrHtml , {
235
- // Process HTML nodes in the input `textOrHtml`
236
- processHtmlNode : function ( tagText , tagName , isClosingTag ) {
237
- if ( tagName === 'a' ) {
238
- if ( ! isClosingTag ) { // it's the start <a> tag
238
+ for ( var i = 0 , len = htmlNodes . length ; i < len ; i ++ ) {
239
+ var htmlNode = htmlNodes [ i ] ;
240
+
241
+ if ( htmlNode . getType ( ) === 'element' ) {
242
+ // Process HTML nodes in the input `textOrHtml`
243
+ if ( htmlNode . getTagName ( ) === 'a' ) {
244
+ if ( ! htmlNode . isClosing ( ) ) { // it's the start <a> tag
239
245
anchorTagStackCount ++ ;
240
246
} else { // it's the end </a> tag
241
247
anchorTagStackCount = Math . max ( anchorTagStackCount - 1 , 0 ) ; // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
242
248
}
243
249
}
244
- resultHtml . push ( tagText ) ; // now add the text of the tag itself verbatim
245
- } ,
246
-
247
- // Process text nodes in the input `textOrHtml`
248
- processTextNode : function ( text ) {
250
+ resultHtml . push ( htmlNode . getText ( ) ) ; // now add the text of the tag itself verbatim
251
+
252
+ } else {
253
+ // Process text nodes in the input `textOrHtml`
254
+ var text = htmlNode . getText ( ) ;
255
+
249
256
if ( anchorTagStackCount === 0 ) {
250
257
// If we're not within an <a> tag, process the text node
251
258
var unescapedText = Autolinker . Util . splitAndCapture ( text , htmlCharacterEntitiesRegex ) ; // split at HTML entities, but include the HTML entities in the results array
252
259
253
- for ( var i = 0 , len = unescapedText . length ; i < len ; i ++ ) {
254
- var textToProcess = unescapedText [ i ] ,
255
- processedTextNode = me . doReplacements ( textToProcess ) ;
260
+ for ( var j = 0 , jlen = unescapedText . length ; j < jlen ; j ++ ) {
261
+ var textToProcess = unescapedText [ j ] ,
262
+ processedTextNode = this . doReplacements ( textToProcess ) ;
256
263
257
264
resultHtml . push ( processedTextNode ) ;
258
265
}
@@ -263,26 +270,9 @@ Autolinker.prototype = {
263
270
resultHtml . push ( text ) ;
264
271
}
265
272
}
266
- } ) ;
267
-
268
- return resultHtml . join ( "" ) ;
269
- } ,
270
-
271
-
272
- /**
273
- * Lazily instantiates and returns the {@link #htmlParser} instance for this Autolinker instance.
274
- *
275
- * @protected
276
- * @return {Autolinker.htmlParser.HtmlParser }
277
- */
278
- getHtmlParser : function ( ) {
279
- var htmlParser = this . htmlParser ;
280
-
281
- if ( ! htmlParser ) {
282
- htmlParser = this . htmlParser = new Autolinker . htmlParser . HtmlParser ( ) ;
283
273
}
284
274
285
- return htmlParser ;
275
+ return resultHtml . join ( "" ) ;
286
276
} ,
287
277
288
278
@@ -301,28 +291,6 @@ Autolinker.prototype = {
301
291
} ,
302
292
303
293
304
- /**
305
- * Lazily instantiates and returns the {@link #matchParser} instance for this Autolinker instance.
306
- *
307
- * @protected
308
- * @return {Autolinker.matchParser.MatchParser }
309
- */
310
- getMatchParser : function ( ) {
311
- var matchParser = this . matchParser ;
312
-
313
- if ( ! matchParser ) {
314
- matchParser = this . matchParser = new Autolinker . matchParser . MatchParser ( {
315
- urls : this . urls ,
316
- email : this . email ,
317
- twitter : this . twitter ,
318
- stripPrefix : this . stripPrefix
319
- } ) ;
320
- }
321
-
322
- return matchParser ;
323
- } ,
324
-
325
-
326
294
/**
327
295
* Creates the return string value for a given match in the input string, for the {@link #processTextNode} method.
328
296
*
@@ -359,6 +327,45 @@ Autolinker.prototype = {
359
327
} ,
360
328
361
329
330
+ /**
331
+ * Lazily instantiates and returns the {@link #htmlParser} instance for this Autolinker instance.
332
+ *
333
+ * @protected
334
+ * @return {Autolinker.htmlParser.HtmlParser }
335
+ */
336
+ getHtmlParser : function ( ) {
337
+ var htmlParser = this . htmlParser ;
338
+
339
+ if ( ! htmlParser ) {
340
+ htmlParser = this . htmlParser = new Autolinker . htmlParser . HtmlParser ( ) ;
341
+ }
342
+
343
+ return htmlParser ;
344
+ } ,
345
+
346
+
347
+ /**
348
+ * Lazily instantiates and returns the {@link #matchParser} instance for this Autolinker instance.
349
+ *
350
+ * @protected
351
+ * @return {Autolinker.matchParser.MatchParser }
352
+ */
353
+ getMatchParser : function ( ) {
354
+ var matchParser = this . matchParser ;
355
+
356
+ if ( ! matchParser ) {
357
+ matchParser = this . matchParser = new Autolinker . matchParser . MatchParser ( {
358
+ urls : this . urls ,
359
+ email : this . email ,
360
+ twitter : this . twitter ,
361
+ stripPrefix : this . stripPrefix
362
+ } ) ;
363
+ }
364
+
365
+ return matchParser ;
366
+ } ,
367
+
368
+
362
369
/**
363
370
* Returns the {@link #tagBuilder} instance for this Autolinker instance, lazily instantiating it
364
371
* if it does not yet exist.
0 commit comments