Bug 1411190 - Collect and parse only eligible fields in form autofill heuristics. r=seanlee, lchang draft
authorRay Lin <ralin@mozilla.com>
Wed, 25 Oct 2017 09:47:58 +0800
changeset 686790 e654975056f70cd477658bee80e434db97c2452f
parent 686766 d734e6acf7778df7c933d33540203f08b44ff977
child 737463 01b566090aade2bc00e778ae0394e75fc2c1f7a6
push id86283
push userbmo:ralin@mozilla.com
push dateThu, 26 Oct 2017 10:35:16 +0000
reviewersseanlee, lchang
bugs1411190
milestone58.0a1
Bug 1411190 - Collect and parse only eligible fields in form autofill heuristics. r=seanlee, lchang 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) {
@@ -624,20 +627,16 @@ this.FormAutofillHeuristics = {
       FormAutofillUtils.isAutofillCreditCardsAvailable,
       isSelectElem
     );
 
     return regexps;
   },
 
   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">`,