From 0ce3ff104d298f4657a1746fe906a53af9cf9a05 Mon Sep 17 00:00:00 2001 From: Zibi Braniecki Date: Fri, 22 Jun 2018 13:14:23 -0700 Subject: [PATCH] Bug 1475903 - Add mozIDOMLocalization API. r=mossop In order to use DOMLocalization from C++ we need an XPIDL interface. mozIDOMLocalization exposes the class and functionality allowing DocumentL10n to hook into it. MozReview-Commit-ID: GPMhw61LPEg --HG-- extra : rebase_source : 62d5909d9db8858c2ad1b4234d6f9b9be8da7efd --- browser/installer/package-manifest.in | 3 + intl/l10n/moz.build | 11 +++ intl/l10n/mozDOMLocalization.js | 9 +++ intl/l10n/mozDOMLocalization.manifest | 2 + intl/l10n/mozIDOMLocalization.idl | 27 +++++++ intl/l10n/test/chrome.ini | 4 ++ .../dom/test_mozdom_translateElements.html | 60 ++++++++++++++++ .../dom/test_mozdom_translateFragment.html | 61 ++++++++++++++++ .../test/dom/test_mozdom_translateRoots.html | 68 ++++++++++++++++++ intl/l10n/test/test_mozdomlocalization.js | 72 +++++++++++++++++++ intl/l10n/test/xpcshell.ini | 1 + 11 files changed, 318 insertions(+) create mode 100644 intl/l10n/mozDOMLocalization.js create mode 100644 intl/l10n/mozDOMLocalization.manifest create mode 100644 intl/l10n/mozIDOMLocalization.idl create mode 100644 intl/l10n/test/dom/test_mozdom_translateElements.html create mode 100644 intl/l10n/test/dom/test_mozdom_translateFragment.html create mode 100644 intl/l10n/test/dom/test_mozdom_translateRoots.html create mode 100644 intl/l10n/test/test_mozdomlocalization.js diff --git a/browser/installer/package-manifest.in b/browser/installer/package-manifest.in index fac3d8c30278..c52639cf45ff 100644 --- a/browser/installer/package-manifest.in +++ b/browser/installer/package-manifest.in @@ -364,6 +364,9 @@ @RESPATH@/browser/components/startupRecorder.js #endif +@RESPATH@/components/mozDOMLocalization.js +@RESPATH@/components/mozDOMLocalization.manifest + ; [Extensions] @RESPATH@/components/extensions-toolkit.manifest @RESPATH@/components/extension-process-script.js diff --git a/intl/l10n/moz.build b/intl/l10n/moz.build index 0f7c587746ca..61242173b636 100644 --- a/intl/l10n/moz.build +++ b/intl/l10n/moz.build @@ -11,6 +11,17 @@ EXTRA_JS_MODULES += [ 'MessageContext.jsm', ] +XPIDL_SOURCES += [ + 'mozIDOMLocalization.idl', +] + +XPIDL_MODULE = 'locale' + +EXTRA_COMPONENTS += [ + 'mozDOMLocalization.js', + 'mozDOMLocalization.manifest', +] + XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell.ini'] MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini'] diff --git a/intl/l10n/mozDOMLocalization.js b/intl/l10n/mozDOMLocalization.js new file mode 100644 index 000000000000..1f9772d15a03 --- /dev/null +++ b/intl/l10n/mozDOMLocalization.js @@ -0,0 +1,9 @@ +const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", {}); +const { DOMLocalization } = ChromeUtils.import("resource://gre/modules/DOMLocalization.jsm", {}); + +DOMLocalization.prototype.classID = + Components.ID("{29cc3895-8835-4c5b-b53a-0c0d1a458dee}"); +DOMLocalization.prototype.QueryInterface = + ChromeUtils.generateQI([Ci.mozIDOMLocalization, Ci.nsISupportsWeakReference]); + +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DOMLocalization]); diff --git a/intl/l10n/mozDOMLocalization.manifest b/intl/l10n/mozDOMLocalization.manifest new file mode 100644 index 000000000000..29c5c227a95e --- /dev/null +++ b/intl/l10n/mozDOMLocalization.manifest @@ -0,0 +1,2 @@ +component {29cc3895-8835-4c5b-b53a-0c0d1a458dee} mozDOMLocalization.js +contract @mozilla.org/intl/domlocalization;1 {29cc3895-8835-4c5b-b53a-0c0d1a458dee} diff --git a/intl/l10n/mozIDOMLocalization.idl b/intl/l10n/mozIDOMLocalization.idl new file mode 100644 index 000000000000..4310e1a268d1 --- /dev/null +++ b/intl/l10n/mozIDOMLocalization.idl @@ -0,0 +1,27 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "nsISupports.idl" + +webidl Document; +webidl Element; + +[scriptable, uuid(7c468500-541f-4fe0-98c9-92a53b63ec8d)] +interface mozIDOMLocalization : nsISupports +{ + unsigned long addResourceIds(in Array resourceIds); + unsigned long removeResourceIds(in Array resourceIds); + + Promise formatMessages(in Array aKeys); + Promise formatValues(in Array aKeys); + Promise formatValue(in DOMString aId, [optional] in jsval aArgs); + + Promise translateFragment(in Element aElement); + Promise translateElements(in Array aElements); + + void connectRoot(in Element aElement); + Promise translateRoots(); + readonly attribute Promise ready; +}; diff --git a/intl/l10n/test/chrome.ini b/intl/l10n/test/chrome.ini index 6ee9ec905574..2fb82fb27ab3 100644 --- a/intl/l10n/test/chrome.ini +++ b/intl/l10n/test/chrome.ini @@ -14,3 +14,7 @@ [dom/test_domloc_overlay_missing_children.html] [dom/test_domloc_overlay_sanitized.html] [dom/test_domloc.xul] + +[dom/test_mozdom_translateElements.html] +[dom/test_mozdom_translateFragment.html] +[dom/test_mozdom_translateRoots.html] diff --git a/intl/l10n/test/dom/test_mozdom_translateElements.html b/intl/l10n/test/dom/test_mozdom_translateElements.html new file mode 100644 index 000000000000..314ed1124506 --- /dev/null +++ b/intl/l10n/test/dom/test_mozdom_translateElements.html @@ -0,0 +1,60 @@ + + + + + Test mozIDOMLocalization.translateElements + + + + + +

+ + + diff --git a/intl/l10n/test/dom/test_mozdom_translateFragment.html b/intl/l10n/test/dom/test_mozdom_translateFragment.html new file mode 100644 index 000000000000..42dfcd063b49 --- /dev/null +++ b/intl/l10n/test/dom/test_mozdom_translateFragment.html @@ -0,0 +1,61 @@ + + + + + Test mozIDOMLocalization.translateFragment + + + + + +

+ + diff --git a/intl/l10n/test/dom/test_mozdom_translateRoots.html b/intl/l10n/test/dom/test_mozdom_translateRoots.html new file mode 100644 index 000000000000..3b5f4be51738 --- /dev/null +++ b/intl/l10n/test/dom/test_mozdom_translateRoots.html @@ -0,0 +1,68 @@ + + + + + Test mozIDOMLocalization.connectRoot and translateRoots + + + + + +
+

+
+
+

+
+ + diff --git a/intl/l10n/test/test_mozdomlocalization.js b/intl/l10n/test/test_mozdomlocalization.js new file mode 100644 index 000000000000..5786f4f3214a --- /dev/null +++ b/intl/l10n/test/test_mozdomlocalization.js @@ -0,0 +1,72 @@ +const domLocalization = + Cc["@mozilla.org/intl/domlocalization;1"].createInstance( + Ci.mozIDOMLocalization); + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm", {}); +const { L10nRegistry, FileSource } = + ChromeUtils.import("resource://gre/modules/L10nRegistry.jsm", {}); + +const fs = { + "/localization/en-US/browser/menu.ftl": "key = [en] Value", +}; +const originalLoad = L10nRegistry.load; +const originalRequested = Services.locale.getRequestedLocales(); + +L10nRegistry.load = async function(url) { + return fs.hasOwnProperty(url) ? fs[url] : false; +}; + +const source = new FileSource("test", ["en-US"], "/localization/{locale}"); +L10nRegistry.registerSource(source); + +add_task(function test_methods_presence() { + equal(typeof domLocalization.addResourceIds, "function"); + equal(typeof domLocalization.removeResourceIds, "function"); + + equal(typeof domLocalization.formatMessages, "function"); + equal(typeof domLocalization.formatValues, "function"); + equal(typeof domLocalization.formatValue, "function"); + + equal(typeof domLocalization.translateFragment, "function"); + equal(typeof domLocalization.translateElements, "function"); + + equal(typeof domLocalization.connectRoot, "function"); + equal(typeof domLocalization.translateRoots, "function"); + + equal(typeof domLocalization.ready, "object"); +}); + +add_task(function test_add_remove_resources() { + equal(domLocalization.addResourceIds(["./path1.ftl", "./path2.ftl"], 2), 2); + equal(domLocalization.removeResourceIds(["./path1.ftl", "./path2.ftl"], 2), 0); +}); + +add_task(async function test_format_messages() { + domLocalization.addResourceIds(["/browser/menu.ftl"], 1); + + let msgs = await domLocalization.formatMessages([{"id": "key"}], 1); + equal(msgs.length, 1); + equal(msgs[0].value, "[en] Value"); +}); + +add_task(async function test_format_values() { + let msgs = await domLocalization.formatValues([{"id": "key"}], 1); + equal(msgs.length, 1); + equal(msgs[0], "[en] Value"); +}); + +add_task(async function test_format_value() { + let msg = await domLocalization.formatValue("key"); + equal(msg, "[en] Value"); +}); + +add_task(async function test_ready() { + await domLocalization.ready; + equal(1, 1); +}); + +add_task(function cleanup() { + L10nRegistry.sources.clear(); + L10nRegistry.load = originalLoad; + Services.locale.setRequestedLocales(originalRequested); +}); diff --git a/intl/l10n/test/xpcshell.ini b/intl/l10n/test/xpcshell.ini index d896d6135149..123d1efeeb95 100644 --- a/intl/l10n/test/xpcshell.ini +++ b/intl/l10n/test/xpcshell.ini @@ -5,4 +5,5 @@ head = [test_l10nregistry.js] [test_localization.js] [test_messagecontext.js] +[test_mozdomlocalization.js] [test_pseudo.js]