tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CompositionEvent.cpp (3773B)


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