1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_l10n_DOMLocalization_h
8 #define mozilla_dom_l10n_DOMLocalization_h
10 #include "nsTHashSet.h"
11 #include "nsXULPrototypeDocument.h"
12 #include "mozilla/intl/Localization.h"
13 #include "mozilla/dom/DOMLocalizationBinding.h"
14 #include "mozilla/dom/L10nMutations.h"
15 #include "mozilla/dom/L10nOverlaysBinding.h"
16 #include "mozilla/dom/LocalizationBinding.h"
17 #include "mozilla/dom/PromiseNativeHandler.h"
18 #include "mozilla/intl/L10nRegistry.h"
20 // XXX Avoid including this here by moving function bodies to the cpp file
23 namespace mozilla::dom
{
28 class DOMLocalization
: public intl::Localization
{
30 NS_DECL_ISUPPORTS_INHERITED
31 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization
, Localization
)
35 static already_AddRefed
<DOMLocalization
> Constructor(
36 const dom::GlobalObject
& aGlobal
,
37 const dom::Sequence
<dom::OwningUTF8StringOrResourceId
>& aResourceIds
,
39 const dom::Optional
<dom::NonNull
<intl::L10nRegistry
>>& aRegistry
,
40 const dom::Optional
<dom::Sequence
<nsCString
>>& aLocales
,
43 JSObject
* WrapObject(JSContext
*, JS::Handle
<JSObject
*> aGivenProto
) override
;
45 bool HasPendingMutations() const;
50 * Methods documentation in DOMLocalization.webidl
53 void ConnectRoot(nsINode
& aNode
);
54 void DisconnectRoot(nsINode
& aNode
);
56 void PauseObserving();
57 void ResumeObserving();
59 void SetAttributes(JSContext
* aCx
, Element
& aElement
, const nsAString
& aId
,
60 const Optional
<JS::Handle
<JSObject
*>>& aArgs
,
62 void GetAttributes(Element
& aElement
, L10nIdArgs
& aResult
, ErrorResult
& aRv
);
64 void SetArgs(JSContext
* aCx
, Element
& aElement
,
65 const Optional
<JS::Handle
<JSObject
*>>& aArgs
, ErrorResult
& aRv
);
67 already_AddRefed
<Promise
> TranslateFragment(nsINode
& aNode
, ErrorResult
& aRv
);
69 already_AddRefed
<Promise
> TranslateElements(
70 const nsTArray
<OwningNonNull
<Element
>>& aElements
, ErrorResult
& aRv
);
71 already_AddRefed
<Promise
> TranslateElements(
72 const nsTArray
<OwningNonNull
<Element
>>& aElements
,
73 nsXULPrototypeDocument
* aProto
, ErrorResult
& aRv
);
75 already_AddRefed
<Promise
> TranslateRoots(ErrorResult
& aRv
);
82 * Accumulates all translatable elements (ones containing
83 * a `data-l10n-id` attribute) from under a node into
86 static void GetTranslatables(nsINode
& aNode
,
87 Sequence
<OwningNonNull
<Element
>>& aElements
,
91 * Sets the root information such as locale and direction.
93 static void SetRootInfo(Element
* aElement
);
96 * Applies l10n translations on translatable elements.
98 * If `aProto` gets passed, it'll be used to cache
99 * the localized elements.
101 * Result is `true` if all translations were applied
102 * successfully, and `false` otherwise.
104 bool ApplyTranslations(nsTArray
<nsCOMPtr
<Element
>>& aElements
,
105 nsTArray
<Nullable
<L10nMessage
>>& aTranslations
,
106 nsXULPrototypeDocument
* aProto
, ErrorResult
& aRv
);
108 bool SubtreeRootInRoots(nsINode
* aSubtreeRoot
) {
109 for (const auto* key
: mRoots
) {
110 nsINode
* subtreeRoot
= key
->SubtreeRoot();
111 if (subtreeRoot
== aSubtreeRoot
) {
118 DOMLocalization(nsIGlobalObject
* aGlobal
, bool aSync
);
119 DOMLocalization(nsIGlobalObject
* aGlobal
, bool aIsSync
,
120 const intl::ffi::LocalizationRc
* aRaw
);
123 virtual ~DOMLocalization();
124 void OnChange() override
;
125 void DisconnectMutations();
126 void DisconnectRoots();
127 void ReportL10nOverlaysErrors(nsTArray
<L10nOverlaysError
>& aErrors
);
128 void ConvertStringToL10nArgs(const nsString
& aInput
, intl::L10nArgs
& aRetVal
,
131 RefPtr
<L10nMutations
> mMutations
;
132 nsTHashSet
<RefPtr
<nsINode
>> mRoots
;
135 } // namespace mozilla::dom