Bug 1660051 [wpt PR 25111] - Origin isolation: expand getter test coverage, a=testonly
[gecko.git] / dom / l10n / DOMLocalization.h
blob044e664fe445c3fb5baed66c01811046ca787aab
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"
18 namespace mozilla {
19 namespace dom {
21 class DOMLocalization : public intl::Localization {
22 public:
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);
30 void Destroy();
32 static already_AddRefed<DOMLocalization> Constructor(
33 const GlobalObject& aGlobal, const Sequence<nsString>& aResourceIds,
34 const bool aSync, const BundleGenerator& aBundleGenerator,
35 ErrorResult& aRv);
37 virtual JSObject* WrapObject(JSContext* aCx,
38 JS::Handle<JSObject*> aGivenProto) override;
40 /**
41 * DOMLocalization API
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,
54 ErrorResult& aRv);
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);
67 /**
68 * Helper methods
71 /**
72 * Accumulates all translatable elements (ones containing
73 * a `data-l10n-id` attribute) from under a node into
74 * a list of elements.
76 static void GetTranslatables(nsINode& aNode,
77 Sequence<OwningNonNull<Element>>& aElements,
78 ErrorResult& aRv);
80 /**
81 * Sets the root information such as locale and direction.
83 static void SetRootInfo(Element* aElement);
85 /**
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) {
102 return true;
105 return false;
108 protected:
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,
117 ErrorResult& aRv);
119 RefPtr<L10nMutations> mMutations;
120 nsTHashtable<nsRefPtrHashKey<nsINode>> mRoots;
123 } // namespace dom
124 } // namespace mozilla
126 #endif