Bug 1411190 - Collect and parse only eligible fields in form autofill heuristics. draft
authorRay Lin <ralin@mozilla.com>
Wed, 25 Oct 2017 09:47:58 +0800
changeset 685692 ccabdf96620a7764f7da007370fc2cd73e41bacd
parent 685447 967c95cee709756596860ed2a3e6ac06ea3a053f
child 737227 823733f17535c0af53fe8a89ee7acc2144fed734
push id86015
push userbmo:ralin@mozilla.com
push dateWed, 25 Oct 2017 01:50:42 +0000
bugs1411190
milestone58.0a1
Bug 1411190 - Collect and parse only eligible fields in form autofill heuristics. MozReview-Commit-ID: GWp0cU5jt6k
browser/extensions/formautofill/FormAutofillHeuristics.jsm
browser/extensions/formautofill/test/unit/test_getInfo.js
--- a/browser/extensions/formautofill/FormAutofillHeuristics.jsm
+++ b/browser/extensions/formautofill/FormAutofillHeuristics.jsm
@@ -534,21 +534,24 @@ this.FormAutofillHeuristics = {
    *        the elements in this form to be predicted the field info.
    * @param {boolean} allowDuplicates
    *        true to remain any duplicated field details otherwise to remove the
    *        duplicated ones.
    * @returns {Array<Object>}
    *        all field details in the form.
    */
   getFormInfo(form, allowDuplicates = false) {
-    if (form.elements.length <= 0) {
+    const eligibleFields = Array.from(form.elements)
+      .filter(elem => FormAutofillUtils.isFieldEligibleForAutofill(elem));
+
+    if (eligibleFields.length <= 0) {
       return [];
     }
 
-    let fieldScanner = new FieldScanner(form.elements);
+    let fieldScanner = new FieldScanner(eligibleFields);
     while (!fieldScanner.parsingFinished) {
       let parsedPhoneFields = this._parsePhoneFields(fieldScanner);
       let parsedAddressFields = this._parseAddressFields(fieldScanner);
       let parsedExpirationDateFields = this._parseCreditCardExpirationDateFields(fieldScanner);
 
       // If there is no any field parsed, the parsing cursor can be moved
       // forward to the next one.
       if (!parsedPhoneFields && !parsedAddressFields && !parsedExpirationDateFields) {
@@ -561,20 +564,16 @@ this.FormAutofillHeuristics = {
     if (allowDuplicates) {
       return fieldScanner.fieldDetails;
     }
 
     return fieldScanner.trimmedFieldDetail;
   },
 
   getInfo(element) {
-    if (!FormAutofillUtils.isFieldEligibleForAutofill(element)) {
-      return null;
-    }
-
     let info = element.getAutocompleteInfo();
     // An input[autocomplete="on"] will not be early return here since it stll
     // needs to find the field name.
     if (info && info.fieldName && info.fieldName != "on" && info.fieldName != "off") {
       info._reason = "autocomplete";
       return info;
     }
 
--- a/browser/extensions/formautofill/test/unit/test_getInfo.js
+++ b/browser/extensions/formautofill/test/unit/test_getInfo.js
@@ -153,22 +153,16 @@ const TESTCASES = [
     expectedReturnValue: {
       fieldName: "name",
       section: "",
       addressType: "",
       contactType: "",
     },
   },
   {
-    description: "non-input element",
-    document: `<label id="targetElement">street</label>`,
-    elementId: "targetElement",
-    expectedReturnValue: null,
-  },
-  {
     description: "input element with \"submit\" type",
     document: `<input id="targetElement" type="submit" />`,
     elementId: "targetElement",
     expectedReturnValue: null,
   },
   {
     description: "The signature in 'name' attr of an email input",
     document: `<input id="targetElement" name="email" type="number">`,