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 "nsXULPrototypeDocument.h"
11 #include "mozilla/intl/Localization.h"
12 #include "mozilla/dom/DOMLocalizationBinding.h"
13 #include "mozilla/dom/Element.h"
14 #include "mozilla/dom/L10nMutations.h"
15 #include "mozilla/dom/L10nOverlaysBinding.h"
16 #include "mozilla/dom/LocalizationBinding.h"
21 class DOMLocalization
: public intl::Localization
{
23 NS_DECL_ISUPPORTS_INHERITED
24 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization
, Localization
)
26 static already_AddRefed
<DOMLocalization
> Create(
27 nsIGlobalObject
* aGlobal
, const bool aSync
,
28 const BundleGenerator
& aBundleGenerator
);
32 static already_AddRefed
<DOMLocalization
> Constructor(
33 const GlobalObject
& aGlobal
, const Sequence
<nsString
>& aResourceIds
,
34 const bool aSync
, const BundleGenerator
& aBundleGenerator
,
37 virtual JSObject
* WrapObject(JSContext
* aCx
,
38 JS::Handle
<JSObject
*> aGivenProto
) override
;
43 * Methods documentation in DOMLocalization.webidl
46 void ConnectRoot(nsINode
& aNode
, ErrorResult
& aRv
);
47 void DisconnectRoot(nsINode
& aNode
, ErrorResult
& aRv
);
49 void PauseObserving(ErrorResult
& aRv
);
50 void ResumeObserving(ErrorResult
& aRv
);
52 void SetAttributes(JSContext
* aCx
, Element
& aElement
, const nsAString
& aId
,
53 const Optional
<JS::Handle
<JSObject
*>>& aArgs
,
55 void GetAttributes(Element
& aElement
, L10nIdArgs
& aResult
, ErrorResult
& aRv
);
57 already_AddRefed
<Promise
> TranslateFragment(nsINode
& aNode
, ErrorResult
& aRv
);
59 already_AddRefed
<Promise
> TranslateElements(
60 const Sequence
<OwningNonNull
<Element
>>& aElements
, ErrorResult
& aRv
);
61 already_AddRefed
<Promise
> TranslateElements(
62 const Sequence
<OwningNonNull
<Element
>>& aElements
,
63 nsXULPrototypeDocument
* aProto
, ErrorResult
& aRv
);
65 already_AddRefed
<Promise
> TranslateRoots(ErrorResult
& aRv
);
72 * Accumulates all translatable elements (ones containing
73 * a `data-l10n-id` attribute) from under a node into
76 static void GetTranslatables(nsINode
& aNode
,
77 Sequence
<OwningNonNull
<Element
>>& aElements
,
81 * Sets the root information such as locale and direction.
83 static void SetRootInfo(Element
* aElement
);
86 * Applies l10n translations on translatable elements.
88 * If `aProto` gets passed, it'll be used to cache
89 * the localized elements.
91 * Result is `true` if all translations were applied
92 * successfully, and `false` otherwise.
94 bool ApplyTranslations(nsTArray
<nsCOMPtr
<Element
>>& aElements
,
95 nsTArray
<Nullable
<L10nMessage
>>& aTranslations
,
96 nsXULPrototypeDocument
* aProto
, ErrorResult
& aRv
);
98 bool SubtreeRootInRoots(nsINode
* aSubtreeRoot
) {
99 for (auto iter
= mRoots
.Iter(); !iter
.Done(); iter
.Next()) {
100 nsINode
* subtreeRoot
= iter
.Get()->GetKey()->SubtreeRoot();
101 if (subtreeRoot
== aSubtreeRoot
) {
109 explicit DOMLocalization(nsIGlobalObject
* aGlobal
, const bool aSync
,
110 const BundleGenerator
& aBundleGenerator
);
111 virtual ~DOMLocalization();
112 void OnChange() override
;
113 void DisconnectMutations();
114 void DisconnectRoots();
115 void ReportL10nOverlaysErrors(nsTArray
<L10nOverlaysError
>& aErrors
);
116 void ConvertStringToL10nArgs(const nsString
& aInput
, intl::L10nArgs
& aRetVal
,
119 RefPtr
<L10nMutations
> mMutations
;
120 nsTHashtable
<nsRefPtrHashKey
<nsINode
>> mRoots
;
124 } // namespace mozilla