Bug 1394854 - [Form Autofill] Identify country code from "country" attribute as well. r=MattN,steveck
authorLuke Chang <lchang@mozilla.com>
Wed, 06 Sep 2017 17:40:39 +0800
changeset 428920 41f763899ab82c9891237001b4426a44aea44a87
parent 428919 f8aac75bad36c86a1ee2119224dfbe49cb2a1ad1
child 428921 b348fea30d5808ddca8badc43e5c6cb5b58754d7
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN, steveck
bugs1394854
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1394854 - [Form Autofill] Identify country code from "country" attribute as well. r=MattN,steveck MozReview-Commit-ID: FJiqFV3AOs4
browser/extensions/formautofill/FormAutofillHandler.jsm
browser/extensions/formautofill/test/unit/test_createRecords.js
--- a/browser/extensions/formautofill/FormAutofillHandler.jsm
+++ b/browser/extensions/formautofill/FormAutofillHandler.jsm
@@ -554,17 +554,17 @@ FormAutofillHandler.prototype = {
         data[type].record[detail.fieldName] = value;
 
         if (detail.state == "AUTO_FILLED") {
           data[type].untouchedFields.push(detail.fieldName);
         }
       });
     });
 
-    this.normalizeAddress(data.address);
+    this._normalizeAddress(data.address);
 
     if (data.address &&
         Object.values(data.address.record).filter(v => v).length <
         FormAutofillUtils.AUTOFILL_FIELDS_THRESHOLD) {
       log.debug("No address record saving since there are only",
                      Object.keys(data.address.record).length,
                      "usable fields");
       delete data.address;
@@ -574,21 +574,34 @@ FormAutofillHandler.prototype = {
         !FormAutofillUtils.isCCNumber(data.creditCard.record["cc-number"]))) {
       log.debug("No credit card record saving since card number is invalid");
       delete data.creditCard;
     }
 
     return data;
   },
 
-  normalizeAddress(address) {
+  _normalizeAddress(address) {
     if (!address) {
       return;
     }
 
+    // Normalize Country
+    if (address.record.country) {
+      let detail = this.getFieldDetailByName("country");
+      // Try identifying country field aggressively if it doesn't come from
+      // @autocomplete.
+      if (detail._reason != "autocomplete") {
+        let countryCode = FormAutofillUtils.identifyCountryCode(address.record.country);
+        if (countryCode) {
+          address.record.country = countryCode;
+        }
+      }
+    }
+
     // Normalize Tel
     FormAutofillUtils.compressTel(address.record);
     if (address.record.tel) {
       let allTelComponentsAreUntouched = Object.keys(address.record)
         .filter(field => FormAutofillUtils.getCategoryFromFieldName(field) == "tel")
         .every(field => address.untouchedFields.includes(field));
       if (allTelComponentsAreUntouched) {
         // No need to verify it if none of related fields are modified after autofilling.
--- a/browser/extensions/formautofill/test/unit/test_createRecords.js
+++ b/browser/extensions/formautofill/test/unit/test_createRecords.js
@@ -18,16 +18,56 @@ const TESTCASES = [
       "given-name": "John",
       "family-name": "Doe",
     },
     expectedRecord: {
       address: undefined,
     },
   },
   {
+    description: "\"country\" using @autocomplete shouldn't be identified aggressively",
+    document: `<form>
+                <input id="given-name" autocomplete="given-name">
+                <input id="family-name" autocomplete="family-name">
+                <input id="country" autocomplete="country">
+               </form>`,
+    formValue: {
+      "given-name": "John",
+      "family-name": "Doe",
+      country: "United States",
+    },
+    expectedRecord: {
+      address: {
+        "given-name": "John",
+        "family-name": "Doe",
+        country: "United States",
+      },
+    },
+  },
+  {
+    description: "\"country\" using heuristics should be identified aggressively",
+    document: `<form>
+                <input id="given-name" autocomplete="given-name">
+                <input id="family-name" autocomplete="family-name">
+                <input id="country" name="country">
+               </form>`,
+    formValue: {
+      "given-name": "John",
+      "family-name": "Doe",
+      country: "United States",
+    },
+    expectedRecord: {
+      address: {
+        "given-name": "John",
+        "family-name": "Doe",
+        country: "US",
+      },
+    },
+  },
+  {
     description: "\"tel\" related fields should be concatenated",
     document: `<form>
                 <input id="given-name" autocomplete="given-name">
                 <input id="family-name" autocomplete="family-name">
                 <input id="tel-country-code" autocomplete="tel-country-code">
                 <input id="tel-national" autocomplete="tel-national">
                </form>`,
     formValue: {