Commit 87f933ff authored by Brian Grinstead's avatar Brian Grinstead
Browse files

Bug 1459245 - Migrate <stringbundle> from a XBL binding to a Custom Element;r=mossop

This loads a JS file to register the Custom Element whenever XUL documents
get created. This is a pattern we plan to follow for future elements as well -
stringbundle is just an easy target to prove it out with.

MozReview-Commit-ID: 7PlisCknrKW

--HG--
rename : toolkit/content/widgets/stringbundle.xml => toolkit/content/widgets/stringbundle.js
extra : rebase_source : 5bf94038556c9b956de3678cd4bc817f5d837f45
parent 4c401366
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ MainProcessSingleton.prototype = {
    switch (topic) {
    case "app-startup": {
      Services.obs.addObserver(this, "xpcom-shutdown");
      Services.obs.addObserver(this, "document-element-inserted");

      // Load this script early so that console.* is initialized
      // before other frame scripts.
@@ -75,8 +76,25 @@ MainProcessSingleton.prototype = {
      break;
    }

    case "document-element-inserted":
      // Set up Custom Elements for XUL windows before anything else happens
      // in the document. Anything loaded here should be considered part of
      // core XUL functionality. Any window-specific elements can be registered
      // via <script> tags at the top of individual documents.
      const doc = subject;
      if (doc.nodePrincipal.isSystemPrincipal &&
          doc.contentType == "application/vnd.mozilla.xul+xml") {
        for (let script of [
          "chrome://global/content/elements/stringbundle.js",
        ]) {
          Services.scriptloader.loadSubScript(script, doc.ownerGlobal);
        }
      }
      break;

    case "xpcom-shutdown":
      Services.mm.removeMessageListener("Search:AddEngine", this.addSearchEngine);
      Services.obs.removeObserver(this, "document-element-inserted");
      break;
    }
  },
+1 −1
Original line number Diff line number Diff line
@@ -90,7 +90,6 @@ toolkit.jar:
   content/global/bindings/scale.xml           (widgets/scale.xml)
   content/global/bindings/scrollbox.xml       (widgets/scrollbox.xml)
   content/global/bindings/spinner.js          (widgets/spinner.js)
   content/global/bindings/stringbundle.xml    (widgets/stringbundle.xml)
*  content/global/bindings/tabbox.xml          (widgets/tabbox.xml)
   content/global/bindings/text.xml            (widgets/text.xml)
*  content/global/bindings/textbox.xml         (widgets/textbox.xml)
@@ -101,6 +100,7 @@ toolkit.jar:
*  content/global/bindings/tree.xml            (widgets/tree.xml)
   content/global/bindings/videocontrols.xml   (widgets/videocontrols.xml)
*  content/global/bindings/wizard.xml          (widgets/wizard.xml)
   content/global/elements/stringbundle.js     (widgets/stringbundle.js)
#ifdef XP_MACOSX
   content/global/macWindowMenu.js
#endif
+63 −0
Original line number Diff line number Diff line
/* 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/. */

"use strict";

// This is loaded into all XUL windows. Wrap in a block to prevent
// leaking to window scope.
{

ChromeUtils.import("resource://gre/modules/Services.jsm");

class MozStringbundle extends XULElement {
  get stringBundle() {
    if (!this._bundle) {
      try {
        this._bundle = Services.strings.createBundle(this.src);
      } catch (e) {
        dump("Failed to get stringbundle:\n");
        dump(e + "\n");
      }
    }
    return this._bundle;
  }

  set src(val) {
    this._bundle = null;
    this.setAttribute("src", val);
    return val;
  }

  get src() {
    return this.getAttribute("src");
  }

  get strings() {
    // Note: this is a sucky method name! Should be:
    //       readonly attribute nsISimpleEnumerator strings;
    return this.stringBundle.getSimpleEnumeration();
  }

  getString(aStringKey) {
    try {
      return this.stringBundle.GetStringFromName(aStringKey);
    } catch (e) {
      dump("*** Failed to get string " + aStringKey + " in bundle: " + this.src + "\n");
      throw e;
    }
  }

  getFormattedString(aStringKey, aStringsArray) {
    try {
      return this.stringBundle.formatStringFromName(aStringKey, aStringsArray, aStringsArray.length);
    } catch (e) {
      dump("*** Failed to format string " + aStringKey + " in bundle: " + this.src + "\n");
      throw e;
    }
  }
}

customElements.define("stringbundle", MozStringbundle);

}
+0 −91
Original line number Diff line number Diff line
<?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/. -->


<bindings id="stringBundleBindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <binding id="stringbundle" extends="xul:spacer">
    <implementation name="XStringBundle">

      <method name="getString">
        <parameter name="aStringKey"/>
        <body>
          <![CDATA[
            try {
              return this.stringBundle.GetStringFromName(aStringKey);
            } catch (e) {
              dump("*** Failed to get string " + aStringKey + " in bundle: " + this.src + "\n");
              throw e;
            }
          ]]>
        </body>
      </method>

      <method name="getFormattedString">
        <parameter name="aStringKey"/>
        <parameter name="aStringsArray"/>
        <body>
          <![CDATA[
            try {
              return this.stringBundle.formatStringFromName(aStringKey, aStringsArray, aStringsArray.length);
            } catch (e) {
              dump("*** Failed to format string " + aStringKey + " in bundle: " + this.src + "\n");
              throw e;
            }
          ]]>
        </body>
      </method>

      <property name="stringBundle" readonly="true">
        <getter>
          <![CDATA[
            if (!this._bundle) {
              try {
                this._bundle = Cc["@mozilla.org/intl/stringbundle;1"]
                                 .getService(Ci.nsIStringBundleService)
                                 .createBundle(this.src);
              } catch (e) {
                dump("Failed to get stringbundle:\n");
                dump(e + "\n");
              }
            }
            return this._bundle;
          ]]>
        </getter>
      </property>

      <property name="src">
        <getter>
          <![CDATA[
            return this.getAttribute("src");
          ]]>
        </getter>
        <setter>
          <![CDATA[
            this._bundle = null;
            this.setAttribute("src", val);
            return val;
          ]]>
        </setter>
      </property>

      <property name="strings">
        <getter>
          <![CDATA[
            // Note: this is a sucky method name! Should be:
            //       readonly attribute nsISimpleEnumerator strings;
            return this.stringBundle.getSimpleEnumeration();
          ]]>
        </getter>
      </property>

      <field name="_bundle">null</field>

    </implementation>
  </binding>

</bindings>
+2 −6
Original line number Diff line number Diff line
@@ -888,13 +888,9 @@ autorepeatbutton {

/********** stringbundle **********/

stringbundle,
stringbundleset {
  visibility: collapse;
}

stringbundle {
  -moz-binding: url("chrome://global/content/bindings/stringbundle.xml#stringbundle");
  visibility: collapse;
  display: none;
}

/********** dialog **********/