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/. */
6 
7 #include "nsCOMPtr.h"
8 #include "mozilla/dom/MutationEvent.h"
9 #include "mozilla/InternalMutationEvent.h"
10 
11 class nsPresContext;
12 
13 namespace mozilla::dom {
14 
MutationEvent(EventTarget * aOwner,nsPresContext * aPresContext,InternalMutationEvent * aEvent)15 MutationEvent::MutationEvent(EventTarget* aOwner, nsPresContext* aPresContext,
16                              InternalMutationEvent* aEvent)
17     : Event(aOwner, aPresContext,
18             aEvent ? aEvent : new InternalMutationEvent(false, eVoidEvent)) {
19   mEventIsInternal = (aEvent == nullptr);
20 }
21 
GetRelatedNode()22 nsINode* MutationEvent::GetRelatedNode() {
23   return mEvent->AsMutationEvent()->mRelatedNode;
24 }
25 
GetPrevValue(nsAString & aPrevValue) const26 void MutationEvent::GetPrevValue(nsAString& aPrevValue) const {
27   InternalMutationEvent* mutation = mEvent->AsMutationEvent();
28   if (mutation->mPrevAttrValue) mutation->mPrevAttrValue->ToString(aPrevValue);
29 }
30 
GetNewValue(nsAString & aNewValue) const31 void MutationEvent::GetNewValue(nsAString& aNewValue) const {
32   InternalMutationEvent* mutation = mEvent->AsMutationEvent();
33   if (mutation->mNewAttrValue) mutation->mNewAttrValue->ToString(aNewValue);
34 }
35 
GetAttrName(nsAString & aAttrName) const36 void MutationEvent::GetAttrName(nsAString& aAttrName) const {
37   InternalMutationEvent* mutation = mEvent->AsMutationEvent();
38   if (mutation->mAttrName) mutation->mAttrName->ToString(aAttrName);
39 }
40 
AttrChange()41 uint16_t MutationEvent::AttrChange() {
42   return mEvent->AsMutationEvent()->mAttrChange;
43 }
44 
InitMutationEvent(const nsAString & aType,bool aCanBubble,bool aCancelable,nsINode * aRelatedNode,const nsAString & aPrevValue,const nsAString & aNewValue,const nsAString & aAttrName,uint16_t & aAttrChange,ErrorResult & aRv)45 void MutationEvent::InitMutationEvent(const nsAString& aType, bool aCanBubble,
46                                       bool aCancelable, nsINode* aRelatedNode,
47                                       const nsAString& aPrevValue,
48                                       const nsAString& aNewValue,
49                                       const nsAString& aAttrName,
50                                       uint16_t& aAttrChange, ErrorResult& aRv) {
51   NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
52 
53   Event::InitEvent(aType, aCanBubble, aCancelable);
54 
55   InternalMutationEvent* mutation = mEvent->AsMutationEvent();
56   mutation->mRelatedNode = aRelatedNode;
57   if (!aPrevValue.IsEmpty()) mutation->mPrevAttrValue = NS_Atomize(aPrevValue);
58   if (!aNewValue.IsEmpty()) mutation->mNewAttrValue = NS_Atomize(aNewValue);
59   if (!aAttrName.IsEmpty()) {
60     mutation->mAttrName = NS_Atomize(aAttrName);
61   }
62   mutation->mAttrChange = aAttrChange;
63 }
64 
65 }  // namespace mozilla::dom
66 
67 using namespace mozilla;
68 using namespace mozilla::dom;
69 
NS_NewDOMMutationEvent(EventTarget * aOwner,nsPresContext * aPresContext,InternalMutationEvent * aEvent)70 already_AddRefed<MutationEvent> NS_NewDOMMutationEvent(
71     EventTarget* aOwner, nsPresContext* aPresContext,
72     InternalMutationEvent* aEvent) {
73   RefPtr<MutationEvent> it = new MutationEvent(aOwner, aPresContext, aEvent);
74   return it.forget();
75 }
76