diff options
| author | Niklas Baumstark | 2012-01-21 04:19:31 +0100 |
|---|---|---|
| committer | Niklas Baumstark | 2012-04-10 23:54:36 +0200 |
| commit | 667f97b8d9efce79cb2f554079a79fadf81542ad (patch) | |
| tree | 8f48be9c5570bf604a0742575a4ce6c0364be9f7 | |
| parent | 69ea703f584f65c962fb04fcae7bb6500a11b76a (diff) | |
| download | vimium-667f97b8d9efce79cb2f554079a79fadf81542ad.tar.bz2 | |
document the HTML matching algorithm better
| -rw-r--r-- | lib/completion.js | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/completion.js b/lib/completion.js index 6e8ea803..0b4c5a0a 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -139,15 +139,20 @@ var completion = (function() { /** Creates a completion that renders by marking fuzzy-matched parts. */ function createHighlightingCompletion(query, str, action, relevancy) { return { + action: action, + relevancy: relevancy, + render: function() { + // we want to match the content in HTML tags, but not the HTML tags themselves, so we remove the + // tags and reinsert them after the matching process var htmlTags = {}; str = stripHtmlTags(str, htmlTags); var match = fuzzyMatcher.getMatcher(query).exec(str); - if (!match) - console.log(query, str); var html = ''; var i = 0; + // this helper function adds the HTML generated _for one single character_ to the HTML output + // and reinserts HTML tags stripped before, if they were at this position function addToHtml(str) { if (i in htmlTags) html += htmlTags[i]; @@ -155,19 +160,24 @@ var completion = (function() { ++i; } + // iterate over the match groups. They are non-matched and matched string parts, in alternating order for (var m = 1; m < match.length; ++m) { if (m % 2 == 1) + // we have a non-matched part, it could have several characters. We need to insert them character + // by character, so that addToHtml can keep track of the position in the original string for (var j = 0; j < match[m].length; ++j) addToHtml(match[m][j]); else + // we have a matched part, which consists of exactly one character. In addition to the character + // itself, we add some decorating HTML. addToHtml('<span class="fuzzyMatch">' + match[m] + '</span>'); }; + + // call it another time so that a tag at the very last position is reinserted addToHtml(''); return html; }, - action: action, - relevancy: relevancy, } } |
