Bug 1427954 - Remove string bundle references from autofillEditForms.js. r=sfoster
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Wed, 14 Mar 2018 18:12:15 -0700
changeset 408637 ac23b9ba0603839616105f4fbe7d015ddff28254
parent 408636 e2eae899dc4ab8352dffdfeacc974ccdc83fa3bf
child 408638 4e5ce4f1f57901ebaf9d932b41db13b4fe574274
push id100996
push userbtara@mozilla.com
push dateSat, 17 Mar 2018 10:37:43 +0000
treeherdermozilla-inbound@97160a734959 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssfoster
bugs1427954
milestone61.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 1427954 - Remove string bundle references from autofillEditForms.js. r=sfoster This is a step on the path to making it unpriveleged. MozReview-Commit-ID: 3DSYvM2Pje9
browser/extensions/formautofill/FormAutofillUtils.jsm
browser/extensions/formautofill/content/autofillEditForms.js
browser/extensions/formautofill/content/editDialog.js
browser/extensions/formautofill/content/manageDialog.js
--- a/browser/extensions/formautofill/FormAutofillUtils.jsm
+++ b/browser/extensions/formautofill/FormAutofillUtils.jsm
@@ -823,37 +823,49 @@ this.FormAutofillUtils = {
     return {
       "addressLevel1Label": dataset.state_name_type || "province",
       "postalCodeLabel": dataset.zip_name_type || "postalCode",
       "fieldsOrder": this.parseAddressFormat(dataset.fmt || "%N%n%O%n%A%n%C, %S %Z"),
     };
   },
 
   /**
-   * Localize elements with "data-localization" attribute
-   * @param   {string} bundleURI
+   * Localize elements with "data-localization" or "data-localization-region" attributes.
    * @param   {DOMElement} root
    */
-  localizeMarkup(bundleURI, root) {
-    const bundle = Services.strings.createBundle(bundleURI);
+  localizeMarkup(root) {
     let elements = root.querySelectorAll("[data-localization]");
     for (let element of elements) {
-      element.textContent = bundle.GetStringFromName(element.getAttribute("data-localization"));
+      element.textContent = this.stringBundle.GetStringFromName(
+        element.getAttribute("data-localization")
+      );
       element.removeAttribute("data-localization");
     }
+
+    let regionElements = root.querySelectorAll("[data-localization-region]");
+    for (let element of regionElements) {
+      element.textContent = this.regionsBundle.GetStringFromName(
+        element.getAttribute("data-localization-region")
+      );
+      element.removeAttribute("data-localization-region");
+    }
   },
 };
 
 this.log = null;
 this.FormAutofillUtils.defineLazyLogGetter(this, EXPORTED_SYMBOLS[0]);
 
 XPCOMUtils.defineLazyGetter(FormAutofillUtils, "stringBundle", function() {
   return Services.strings.createBundle("chrome://formautofill/locale/formautofill.properties");
 });
 
+XPCOMUtils.defineLazyGetter(FormAutofillUtils, "regionsBundle", function() {
+  return Services.strings.createBundle("chrome://global/locale/regionNames.properties");
+});
+
 XPCOMUtils.defineLazyGetter(FormAutofillUtils, "brandBundle", function() {
   return Services.strings.createBundle("chrome://branding/locale/brand.properties");
 });
 
 XPCOMUtils.defineLazyPreferenceGetter(this.FormAutofillUtils,
                                       "DEFAULT_REGION", DEFAULT_REGION_PREF, "US");
 XPCOMUtils.defineLazyPreferenceGetter(this.FormAutofillUtils,
                                       "isAutofillAddressesEnabled", ENABLED_AUTOFILL_ADDRESSES_PREF);
--- a/browser/extensions/formautofill/content/autofillEditForms.js
+++ b/browser/extensions/formautofill/content/autofillEditForms.js
@@ -2,19 +2,16 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* exported EditAddress, EditCreditCard */
 /* eslint-disable mozilla/balanced-listeners */ // Not relevant since the document gets unloaded.
 
 "use strict";
 
-const AUTOFILL_BUNDLE_URI = "chrome://formautofill/locale/formautofill.properties";
-const REGIONS_BUNDLE_URI = "chrome://global/locale/regionNames.properties";
-
 ChromeUtils.import("resource://formautofill/FormAutofillUtils.jsm");
 
 class EditAutofillForm {
   constructor(elements, record) {
     this._elements = elements;
     this._record = record;
   }
 
@@ -72,49 +69,46 @@ class EditAutofillForm {
   /**
    * Attach event listener
    */
   attachEventListeners() {
     this._elements.form.addEventListener("input", this);
   }
 
   // An interface to be inherited.
-  localizeDocument() {}
-
-  // An interface to be inherited.
   handleChange(event) {}
 }
 
 class EditAddress extends EditAutofillForm {
   constructor(elements, record) {
     let country = record ? record.country :
                   FormAutofillUtils.supportedCountries.find(supported => supported == FormAutofillUtils.DEFAULT_REGION);
     super(elements, record || {country});
 
     Object.assign(this._elements, {
       addressLevel1Label: this._elements.form.querySelector("#address-level1-container > span"),
       postalCodeLabel: this._elements.form.querySelector("#postal-code-container > span"),
       country: this._elements.form.querySelector("#country"),
     });
 
-    this.localizeDocument();
+    this.populateCountries();
     this.formatForm(country);
     this.attachEventListeners();
   }
 
   /**
    * Format the form based on country. The address-level1 and postal-code labels
    * should be specific to the given country.
    * @param  {string} country
    */
   formatForm(country) {
     const {addressLevel1Label, postalCodeLabel, fieldsOrder} = FormAutofillUtils.getFormFormat(country);
     this._elements.addressLevel1Label.dataset.localization = addressLevel1Label;
     this._elements.postalCodeLabel.dataset.localization = postalCodeLabel;
-    FormAutofillUtils.localizeMarkup(AUTOFILL_BUNDLE_URI, document);
+    FormAutofillUtils.localizeMarkup(document);
     this.arrangeFields(fieldsOrder);
   }
 
   arrangeFields(fieldsOrder) {
     let fields = [
       "name",
       "organization",
       "street-address",
@@ -139,26 +133,26 @@ class EditAddress extends EditAutofillFo
     }
     // Hide the remaining fields
     for (let field of fields) {
       let container = document.getElementById(`${field}-container`);
       container.style.display = "none";
     }
   }
 
-  localizeDocument() {
+  populateCountries() {
     let fragment = document.createDocumentFragment();
     for (let country of FormAutofillUtils.supportedCountries) {
       let option = new Option();
       option.value = country;
-      option.dataset.localization = country.toLowerCase();
+      option.dataset.localizationRegion = country.toLowerCase();
       fragment.appendChild(option);
     }
     this._elements.country.appendChild(fragment);
-    FormAutofillUtils.localizeMarkup(REGIONS_BUNDLE_URI, this._elements.country);
+    FormAutofillUtils.localizeMarkup(this._elements.country);
   }
 
   handleChange(event) {
     this.formatForm(event.target.value);
   }
 
   attachEventListeners() {
     this._elements.country.addEventListener("change", this);
@@ -168,17 +162,16 @@ class EditAddress extends EditAutofillFo
 
 class EditCreditCard extends EditAutofillForm {
   constructor(elements, record) {
     super(elements, record);
     Object.assign(this._elements, {
       ccNumber: this._elements.form.querySelector("#cc-number"),
       year: this._elements.form.querySelector("#cc-exp-year"),
     });
-    this.localizeDocument();
     this.generateYears();
     this.attachEventListeners();
   }
 
   generateYears() {
     const count = 11;
     const currentYear = new Date().getFullYear();
     const ccExpYear = this._record && this._record["cc-exp-year"];
@@ -193,20 +186,16 @@ class EditCreditCard extends EditAutofil
       this._elements.year.appendChild(option);
     }
 
     if (ccExpYear && ccExpYear > currentYear + count) {
       this._elements.year.appendChild(new Option(ccExpYear));
     }
   }
 
-  localizeDocument() {
-    FormAutofillUtils.localizeMarkup(AUTOFILL_BUNDLE_URI, document);
-  }
-
   handleInput(event) {
     // Clear the error message if cc-number is valid
     if (event.target == this._elements.ccNumber &&
         FormAutofillUtils.isCCNumber(this._elements.ccNumber.value)) {
       this._elements.ccNumber.setCustomValidity("");
     }
     super.handleInput(event);
   }
--- a/browser/extensions/formautofill/content/editDialog.js
+++ b/browser/extensions/formautofill/content/editDialog.js
@@ -2,18 +2,16 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* exported EditAddressDialog, EditCreditCardDialog */
 /* eslint-disable mozilla/balanced-listeners */ // Not relevant since the document gets unloaded.
 
 "use strict";
 
-const AUTOFILL_BUNDLE_URI = "chrome://formautofill/locale/formautofill.properties";
-
 ChromeUtils.import("resource://formautofill/FormAutofillUtils.jsm");
 
 ChromeUtils.defineModuleGetter(this, "formAutofillStorage",
                                "resource://formautofill/FormAutofillStorage.jsm");
 ChromeUtils.defineModuleGetter(this, "MasterPassword",
                                "resource://formautofill/MasterPassword.jsm");
 
 class AutofillEditDialog {
@@ -165,17 +163,17 @@ class EditAddressDialog extends Autofill
                   FormAutofillUtils.supportedCountries.find(supported => supported == FormAutofillUtils.DEFAULT_REGION);
     super("addresses", elements, record || {country});
   }
 
   localizeDocument() {
     if (this._record) {
       this._elements.title.dataset.localization = "editAddressTitle";
     }
-    FormAutofillUtils.localizeMarkup(AUTOFILL_BUNDLE_URI, document);
+    FormAutofillUtils.localizeMarkup(document);
   }
 
   async handleSubmit() {
     await this.saveRecord(this._elements.fieldContainer.buildFormObject(), this._record ? this._record.guid : null);
     window.close();
   }
 }
 
@@ -183,17 +181,17 @@ class EditCreditCardDialog extends Autof
   constructor(elements, record) {
     super("creditCards", elements, record);
   }
 
   localizeDocument() {
     if (this._record) {
       this._elements.title.dataset.localization = "editCreditCardTitle";
     }
-    FormAutofillUtils.localizeMarkup(AUTOFILL_BUNDLE_URI, document);
+    FormAutofillUtils.localizeMarkup(document);
   }
 
   /**
    * Decrypt cc-number first and fill the form.
    * @param  {object} creditCard
    */
   async loadInitialValues(creditCard) {
     let decryptedCC = await MasterPassword.decrypt(creditCard["cc-number-encrypted"]);
--- a/browser/extensions/formautofill/content/manageDialog.js
+++ b/browser/extensions/formautofill/content/manageDialog.js
@@ -3,17 +3,16 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* exported ManageAddresses, ManageCreditCards */
 
 "use strict";
 
 const EDIT_ADDRESS_URL = "chrome://formautofill/content/editAddress.xhtml";
 const EDIT_CREDIT_CARD_URL = "chrome://formautofill/content/editCreditCard.xhtml";
-const AUTOFILL_BUNDLE_URI = "chrome://formautofill/locale/formautofill.properties";
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 ChromeUtils.import("resource://formautofill/FormAutofillUtils.jsm");
 
 ChromeUtils.defineModuleGetter(this, "formAutofillStorage",
                                "resource://formautofill/FormAutofillStorage.jsm");
 ChromeUtils.defineModuleGetter(this, "MasterPassword",
@@ -44,17 +43,17 @@ class ManageRecords {
   uninit() {
     log.debug("uninit");
     this.detachEventListeners();
     this._elements = null;
   }
 
   localizeDocument() {
     document.documentElement.style.minWidth = FormAutofillUtils.stringBundle.GetStringFromName("manageDialogsWidth");
-    FormAutofillUtils.localizeMarkup(AUTOFILL_BUNDLE_URI, document);
+    FormAutofillUtils.localizeMarkup(document);
   }
 
   /**
    * Get the selected options on the addresses element.
    *
    * @returns {array<DOMElement>}
    */
   get _selectedOptions() {