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"
18 // XXX Avoid including this here by moving function bodies to the cpp file
27 class DOMLocalization
: public intl::Localization
{
29 NS_DECL_ISUPPORTS_INHERITED
30 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization
, Localization
)
32 static already_AddRefed
<DOMLocalization
> Create(
33 nsIGlobalObject
* aGlobal
, const bool aSync
,
34 const BundleGenerator
& aBundleGenerator
);
38 static already_AddRefed
<DOMLocalization
> Constructor(
39 const GlobalObject
& aGlobal
, const Sequence
<nsString
>& aResourceIds
,
40 const bool aSync
, const BundleGenerator
& aBundleGenerator
,
43 virtual JSObject
* WrapObject(JSContext
* aCx
,
44 JS::Handle
<JSObject
*> aGivenProto
) override
;
49 * Methods documentation in DOMLocalization.webidl
52 void ConnectRoot(nsINode
& aNode
, ErrorResult
& aRv
);
53 void DisconnectRoot(nsINode
& aNode
, ErrorResult
& aRv
);
55 void PauseObserving(ErrorResult
& aRv
);
56 void ResumeObserving(ErrorResult
& aRv
);
58 void SetAttributes(JSContext
* aCx
, Element
& aElement
, const nsAString
& aId
,
59 const Optional
<JS::Handle
<JSObject
*>>& aArgs
,
61 void GetAttributes(Element
& aElement
, L10nIdArgs
& aResult
, ErrorResult
& aRv
);
63 already_AddRefed
<Promise
> TranslateFragment(nsINode
& aNode
, ErrorResult
& aRv
);
65 already_AddRefed
<Promise
> TranslateElements(
66 const Sequence
<OwningNonNull
<Element
>>& aElements
, ErrorResult
& aRv
);
67 already_AddRefed
<Promise
> TranslateElements(
68 const Sequence
<OwningNonNull
<Element
>>& aElements
,
69 nsXULPrototypeDocument
* aProto
, ErrorResult
& aRv
);
71 already_AddRefed
<Promise
> TranslateRoots(ErrorResult
& aRv
);
78 * Accumulates all translatable elements (ones containing
79 * a `data-l10n-id` attribute) from under a node into
82 static void GetTranslatables(nsINode
& aNode
,
83 Sequence
<OwningNonNull
<Element
>>& aElements
,
87 * Sets the root information such as locale and direction.
89 static void SetRootInfo(Element
* aElement
);
92 * Applies l10n translations on translatable elements.
94 * If `aProto` gets passed, it'll be used to cache
95 * the localized elements.
97 * Result is `true` if all translations were applied
98 * successfully, and `false` otherwise.
100 bool ApplyTranslations(nsTArray
<nsCOMPtr
<Element
>>& aElements
,
101 nsTArray
<Nullable
<L10nMessage
>>& aTranslations
,
102 nsXULPrototypeDocument
* aProto
, ErrorResult
& aRv
);
104 bool SubtreeRootInRoots(nsINode
* aSubtreeRoot
) {
105 for (const auto* key
: mRoots
) {
106 nsINode
* subtreeRoot
= key
->SubtreeRoot();
107 if (subtreeRoot
== aSubtreeRoot
) {
115 explicit DOMLocalization(nsIGlobalObject
* aGlobal
, const bool aSync
,
116 const BundleGenerator
& aBundleGenerator
);
117 virtual ~DOMLocalization();
118 void OnChange() override
;
119 void DisconnectMutations();
120 void DisconnectRoots();
121 void ReportL10nOverlaysErrors(nsTArray
<L10nOverlaysError
>& aErrors
);
122 void ConvertStringToL10nArgs(const nsString
& aInput
, intl::L10nArgs
& aRetVal
,
125 RefPtr
<L10nMutations
> mMutations
;
126 nsTHashSet
<RefPtr
<nsINode
>> mRoots
;
130 } // namespace mozilla