diff options
| author | Stephen Blott | 2014-12-29 16:33:23 +0000 | 
|---|---|---|
| committer | Stephen Blott | 2014-12-29 16:45:34 +0000 | 
| commit | c52c0ea57f86c1c5a132819fe85e763db84ce712 (patch) | |
| tree | 4e46ddbc9b5e338843ee6a465ad68d3e15920efb | |
| parent | ba743054dcd26b225db407db261e480ab485e2d0 (diff) | |
| download | vimium-c52c0ea57f86c1c5a132819fe85e763db84ce712.tar.bz2 | |
Descriptions for custom search engines.
| -rw-r--r-- | background_scripts/completion.coffee | 16 | ||||
| -rw-r--r-- | background_scripts/settings.coffee | 16 | ||||
| -rw-r--r-- | pages/options.html | 12 | ||||
| -rw-r--r-- | tests/unit_tests/completion_test.coffee | 11 | ||||
| -rw-r--r-- | tests/unit_tests/settings_test.coffee | 13 | 
5 files changed, 45 insertions, 23 deletions
| diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index d62f82fe..4e570313 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -324,11 +324,17 @@ class SearchEngineCompleter    searchEngines: {}    filter: (queryTerms, onComplete) -> -    searchEngineMatch = this.getSearchEngineMatches(queryTerms[0]) +    {url: url, description: description} = this.getSearchEngineMatches(queryTerms[0])      suggestions = [] -    if searchEngineMatch -      searchEngineMatch = searchEngineMatch.replace(/%s/g, Utils.createSearchQuery queryTerms[1..]) -      suggestion = new Suggestion(queryTerms, "search", searchEngineMatch, queryTerms[0] + ": " + queryTerms[1..].join(" "), @computeRelevancy) +    if url +      url = url.replace(/%s/g, Utils.createSearchQuery queryTerms[1..]) +      if description +        type = description +        query = queryTerms[1..].join " " +      else +        type = "search" +        query = queryTerms[0] + ": " + queryTerms[1..].join(" ") +      suggestion = new Suggestion(queryTerms, type, url, query, @computeRelevancy)        suggestions.push(suggestion)      onComplete(suggestions) @@ -338,7 +344,7 @@ class SearchEngineCompleter      this.searchEngines = root.Settings.getSearchEngines()    getSearchEngineMatches: (queryTerm) -> -    this.searchEngines[queryTerm] +    this.searchEngines[queryTerm] || {}  # A completer which calls filter() on many completers, aggregates the results, ranks them, and returns the top  # 10. Queries from the vomnibar frontend script come through a multi completer. diff --git a/background_scripts/settings.coffee b/background_scripts/settings.coffee index e90bc1f8..3ff74749 100644 --- a/background_scripts/settings.coffee +++ b/background_scripts/settings.coffee @@ -46,13 +46,19 @@ root.Settings = Settings =    # this is a map that we use to store our search engines for use.    searchEnginesMap: {} -  # this parses the search engines settings and clears the old searchEngines and sets the new one +  # Parse the custom search engines setting and cache it.    parseSearchEngines: (searchEnginesText) ->      @searchEnginesMap = {} -    # find the split pairs by first splitting by line then splitting on the first `: ` -    split_pairs = ( pair.split( /: (.+)/, 2) for pair in searchEnginesText.split( /\n/ ) when pair[0] != "#" ) -    @searchEnginesMap[a[0]] = a[1] for a in split_pairs -    @searchEnginesMap +    for line in searchEnginesText.split /\n/ +      tokens = line.trim().split /\s+/ +      continue if tokens.length < 2 or tokens[0].startsWith('"') or tokens[0].startsWith("#") +      keywords = tokens[0].split ":" +      continue unless keywords.length == 2 and not keywords[1] # So, like: [ "w", "" ]. +      @searchEnginesMap[keywords[0]] = +        url: tokens[1] +        description: tokens[2..].join(" ") + +  # Fetch the search-engine map, building it if necessary.    getSearchEngines: ->      this.parseSearchEngines(@get("searchEngines") || "") if Object.keys(@searchEnginesMap).length == 0      @searchEnginesMap diff --git a/pages/options.html b/pages/options.html index 8e685304..e765334c 100644 --- a/pages/options.html +++ b/pages/options.html @@ -300,11 +300,13 @@ unmapAll            <td verticalAlign="top">                <div class="help">                  <div class="example"> -                  This adds search-engine shortcuts to the Vomnibar.<br/><br/> -                  The format is:<br/> -                  <pre>your-keyword: http://the-site.com/?q=%s</pre> -                  %s will be replaced with your search terms.<br/> -                  Lines which start with "#" are comments. +                  Add search-engine shortcuts to the Vomnibar. Format:<br/> +                  <pre> +a: http://a.com/?q=%s +b: http://b.com/?q=%s description +" this is a comment +# this is also a comment</pre> +                  %s is replaced with the search terms.                  </div>                </div>                <textarea id="searchEngines"></textarea> diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index e4966016..b7b73cc2 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -233,17 +233,24 @@ context "tab completer",  context "search engines",    setup -> -    searchEngines = "foo: bar?q=%s\n# comment\nbaz: qux?q=%s" +    searchEngines = "foo: bar?q=%s\n# comment\nbaz: qux?q=%s baz description"      Settings.set 'searchEngines', searchEngines      @completer = new SearchEngineCompleter()      # note, I couldn't just call @completer.refresh() here as I couldn't set root.Settings without errors      # workaround is below, would be good for someone that understands the testing system better than me to improve      @completer.searchEngines = Settings.getSearchEngines() -  should "return search engine suggestion", -> +  should "return search engine suggestion without description", ->      results = filterCompleter(@completer, ["foo", "hello"])      assert.arrayEqual ["bar?q=hello"], results.map (result) -> result.url      assert.arrayEqual ["foo: hello"], results.map (result) -> result.title +    assert.arrayEqual ["search"], results.map (result) -> result.type + +  should "return search engine suggestion with description", -> +    results = filterCompleter(@completer, ["baz", "hello"]) +    assert.arrayEqual ["qux?q=hello"], results.map (result) -> result.url +    assert.arrayEqual ["hello"], results.map (result) -> result.title +    assert.arrayEqual ["baz description"], results.map (result) -> result.type  context "suggestions",    should "escape html in page titles", -> diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee index 4625457b..afe862a4 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -70,14 +70,15 @@ context "settings",      chrome.storage.sync.set { scrollStepSize: JSON.stringify(message) }      assert.equal message, Sync.message -  should "set search engines, retrieve them correctly and check that it has been parsed correctly", -> -    searchEngines = "foo: bar?q=%s\n# comment\nbaz: qux?q=%s" -    parsedSearchEngines = {"foo": "bar?q=%s", "baz": "qux?q=%s"} +  should "set search engines, retrieve them correctly and check that they have been parsed correctly", -> +    searchEngines = "foo: bar?q=%s\n# comment\nbaz: qux?q=%s baz description"      Settings.set 'searchEngines', searchEngines -    assert.equal(searchEngines, Settings.get('searchEngines'))      result = Settings.getSearchEngines() -    assert.isTrue(parsedSearchEngines["foo"] == result["foo"] && -      parsedSearchEngines["baz"] == result["baz"] && Object.keys(result).length == 2) +    assert.equal Object.keys(result).length, 2 +    assert.equal "bar?q=%s", result["foo"].url +    assert.isFalse result["foo"].description +    assert.equal "qux?q=%s", result["baz"].url +    assert.equal "baz description", result["baz"].description    should "sync a key which is not a known setting (without crashing)", ->      chrome.storage.sync.set { notASetting: JSON.stringify("notAUsefullValue") } | 
