summaryrefslogtreecommitdiffstats
path: root/dom/base/MutationObservers.h
blob: 7ed18e81d7c1ef3aa9a32924534ad100cefdfb0e (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* -*- 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 DOM_BASE_MUTATIONOBSERVERS_H_
#define DOM_BASE_MUTATIONOBSERVERS_H_

#include "mozilla/DoublyLinkedList.h"
#include "nsIContent.h"  // for use in inline function (NotifyParentChainChanged)
#include "nsIMutationObserver.h"  // for use in inline function (NotifyParentChainChanged)
#include "nsINode.h"

class nsAtom;
class nsAttrValue;

namespace mozilla::dom {
class Animation;
class Element;

class MutationObservers {
 public:
  /**
   * Send CharacterDataWillChange notifications to nsIMutationObservers.
   * @param aContent  Node whose data changed
   * @param aInfo     Struct with information details about the change
   * @see nsIMutationObserver::CharacterDataWillChange
   */
  static void NotifyCharacterDataWillChange(nsIContent* aContent,
                                            const CharacterDataChangeInfo&);

  /**
   * Send CharacterDataChanged notifications to nsIMutationObservers.
   * @param aContent  Node whose data changed
   * @param aInfo     Struct with information details about the change
   * @see nsIMutationObserver::CharacterDataChanged
   */
  static void NotifyCharacterDataChanged(nsIContent* aContent,
                                         const CharacterDataChangeInfo&);

  /**
   * Send AttributeWillChange notifications to nsIMutationObservers.
   * @param aElement      Element whose data will change
   * @param aNameSpaceID  Namespace of changing attribute
   * @param aAttribute    Local-name of changing attribute
   * @param aModType      Type of change (add/change/removal)
   * @param aNewValue     The parsed new value, but only if BeforeSetAttr
   *                      preparsed it!!!
   * @see nsIMutationObserver::AttributeWillChange
   */
  static void NotifyAttributeWillChange(mozilla::dom::Element* aElement,
                                        int32_t aNameSpaceID,
                                        nsAtom* aAttribute, int32_t aModType);

  /**
   * Send AttributeChanged notifications to nsIMutationObservers.
   * @param aElement      Element whose data changed
   * @param aNameSpaceID  Namespace of changed attribute
   * @param aAttribute    Local-name of changed attribute
   * @param aModType      Type of change (add/change/removal)
   * @param aOldValue     If the old value was StoresOwnData() (or absent),
   *                      that value, otherwise null
   * @see nsIMutationObserver::AttributeChanged
   */
  static void NotifyAttributeChanged(mozilla::dom::Element* aElement,
                                     int32_t aNameSpaceID, nsAtom* aAttribute,
                                     int32_t aModType,
                                     const nsAttrValue* aOldValue);

  /**
   * Send AttributeSetToCurrentValue notifications to nsIMutationObservers.
   * @param aElement      Element whose data changed
   * @param aNameSpaceID  Namespace of the attribute
   * @param aAttribute    Local-name of the attribute
   * @see nsIMutationObserver::AttributeSetToCurrentValue
   */
  static void NotifyAttributeSetToCurrentValue(mozilla::dom::Element* aElement,
                                               int32_t aNameSpaceID,
                                               nsAtom* aAttribute);

  /**
   * Send ContentAppended notifications to nsIMutationObservers
   * @param aContainer           Node into which new child/children were added
   * @param aFirstNewContent     First new child
   * @see nsIMutationObserver::ContentAppended
   */
  static void NotifyContentAppended(nsIContent* aContainer,
                                    nsIContent* aFirstNewContent);

  /**
   * Send NativeAnonymousChildList notifications to nsIMutationObservers
   * @param aContent             Anonymous node that's been added or removed
   * @param aIsRemove            True if it's a removal, false if an addition
   * @see nsIMutationObserver::NativeAnonymousChildListChange
   */
  static void NotifyNativeAnonymousChildListChange(nsIContent* aContent,
                                                   bool aIsRemove);

  /**
   * Send ContentInserted notifications to nsIMutationObservers
   * @param aContainer        Node into which new child was inserted
   * @param aChild            Newly inserted child
   * @see nsIMutationObserver::ContentInserted
   */
  static void NotifyContentInserted(nsINode* aContainer, nsIContent* aChild);
  /**
   * Send ContentRemoved notifications to nsIMutationObservers
   * @param aContainer        Node from which child was removed
   * @param aChild            Removed child
   * @param aPreviousSibling  Previous sibling of the removed child
   * @see nsIMutationObserver::ContentRemoved
   */
  static void NotifyContentRemoved(nsINode* aContainer, nsIContent* aChild,
                                   nsIContent* aPreviousSibling);

  /**
   * Send ParentChainChanged notifications to nsIMutationObservers
   * @param aContent  The piece of content that had its parent changed.
   * @see nsIMutationObserver::ParentChainChanged
   */
  static inline void NotifyParentChainChanged(nsIContent* aContent) {
    mozilla::SafeDoublyLinkedList<nsIMutationObserver>* observers =
        aContent->GetMutationObservers();
    if (observers && !observers->isEmpty()) {
      for (auto iter = observers->begin(); iter != observers->end(); ++iter) {
        iter->ParentChainChanged(aContent);
      }
    }
  }

  static void NotifyARIAAttributeDefaultWillChange(
      mozilla::dom::Element* aElement, nsAtom* aAttribute, int32_t aModType);
  static void NotifyARIAAttributeDefaultChanged(mozilla::dom::Element* aElement,
                                                nsAtom* aAttribute,
                                                int32_t aModType);

  /**
   * Notify that an animation is added/changed/removed.
   * @param aAnimation The animation we added/changed/removed.
   */
  static void NotifyAnimationAdded(mozilla::dom::Animation* aAnimation);
  static void NotifyAnimationChanged(mozilla::dom::Animation* aAnimation);
  static void NotifyAnimationRemoved(mozilla::dom::Animation* aAnimation);

 private:
  enum class AnimationMutationType { Added, Changed, Removed };
  /**
   * Notify the observers of the target of an animation
   * @param aAnimation The mutated animation.
   * @param aMutationType The mutation type of this animation. It could be
   *                      Added, Changed, or Removed.
   */
  static void NotifyAnimationMutated(mozilla::dom::Animation* aAnimation,
                                     AnimationMutationType aMutatedType);
};
}  // namespace mozilla::dom

#endif  // DOM_BASE_MUTATIONOBSERVERS_H_