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-25 22:39:53 +03:00
|
|
|
#ifndef mozilla_dom_l10n_DOMLocalization_h
|
|
|
|
#define mozilla_dom_l10n_DOMLocalization_h
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2021-03-23 13:36:37 +03:00
|
|
|
#include "nsTHashSet.h"
|
2019-07-26 23:17:21 +03:00
|
|
|
#include "nsXULPrototypeDocument.h"
|
2019-06-25 22:39:53 +03:00
|
|
|
#include "mozilla/intl/Localization.h"
|
2019-06-06 19:31:25 +03:00
|
|
|
#include "mozilla/dom/DOMLocalizationBinding.h"
|
2019-06-06 19:32:58 +03:00
|
|
|
#include "mozilla/dom/L10nMutations.h"
|
2019-06-25 22:39:53 +03:00
|
|
|
#include "mozilla/dom/L10nOverlaysBinding.h"
|
|
|
|
#include "mozilla/dom/LocalizationBinding.h"
|
2021-08-03 19:25:10 +03:00
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2021-08-03 19:25:13 +03:00
|
|
|
#include "mozilla/intl/L10nRegistry.h"
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2020-11-23 19:21:38 +03:00
|
|
|
// XXX Avoid including this here by moving function bodies to the cpp file
|
|
|
|
#include "nsINode.h"
|
|
|
|
|
2022-05-09 23:41:09 +03:00
|
|
|
namespace mozilla::dom {
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2020-11-23 19:08:40 +03:00
|
|
|
class Element;
|
2020-10-28 15:49:46 +03:00
|
|
|
class L10nMutations;
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
class DOMLocalization : public intl::Localization {
|
2019-06-06 19:31:25 +03:00
|
|
|
public:
|
2019-06-25 22:39:53 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization, Localization)
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2020-05-31 10:12:31 +03:00
|
|
|
void Destroy();
|
|
|
|
|
2019-06-06 19:31:25 +03:00
|
|
|
static already_AddRefed<DOMLocalization> Constructor(
|
2021-08-03 19:25:13 +03:00
|
|
|
const dom::GlobalObject& aGlobal,
|
2021-12-22 22:08:38 +03:00
|
|
|
const dom::Sequence<dom::OwningUTF8StringOrResourceId>& aResourceIds,
|
|
|
|
bool aIsSync,
|
2021-08-03 19:25:13 +03:00
|
|
|
const dom::Optional<dom::NonNull<intl::L10nRegistry>>& aRegistry,
|
|
|
|
const dom::Optional<dom::Sequence<nsCString>>& aLocales,
|
2019-06-06 19:32:41 +03:00
|
|
|
ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DOMLocalization API
|
|
|
|
*
|
|
|
|
* Methods documentation in DOMLocalization.webidl
|
|
|
|
*/
|
|
|
|
|
2019-06-26 20:04:43 +03:00
|
|
|
void ConnectRoot(nsINode& aNode, ErrorResult& aRv);
|
|
|
|
void DisconnectRoot(nsINode& aNode, ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
void PauseObserving(ErrorResult& aRv);
|
|
|
|
void ResumeObserving(ErrorResult& aRv);
|
|
|
|
|
|
|
|
void SetAttributes(JSContext* aCx, Element& aElement, const nsAString& aId,
|
|
|
|
const Optional<JS::Handle<JSObject*>>& aArgs,
|
|
|
|
ErrorResult& aRv);
|
2020-06-08 21:11:00 +03:00
|
|
|
void GetAttributes(Element& aElement, L10nIdArgs& aResult, ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise> TranslateFragment(nsINode& aNode, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<Promise> TranslateElements(
|
2022-05-02 18:12:30 +03:00
|
|
|
const nsTArray<OwningNonNull<Element>>& aElements, ErrorResult& aRv);
|
2019-07-26 23:17:21 +03:00
|
|
|
already_AddRefed<Promise> TranslateElements(
|
2022-05-02 18:12:30 +03:00
|
|
|
const nsTArray<OwningNonNull<Element>>& aElements,
|
2019-07-26 23:17:21 +03:00
|
|
|
nsXULPrototypeDocument* aProto, ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise> TranslateRoots(ErrorResult& aRv);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accumulates all translatable elements (ones containing
|
|
|
|
* a `data-l10n-id` attribute) from under a node into
|
|
|
|
* a list of elements.
|
|
|
|
*/
|
|
|
|
static void GetTranslatables(nsINode& aNode,
|
|
|
|
Sequence<OwningNonNull<Element>>& aElements,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the root information such as locale and direction.
|
|
|
|
*/
|
|
|
|
static void SetRootInfo(Element* aElement);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies l10n translations on translatable elements.
|
2019-07-26 23:17:21 +03:00
|
|
|
*
|
|
|
|
* If `aProto` gets passed, it'll be used to cache
|
|
|
|
* the localized elements.
|
2020-05-14 19:56:57 +03:00
|
|
|
*
|
|
|
|
* Result is `true` if all translations were applied
|
|
|
|
* successfully, and `false` otherwise.
|
2019-06-06 19:31:25 +03:00
|
|
|
*/
|
2020-05-14 19:56:57 +03:00
|
|
|
bool ApplyTranslations(nsTArray<nsCOMPtr<Element>>& aElements,
|
2020-04-11 02:35:03 +03:00
|
|
|
nsTArray<Nullable<L10nMessage>>& aTranslations,
|
2019-07-26 23:17:21 +03:00
|
|
|
nsXULPrototypeDocument* aProto, ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2020-03-31 22:57:21 +03:00
|
|
|
bool SubtreeRootInRoots(nsINode* aSubtreeRoot) {
|
2021-03-23 13:36:37 +03:00
|
|
|
for (const auto* key : mRoots) {
|
|
|
|
nsINode* subtreeRoot = key->SubtreeRoot();
|
2020-03-31 22:57:21 +03:00
|
|
|
if (subtreeRoot == aSubtreeRoot) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-03 19:25:13 +03:00
|
|
|
DOMLocalization(nsIGlobalObject* aGlobal, bool aSync);
|
|
|
|
DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
|
|
|
|
const intl::ffi::LocalizationRc* aRaw);
|
|
|
|
|
2019-06-06 19:31:25 +03:00
|
|
|
protected:
|
|
|
|
virtual ~DOMLocalization();
|
2019-09-13 13:08:26 +03:00
|
|
|
void OnChange() override;
|
2019-06-06 19:31:25 +03:00
|
|
|
void DisconnectMutations();
|
|
|
|
void DisconnectRoots();
|
2019-06-06 19:32:58 +03:00
|
|
|
void ReportL10nOverlaysErrors(nsTArray<L10nOverlaysError>& aErrors);
|
2020-05-19 23:48:21 +03:00
|
|
|
void ConvertStringToL10nArgs(const nsString& aInput, intl::L10nArgs& aRetVal,
|
|
|
|
ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2019-06-06 19:32:58 +03:00
|
|
|
RefPtr<L10nMutations> mMutations;
|
2021-03-23 13:36:37 +03:00
|
|
|
nsTHashSet<RefPtr<nsINode>> mRoots;
|
2019-06-06 19:31:25 +03:00
|
|
|
};
|
|
|
|
|
2022-05-09 23:41:09 +03:00
|
|
|
} // namespace mozilla::dom
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2019-07-05 20:05:57 +03:00
|
|
|
#endif
|