|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import * as linkify from './linkify'; |
6 | | -var tokenize = linkify.tokenize; |
7 | | -var options = linkify.options; |
| 6 | +let tokenize = linkify.tokenize; |
| 7 | +let options = linkify.options; |
| 8 | +let TEXT_TOKEN = linkify.parser.TOKENS.TEXT; |
8 | 9 |
|
9 | 10 | const HTML_NODE = 1, TXT_NODE = 3; |
10 | 11 |
|
@@ -34,53 +35,54 @@ function tokensToNodes(tokens, opts, doc) { |
34 | 35 |
|
35 | 36 | for (let i = 0; i < tokens.length; i++) { |
36 | 37 | let token = tokens[i]; |
37 | | - let validated = token.isLink && options.resolve(opts.validate, token.toString(), token.type); |
38 | | - |
39 | | - if (token.isLink && validated) { |
40 | | - |
41 | | - let |
42 | | - href = token.toHref(opts.defaultProtocol), |
43 | | - formatted = options.resolve(opts.format, token.toString(), token.type), |
44 | | - formattedHref = options.resolve(opts.formatHref, href, token.type), |
45 | | - attributesHash = options.resolve(opts.attributes, href, token.type), |
46 | | - tagName = options.resolve(opts.tagName, href, token.type), |
47 | | - linkClass = options.resolve(opts.linkClass, href, token.type), |
48 | | - target = options.resolve(opts.target, href, token.type), |
49 | | - events = options.resolve(opts.events, href, token.type); |
50 | | - |
51 | | - // Build the link |
52 | | - let link = doc.createElement(tagName); |
53 | | - link.setAttribute('href', formattedHref); |
54 | | - link.setAttribute('class', linkClass); |
55 | | - if (target) { |
56 | | - link.setAttribute('target', target); |
57 | | - } |
58 | 38 |
|
59 | | - // Build up additional attributes |
60 | | - if (attributesHash) { |
61 | | - for (var attr in attributesHash) { |
62 | | - link.setAttribute(attr, attributesHash[attr]); |
63 | | - } |
| 39 | + if (token.type === 'nl' && opts.nl2br) { |
| 40 | + result.push(doc.createElement('br')); |
| 41 | + continue; |
| 42 | + } else if ( |
| 43 | + !token.isLink || |
| 44 | + !options.resolve(opts.validate, token.toString(), token.type) |
| 45 | + ) { |
| 46 | + result.push(doc.createTextNode(token.toString())); |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + let href = token.toHref(opts.defaultProtocol); |
| 51 | + let formatted = options.resolve(opts.format, token.toString(), token.type); |
| 52 | + let formattedHref = options.resolve(opts.formatHref, href, token.type); |
| 53 | + let attributesHash = options.resolve(opts.attributes, href, token.type); |
| 54 | + let tagName = options.resolve(opts.tagName, href, token.type); |
| 55 | + let linkClass = options.resolve(opts.linkClass, href, token.type); |
| 56 | + let target = options.resolve(opts.target, href, token.type); |
| 57 | + let events = options.resolve(opts.events, href, token.type); |
| 58 | + |
| 59 | + // Build the link |
| 60 | + let link = doc.createElement(tagName); |
| 61 | + link.setAttribute('href', formattedHref); |
| 62 | + link.setAttribute('class', linkClass); |
| 63 | + if (target) { |
| 64 | + link.setAttribute('target', target); |
| 65 | + } |
| 66 | + |
| 67 | + // Build up additional attributes |
| 68 | + if (attributesHash) { |
| 69 | + for (var attr in attributesHash) { |
| 70 | + link.setAttribute(attr, attributesHash[attr]); |
64 | 71 | } |
| 72 | + } |
65 | 73 |
|
66 | | - if (events) { |
67 | | - for (var event in events) { |
68 | | - if (link.addEventListener) { |
69 | | - link.addEventListener(event, events[event]); |
70 | | - } else if (link.attachEvent) { |
71 | | - link.attachEvent('on' + event, events[event]); |
72 | | - } |
| 74 | + if (events) { |
| 75 | + for (var event in events) { |
| 76 | + if (link.addEventListener) { |
| 77 | + link.addEventListener(event, events[event]); |
| 78 | + } else if (link.attachEvent) { |
| 79 | + link.attachEvent('on' + event, events[event]); |
73 | 80 | } |
74 | 81 | } |
75 | | - |
76 | | - link.appendChild(doc.createTextNode(formatted)); |
77 | | - result.push(link); |
78 | | - |
79 | | - } else if (token.type === 'nl' && opts.nl2br) { |
80 | | - result.push(doc.createElement('br')); |
81 | | - } else { |
82 | | - result.push(doc.createTextNode(token.toString())); |
83 | 82 | } |
| 83 | + |
| 84 | + link.appendChild(doc.createTextNode(formatted)); |
| 85 | + result.push(link); |
84 | 86 | } |
85 | 87 |
|
86 | 88 | return result; |
@@ -112,15 +114,20 @@ function linkifyElementHelper(element, opts, doc) { |
112 | 114 | break; |
113 | 115 | case TXT_NODE: |
114 | 116 |
|
115 | | - let |
116 | | - str = childElement.nodeValue, |
117 | | - tokens = tokenize(str), |
118 | | - nodes = tokensToNodes(tokens, opts, doc); |
| 117 | + let str = childElement.nodeValue; |
| 118 | + let tokens = tokenize(str); |
| 119 | + |
| 120 | + if (tokens.length === 0 || tokens.length === 1 && tokens[0] instanceof TEXT_TOKEN) { |
| 121 | + // No node replacement required |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + let nodes = tokensToNodes(tokens, opts, doc); |
119 | 126 |
|
120 | 127 | // Swap out the current child for the set of nodes |
121 | 128 | replaceChildWithChildren(element, childElement, nodes); |
122 | 129 |
|
123 | | - // so that the correct sibling is selected |
| 130 | + // so that the correct sibling is selected next |
124 | 131 | childElement = nodes[nodes.length - 1]; |
125 | 132 |
|
126 | 133 | break; |
|
0 commit comments