Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/events/CompositionEvent.cpp
Line
Count
Source (jump to first uncovered line)
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 "mozilla/dom/CompositionEvent.h"
8
#include "mozilla/TextEvents.h"
9
#include "prtime.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
CompositionEvent::CompositionEvent(EventTarget* aOwner,
15
                                   nsPresContext* aPresContext,
16
                                   WidgetCompositionEvent* aEvent)
17
  : UIEvent(aOwner, aPresContext,
18
            aEvent ? aEvent :
19
                     new WidgetCompositionEvent(false, eVoidEvent, nullptr))
20
0
{
21
0
  NS_ASSERTION(mEvent->mClass == eCompositionEventClass,
22
0
               "event type mismatch");
23
0
24
0
  if (aEvent) {
25
0
    mEventIsInternal = false;
26
0
  } else {
27
0
    mEventIsInternal = true;
28
0
    mEvent->mTime = PR_Now();
29
0
30
0
    // XXX compositionstart is cancelable in draft of DOM3 Events.
31
0
    //     However, it doesn't make sence for us, we cannot cancel composition
32
0
    //     when we sends compositionstart event.
33
0
    mEvent->mFlags.mCancelable = false;
34
0
  }
35
0
36
0
  // XXX Do we really need to duplicate the data value?
37
0
  mData = mEvent->AsCompositionEvent()->mData;
38
0
  // TODO: Native event should have locale information.
39
0
}
40
41
// static
42
already_AddRefed<CompositionEvent>
43
CompositionEvent::Constructor(const GlobalObject& aGlobal,
44
                     const nsAString& aType,
45
                     const CompositionEventInit& aParam,
46
                     ErrorResult& aRv)
47
0
{
48
0
  nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
49
0
  RefPtr<CompositionEvent> e = new CompositionEvent(t, nullptr, nullptr);
50
0
  bool trusted = e->Init(t);
51
0
  e->InitCompositionEvent(aType, aParam.mBubbles, aParam.mCancelable,
52
0
                          aParam.mView, aParam.mData, EmptyString());
53
0
  e->mDetail = aParam.mDetail;
54
0
  e->SetTrusted(trusted);
55
0
  e->SetComposed(aParam.mComposed);
56
0
  return e.forget();
57
0
}
58
59
NS_IMPL_CYCLE_COLLECTION_INHERITED(CompositionEvent, UIEvent,
60
                                   mRanges)
61
62
NS_IMPL_ADDREF_INHERITED(CompositionEvent, UIEvent)
63
NS_IMPL_RELEASE_INHERITED(CompositionEvent, UIEvent)
64
65
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CompositionEvent)
66
0
NS_INTERFACE_MAP_END_INHERITING(UIEvent)
67
68
void
69
CompositionEvent::GetData(nsAString& aData) const
70
0
{
71
0
  aData = mData;
72
0
}
73
74
void
75
CompositionEvent::GetLocale(nsAString& aLocale) const
76
0
{
77
0
  aLocale = mLocale;
78
0
}
79
80
void
81
CompositionEvent::InitCompositionEvent(const nsAString& aType,
82
                                       bool aCanBubble,
83
                                       bool aCancelable,
84
                                       nsGlobalWindowInner* aView,
85
                                       const nsAString& aData,
86
                                       const nsAString& aLocale)
87
0
{
88
0
  NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
89
0
90
0
  UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
91
0
  mData = aData;
92
0
  mLocale = aLocale;
93
0
}
94
95
void
96
CompositionEvent::GetRanges(TextClauseArray& aRanges)
97
0
{
98
0
  // If the mRanges is not empty, we return the cached value.
99
0
  if (!mRanges.IsEmpty()) {
100
0
    aRanges = mRanges;
101
0
    return;
102
0
  }
103
0
  RefPtr<TextRangeArray> textRangeArray = mEvent->AsCompositionEvent()->mRanges;
104
0
  if (!textRangeArray) {
105
0
    return;
106
0
  }
107
0
  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mOwner);
108
0
  const TextRange* targetRange = textRangeArray->GetTargetClause();
109
0
  for (size_t i = 0; i < textRangeArray->Length(); i++) {
110
0
    const TextRange& range = textRangeArray->ElementAt(i);
111
0
    mRanges.AppendElement(new TextClause(window, range, targetRange));
112
0
  }
113
0
  aRanges = mRanges;
114
0
}
115
116
} // namespace dom
117
} // namespace mozilla
118
119
using namespace mozilla;
120
using namespace mozilla::dom;
121
122
already_AddRefed<CompositionEvent>
123
NS_NewDOMCompositionEvent(EventTarget* aOwner,
124
                          nsPresContext* aPresContext,
125
                          WidgetCompositionEvent* aEvent)
126
0
{
127
0
  RefPtr<CompositionEvent> event =
128
0
    new CompositionEvent(aOwner, aPresContext, aEvent);
129
0
  return event.forget();
130
0
}