From 4cfe44bb597d49bb853a207072d064283f118ec4 Mon Sep 17 00:00:00 2001 From: Niklas Baumstark Date: Wed, 25 Jan 2012 02:29:38 +0100 Subject: fix HTML tag stripping --- lib/completion.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'lib/completion.js') 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. */ -- cgit v1.2.3