Bug 1360370 - Part 1: Implement FormAutofillUtils.isFieldEligibleForAutofill.; r?MattN draft
authorSean Lee <selee@mozilla.com>
Mon, 08 May 2017 17:36:10 +0800
changeset 585185 7ec96df241aeebe76538a18cc12d512075fd78df
parent 585119 24b849cb1a49c102ab13de2557b1e05d1cb93cd5
child 585186 4a00074596c0641c80f079d5db59a77b14bca954
push id61042
push userbmo:selee@mozilla.com
push dateFri, 26 May 2017 16:52:28 +0000
reviewersMattN
bugs1360370
milestone55.0a1
Bug 1360370 - Part 1: Implement FormAutofillUtils.isFieldEligibleForAutofill.; r?MattN MozReview-Commit-ID: A0JfQISLVHO
browser/extensions/formautofill/FormAutofillUtils.jsm
browser/extensions/formautofill/test/unit/test_isFieldEligibleForAutofill.js
browser/extensions/formautofill/test/unit/xpcshell.ini
--- a/browser/extensions/formautofill/FormAutofillUtils.jsm
+++ b/browser/extensions/formautofill/FormAutofillUtils.jsm
@@ -16,16 +16,33 @@ this.FormAutofillUtils = {
       let ConsoleAPI = Cu.import("resource://gre/modules/Console.jsm", {}).ConsoleAPI;
       return new ConsoleAPI({
         maxLogLevelPref: "extensions.formautofill.loglevel",
         prefix: logPrefix,
       });
     });
   },
 
+  ALLOW_TYPES: ["text", "email", "tel", "number"],
+  isFieldEligibleForAutofill(element) {
+    if (element.autocomplete == "off") {
+      return false;
+    }
+
+    if (element instanceof Ci.nsIDOMHTMLInputElement) {
+      if (!this.ALLOW_TYPES.includes(element.type)) {
+        return false;
+      }
+    } else if (!(element instanceof Ci.nsIDOMHTMLSelectElement)) {
+      return false;
+    }
+
+    return true;
+  },
+
   // The tag name list is from Chromium except for "STYLE":
   // eslint-disable-next-line max-len
   // https://cs.chromium.org/chromium/src/components/autofill/content/renderer/form_autofill_util.cc?l=216&rcl=d33a171b7c308a64dc3372fac3da2179c63b419e
   EXCLUDED_TAGS: ["SCRIPT", "NOSCRIPT", "OPTION", "STYLE"],
   /**
    * Extract all strings of an element's children to an array.
    * "element.textContent" is a string which is merged of all children nodes,
    * and this function provides an array of the strings contains in an element.
new file mode 100644
--- /dev/null
+++ b/browser/extensions/formautofill/test/unit/test_isFieldEligibleForAutofill.js
@@ -0,0 +1,59 @@
+"use strict";
+
+Cu.import("resource://formautofill/FormAutofillUtils.jsm");
+
+const TESTCASES = [
+  {
+    document: `<input id="targetElement" type="text">`,
+    fieldId: "targetElement",
+    expectedResult: true,
+  },
+  {
+    document: `<input id="targetElement" type="email">`,
+    fieldId: "targetElement",
+    expectedResult: true,
+  },
+  {
+    document: `<input id="targetElement" type="number">`,
+    fieldId: "targetElement",
+    expectedResult: true,
+  },
+  {
+    document: `<input id="targetElement" type="tel">`,
+    fieldId: "targetElement",
+    expectedResult: true,
+  },
+  {
+    document: `<input id="targetElement" type="radio">`,
+    fieldId: "targetElement",
+    expectedResult: false,
+  },
+  {
+    document: `<input id="targetElement" type="text" autocomplete="off">`,
+    fieldId: "targetElement",
+    expectedResult: false,
+  },
+  {
+    document: `<select id="targetElement"></select>`,
+    fieldId: "targetElement",
+    expectedResult: true,
+  },
+  {
+    document: `<div id="targetElement"></div>`,
+    fieldId: "targetElement",
+    expectedResult: false,
+  },
+];
+
+TESTCASES.forEach(testcase => {
+  add_task(function* () {
+    do_print("Starting testcase: " + testcase.document);
+
+    let doc = MockDocument.createTestDocument(
+      "http://localhost:8080/test/", testcase.document);
+
+    let field = doc.getElementById(testcase.fieldId);
+    Assert.equal(FormAutofillUtils.isFieldEligibleForAutofill(field),
+                     testcase.expectedResult);
+  });
+});
--- a/browser/extensions/formautofill/test/unit/xpcshell.ini
+++ b/browser/extensions/formautofill/test/unit/xpcshell.ini
@@ -21,14 +21,15 @@ support-files =
 [test_autofillFormFields.js]
 [test_collectFormFields.js]
 [test_creditCardRecords.js]
 [test_extractLabelStrings.js]
 [test_findLabelElements.js]
 [test_getFormInputDetails.js]
 [test_getInfo.js]
 [test_isCJKName.js]
+[test_isFieldEligibleForAutofill.js]
 [test_markAsAutofillField.js]
 [test_nameUtils.js]
 [test_onFormSubmitted.js]
 [test_profileAutocompleteResult.js]
 [test_savedFieldNames.js]
 [test_transformFields.js]