aboutsummaryrefslogtreecommitdiffstats
path: root/lib/completion.js
diff options
context:
space:
mode:
authorNiklas Baumstark2012-01-25 02:29:38 +0100
committerNiklas Baumstark2012-04-10 23:58:07 +0200
commit4cfe44bb597d49bb853a207072d064283f118ec4 (patch)
tree9d4323d7ca70b450e670c961236cb06d8e35d146 /lib/completion.js
parentccd7998112740e8f19efd2bdcab0be1c2b04bbc7 (diff)
downloadvimium-4cfe44bb597d49bb853a207072d064283f118ec4.tar.bz2
fix HTML tag stripping
Diffstat (limited to 'lib/completion.js')
-rw-r--r--lib/completion.js27
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. */