Skip to content

Commit

Permalink
Bug 1613705 - [localization] part9: Update DOMLocalization to match L…
Browse files Browse the repository at this point in the history
…ocalization constructor. r=nika,emilio

Depends on D113691

Differential Revision: https://phabricator.services.mozilla.com/D113692
  • Loading branch information
Zibi Braniecki committed Aug 2, 2021
1 parent c5240d3 commit 458a249
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 211 deletions.
5 changes: 3 additions & 2 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4171,7 +4171,7 @@ void Document::LocalizationLinkAdded(Element* aLinkElement) {
mDocumentL10n = DocumentL10n::Create(this, isSync);
MOZ_ASSERT(mDocumentL10n);
}
mDocumentL10n->AddResourceId(href);
mDocumentL10n->AddResourceId(NS_ConvertUTF16toUTF8(href));

if (mReadyState >= READYSTATE_INTERACTIVE) {
mDocumentL10n->TriggerInitialTranslation();
Expand All @@ -4194,7 +4194,8 @@ void Document::LocalizationLinkRemoved(Element* aLinkElement) {
if (mDocumentL10n) {
nsAutoString href;
aLinkElement->GetAttr(kNameSpaceID_None, nsGkAtoms::href, href);
uint32_t remaining = mDocumentL10n->RemoveResourceId(href);
uint32_t remaining =
mDocumentL10n->RemoveResourceId(NS_ConvertUTF16toUTF8(href));
if (remaining == 0) {
if (mDocumentL10n->mBlockingLayout) {
mDocumentL10n->mBlockingLayout = false;
Expand Down
59 changes: 36 additions & 23 deletions dom/l10n/DOMLocalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,53 @@ NS_IMPL_RELEASE_INHERITED(DOMLocalization, Localization)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMLocalization)
NS_INTERFACE_MAP_END_INHERITING(Localization)

/* static */
already_AddRefed<DOMLocalization> DOMLocalization::Create(
nsIGlobalObject* aGlobal, const bool aSync,
const BundleGenerator& aBundleGenerator) {
RefPtr<DOMLocalization> domLoc =
new DOMLocalization(aGlobal, aSync, aBundleGenerator);

return domLoc.forget();
DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, bool aSync)
: Localization(aGlobal, aSync) {
mMutations = new L10nMutations(this);
}

DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, const bool aSync,
const BundleGenerator& aBundleGenerator)
: Localization(aGlobal, aSync) {
DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
const ffi::LocalizationRc* aRaw)
: Localization(aGlobal, aIsSync, aRaw) {
mMutations = new L10nMutations(this);
}

already_AddRefed<DOMLocalization> DOMLocalization::Constructor(
const GlobalObject& aGlobal, const Sequence<nsString>& aResourceIds,
const bool aSync, const BundleGenerator& aBundleGenerator,
ErrorResult& aRv) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
if (!global) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
const GlobalObject& aGlobal, const Sequence<nsCString>& aResourceIds,
bool aIsSync, const Optional<NonNull<L10nRegistry>>& aRegistry,
const Optional<Sequence<nsCString>>& aLocales, ErrorResult& aRv) {
nsTArray<nsCString> resIds = ToTArray<nsTArray<nsCString>>(aResourceIds);
Maybe<nsTArray<nsCString>> locales;

if (aLocales.WasPassed()) {
locales.emplace();
locales->SetCapacity(aLocales.Value().Length());
for (const auto& locale : aLocales.Value()) {
locales->AppendElement(locale);
}
}

RefPtr<DOMLocalization> domLoc =
DOMLocalization::Create(global, aSync, aBundleGenerator);
RefPtr<const ffi::LocalizationRc> raw;
bool result;

if (aResourceIds.Length()) {
domLoc->AddResourceIds(aResourceIds);
if (aRegistry.WasPassed()) {
result = ffi::localization_new_with_locales(
&resIds, aIsSync, aRegistry.Value().Raw(), locales.ptrOr(nullptr),
getter_AddRefs(raw));
} else {
result = ffi::localization_new_with_locales(
&resIds, aIsSync, nullptr, locales.ptrOr(nullptr), getter_AddRefs(raw));
}

return domLoc.forget();
if (result) {
nsCOMPtr<nsIGlobalObject> global =
do_QueryInterface(aGlobal.GetAsSupports());

return do_AddRef(new DOMLocalization(global, aIsSync, raw));
}
aRv.ThrowInvalidStateError(
"Failed to create the Localization. Check the locales arguments.");
return nullptr;
}

JSObject* DOMLocalization::WrapObject(JSContext* aCx,
Expand Down
17 changes: 9 additions & 8 deletions dom/l10n/DOMLocalization.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "mozilla/dom/L10nOverlaysBinding.h"
#include "mozilla/dom/LocalizationBinding.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/intl/L10nRegistry.h"

// XXX Avoid including this here by moving function bodies to the cpp file
#include "nsINode.h"
Expand All @@ -30,15 +31,13 @@ class DOMLocalization : public intl::Localization {
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization, Localization)

static already_AddRefed<DOMLocalization> Create(
nsIGlobalObject* aGlobal, const bool aSync,
const BundleGenerator& aBundleGenerator);

void Destroy();

static already_AddRefed<DOMLocalization> Constructor(
const GlobalObject& aGlobal, const Sequence<nsString>& aResourceIds,
const bool aSync, const BundleGenerator& aBundleGenerator,
const dom::GlobalObject& aGlobal,
const dom::Sequence<nsCString>& aResourceIds, bool aIsSync,
const dom::Optional<dom::NonNull<intl::L10nRegistry>>& aRegistry,
const dom::Optional<dom::Sequence<nsCString>>& aLocales,
ErrorResult& aRv);

virtual JSObject* WrapObject(JSContext* aCx,
Expand Down Expand Up @@ -112,9 +111,11 @@ class DOMLocalization : public intl::Localization {
return false;
}

DOMLocalization(nsIGlobalObject* aGlobal, bool aSync);
DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
const intl::ffi::LocalizationRc* aRaw);

protected:
explicit DOMLocalization(nsIGlobalObject* aGlobal, const bool aSync,
const BundleGenerator& aBundleGenerator);
virtual ~DOMLocalization();
void OnChange() override;
void DisconnectMutations();
Expand Down
2 changes: 1 addition & 1 deletion dom/l10n/DocumentL10n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ RefPtr<DocumentL10n> DocumentL10n::Create(Document* aDocument, bool aSync) {
}

DocumentL10n::DocumentL10n(Document* aDocument, bool aSync)
: DOMLocalization(aDocument->GetScopeObject(), aSync, {}),
: DOMLocalization(aDocument->GetScopeObject(), aSync),
mDocument(aDocument),
mState(DocumentL10nState::Constructed) {
mContentSink = do_QueryInterface(aDocument->GetCurrentContentSink());
Expand Down
18 changes: 10 additions & 8 deletions dom/l10n/tests/mochitest/dom_localization/test_attr_sanitized.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
const l10nReg = new L10nRegistry();
const fs = [
{ path: "/localization/en-US/mock.ftl", source: `
key1 = Value for Key 1
key2 = Value for <a>Key 2<a/>.
`));
yield bundle;
}
` },
];
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);

SimpleTest.waitForExplicitFinish();
addLoadEvent(async () => {
const domLoc = new DOMLocalization(
[],
["/mock.ftl"],
false,
{ generateBundles },
l10nReg,
["en-US"],
);

await domLoc.translateFragment(document.body);
Expand Down
18 changes: 10 additions & 8 deletions dom/l10n/tests/mochitest/dom_localization/test_connectRoot.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
const l10nReg = new L10nRegistry();
const fs = [
{ path: "/localization/en-US/mock.ftl", source: `
key1 = Value for Key 1
`));
yield bundle;
}
` },
];
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);

window.onload = async function() {
SimpleTest.waitForExplicitFinish();

const p1 = document.getElementById("p1");

const domLoc = new DOMLocalization(
[],
["/mock.ftl"],
false,
{ generateBundles },
l10nReg,
["en-US"],
);

await domLoc.translateRoots();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,20 @@
</script>
<script type="application/javascript">
"use strict";
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
const l10nReg = new L10nRegistry();
const fs = [
{ path: "/localization/en-US/mock.ftl", source: `
key1 = Value for Key 1
`));
yield bundle;
}
` },
];
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);

document.domLoc = new DOMLocalization(
[],
["/mock.ftl"],
false,
{ generateBundles }
l10nReg,
["en-US"],
);
</script>
</head>
Expand Down
18 changes: 10 additions & 8 deletions dom/l10n/tests/mochitest/dom_localization/test_disconnectRoot.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
const l10nReg = new L10nRegistry();
const fs = [
{ path: "/localization/en-US/mock.ftl", source: `
key1 = Value for Key 1
key2 = Value for Key 2
`));
yield bundle;
}
` },
];
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);

window.onload = async function() {
SimpleTest.waitForExplicitFinish();

const p1 = document.getElementById("p1");

const domLoc = new DOMLocalization(
[],
["/mock.ftl"],
false,
{ generateBundles },
l10nReg,
["en-US"],
);

await domLoc.translateRoots();
Expand Down
18 changes: 10 additions & 8 deletions dom/l10n/tests/mochitest/dom_localization/test_domloc.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,33 @@
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
<script type="application/javascript">
<![CDATA[
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
const l10nReg = new L10nRegistry();
const fs = [
{ path: "/localization/en-US/mock.ftl", source: `
file-menu =
.label = File
.accesskey = F
new-tab =
.label = New Tab
.accesskey = N
container = Some text with an <image data-l10n-name="foo"> inside it.
`));
yield bundle;
}
` },
];
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);

SimpleTest.waitForExplicitFinish();


const domLoc = new DOMLocalization(
[],
false,
{ generateBundles },
l10nReg,
["en-US"],
);

async function foo() {
domLoc.addResourceIds(["dummy.ftl"]);
domLoc.addResourceIds(["/mock.ftl"]);
domLoc.connectRoot(document.documentElement);
await domLoc.translateRoots();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
<script type="application/javascript">
"use strict";

async function* generateBundles(resourceIds) {}

window.onload = function() {
SimpleTest.waitForExplicitFinish();

const domLoc = new DOMLocalization(
[],
false,
{ generateBundles },
);

const p1 = document.querySelectorAll("p")[0];
Expand Down
20 changes: 12 additions & 8 deletions dom/l10n/tests/mochitest/dom_localization/test_l10n_mutations.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource("title = Hello World"));
bundle.addResource(new FluentResource("title2 = Hello Another World"));
yield bundle;
}
const l10nReg = new L10nRegistry();
const fs = [
{ path: "/localization/en-US/mock.ftl", source: `
title = Hello World
title2 = Hello Another World
` },
];
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);

window.onload = async function() {
SimpleTest.waitForExplicitFinish();

const domLoc = new DOMLocalization(
[],
false,
{ generateBundles },
l10nReg,
["en-US"],
);

const h1 = document.querySelectorAll("h1")[0];

domLoc.addResourceIds(["dummy.ftl"]);
domLoc.addResourceIds(["/mock.ftl"]);
domLoc.connectRoot(document.body);

await domLoc.translateRoots();
Expand Down
Loading

0 comments on commit 458a249

Please sign in to comment.