Bug 1733673 [wpt PR 31066] - Annotate CSS Transforms WPT reftests as fuzzy where...
[gecko.git] / dom / l10n / DOMLocalization.h
blob5ebeb4f41f68486c00164e3ec551e71e4cdbb6ec
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
21 #include "nsINode.h"
23 namespace mozilla {
24 namespace dom {
26 class Element;
27 class L10nMutations;
29 class DOMLocalization : public intl::Localization {
30 public:
31 NS_DECL_ISUPPORTS_INHERITED
32 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization, Localization)
34 void Destroy();
36 static already_AddRefed<DOMLocalization> Constructor(
37 const dom::GlobalObject& aGlobal,
38 const dom::Sequence<nsCString>& aResourceIds, bool aIsSync,
39 const dom::Optional<dom::NonNull<intl::L10nRegistry>>& aRegistry,
40 const dom::Optional<dom::Sequence<nsCString>>& aLocales,
41 ErrorResult& aRv);
43 virtual JSObject* WrapObject(JSContext* aCx,
44 JS::Handle<JSObject*> aGivenProto) override;
46 /**
47 * DOMLocalization API
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,
60 ErrorResult& aRv);
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);
73 /**
74 * Helper methods
77 /**
78 * Accumulates all translatable elements (ones containing
79 * a `data-l10n-id` attribute) from under a node into
80 * a list of elements.
82 static void GetTranslatables(nsINode& aNode,
83 Sequence<OwningNonNull<Element>>& aElements,
84 ErrorResult& aRv);
86 /**
87 * Sets the root information such as locale and direction.
89 static void SetRootInfo(Element* aElement);
91 /**
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) {
108 return true;
111 return false;
114 DOMLocalization(nsIGlobalObject* aGlobal, bool aSync);
115 DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
116 const intl::ffi::LocalizationRc* aRaw);
118 protected:
119 virtual ~DOMLocalization();
120 void OnChange() override;
121 void DisconnectMutations();
122 void DisconnectRoots();
123 void ReportL10nOverlaysErrors(nsTArray<L10nOverlaysError>& aErrors);
124 void ConvertStringToL10nArgs(const nsString& aInput, intl::L10nArgs& aRetVal,
125 ErrorResult& aRv);
127 RefPtr<L10nMutations> mMutations;
128 nsTHashSet<RefPtr<nsINode>> mRoots;
131 } // namespace dom
132 } // namespace mozilla
134 #endif