Bug 1228258 - search aliases should be trimmed; r?mak draft
authorgasolin <gasolin@gmail.com>
Mon, 25 Apr 2016 15:19:36 +0800
changeset 357706 9f0b8f2c60dc38448a5776d6058f10db331d92ab
parent 357705 3f3dc83fdd267932447679728f30ed1ddf0d0746
child 519678 ad054801fc2b21eb1d8ed3534ecc2df5dfbc4c06
push id16816
push userbmo:gasolin@mozilla.com
push dateFri, 29 Apr 2016 03:33:20 +0000
reviewersmak
bugs1228258
milestone49.0a1
Bug 1228258 - search aliases should be trimmed; r?mak MozReview-Commit-ID: 4pFEBpDHs5n
browser/components/preferences/in-content/search.js
toolkit/components/search/nsSearchService.js
toolkit/components/search/tests/xpcshell/test_engine_set_alias.js
toolkit/components/search/tests/xpcshell/xpcshell.ini
--- a/browser/components/preferences/in-content/search.js
+++ b/browser/components/preferences/in-content/search.js
@@ -237,27 +237,28 @@ var gSearchPane = {
     gEngineView.rowCountChanged(index, -1);
     gEngineView.invalidate();
     gEngineView.selection.select(Math.min(index, gEngineView.lastIndex));
     gEngineView.ensureRowIsVisible(gEngineView.currentIndex);
     document.getElementById("engineList").focus();
   },
 
   editKeyword: Task.async(function* (aEngine, aNewKeyword) {
-    if (aNewKeyword) {
+    let keyword = aNewKeyword.trim();
+    if (keyword) {
       let eduplicate = false;
       let dupName = "";
 
       // Check for duplicates in Places keywords.
-      let bduplicate = !!(yield PlacesUtils.keywords.fetch(aNewKeyword));
+      let bduplicate = !!(yield PlacesUtils.keywords.fetch(keyword));
 
       // Check for duplicates in changes we haven't committed yet
       let engines = gEngineView._engineStore.engines;
       for (let engine of engines) {
-        if (engine.alias == aNewKeyword &&
+        if (engine.alias == keyword &&
             engine.name != aEngine.name) {
           eduplicate = true;
           dupName = engine.name;
           break;
         }
       }
 
       // Notify the user if they have chosen an existing engine/bookmark keyword
@@ -267,17 +268,17 @@ var gSearchPane = {
         let bmsg = strings.getString("duplicateBookmarkMsg");
         let emsg = strings.getFormattedString("duplicateEngineMsg", [dupName]);
 
         Services.prompt.alert(window, dtitle, eduplicate ? emsg : bmsg);
         return false;
       }
     }
 
-    gEngineView._engineStore.changeEngine(aEngine, "alias", aNewKeyword);
+    gEngineView._engineStore.changeEngine(aEngine, "alias", keyword);
     gEngineView.invalidate();
     return true;
   }),
 
   saveOneClickEnginesList: function () {
     let hiddenList = [];
     for (let engine of gEngineView._engineStore.engines) {
       if (!engine.shown)
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -979,17 +979,17 @@ function getMozParamPref(prefName) {
 var gInitialized = false;
 function notifyAction(aEngine, aVerb) {
   if (gInitialized) {
     LOG("NOTIFY: Engine: \"" + aEngine.name + "\"; Verb: \"" + aVerb + "\"");
     Services.obs.notifyObservers(aEngine, SEARCH_ENGINE_TOPIC, aVerb);
   }
 }
 
-function  parseJsonFromStream(aInputStream) {
+function parseJsonFromStream(aInputStream) {
   const json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
   const data = json.decodeFromStream(aInputStream, aInputStream.available());
   return data;
 }
 
 /**
  * Simple object representing a name/value pair.
  */
@@ -2131,17 +2131,17 @@ Engine.prototype = {
     return this._metaData[name] || undefined;
   },
 
   // nsISearchEngine
   get alias() {
     return this.getAttr("alias");
   },
   set alias(val) {
-    this.setAttr("alias", val);
+    this.setAttr("alias", val.trim());
     notifyAction(this, SEARCH_ENGINE_CHANGED);
   },
 
   /**
    * Return the built-in identifier of app-provided engines.
    *
    * Note that this identifier is substantially similar to _id, with the
    * following exceptions:
new file mode 100644
--- /dev/null
+++ b/toolkit/components/search/tests/xpcshell/test_engine_set_alias.js
@@ -0,0 +1,42 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+add_task(function* test_engine_set_alias() {
+  asyncInit().then(function() {
+    do_print("Set engine alias");
+    let engine = new Services.search.Engine("test", false);
+    engine.alias = "t";
+    Assert.equal(engine.alias, "t");
+    removeEngine(engine);
+  });
+});
+
+add_task(function* test_engine_set_alias_with_left_space() {
+  asyncInit().then(function() {
+    do_print("Set engine alias with left space");
+    let engine = new Services.search.Engine("test2", false);
+    engine.alias = "   e";
+    Assert.equal(engine.alias, "e");
+    removeEngine(engine);
+  });
+});
+
+add_task(function* test_engine_set_alias_with_right_space() {
+  asyncInit().then(function() {
+    do_print("Set engine alias with right space");
+    let engine = new Services.search.Engine("test3", false);
+    engine.alias = "s   ";
+    Assert.equal(engine.alias, "s");
+    removeEngine(engine);
+  });
+});
+
+add_task(function* test_engine_set_alias_with_right_left_space() {
+  asyncInit().then(function() {
+    do_print("Set engine alias with left and right space");
+    let engine = new Services.search.Engine("test4", false);
+    engine.alias = "   g  ";
+    Assert.equal(engine.alias, "g");
+    removeEngine(engine);
+  });
+});
--- a/toolkit/components/search/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/search/tests/xpcshell/xpcshell.ini
@@ -30,16 +30,17 @@ support-files =
   data/search.sqlite
   data/searchSuggestions.sjs
   data/searchTest.jar
 
 [test_nocache.js]
 [test_645970.js]
 [test_bug930456.js]
 [test_bug930456_child.js]
+[test_engine_set_alias.js]
 [test_identifiers.js]
 [test_invalid_engine_from_dir.js]
 [test_init_async_multiple.js]
 [test_init_async_multiple_then_sync.js]
 [test_json_cache.js]
 [test_location.js]
 [test_location_error.js]
 [test_location_malformed_json.js]