2019-07-05 20:05:57 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2019-06-06 19:31:25 +03:00
|
|
|
#include "js/ForOfIterator.h" // JS::ForOfIterator
|
|
|
|
#include "js/JSON.h" // JS_ParseJSON
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsContentUtils.h"
|
2019-06-06 19:31:25 +03:00
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "DOMLocalization.h"
|
|
|
|
#include "mozilla/intl/LocaleService.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2019-06-06 19:32:58 +03:00
|
|
|
#include "mozilla/dom/L10nOverlays.h"
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::intl;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(DOMLocalization)
|
2019-06-25 22:39:53 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DOMLocalization, Localization)
|
2019-06-06 19:31:25 +03:00
|
|
|
tmp->DisconnectMutations();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMutations)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoots)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2019-06-25 22:39:53 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DOMLocalization, Localization)
|
2019-06-06 19:31:25 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMutations)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoots)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(DOMLocalization, Localization)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DOMLocalization, Localization)
|
2019-06-06 19:31:25 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMLocalization)
|
2019-06-25 22:39:53 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(Localization)
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2020-06-04 20:02:01 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<DOMLocalization> DOMLocalization::Create(
|
|
|
|
nsIGlobalObject* aGlobal, const bool aSync,
|
|
|
|
const BundleGenerator& aBundleGenerator) {
|
|
|
|
RefPtr<DOMLocalization> domLoc =
|
|
|
|
new DOMLocalization(aGlobal, aSync, aBundleGenerator);
|
|
|
|
|
|
|
|
domLoc->Init();
|
|
|
|
|
|
|
|
return domLoc.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, const bool aSync,
|
|
|
|
const BundleGenerator& aBundleGenerator)
|
|
|
|
: Localization(aGlobal, aSync, aBundleGenerator) {
|
2019-06-06 19:32:58 +03:00
|
|
|
mMutations = new L10nMutations(this);
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
2019-06-06 19:32:41 +03:00
|
|
|
|
2019-06-06 19:31:37 +03:00
|
|
|
already_AddRefed<DOMLocalization> DOMLocalization::Constructor(
|
2020-04-11 02:35:03 +03:00
|
|
|
const GlobalObject& aGlobal, const Sequence<nsString>& aResourceIds,
|
|
|
|
const bool aSync, const BundleGenerator& aBundleGenerator,
|
2019-06-06 19:32:41 +03:00
|
|
|
ErrorResult& aRv) {
|
2019-06-06 19:31:37 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-04 20:02:01 +03:00
|
|
|
RefPtr<DOMLocalization> domLoc =
|
|
|
|
DOMLocalization::Create(global, aSync, aBundleGenerator);
|
2019-06-06 19:32:41 +03:00
|
|
|
|
2020-04-30 21:22:38 +03:00
|
|
|
if (aResourceIds.Length()) {
|
|
|
|
domLoc->AddResourceIds(aResourceIds);
|
2019-06-06 19:31:37 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:02:01 +03:00
|
|
|
domLoc->Activate(true);
|
2020-04-30 21:22:38 +03:00
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
return domLoc.forget();
|
2019-06-06 19:31:37 +03:00
|
|
|
}
|
|
|
|
|
2019-06-06 19:31:25 +03:00
|
|
|
JSObject* DOMLocalization::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
return DOMLocalization_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:12:31 +03:00
|
|
|
void DOMLocalization::Destroy() { DisconnectMutations(); }
|
|
|
|
|
|
|
|
DOMLocalization::~DOMLocalization() { Destroy(); }
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DOMLocalization API
|
|
|
|
*/
|
|
|
|
|
2019-06-26 20:04:43 +03:00
|
|
|
void DOMLocalization::ConnectRoot(nsINode& aNode, ErrorResult& aRv) {
|
2019-06-06 19:31:25 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = aNode.GetOwnerGlobal();
|
|
|
|
if (!global) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(global == mGlobal,
|
|
|
|
"Cannot add a root that overlaps with existing root.");
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (auto iter = mRoots.ConstIter(); !iter.Done(); iter.Next()) {
|
2019-06-26 20:04:43 +03:00
|
|
|
nsINode* root = iter.Get()->GetKey();
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(
|
|
|
|
root != &aNode && !root->Contains(&aNode) && !aNode.Contains(root),
|
|
|
|
"Cannot add a root that overlaps with existing root.");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mRoots.PutEntry(&aNode);
|
|
|
|
|
|
|
|
aNode.AddMutationObserverUnlessExists(mMutations);
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:04:43 +03:00
|
|
|
void DOMLocalization::DisconnectRoot(nsINode& aNode, ErrorResult& aRv) {
|
2019-06-06 19:31:25 +03:00
|
|
|
if (mRoots.Contains(&aNode)) {
|
|
|
|
aNode.RemoveMutationObserver(mMutations);
|
|
|
|
mRoots.RemoveEntry(&aNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMLocalization::PauseObserving(ErrorResult& aRv) {
|
|
|
|
mMutations->PauseObserving();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMLocalization::ResumeObserving(ErrorResult& aRv) {
|
|
|
|
mMutations->ResumeObserving();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMLocalization::SetAttributes(
|
|
|
|
JSContext* aCx, Element& aElement, const nsAString& aId,
|
|
|
|
const Optional<JS::Handle<JSObject*>>& aArgs, ErrorResult& aRv) {
|
|
|
|
if (!aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::datal10nid, aId,
|
|
|
|
eCaseMatters)) {
|
|
|
|
aElement.SetAttr(kNameSpaceID_None, nsGkAtoms::datal10nid, aId, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aArgs.WasPassed() && aArgs.Value()) {
|
|
|
|
nsAutoString data;
|
|
|
|
JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*aArgs.Value()));
|
|
|
|
if (!nsContentUtils::StringifyJSON(aCx, &val, data)) {
|
2020-03-13 20:36:53 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2019-06-06 19:31:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::datal10nargs, data,
|
|
|
|
eCaseMatters)) {
|
|
|
|
aElement.SetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, data, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aElement.UnsetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:11:00 +03:00
|
|
|
void DOMLocalization::GetAttributes(Element& aElement, L10nIdArgs& aResult,
|
2020-05-19 23:48:21 +03:00
|
|
|
ErrorResult& aRv) {
|
2019-06-06 19:31:25 +03:00
|
|
|
nsAutoString l10nId;
|
|
|
|
nsAutoString l10nArgs;
|
|
|
|
|
|
|
|
if (aElement.GetAttr(kNameSpaceID_None, nsGkAtoms::datal10nid, l10nId)) {
|
2020-09-02 12:54:37 +03:00
|
|
|
CopyUTF16toUTF8(l10nId, aResult.mId);
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aElement.GetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, l10nArgs)) {
|
2020-05-19 23:48:21 +03:00
|
|
|
ConvertStringToL10nArgs(l10nArgs, aResult.mArgs.SetValue(), aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> DOMLocalization::TranslateFragment(nsINode& aNode,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
Sequence<OwningNonNull<Element>> elements;
|
|
|
|
|
|
|
|
GetTranslatables(aNode, elements, aRv);
|
|
|
|
|
|
|
|
return TranslateElements(elements, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Promise Handler used to apply the result of
|
|
|
|
* a call to Localization::FormatMessages onto the list
|
|
|
|
* of translatable elements.
|
|
|
|
*/
|
|
|
|
class ElementTranslationHandler : public PromiseNativeHandler {
|
|
|
|
public:
|
2019-07-26 23:17:21 +03:00
|
|
|
explicit ElementTranslationHandler(DOMLocalization* aDOMLocalization,
|
|
|
|
nsXULPrototypeDocument* aProto)
|
|
|
|
: mDOMLocalization(aDOMLocalization), mProto(aProto){};
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(ElementTranslationHandler)
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<Element>>& Elements() { return mElements; }
|
|
|
|
|
|
|
|
void SetReturnValuePromise(Promise* aReturnValuePromise) {
|
|
|
|
mReturnValuePromise = aReturnValuePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ResolvedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override {
|
|
|
|
ErrorResult rv;
|
|
|
|
|
2020-04-11 02:35:03 +03:00
|
|
|
nsTArray<Nullable<L10nMessage>> l10nData;
|
2019-06-06 19:31:25 +03:00
|
|
|
if (aValue.isObject()) {
|
|
|
|
JS::ForOfIterator iter(aCx);
|
|
|
|
if (!iter.init(aValue, JS::ForOfIterator::AllowNonIterable)) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!iter.valueIsIterable()) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> temp(aCx);
|
|
|
|
while (true) {
|
|
|
|
bool done;
|
|
|
|
if (!iter.next(&temp, &done)) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-11 02:35:03 +03:00
|
|
|
Nullable<L10nMessage>* slotPtr =
|
|
|
|
l10nData.AppendElement(mozilla::fallible);
|
2019-06-06 19:31:25 +03:00
|
|
|
if (!slotPtr) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-11 02:35:03 +03:00
|
|
|
if (!temp.isNull()) {
|
|
|
|
if (!slotPtr->SetValue().Init(aCx, temp)) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 19:56:57 +03:00
|
|
|
bool allTranslated =
|
|
|
|
mDOMLocalization->ApplyTranslations(mElements, l10nData, mProto, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed()) || !allTranslated) {
|
2019-06-06 19:31:25 +03:00
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReturnValuePromise->MaybeResolveWithUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void RejectedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override {
|
|
|
|
mReturnValuePromise->MaybeRejectWithClone(aCx, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ElementTranslationHandler() = default;
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<Element>> mElements;
|
|
|
|
RefPtr<DOMLocalization> mDOMLocalization;
|
|
|
|
RefPtr<Promise> mReturnValuePromise;
|
2019-07-26 23:17:21 +03:00
|
|
|
RefPtr<nsXULPrototypeDocument> mProto;
|
2019-06-06 19:31:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ElementTranslationHandler)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ElementTranslationHandler)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ElementTranslationHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ElementTranslationHandler)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ElementTranslationHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mElements)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDOMLocalization)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReturnValuePromise)
|
2019-07-26 23:17:21 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mProto)
|
2019-06-06 19:31:25 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ElementTranslationHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mElements)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDOMLocalization)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReturnValuePromise)
|
2019-07-26 23:17:21 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mProto)
|
2019-06-06 19:31:25 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
already_AddRefed<Promise> DOMLocalization::TranslateElements(
|
|
|
|
const Sequence<OwningNonNull<Element>>& aElements, ErrorResult& aRv) {
|
2019-07-26 23:17:21 +03:00
|
|
|
return TranslateElements(aElements, nullptr, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> DOMLocalization::TranslateElements(
|
|
|
|
const Sequence<OwningNonNull<Element>>& aElements,
|
|
|
|
nsXULPrototypeDocument* aProto, ErrorResult& aRv) {
|
2019-06-06 19:31:25 +03:00
|
|
|
JS::RootingContext* rcx = RootingCx();
|
2020-06-08 21:11:00 +03:00
|
|
|
Sequence<OwningUTF8StringOrL10nIdArgs> l10nKeys;
|
|
|
|
SequenceRooter<OwningUTF8StringOrL10nIdArgs> rooter(rcx, &l10nKeys);
|
2019-06-06 19:31:25 +03:00
|
|
|
RefPtr<ElementTranslationHandler> nativeHandler =
|
2019-07-26 23:17:21 +03:00
|
|
|
new ElementTranslationHandler(this, aProto);
|
2019-06-06 19:31:25 +03:00
|
|
|
nsTArray<nsCOMPtr<Element>>& domElements = nativeHandler->Elements();
|
|
|
|
domElements.SetCapacity(aElements.Length());
|
|
|
|
|
|
|
|
if (!mGlobal) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-25 22:43:50 +03:00
|
|
|
AutoEntryScript aes(mGlobal, "DOMLocalization TranslateElements");
|
2019-06-06 19:31:25 +03:00
|
|
|
JSContext* cx = aes.cx();
|
|
|
|
|
|
|
|
for (auto& domElement : aElements) {
|
|
|
|
if (!domElement->HasAttr(kNameSpaceID_None, nsGkAtoms::datal10nid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:11:00 +03:00
|
|
|
OwningUTF8StringOrL10nIdArgs* key = l10nKeys.AppendElement(fallible);
|
2019-06-06 19:31:25 +03:00
|
|
|
if (!key) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:11:00 +03:00
|
|
|
GetAttributes(*domElement, key->SetAsL10nIdArgs(), aRv);
|
2019-06-25 22:43:50 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2019-06-06 19:31:25 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!domElements.AppendElement(domElement, fallible)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:56:57 +03:00
|
|
|
if (mIsSync) {
|
2020-04-11 02:35:03 +03:00
|
|
|
nsTArray<Nullable<L10nMessage>> l10nMessages;
|
2019-07-01 20:56:57 +03:00
|
|
|
|
2020-04-11 02:35:03 +03:00
|
|
|
FormatMessagesSync(cx, l10nKeys, l10nMessages, aRv);
|
2019-07-01 20:56:57 +03:00
|
|
|
|
2020-05-14 19:56:57 +03:00
|
|
|
bool allTranslated =
|
|
|
|
ApplyTranslations(domElements, l10nMessages, aProto, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !allTranslated) {
|
2019-07-01 20:56:57 +03:00
|
|
|
promise->MaybeRejectWithUndefined();
|
|
|
|
return MaybeWrapPromise(promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
promise->MaybeResolveWithUndefined();
|
|
|
|
} else {
|
|
|
|
RefPtr<Promise> callbackResult = FormatMessages(cx, l10nKeys, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nativeHandler->SetReturnValuePromise(promise);
|
|
|
|
callbackResult->AppendNativeHandler(nativeHandler);
|
|
|
|
}
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
return MaybeWrapPromise(promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise handler used to set localization data on
|
|
|
|
* roots of elements that got successfully translated.
|
|
|
|
*/
|
|
|
|
class L10nRootTranslationHandler final : public PromiseNativeHandler {
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(L10nRootTranslationHandler)
|
|
|
|
|
|
|
|
explicit L10nRootTranslationHandler(Element* aRoot) : mRoot(aRoot) {}
|
|
|
|
|
|
|
|
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
|
|
|
|
DOMLocalization::SetRootInfo(mRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~L10nRootTranslationHandler() = default;
|
|
|
|
|
|
|
|
RefPtr<Element> mRoot;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(L10nRootTranslationHandler, mRoot)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(L10nRootTranslationHandler)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(L10nRootTranslationHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(L10nRootTranslationHandler)
|
|
|
|
|
|
|
|
already_AddRefed<Promise> DOMLocalization::TranslateRoots(ErrorResult& aRv) {
|
|
|
|
nsTArray<RefPtr<Promise>> promises;
|
|
|
|
|
|
|
|
for (auto iter = mRoots.ConstIter(); !iter.Done(); iter.Next()) {
|
2019-06-26 20:04:43 +03:00
|
|
|
nsINode* root = iter.Get()->GetKey();
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
RefPtr<Promise> promise = TranslateFragment(*root, aRv);
|
2019-06-26 20:04:43 +03:00
|
|
|
|
|
|
|
// If the root is an element, we'll add a native handler
|
|
|
|
// to set root info (language, direction etc.) on it
|
|
|
|
// once the localization finishes.
|
|
|
|
if (root->IsElement()) {
|
|
|
|
RefPtr<L10nRootTranslationHandler> nativeHandler =
|
|
|
|
new L10nRootTranslationHandler(root->AsElement());
|
|
|
|
promise->AppendNativeHandler(nativeHandler);
|
|
|
|
}
|
|
|
|
|
2019-06-06 19:31:25 +03:00
|
|
|
promises.AppendElement(promise);
|
|
|
|
}
|
|
|
|
AutoEntryScript aes(mGlobal, "DOMLocalization TranslateRoots");
|
|
|
|
return Promise::All(aes.cx(), promises, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void DOMLocalization::GetTranslatables(
|
|
|
|
nsINode& aNode, Sequence<OwningNonNull<Element>>& aElements,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
nsIContent* node =
|
|
|
|
aNode.IsContent() ? aNode.AsContent() : aNode.GetFirstChild();
|
|
|
|
for (; node; node = node->GetNextNode(&aNode)) {
|
|
|
|
if (!node->IsElement()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element* domElement = node->AsElement();
|
|
|
|
|
|
|
|
if (!domElement->HasAttr(kNameSpaceID_None, nsGkAtoms::datal10nid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aElements.AppendElement(*domElement, fallible)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void DOMLocalization::SetRootInfo(Element* aElement) {
|
|
|
|
nsAutoCString primaryLocale;
|
|
|
|
LocaleService::GetInstance()->GetAppLocaleAsBCP47(primaryLocale);
|
|
|
|
aElement->SetAttr(kNameSpaceID_None, nsGkAtoms::lang,
|
|
|
|
NS_ConvertUTF8toUTF16(primaryLocale), true);
|
|
|
|
|
|
|
|
nsAutoString dir;
|
|
|
|
if (LocaleService::GetInstance()->IsAppLocaleRTL()) {
|
|
|
|
nsGkAtoms::rtl->ToString(dir);
|
|
|
|
} else {
|
|
|
|
nsGkAtoms::ltr->ToString(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t nameSpace = aElement->GetNameSpaceID();
|
|
|
|
nsAtom* dirAtom =
|
|
|
|
nameSpace == kNameSpaceID_XUL ? nsGkAtoms::localedir : nsGkAtoms::dir;
|
|
|
|
|
|
|
|
aElement->SetAttr(kNameSpaceID_None, dirAtom, dir, true);
|
|
|
|
}
|
|
|
|
|
2020-05-14 19:56:57 +03:00
|
|
|
bool DOMLocalization::ApplyTranslations(
|
2020-04-11 02:35:03 +03:00
|
|
|
nsTArray<nsCOMPtr<Element>>& aElements,
|
|
|
|
nsTArray<Nullable<L10nMessage>>& aTranslations,
|
|
|
|
nsXULPrototypeDocument* aProto, ErrorResult& aRv) {
|
2019-06-06 19:31:25 +03:00
|
|
|
if (aElements.Length() != aTranslations.Length()) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2020-05-14 19:56:57 +03:00
|
|
|
return false;
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PauseObserving(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2020-05-14 19:56:57 +03:00
|
|
|
return false;
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
|
2020-05-14 19:56:57 +03:00
|
|
|
bool hasMissingTranslation = false;
|
|
|
|
|
2019-06-06 19:32:58 +03:00
|
|
|
nsTArray<L10nOverlaysError> errors;
|
2019-06-06 19:31:25 +03:00
|
|
|
for (size_t i = 0; i < aTranslations.Length(); ++i) {
|
|
|
|
Element* elem = aElements[i];
|
2020-04-11 02:35:03 +03:00
|
|
|
if (aTranslations[i].IsNull()) {
|
2020-05-14 19:56:57 +03:00
|
|
|
hasMissingTranslation = true;
|
2020-04-11 02:35:03 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-17 16:07:15 +03:00
|
|
|
// If we have a proto, we expect all elements are connected up.
|
|
|
|
// If they're not, they may have been removed by earlier translations.
|
|
|
|
// We will have added an error in L10nOverlays in this case.
|
|
|
|
// This is an error in fluent use, but shouldn't be crashing. There's
|
|
|
|
// also no point translating the element - skip it:
|
|
|
|
if (aProto && !elem->IsInComposedDoc()) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-11 02:35:03 +03:00
|
|
|
L10nOverlays::TranslateElement(*elem, aTranslations[i].Value(), errors,
|
|
|
|
aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2020-05-14 19:56:57 +03:00
|
|
|
hasMissingTranslation = true;
|
|
|
|
continue;
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
2019-07-26 23:17:21 +03:00
|
|
|
if (aProto) {
|
|
|
|
// We only need to rebuild deep if the translation has a value.
|
|
|
|
// Otherwise we'll only rebuild the attributes.
|
2020-04-11 02:35:03 +03:00
|
|
|
aProto->RebuildL10nPrototype(elem,
|
|
|
|
!aTranslations[i].Value().mValue.IsVoid());
|
2019-07-26 23:17:21 +03:00
|
|
|
}
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
|
2020-05-14 19:56:57 +03:00
|
|
|
ReportL10nOverlaysErrors(errors);
|
|
|
|
|
2019-06-06 19:31:25 +03:00
|
|
|
ResumeObserving(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2020-05-14 19:56:57 +03:00
|
|
|
return false;
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
|
2020-05-14 19:56:57 +03:00
|
|
|
return !hasMissingTranslation;
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Protected */
|
|
|
|
|
|
|
|
void DOMLocalization::OnChange() {
|
2019-06-25 22:39:53 +03:00
|
|
|
Localization::OnChange();
|
2020-05-31 10:12:38 +03:00
|
|
|
if (mLocalization && !mResourceIds.IsEmpty()) {
|
2019-06-06 19:31:25 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<Promise> promise = TranslateRoots(rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMLocalization::DisconnectMutations() {
|
|
|
|
if (mMutations) {
|
|
|
|
mMutations->Disconnect();
|
|
|
|
DisconnectRoots();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMLocalization::DisconnectRoots() {
|
|
|
|
for (auto iter = mRoots.ConstIter(); !iter.Done(); iter.Next()) {
|
2019-06-26 20:04:43 +03:00
|
|
|
nsINode* node = iter.Get()->GetKey();
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2019-06-26 20:04:43 +03:00
|
|
|
node->RemoveMutationObserver(mMutations);
|
2019-06-06 19:31:25 +03:00
|
|
|
}
|
|
|
|
mRoots.Clear();
|
|
|
|
}
|
|
|
|
|
2019-06-06 19:32:58 +03:00
|
|
|
void DOMLocalization::ReportL10nOverlaysErrors(
|
|
|
|
nsTArray<L10nOverlaysError>& aErrors) {
|
2019-06-06 19:31:25 +03:00
|
|
|
nsAutoString msg;
|
|
|
|
|
|
|
|
for (auto& error : aErrors) {
|
|
|
|
if (error.mCode.WasPassed()) {
|
|
|
|
msg = u"[fluent-dom] "_ns;
|
|
|
|
switch (error.mCode.Value()) {
|
2019-06-06 19:32:58 +03:00
|
|
|
case L10nOverlays_Binding::ERROR_FORBIDDEN_TYPE:
|
2019-06-06 19:31:25 +03:00
|
|
|
msg += u"An element of forbidden type \""_ns +
|
|
|
|
error.mTranslatedElementName.Value() +
|
|
|
|
nsLiteralString(
|
|
|
|
u"\" was found in the translation. Only safe text-level "
|
|
|
|
"elements and elements with data-l10n-name are allowed.");
|
|
|
|
break;
|
2019-06-06 19:32:58 +03:00
|
|
|
case L10nOverlays_Binding::ERROR_NAMED_ELEMENT_MISSING:
|
2019-06-06 19:31:25 +03:00
|
|
|
msg += u"An element named \""_ns + error.mL10nName.Value() +
|
|
|
|
u"\" wasn't found in the source."_ns;
|
|
|
|
break;
|
2019-06-06 19:32:58 +03:00
|
|
|
case L10nOverlays_Binding::ERROR_NAMED_ELEMENT_TYPE_MISMATCH:
|
2019-06-06 19:31:25 +03:00
|
|
|
msg += u"An element named \""_ns + error.mL10nName.Value() +
|
|
|
|
nsLiteralString(
|
|
|
|
u"\" was found in the translation but its type ") +
|
|
|
|
error.mTranslatedElementName.Value() +
|
|
|
|
nsLiteralString(
|
|
|
|
u" didn't match the element found in the source ") +
|
|
|
|
error.mSourceElementName.Value() + u"."_ns;
|
|
|
|
break;
|
2020-06-17 02:54:51 +03:00
|
|
|
case L10nOverlays_Binding::ERROR_TRANSLATED_ELEMENT_DISCONNECTED:
|
|
|
|
msg += u"The element using message \""_ns + error.mL10nName.Value() +
|
|
|
|
nsLiteralString(
|
|
|
|
u"\" was removed from the DOM when translating its \"") +
|
|
|
|
error.mTranslatedElementName.Value() + u"\" parent."_ns;
|
|
|
|
break;
|
|
|
|
case L10nOverlays_Binding::ERROR_TRANSLATED_ELEMENT_DISALLOWED_DOM:
|
|
|
|
msg += nsLiteralString(
|
|
|
|
u"While translating an element with fluent ID \"") +
|
|
|
|
error.mL10nName.Value() + u"\" a child element of type \""_ns +
|
|
|
|
error.mTranslatedElementName.Value() +
|
|
|
|
nsLiteralString(
|
|
|
|
u"\" was removed. Either the fluent message "
|
|
|
|
"does not contain markup, or it does not contain markup "
|
|
|
|
"of this type.");
|
|
|
|
break;
|
2019-06-06 19:32:58 +03:00
|
|
|
case L10nOverlays_Binding::ERROR_UNKNOWN:
|
2019-06-06 19:31:25 +03:00
|
|
|
default:
|
|
|
|
msg += nsLiteralString(
|
2020-06-17 02:54:51 +03:00
|
|
|
u"Unknown error happened while translating an element.");
|
2019-06-06 19:31:25 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsPIDOMWindowInner* innerWindow = GetParentObject()->AsInnerWindow();
|
|
|
|
Document* doc = innerWindow ? innerWindow->GetExtantDoc() : nullptr;
|
|
|
|
if (doc) {
|
|
|
|
nsContentUtils::ReportToConsoleNonLocalized(
|
|
|
|
msg, nsIScriptError::warningFlag, "DOM"_ns, doc);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to report l10n DOM Overlay errors to console.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 22:43:50 +03:00
|
|
|
|
2020-05-19 23:48:21 +03:00
|
|
|
void DOMLocalization::ConvertStringToL10nArgs(const nsString& aInput,
|
2019-06-25 22:43:50 +03:00
|
|
|
intl::L10nArgs& aRetVal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
// This method uses a temporary dictionary to automate
|
|
|
|
// converting a JSON string into an IDL Record via a dictionary.
|
|
|
|
//
|
|
|
|
// Once we get Record::Init(const nsAString& aJSON), we'll switch to
|
|
|
|
// that.
|
|
|
|
L10nArgsHelperDict helperDict;
|
|
|
|
if (!helperDict.Init(u"{\"args\": "_ns + aInput + u"}"_ns)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto& entry : helperDict.mArgs.Entries()) {
|
|
|
|
L10nArgs::EntryType* newEntry = aRetVal.Entries().AppendElement(fallible);
|
|
|
|
if (!newEntry) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newEntry->mKey = entry.mKey;
|
|
|
|
newEntry->mValue = entry.mValue;
|
|
|
|
}
|
2019-07-05 20:05:57 +03:00
|
|
|
}
|