Bug 1365068 - Cleanup getListSuggestions. r=mattn draft
authorJonathan Guillotte-Blouin <jguillotteblouin@mozilla.com>
Mon, 15 May 2017 14:15:36 -0700
changeset 580782 a56f0525bd44
parent 577862 0f4df67c5f16
child 580783 ce52f933e49b
child 580789 b85b385b99f0
push id59661
push userbmo:jguillotteblouin@mozilla.com
push dateThu, 18 May 2017 23:19:46 +0000
reviewersmattn
bugs1365068
milestone55.0a1
Bug 1365068 - Cleanup getListSuggestions. r=mattn MozReview-Commit-ID: 2FVelDjB4pV
toolkit/components/satchel/nsInputListAutoComplete.js
--- a/toolkit/components/satchel/nsInputListAutoComplete.js
+++ b/toolkit/components/satchel/nsInputListAutoComplete.js
@@ -14,51 +14,50 @@ InputListAutoComplete.prototype = {
   classID: Components.ID("{bf1e01d0-953e-11df-981c-0800200c9a66}"),
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIInputListAutoComplete]),
 
   autoCompleteSearch(aUntrimmedSearchString, aField) {
     let [values, labels] = this.getListSuggestions(aField);
     let searchResult = values.length > 0 ? Ci.nsIAutoCompleteResult.RESULT_SUCCESS
                                          : Ci.nsIAutoCompleteResult.RESULT_NOMATCH;
     let defaultIndex = values.length > 0 ? 0 : -1;
+
     return new FormAutoCompleteResult(aUntrimmedSearchString,
                                       searchResult, defaultIndex, "",
                                       values, labels, [], null);
   },
 
   getListSuggestions(aField) {
     let values = [];
     let labels = [];
+    if (!aField || !aField.list) {
+      return [values, labels];
+    }
 
-    if (aField) {
-      let filter = !aField.hasAttribute("mozNoFilter");
-      let lowerFieldValue = aField.value.toLowerCase();
+    let filter = !aField.hasAttribute("mozNoFilter");
+    let lowerFieldValue = aField.value.toLowerCase();
+    let options = aField.list.options;
 
-      if (aField.list) {
-        let options = aField.list.options;
-        let length = options.length;
-        for (let i = 0; i < length; i++) {
-          let item = options.item(i);
-          let label = "";
-          if (item.label) {
-            label = item.label;
-          } else if (item.text) {
-            label = item.text;
-          } else {
-            label = item.value;
-          }
+    for (let item of options) {
+      let label = "";
+      if (item.label) {
+        label = item.label;
+      } else if (item.text) {
+        label = item.text;
+      } else {
+        label = item.value;
+      }
 
-          if (filter && label.toLowerCase().indexOf(lowerFieldValue) == -1) {
-            continue;
-          }
+      if (filter && !label.toLowerCase().includes(lowerFieldValue)) {
+        continue;
+      }
 
-          labels.push(label);
-          values.push(item.value);
-        }
-      }
+      labels.push(label);
+      values.push(item.value);
     }
 
     return [values, labels];
+
   }
 };
 
 var component = [InputListAutoComplete];
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);