Bug 1826136 [wpt PR 39338] - Update wpt metadata, a=testonly
[gecko.git] / dom / events / CompositionEvent.cpp
blob7e52b9e1615c1377beda681ce2f7e3114c09fdf4
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 #include "mozilla/dom/CompositionEvent.h"
8 #include "mozilla/TextEvents.h"
9 #include "prtime.h"
11 namespace mozilla::dom {
13 CompositionEvent::CompositionEvent(EventTarget* aOwner,
14 nsPresContext* aPresContext,
15 WidgetCompositionEvent* aEvent)
16 : UIEvent(aOwner, aPresContext,
17 aEvent ? aEvent
18 : new WidgetCompositionEvent(false, eVoidEvent, nullptr)) {
19 NS_ASSERTION(mEvent->mClass == eCompositionEventClass, "event type mismatch");
21 if (aEvent) {
22 mEventIsInternal = false;
23 } else {
24 mEventIsInternal = true;
26 // XXX compositionstart is cancelable in draft of DOM3 Events.
27 // However, it doesn't make sence for us, we cannot cancel composition
28 // when we sends compositionstart event.
29 mEvent->mFlags.mCancelable = false;
32 // XXX Do we really need to duplicate the data value?
33 mData = mEvent->AsCompositionEvent()->mData;
34 // TODO: Native event should have locale information.
37 // static
38 already_AddRefed<CompositionEvent> CompositionEvent::Constructor(
39 const GlobalObject& aGlobal, const nsAString& aType,
40 const CompositionEventInit& aParam) {
41 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
42 RefPtr<CompositionEvent> e = new CompositionEvent(t, nullptr, nullptr);
43 bool trusted = e->Init(t);
44 e->InitCompositionEvent(aType, aParam.mBubbles, aParam.mCancelable,
45 aParam.mView, aParam.mData, u""_ns);
46 e->mDetail = aParam.mDetail;
47 e->SetTrusted(trusted);
48 e->SetComposed(aParam.mComposed);
49 return e.forget();
52 NS_IMPL_CYCLE_COLLECTION_INHERITED(CompositionEvent, UIEvent, mRanges)
54 NS_IMPL_ADDREF_INHERITED(CompositionEvent, UIEvent)
55 NS_IMPL_RELEASE_INHERITED(CompositionEvent, UIEvent)
57 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CompositionEvent)
58 NS_INTERFACE_MAP_END_INHERITING(UIEvent)
60 void CompositionEvent::GetData(nsAString& aData) const { aData = mData; }
62 void CompositionEvent::GetLocale(nsAString& aLocale) const {
63 aLocale = mLocale;
66 void CompositionEvent::InitCompositionEvent(const nsAString& aType,
67 bool aCanBubble, bool aCancelable,
68 nsGlobalWindowInner* aView,
69 const nsAString& aData,
70 const nsAString& aLocale) {
71 NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
73 UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
74 mData = aData;
75 mLocale = aLocale;
78 void CompositionEvent::GetRanges(TextClauseArray& aRanges) {
79 // If the mRanges is not empty, we return the cached value.
80 if (!mRanges.IsEmpty()) {
81 aRanges = mRanges.Clone();
82 return;
84 RefPtr<TextRangeArray> textRangeArray = mEvent->AsCompositionEvent()->mRanges;
85 if (!textRangeArray) {
86 return;
88 nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mOwner);
89 const TextRange* targetRange = textRangeArray->GetTargetClause();
90 for (size_t i = 0; i < textRangeArray->Length(); i++) {
91 const TextRange& range = textRangeArray->ElementAt(i);
92 mRanges.AppendElement(new TextClause(window, range, targetRange));
94 aRanges = mRanges.Clone();
97 } // namespace mozilla::dom
99 using namespace mozilla;
100 using namespace mozilla::dom;
102 already_AddRefed<CompositionEvent> NS_NewDOMCompositionEvent(
103 EventTarget* aOwner, nsPresContext* aPresContext,
104 WidgetCompositionEvent* aEvent) {
105 RefPtr<CompositionEvent> event =
106 new CompositionEvent(aOwner, aPresContext, aEvent);
107 return event.forget();