diff options
Diffstat (limited to 'lib/completion.js')
| -rw-r--r-- | lib/completion.js | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/lib/completion.js b/lib/completion.js index a2f97c2c..2163dbdd 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -153,23 +153,24 @@ var completion = (function() { return self; })(); + var htmlRegex = /<[^>]*>|&[a-z]+;/gi; + /** Strips HTML tags and escape sequences using a naive regex replacement. Optionally, saves the stripped * HTML tags in a dictionary indexed by the position where the tag should be reinserted. */ function stripHtmlTags(str, positions) { - var result = str.replace(/<[^>]*>|&[a-z]+;/gi, ''); if (!positions) - return result; - - // we need to get information about where the tags can be reinserted after some string processing - var start; - var end = -1; - var stripped = 0; - while (0 <= (start = str.indexOf('<', end + 1))) { - end = str.indexOf('>', start); - positions[start - stripped] = str.slice(start, end + 1); - stripped += end - start + 1; - } - return result; + return str.replace(htmlRegex, ''); + + var match = str.match(htmlRegex).reverse(); + var split = str.split(htmlRegex); + var offset = 0; + var i = 0; + split.forEach(function(text) { + if (match.length > 0) + positions[offset += text.length] = match.pop(); + }); + + return split.join(''); } /** Creates an action that opens :url in the current tab by default or in a new tab as an alternative. */ |
