|
| 1 | +import HTML5Tokenizer from './simple-html-tokenizer'; |
| 2 | +import * as linkify from './linkify'; |
| 3 | + |
| 4 | +const StartTag = 'StartTag'; |
| 5 | +const EndTag = 'EndTag'; |
| 6 | +const Chars = 'Chars'; |
| 7 | +const Comment = 'Comment'; |
| 8 | + |
| 9 | +/** |
| 10 | + `tokens` and `token` in this section refer to tokens generated by the HTML |
| 11 | + parser. |
| 12 | +*/ |
| 13 | +export default function linkifyHtml(str, opts={}) { |
| 14 | + let tokens = HTML5Tokenizer.tokenize(str); |
| 15 | + let linkifiedTokens = []; |
| 16 | + let linkified = []; |
| 17 | + var i; |
| 18 | + |
| 19 | + opts = linkify.options.normalize(opts); |
| 20 | + |
| 21 | + // Linkify the tokens given by the parser |
| 22 | + for (i = 0; i < tokens.length; i++) { |
| 23 | + let token = tokens[i]; |
| 24 | + |
| 25 | + if (token.type === StartTag && token.tagName.toUpperCase() === 'A') { |
| 26 | + // Ignore all the contents of an anchor tag |
| 27 | + let preskipLen = linkifiedTokens.length; |
| 28 | + skipTokens('A', tokens, ++i, linkifiedTokens); |
| 29 | + |
| 30 | + i += linkifiedTokens.length - preskipLen; |
| 31 | + continue; |
| 32 | + |
| 33 | + } else if (token.type !== Chars) { |
| 34 | + // Skip this token, it's not important |
| 35 | + linkifiedTokens.push(token); |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + // Valid text token, linkify it! |
| 40 | + let linkifedChars = linkifyChars(token.chars, opts); |
| 41 | + linkifiedTokens.push.apply(linkifiedTokens, linkifedChars); |
| 42 | + } |
| 43 | + |
| 44 | + // Convert the tokens back into a string |
| 45 | + for (i = 0; i < linkifiedTokens.length; i++) { |
| 46 | + let token = linkifiedTokens[i]; |
| 47 | + switch (token.type) { |
| 48 | + case StartTag: |
| 49 | + let attrs = attrsToStrings(token.attributes); |
| 50 | + let link = '<' + token.tagName; |
| 51 | + if (attrs.length > 0) { link += ' ' + attrs.join(' '); } |
| 52 | + link += '>'; |
| 53 | + linkified.push(link); |
| 54 | + break; |
| 55 | + case EndTag: |
| 56 | + linkified.push(`</${token.tagName}>`); |
| 57 | + break; |
| 58 | + case Chars: |
| 59 | + linkified.push(escapeText(token.chars)); |
| 60 | + break; |
| 61 | + case Comment: |
| 62 | + linkified.push(`<!--${escapeText(token.chars)}-->`); |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return linkified.join(''); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + `tokens` and `token` in this section referes to tokens returned by |
| 72 | + `linkify.tokenize`. `linkified` will contain HTML Parser-style tokens |
| 73 | +*/ |
| 74 | +function linkifyChars(str, opts) { |
| 75 | + let tokens = linkify.tokenize(str); |
| 76 | + let result = []; |
| 77 | + |
| 78 | + for (var i = 0; i < tokens.length; i++) { |
| 79 | + let token = tokens[i]; |
| 80 | + if (token.type === 'nl' && opts.nl2br) { |
| 81 | + result.push({ |
| 82 | + type: StartTag, |
| 83 | + tagName: 'br', |
| 84 | + attributes: [], |
| 85 | + selfClosing: true |
| 86 | + }); |
| 87 | + continue; |
| 88 | + } else if (!token.isLink) { |
| 89 | + result.push({type: Chars, chars: token.toString()}); |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + let href = token.toHref(opts.defaultProtocol); |
| 94 | + let formatted = linkify.options.resolve(opts.format, token.toString(), token.type); |
| 95 | + let formattedHref = linkify.options.resolve(opts.formatHref, href, token.type); |
| 96 | + let attributesHash = linkify.options.resolve(opts.attributes, href, token.type); |
| 97 | + let tagName = linkify.options.resolve(opts.tagName, href, token.type); |
| 98 | + let linkClass = linkify.options.resolve(opts.linkClass, href, token.type); |
| 99 | + let target = linkify.options.resolve(opts.target, href, token.type); |
| 100 | + |
| 101 | + // Build up attributes |
| 102 | + let attributes = [ |
| 103 | + ['href', formattedHref], |
| 104 | + ['class', linkClass] |
| 105 | + ]; |
| 106 | + |
| 107 | + if (target) { |
| 108 | + attributes.push(['target', target]); |
| 109 | + } |
| 110 | + |
| 111 | + for (var attr in attributesHash) { |
| 112 | + attributes.push([attr, attributesHash[attr]]); |
| 113 | + } |
| 114 | + |
| 115 | + // Add the required tokens |
| 116 | + result.push({ |
| 117 | + type: StartTag, |
| 118 | + tagName: tagName, |
| 119 | + attributes: attributes, |
| 120 | + selfClosing: false |
| 121 | + }); |
| 122 | + result.push({type: Chars, chars: formatted}); |
| 123 | + result.push({type: EndTag, tagName: tagName}); |
| 124 | + } |
| 125 | + |
| 126 | + return result; |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + Returns a list of tokens skipped until the closing tag of tagName. |
| 131 | +
|
| 132 | + * `tagName` is the closing tag which will prompt us to stop skipping |
| 133 | + * `tokens` is the array of tokens generated by HTML5Tokenizer which |
| 134 | + * `i` is the index immediately after the opening tag to skip |
| 135 | + * `skippedTokens` is an array which skipped tokens are being pushed into |
| 136 | +
|
| 137 | + Caveats |
| 138 | +
|
| 139 | + * Assumes that i is the first token after the given opening tagName |
| 140 | + * The closing tag will be skipped, but nothing after it |
| 141 | + * Will track whether there is a nested tag of the same type |
| 142 | +*/ |
| 143 | +function skipTagTokens(tagName, tokens, i, skippedTokens) { |
| 144 | + |
| 145 | + // number of tokens of this type on the [fictional] stack |
| 146 | + var stackCount = 1; |
| 147 | + |
| 148 | + while (i < tokens.length && stackCount > 0) { |
| 149 | + let token = tokens[i]; |
| 150 | + if (token.type === StartTag && token.tagName.toUpperCase() === tagName) { |
| 151 | + // Nested tag of the same type, "add to stack" |
| 152 | + stackCount++; |
| 153 | + } else if (token.type === EndTag && token.tagName.toUpperCase() === tagName) { |
| 154 | + // Closing tag |
| 155 | + stackCount--; |
| 156 | + } |
| 157 | + skippedTokens.push(token); |
| 158 | + i++; |
| 159 | + } |
| 160 | + |
| 161 | + // Note that if stackCount > 0 here, the HTML is probably invalid |
| 162 | + return skippedTokens; |
| 163 | +} |
| 164 | + |
| 165 | +function escapeText(text) { |
| 166 | + // Not required, HTML tokenizer ensures this occurs properly |
| 167 | + return text; |
| 168 | +} |
| 169 | + |
| 170 | +function escapeAttr(attr) { |
| 171 | + return attr.replace(/"/g, '"'); |
| 172 | +} |
| 173 | + |
| 174 | +function attrsToStrings(attrs) { |
| 175 | + let attrStrs = []; |
| 176 | + for (let i = 0; i < attrs.length; i++) { |
| 177 | + let [name, value] = attrs[i]; |
| 178 | + attrStrs.push(`${name}="${escapeAttr(value)}"`); |
| 179 | + } |
| 180 | + return attrStrs; |
| 181 | +} |
0 commit comments