Bug 1422404 - [Form Autofill] Adapt profiles to the maxlength restriction of fields. r=seanlee draft
authorLuke Chang <lchang@mozilla.com>
Wed, 06 Dec 2017 17:51:32 +0800
changeset 711245 a0844a016344996cf993dd9310aa9ed7fbf95064
parent 711178 defccba824aa91e8d4d820b1defaadfdca34bac7
child 743777 89f8a5b8f2e4052b46fe060c36597dc48e766041
push id93035
push userbmo:lchang@mozilla.com
push dateWed, 13 Dec 2017 17:07:40 +0000
reviewersseanlee
bugs1422404
milestone59.0a1
Bug 1422404 - [Form Autofill] Adapt profiles to the maxlength restriction of fields. r=seanlee MozReview-Commit-ID: 5bNqQABJy1C
browser/extensions/formautofill/FormAutofillHandler.jsm
browser/extensions/formautofill/test/unit/test_getAdaptedProfiles.js
--- a/browser/extensions/formautofill/FormAutofillHandler.jsm
+++ b/browser/extensions/formautofill/FormAutofillHandler.jsm
@@ -319,22 +319,48 @@ class FormAutofillSection {
     result = /(?:[^y]|\b)(y{2,4})\s*([-/\\]*)\s*(m{1,2})(?!m)/i.exec(placeholder);
     if (result) {
       profile["cc-exp"] = String(ccExpYear).substr(-1 * result[1].length) +
                           result[2] +
                           String(ccExpMonth).padStart(result[3].length, "0");
     }
   }
 
+  _adaptFieldMaxLength(profile) {
+    for (let key in profile) {
+      let detail = this.getFieldDetailByName(key);
+      if (!detail) {
+        continue;
+      }
+
+      let element = detail.elementWeakRef.get();
+      if (!element) {
+        continue;
+      }
+
+      let maxLength = element.maxLength;
+      if (maxLength === undefined || maxLength < 0 || profile[key].length <= maxLength) {
+        continue;
+      }
+
+      if (maxLength) {
+        profile[key] = profile[key].substr(0, maxLength);
+      } else {
+        delete profile[key];
+      }
+    }
+  }
+
   getAdaptedProfiles(originalProfiles) {
     for (let profile of originalProfiles) {
       this._addressTransformer(profile);
       this._telTransformer(profile);
       this._matchSelectOptions(profile);
       this._creditCardExpDateTransformer(profile);
+      this._adaptFieldMaxLength(profile);
     }
     return originalProfiles;
   }
 
   /**
    * Processes form fields that can be autofilled, and populates them with the
    * profile provided by backend.
    *
--- a/browser/extensions/formautofill/test/unit/test_getAdaptedProfiles.js
+++ b/browser/extensions/formautofill/test/unit/test_getAdaptedProfiles.js
@@ -13,16 +13,24 @@ const DEFAULT_ADDRESS_RECORD = {
   "address-line2": "line2",
   "address-line3": "line3",
   "address-level1": "CA",
   "country": "US",
   "tel": "+19876543210",
   "tel-national": "9876543210",
 };
 
+const ADDRESS_RECORD_2 = {
+  "guid": "address2",
+  "given-name": "John",
+  "additional-name": "Middle",
+  "family-name": "Doe",
+  "postal-code": "940012345",
+};
+
 const DEFAULT_CREDITCARD_RECORD = {
   "guid": "123",
   "cc-exp-month": 1,
   "cc-exp-year": 2025,
   "cc-exp": "2025-01",
 };
 
 const TESTCASES = [
@@ -445,17 +453,17 @@ const TESTCASES = [
       "address-line3": "line3",
       "address-level1": "CA",
       "country": "US",
       "tel": "9876543210",
       "tel-national": "9876543210",
     }],
   },
   {
-    description: "Checking maxlength first when a field is with maxlength.",
+    description: "Checking maxlength of tel field first when a field is with maxlength.",
     document: `<form>
                <input id="tel" autocomplete="tel" maxlength="10">
                <input id="line1" autocomplete="address-line1">
                <input id="line2" autocomplete="address-line2">
                </form>`,
     profileData: [Object.assign({}, DEFAULT_ADDRESS_RECORD)],
     expectedResult: [{
       "guid": "123",
@@ -466,16 +474,48 @@ const TESTCASES = [
       "address-line3": "line3",
       "address-level1": "CA",
       "country": "US",
       "tel": "9876543210",
       "tel-national": "9876543210",
     }],
   },
   {
+    description: "Address form with maxlength restriction",
+    document: `<form>
+               <input autocomplete="given-name" maxlength="1">
+               <input autocomplete="additional-name" maxlength="1">
+               <input autocomplete="family-name" maxlength="1">
+               <input autocomplete="postal-code" maxlength="5">
+               </form>`,
+    profileData: [Object.assign({}, ADDRESS_RECORD_2)],
+    expectedResult: [{
+      "guid": "address2",
+      "given-name": "J",
+      "additional-name": "M",
+      "family-name": "D",
+      "postal-code": "94001",
+    }],
+  },
+  {
+    description: "Address form with the special cases of the maxlength restriction",
+    document: `<form>
+               <input autocomplete="given-name" maxlength="-1">
+               <input autocomplete="additional-name" maxlength="0">
+               <input autocomplete="family-name" maxlength="1">
+               </form>`,
+    profileData: [Object.assign({}, ADDRESS_RECORD_2)],
+    expectedResult: [{
+      "guid": "address2",
+      "given-name": "John",
+      "family-name": "D",
+      "postal-code": "940012345",
+    }],
+  },
+  {
     description: "Credit Card form with matching options of cc-exp-year and cc-exp-month",
     document: `<form>
                <input autocomplete="cc-number">
                <select autocomplete="cc-exp-month">
                  <option id="option-cc-exp-month-01" value="1">01</option>
                  <option id="option-cc-exp-month-02" value="2">02</option>
                  <option id="option-cc-exp-month-03" value="3">03</option>
                  <option id="option-cc-exp-month-04" value="4">04</option>