Bug 1890793: Assert CallArgs::newTarget is not gray. r=spidermonkey-reviewers,sfink...
[gecko.git] / dom / l10n / DOMLocalization.h
blobab66194d31153826ec49e12ccc857eda525533c1
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::dom {
25 class Element;
26 class L10nMutations;
28 class DOMLocalization : public intl::Localization {
29 public:
30 NS_DECL_ISUPPORTS_INHERITED
31 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization, Localization)
33 void Destroy();
35 static already_AddRefed<DOMLocalization> Constructor(
36 const dom::GlobalObject& aGlobal,
37 const dom::Sequence<dom::OwningUTF8StringOrResourceId>& aResourceIds,
38 bool aIsSync,
39 const dom::Optional<dom::NonNull<intl::L10nRegistry>>& aRegistry,
40 const dom::Optional<dom::Sequence<nsCString>>& aLocales,
41 ErrorResult& aRv);
43 JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override;
45 bool HasPendingMutations() const;
47 /**
48 * DOMLocalization API
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,
61 ErrorResult& aRv);
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);
77 /**
78 * Helper methods
81 /**
82 * Accumulates all translatable elements (ones containing
83 * a `data-l10n-id` attribute) from under a node into
84 * a list of elements.
86 static void GetTranslatables(nsINode& aNode,
87 Sequence<OwningNonNull<Element>>& aElements,
88 ErrorResult& aRv);
90 /**
91 * Sets the root information such as locale and direction.
93 static void SetRootInfo(Element* aElement);
95 /**
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) {
112 return true;
115 return false;
118 DOMLocalization(nsIGlobalObject* aGlobal, bool aSync);
119 DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
120 const intl::ffi::LocalizationRc* aRaw);
122 protected:
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,
129 ErrorResult& aRv);
131 RefPtr<L10nMutations> mMutations;
132 nsTHashSet<RefPtr<nsINode>> mRoots;
135 } // namespace mozilla::dom
137 #endif