summaryrefslogtreecommitdiffstats
path: root/dom/l10n/DOMLocalization.h
blob: ab66194d31153826ec49e12ccc857eda525533c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_l10n_DOMLocalization_h
#define mozilla_dom_l10n_DOMLocalization_h

#include "nsTHashSet.h"
#include "nsXULPrototypeDocument.h"
#include "mozilla/intl/Localization.h"
#include "mozilla/dom/DOMLocalizationBinding.h"
#include "mozilla/dom/L10nMutations.h"
#include "mozilla/dom/L10nOverlaysBinding.h"
#include "mozilla/dom/LocalizationBinding.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/intl/L10nRegistry.h"

// XXX Avoid including this here by moving function bodies to the cpp file
#include "nsINode.h"

namespace mozilla::dom {

class Element;
class L10nMutations;

class DOMLocalization : public intl::Localization {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization, Localization)

  void Destroy();

  static already_AddRefed<DOMLocalization> Constructor(
      const dom::GlobalObject& aGlobal,
      const dom::Sequence<dom::OwningUTF8StringOrResourceId>& aResourceIds,
      bool aIsSync,
      const dom::Optional<dom::NonNull<intl::L10nRegistry>>& aRegistry,
      const dom::Optional<dom::Sequence<nsCString>>& aLocales,
      ErrorResult& aRv);

  JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override;

  bool HasPendingMutations() const;

  /**
   * DOMLocalization API
   *
   * Methods documentation in DOMLocalization.webidl
   */

  void ConnectRoot(nsINode& aNode);
  void DisconnectRoot(nsINode& aNode);

  void PauseObserving();
  void ResumeObserving();

  void SetAttributes(JSContext* aCx, Element& aElement, const nsAString& aId,
                     const Optional<JS::Handle<JSObject*>>& aArgs,
                     ErrorResult& aRv);
  void GetAttributes(Element& aElement, L10nIdArgs& aResult, ErrorResult& aRv);

  void SetArgs(JSContext* aCx, Element& aElement,
               const Optional<JS::Handle<JSObject*>>& aArgs, ErrorResult& aRv);

  already_AddRefed<Promise> TranslateFragment(nsINode& aNode, ErrorResult& aRv);

  already_AddRefed<Promise> TranslateElements(
      const nsTArray<OwningNonNull<Element>>& aElements, ErrorResult& aRv);
  already_AddRefed<Promise> TranslateElements(
      const nsTArray<OwningNonNull<Element>>& aElements,
      nsXULPrototypeDocument* aProto, ErrorResult& aRv);

  already_AddRefed<Promise> TranslateRoots(ErrorResult& aRv);

  /**
   * Helper methods
   */

  /**
   * Accumulates all translatable elements (ones containing
   * a `data-l10n-id` attribute) from under a node into
   * a list of elements.
   */
  static void GetTranslatables(nsINode& aNode,
                               Sequence<OwningNonNull<Element>>& aElements,
                               ErrorResult& aRv);

  /**
   * Sets the root information such as locale and direction.
   */
  static void SetRootInfo(Element* aElement);

  /**
   * Applies l10n translations on translatable elements.
   *
   * If `aProto` gets passed, it'll be used to cache
   * the localized elements.
   *
   * Result is `true` if all translations were applied
   * successfully, and `false` otherwise.
   */
  bool ApplyTranslations(nsTArray<nsCOMPtr<Element>>& aElements,
                         nsTArray<Nullable<L10nMessage>>& aTranslations,
                         nsXULPrototypeDocument* aProto, ErrorResult& aRv);

  bool SubtreeRootInRoots(nsINode* aSubtreeRoot) {
    for (const auto* key : mRoots) {
      nsINode* subtreeRoot = key->SubtreeRoot();
      if (subtreeRoot == aSubtreeRoot) {
        return true;
      }
    }
    return false;
  }

  DOMLocalization(nsIGlobalObject* aGlobal, bool aSync);
  DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
                  const intl::ffi::LocalizationRc* aRaw);

 protected:
  virtual ~DOMLocalization();
  void OnChange() override;
  void DisconnectMutations();
  void DisconnectRoots();
  void ReportL10nOverlaysErrors(nsTArray<L10nOverlaysError>& aErrors);
  void ConvertStringToL10nArgs(const nsString& aInput, intl::L10nArgs& aRetVal,
                               ErrorResult& aRv);

  RefPtr<L10nMutations> mMutations;
  nsTHashSet<RefPtr<nsINode>> mRoots;
};

}  // namespace mozilla::dom

#endif