Bug 1420853 - Ensure only fields valid for the given country are saved. r=steveck,lchang draft
authorScott Wu <scottcwwu@gmail.com>
Mon, 15 Jan 2018 13:43:42 +0800
changeset 720314 a4fd338fccb3
parent 720191 21ddfb9e6cc0
child 746029 39856dc4264c
push id95505
push userbmo:scwwu@mozilla.com
push dateMon, 15 Jan 2018 07:55:52 +0000
reviewerssteveck, lchang
bugs1420853
milestone59.0a1
Bug 1420853 - Ensure only fields valid for the given country are saved. r=steveck,lchang MozReview-Commit-ID: HymlGP7rQQo
browser/extensions/formautofill/content/editDialog.js
browser/extensions/formautofill/test/browser/browser_editAddressDialog.js
--- a/browser/extensions/formautofill/content/editDialog.js
+++ b/browser/extensions/formautofill/content/editDialog.js
@@ -20,16 +20,17 @@ XPCOMUtils.defineLazyModuleGetter(this, 
                                   "resource://formautofill/MasterPassword.jsm");
 
 class EditDialog {
   constructor(subStorageName, elements, record) {
     this._storageInitPromise = profileStorage.initialize();
     this._subStorageName = subStorageName;
     this._elements = elements;
     this._record = record;
+    this._hiddenFields = [];
     this.localizeDocument();
     window.addEventListener("DOMContentLoaded", this, {once: true});
   }
 
   async init() {
     if (this._record) {
       await this.loadInitialValues(this._record);
     }
@@ -53,22 +54,22 @@ class EditDialog {
       let input = document.getElementById(field);
       if (input) {
         input.value = record[field];
       }
     }
   }
 
   /**
-   * Get inputs from the form.
+   * Get inputs from the form, but exclude those that are hidden.
    * @returns {object}
    */
   buildFormObject() {
     return Array.from(document.forms[0].elements).reduce((obj, input) => {
-      if (input.value) {
+      if (input.value && !this._hiddenFields.includes(input.id)) {
         obj[input.id] = input.value;
       }
       return obj;
     }, {});
   }
 
   /**
    * Get storage and ensure it has been initialized.
@@ -241,16 +242,17 @@ class EditAddress extends EditDialog {
       // Assign tabIndex starting from 1
       inputs[i].tabIndex = i + 1;
     }
     // Hide the remaining fields
     for (let field of fields) {
       let container = document.getElementById(`${field}-container`);
       container.style.display = "none";
     }
+    this._hiddenFields = fields;
   }
 
   localizeDocument() {
     if (this._record) {
       this._elements.title.dataset.localization = "editAddressTitle";
     }
     let fragment = document.createDocumentFragment();
     for (let country of FormAutofillUtils.supportedCountries) {
--- a/browser/extensions/formautofill/test/browser/browser_editAddressDialog.js
+++ b/browser/extensions/formautofill/test/browser/browser_editAddressDialog.js
@@ -189,8 +189,28 @@ add_task(async function test_saveAddress
     keyInputs.forEach(input => EventUtils.synthesizeKey(input, {}, win));
   });
   let addresses = await getAddresses();
   for (let [fieldName, fieldValue] of Object.entries(TEST_ADDRESS_DE_1)) {
     is(addresses[0][fieldName], fieldValue, "check " + fieldName);
   }
   await removeAllRecords();
 });
+
+add_task(async function test_hiddenFieldNotSaved() {
+  await testDialog(EDIT_ADDRESS_DIALOG_URL, win => {
+    let doc = win.document;
+    doc.querySelector("#address-level2").focus();
+    EventUtils.synthesizeKey(TEST_ADDRESS_1["address-level2"], {}, win);
+    doc.querySelector("#address-level1").focus();
+    EventUtils.synthesizeKey(TEST_ADDRESS_1["address-level1"], {}, win);
+    doc.querySelector("#country").focus();
+    EventUtils.synthesizeKey("Germany", {}, win);
+    doc.querySelector("#save").focus();
+    EventUtils.synthesizeKey("VK_RETURN", {}, win);
+  });
+  let addresses = await getAddresses();
+  is(addresses[0].country, "DE", "check country");
+  is(addresses[0]["address-level2"], TEST_ADDRESS_1["address-level2"], "check address-level2");
+  is(addresses[0]["address-level1"], undefined, "address-level1 should not be saved");
+
+  await removeAllRecords();
+});