From 79b0340f40149bafd48d3d23200c3e613068a9dc Mon Sep 17 00:00:00 2001 From: Niklas Baumstark Date: Sat, 21 Jan 2012 03:51:17 +0100 Subject: allow fuzzy completion of HTML-decorated content and implement this for history and bookmarks --- lib/completion.js | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) (limited to 'lib/completion.js') diff --git a/lib/completion.js b/lib/completion.js index c7ac0417..2b29258f 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -109,6 +109,25 @@ var completion = (function() { return self; })(); + /** Strips HTML tags using a naive regex replacement. Optinally, 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(/<[^>]*>/g, ''); + 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; + } + /** Creates an action that opens :url in the current tab by default or in a new tab as an alternative. */ function createActionOpenUrl(url) { return [ @@ -121,14 +140,30 @@ var completion = (function() { function createHighlightingCompletion(query, str, action, relevancy) { return { render: function() { + var htmlTags = {}; + str = stripHtmlTags(str, htmlTags); var match = fuzzyMatcher.getMatcher(query).exec(str); + if (!match) + console.log(query, str); var html = ''; - for (var i = 1; i < match.length; ++i) { - if (i % 2 == 1) - html += match[i]; + var i = 0; + + function addToHtml(str) { + if (i in htmlTags) + html += htmlTags[i]; + html += str; + ++i; + } + + for (var m = 1; m < match.length; ++m) { + if (m % 2 == 1) + for (var j = 0; j < match[m].length; ++j) + addToHtml(match[m][j]); else - html += '' + match[i] + ''; + addToHtml('' + match[m] + ''); }; + addToHtml(''); + return html; }, action: action, @@ -164,7 +199,7 @@ var completion = (function() { var handler = function(results) { var filtered = []; fuzzyMatcher.filter(query, - results, function(comp) { return comp.str }, + results, function(comp) { return stripHtmlTags(comp.str) }, self.id, function(match) { filtered.push(createHighlightingCompletion( @@ -269,10 +304,10 @@ var completion = (function() { var title = ''; if (historyItem.title.length > 0) - title = ' (' + historyItem.title + ')'; + title = ' ' + historyItem.title + ''; results.push({ - str: 'history: ' + historyItem.url + title, + str: 'history ' + historyItem.url + title, url: historyItem.url, }); } @@ -300,10 +335,10 @@ var completion = (function() { var title = ''; if (bookmark.title.length > 0) - title = ' (' + bookmark.title + ')'; + title = ' ' + bookmark.title + ''; results.push({ - str: 'bookmark: ' + bookmark.url + title, + str: 'bookmark ' + bookmark.url + title, url: bookmark.url, }); } -- cgit v1.2.3