Bug 1524508 - [de-xbl] convert charsetpicker binding to customized built-in based on menulist. r=pmorris
authorMagnus Melin <mkmelin+mozilla@iki.fi>
Sat, 16 Mar 2019 23:07:01 +0100
changeset 26109 86cdbc8a3cef
parent 26108 e77ea756f198
child 26110 34babfc74629
push id15674
push usermozilla@jorgk.com
push dateSat, 16 Mar 2019 22:12:32 +0000
treeherdercomm-central@34babfc74629 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspmorris
bugs1524508
Bug 1524508 - [de-xbl] convert charsetpicker binding to customized built-in based on menulist. r=pmorris
mail/components/preferences/fonts.xul
mailnews/base/content/charsetList.css
mailnews/base/content/charsetList.xml
mailnews/base/content/folderProps.xul
mailnews/base/content/menulist-charsetpicker.js
mailnews/base/prefs/content/am-server.js
mailnews/base/prefs/content/am-server.xul
mailnews/jar.mn
--- a/mail/components/preferences/fonts.xul
+++ b/mail/components/preferences/fonts.xul
@@ -1,16 +1,14 @@
 <?xml version="1.0"?>
-<!-- -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
-   - This Source Code Form is subject to the terms of the Mozilla Public
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
    - 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/. -->
+   - file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
 
 <?xml-stylesheet href="chrome://global/skin/"?>
-<?xml-stylesheet href="chrome://messenger/content/charsetList.css"?>
 <?xml-stylesheet href="chrome://messenger/skin/preferences/preferences.css"?>
 
 <!DOCTYPE dialog [
 <!ENTITY % fontsDTD SYSTEM "chrome://messenger/locale/preferences/fonts.dtd" >
 %fontsDTD;
 ]>
 
 <dialog id="FontsDialog" type="child" class="prefwindow"
@@ -27,16 +25,17 @@
   </keyset>
 
   <vbox id="FontsDialogPane" class="prefpane largeDialogContainer">
 
     <stringbundle id="bundlePreferences" src="chrome://messenger/locale/preferences/preferences.properties"/>
     <script type="application/javascript" src="chrome://global/content/preferencesBindings.js"/>
     <script type="application/javascript" src="chrome://mozapps/content/preferences/fontbuilder.js"/>
     <script type="application/javascript" src="chrome://messenger/content/preferences/fonts.js"/>
+    <script type="application/javascript" src="chrome://messenger/content/menulist-charsetpicker.js"/>
     <linkset>
       <html:link rel="localization" href="messenger/preferences/fonts.ftl"/>
     </linkset>
 
     <groupbox>
       <hbox align="center">
         <label control="selectLangs">
           <html:h2 accesskey="&language.accesskey;">&language.label;</html:h2>
@@ -279,26 +278,26 @@
           <column flex="1"/>
         </columns>
 
         <rows>
           <row align="center" class="indent">
             <label control="sendDefaultCharsetList"
                    value="&sendDefaultCharset.label;"
                    accesskey="&sendDefaultCharset.accesskey;"/>
-            <menulist id="sendDefaultCharsetList" type="charset"
+            <menulist is="menulist-charsetpicker" id="sendDefaultCharsetList"
                       subset="sending"
                       preference="mailnews.send_default_charset"/>
           </row>
 
           <row align="center" class="indent">
             <label control="viewDefaultCharsetList"
                    value="&viewDefaultCharsetList.label;"
                    accesskey="&viewDefaultCharsetList.accesskey;"/>
-            <menulist id="viewDefaultCharsetList" type="charset"
+            <menulist is="menulist-charsetpicker" id="viewDefaultCharsetList"
                       subset="viewing"
                       preference="mailnews.view_default_charset"/>
           </row>
         </rows>
       </grid>
 
       <separator class="thin"/>
 
deleted file mode 100644
--- a/mailnews/base/content/charsetList.css
+++ /dev/null
@@ -1,5 +0,0 @@
-@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
-
-menulist[type="charset"] {
-  -moz-binding: url("chrome://messenger/content/charsetList.xml#charsetpicker");
-}
--- a/mailnews/base/content/folderProps.xul
+++ b/mailnews/base/content/folderProps.xul
@@ -1,40 +1,32 @@
 <?xml version="1.0"?> <!-- -*- Mode: SGML; indent-tabs-mode: nil; -*- -->
-<!--
-
- This Source Code Form is subject to the terms of the Mozilla Public
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
    - 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/. -->
+   - file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
 
 <?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
 <?xml-stylesheet href="chrome://messenger/skin/preferences/preferences.css" type="text/css"?>
-<?xml-stylesheet href="chrome://messenger/content/charsetList.css" type="text/css"?>
 
 <!DOCTYPE dialog SYSTEM "chrome://messenger/locale/folderProps.dtd">
 
 <dialog
   id="folderPropertiesDialog"
   xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
   xmlns:html="http://www.w3.org/1999/xhtml"
   title="&folderProps.windowtitle.label;"
   buttons="accept,cancel"
   onload="folderPropsOnLoad();" style="width: 56ch;"
   ondialogaccept="return folderPropsOKButton();">
 
   <stringbundle id="bundle_messenger" src="chrome://messenger/locale/messenger.properties"/>
   <script type="application/javascript" src="chrome://global/content/globalOverlay.js"/>
   <script type="application/javascript" src="chrome://messenger/content/retention.js"/>
   <script type="application/javascript" src="chrome://messenger/content/folderProps.js"/>
-  <script type="application/javascript">
-  <![CDATA[
-    const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
-    Services.obs.notifyObservers(null, "charsetmenu-selected", "other");
-  ]]>
-  </script>
+  <script type="application/javascript" src="chrome://messenger/content/menulist-charsetpicker.js"/>
 
 <tabbox id="folderPropTabBox">
   <tabs id="folderPropTabs">
     <tab id="GeneralTab" label="&generalInfo.label;"/>
     <tab id="Retention" label="&retention.label;"/>
     <tab id="SynchronizationTab" hidefor="movemail,pop3,rss,none" label="&folderSynchronizationTab.label;"/>
     <tab id="SharingTab" hidefor="movemail,pop3,rss,none,nntp" label="&folderSharingTab.label;"/>
     <tab id="QuotaTab" hidefor="movemail,pop3,rss,none,nntp" label="&folderQuotaTab.label;"/>
@@ -70,17 +62,17 @@
                 id="folderCheckForNewMessages"
                 label="&folderCheckForNewMessages2.label;"
                 accesskey="&folderCheckForNewMessages2.accesskey;"/>
       <vbox>
         <hbox align="center" valign="middle">
           <label value="&folderCharsetFallback2.label;"
                  accesskey="&folderCharsetFallback2.accesskey;"
                  control="folderCharsetList"/>
-          <menulist id="folderCharsetList" type="charset"/>
+          <menulist is="menulist-charsetpicker" id="folderCharsetList" subset="viewing"/>
         </hbox>
         <checkbox class="indent" id="folderCharsetOverride"
                   label="&folderCharsetEnforce2.label;"
                   accesskey="&folderCharsetEnforce2.accesskey;"/>
       </vbox>
       <groupbox id="folderRebuildSummaryGroupBox" align="baseline">
         <grid>
           <columns>
rename from mailnews/base/content/charsetList.xml
rename to mailnews/base/content/menulist-charsetpicker.js
--- a/mailnews/base/content/charsetList.xml
+++ b/mailnews/base/content/menulist-charsetpicker.js
@@ -1,67 +1,104 @@
-<?xml version="1.0"?>
-<!-- This Source Code Form is subject to the terms of the Mozilla Public
-   - 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/.
--->
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+customElements.whenDefined("menulist").then(() => {
+  const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
-<bindings id="charsetListBinding"
-          xmlns="http://www.mozilla.org/xbl"
-          xmlns:xbl="http://www.mozilla.org/xbl"
-          xmlns:html="http://www.w3.org/1999/xhtml"
-          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  /**
+   * MozMenulistCharsetpicker is a menulist widget that is automatically
+   * populated with charset selections.
+   * - Setting subset="sending" will show the values applicable for sending
+   * - Setting subset="viewing" will show the values applicable for viewing
+   * - Setting preference="<name>" will set the selected value to that of the named preference value
+   * @extends {MozMenuList}
+   */
+  class MozMenulistCharsetpicker extends customElements.get("menulist") {
+    static get observedAttributes() {
+      return ["subset", "preference"];
+    }
 
-  <binding id="charsetpicker"
-           extends="chrome://messenger/content/menulist.xml#xbl-menulist">
-    <implementation>
-      <constructor><![CDATA[
-        let charsetValues = "";
+    connectedCallback() {
+      super.connectedCallback();
+      if (this.delayConnectedCallback()) {
+        return;
+      }
 
-        if (this.getAttribute("subset") == "sending")
-          charsetValues = ["UTF-8", "EUC-KR", "gbk", "gb18030", "ISO-2022-JP",
-                           "ISO-8859-1", "ISO-8859-7", "windows-1252"];
+      if (this.menupopup) {
+        return;
+      }
+
+      this._setupCharsets();
+      this._setupSelectedValueFromPref();
+    }
 
-        else if (!this.getAttribute("subset")
-                 || this.getAttribute("subset") == "viewing")
-          charsetValues = ["UTF-8", "Big5", "EUC-KR", "gbk", "ISO-2022-JP",
-                           "ISO-8859-1", "ISO-8859-2", "ISO-8859-7",
-                           "windows-874", "windows-1250", "windows-1251",
-                           "windows-1252", "windows-1255", "windows-1256",
-                           "windows-1257", "windows-1258"];
-
-        const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
+    _setupCharsets() {
+      let charsetValues;
+      if (this.getAttribute("subset") == "sending") {
+        charsetValues = [
+          "UTF-8", "EUC-KR", "gbk", "gb18030", "ISO-2022-JP",
+          "ISO-8859-1", "ISO-8859-7", "windows-1252",
+        ];
+      } else if (this.getAttribute("subset") == "viewing") {
+        charsetValues = [
+          "UTF-8", "Big5", "EUC-KR", "gbk", "ISO-2022-JP",
+          "ISO-8859-1", "ISO-8859-2", "ISO-8859-7",
+          "windows-874", "windows-1250", "windows-1251",
+          "windows-1252", "windows-1255", "windows-1256",
+          "windows-1257", "windows-1258",
+        ];
+      }
 
-        let charsetBundle = Services.strings.createBundle(
-          "chrome://messenger/locale/charsetTitles.properties");
-        let aMenuList = this;
-        let menuLabels = [];
+      let charsetBundle = Services.strings.createBundle(
+        "chrome://messenger/locale/charsetTitles.properties");
+      let menuLabels = charsetValues.map((item) => {
+        let strCharset = charsetBundle.GetStringFromName(
+          item.toLowerCase() + ".title");
+        return { label: strCharset, value: item };
+      });
 
-        charsetValues.forEach(function(item) {
-          let strCharset = charsetBundle.GetStringFromName(
-            item.toLowerCase() + ".title");
+      menuLabels.sort((a, b) => {
+        if (a.value == "UTF-8" || a.label < b.label) {
+          return -1;
+        } else if (b.value == "UTF-8" || a.label > b.label) {
+          return 1;
+        }
+        return 0;
+      });
 
-          menuLabels.push({label: strCharset, value: item});
-        });
+      menuLabels.forEach((item) => {
+        this.appendItem(item.label, item.value);
+      });
+    }
 
-        menuLabels.sort(function(a, b) {
-          if (a.value == "UTF-8" || a.label < b.label)
-            return -1;
-          else if (b.value == "UTF-8" || a.label > b.label)
-            return 1;
-          return 0;
-        });
-
-        menuLabels.forEach(function(item) {
-          aMenuList.appendItem(item.label, item.value);
-        });
+    _setupSelectedValueFromPref() {
+      // Set appropriate selected menu item based on preference value.
+      if (this.hasAttribute("preference")) {
+        let preference = Services.prefs.getComplexValue(
+          this.getAttribute("preference"), Ci.nsIPrefLocalizedString);
+        this.value = preference.data;
+      }
+    }
 
-        // Selecting appropriate menu item corresponding to preference stored
-        // value.
-        if (this.hasAttribute("preference")) {
-          let preference = Services.prefs.getComplexValue(
-            this.getAttribute("preference"), Ci.nsIPrefLocalizedString);
-          this.value = preference.data;
-        }
-     ]]></constructor>
-    </implementation>
-  </binding>
-</bindings>
+    attributeChangedCallback() {
+      super.attributeChangedCallback();
+      if (!this.isConnectedAndReady) {
+        return;
+      }
+      this._updateAttributes();
+    }
+
+    _updateAttributes() {
+      this.removeAllItems();
+      this._setupCharsets();
+      this._setupSelectedValueFromPref();
+    }
+  }
+  customElements.define("menulist-charsetpicker", MozMenulistCharsetpicker, { extends: "menulist" });
+});
+
+// The menulist CE is defined lazily. Create one now to get menulist defined,
+// allowing us to inherit from it.
+if (!customElements.get("menulist")) {
+  delete document.createElement("menulist");
+}
--- a/mailnews/base/prefs/content/am-server.js
+++ b/mailnews/base/prefs/content/am-server.js
@@ -107,18 +107,16 @@ function onInit(aPageId, aServerId)
   gOriginalStoreType = storeTypeElement.value;
 }
 
 function onPreInit(account, accountValues)
 {
   var type = parent.getAccountValue(account, accountValues, "server", "type", null, false);
   hideShowControls(type);
 
-  Services.obs.notifyObservers(null, "charsetmenu-selected", "other");
-
   gServer = account.incomingServer;
 
   if(!account.incomingServer.canEmptyTrashOnExit)
   {
     document.getElementById("server.emptyTrashOnExit").setAttribute("hidden", "true");
     document.getElementById("imap.deleteModel.box").setAttribute("hidden", "true");
   }
 }
--- a/mailnews/base/prefs/content/am-server.xul
+++ b/mailnews/base/prefs/content/am-server.xul
@@ -1,35 +1,32 @@
 <?xml version="1.0"?>
-
-<!--
-
- This Source Code Form is subject to the terms of the Mozilla Public
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
    - 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/. -->
+   - file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
 
 <?xml-stylesheet href="chrome://messenger/skin/accountManage.css" type="text/css"?>
 <?xml-stylesheet href="chrome://messenger/skin/messenger.css" type="text/css"?>
 <?xml-stylesheet href="chrome://messenger/skin/folderMenus.css" type="text/css"?>
-<?xml-stylesheet href="chrome://messenger/content/charsetList.css" type="text/css"?>
 
 <!DOCTYPE page [
 <!ENTITY % trashDTD SYSTEM "chrome://messenger/locale/am-server-top.dtd">
 %trashDTD;
 ]>
 
 <page xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml"
       title="&serverSettings.label;"
       onload="parent.onPanelLoaded('am-server.xul');">
 
   <vbox flex="1" style="overflow: auto;">
     <script type="application/javascript" src="chrome://messenger/content/AccountManager.js"/>
     <script type="application/javascript" src="chrome://messenger/content/am-server.js"/>
     <script type="application/javascript" src="chrome://messenger/content/am-prefs.js"/>
     <script type="application/javascript" src="chrome://messenger/content/amUtils.js"/>
+    <script type="application/javascript" src="chrome://messenger/content/menulist-charsetpicker.js"/>
     <stringbundle id="bundle_messenger" src="chrome://messenger/locale/messenger.properties"/>
 
     <label hidden="true" wsm_persist="true" id="server.type"/>
     <label hidden="true"
            wsm_persist="true"
            preftype="string"
            prefattribute="value"
            prefstring="mail.server.%serverkey%.storeContractID"
@@ -454,18 +451,17 @@
       </vbox>
 
     </groupbox>
 
     <separator class="thin"/>
 
     <hbox hidefor="imap,pop3,movemail" align="center" valign="middle" iscontrolcontainer="true">
       <label value="&serverDefaultCharset2.label;" control="nntp.charset"/>
-      <menulist hidable="true"
-                type="charset"
+      <menulist is="menulist-charsetpicker" id="nntp.charset" subset="viewing"
+                hidable="true"
                 hidefor="imap,pop3,movemail"
                 wsm_persist="true"
-                id="nntp.charset"
                 preftype="string"
                 prefstring="mail.server.%serverkey%.charset"/>
     </hbox>
   </vbox>
 </page>
--- a/mailnews/jar.mn
+++ b/mailnews/jar.mn
@@ -58,20 +58,19 @@ messenger.jar:
     content/messenger/converterDialog.js                                       (base/prefs/content/converterDialog.js)
     content/messenger/removeAccount.xul                                        (base/prefs/content/removeAccount.xul)
     content/messenger/removeAccount.js                                         (base/prefs/content/removeAccount.js)
     content/messenger/msgSelectOfflineFolders.xul                              (base/content/msgSelectOfflineFolders.xul)
     content/messenger/msgSelectOfflineFolders.js                               (base/content/msgSelectOfflineFolders.js)
     content/messenger/msgSynchronize.xul                                       (base/content/msgSynchronize.xul)
     content/messenger/msgSynchronize.js                                        (base/content/msgSynchronize.js)
     content/messenger/folderProps.xul                                          (base/content/folderProps.xul)
+    content/messenger/menulist-charsetpicker.js                                (base/content/menulist-charsetpicker.js)
     content/messenger/folderProps.js                                           (base/content/folderProps.js)
     content/messenger/folderWidgets.xml                                        (base/content/folderWidgets.xml)
-    content/messenger/charsetList.xml                                          (base/content/charsetList.xml)
-    content/messenger/charsetList.css                                          (base/content/charsetList.css)
     content/messenger/newFolderDialog.xul                                      (base/content/newFolderDialog.xul)
     content/messenger/newFolderDialog.js                                       (base/content/newFolderDialog.js)
 *   content/messenger/msgAccountCentral.xul                                    (base/content/msgAccountCentral.xul)
     content/messenger/msgAccountCentral.js                                     (base/content/msgAccountCentral.js)
     content/messenger/msgFolderPickerOverlay.js                                (base/content/msgFolderPickerOverlay.js)
     content/messenger/renameFolderDialog.xul                                   (base/content/renameFolderDialog.xul)
     content/messenger/renameFolderDialog.js                                    (base/content/renameFolderDialog.js)
     content/messenger/retention.js                                             (base/content/retention.js)