DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (27a812186ff4)

VCS Links

Line Code
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 160
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#include "CompositionStringSynthesizer.h"
#include "nsContentUtils.h"
#include "nsIDocShell.h"
#include "nsIFrame.h"
#include "nsIPresShell.h"
#include "nsIWidget.h"
#include "nsPIDOMWindow.h"
#include "nsView.h"
#include "mozilla/TextEvents.h"

namespace mozilla {
namespace dom {

NS_IMPL_ISUPPORTS1(CompositionStringSynthesizer,
                   nsICompositionStringSynthesizer)

CompositionStringSynthesizer::CompositionStringSynthesizer(
                                nsPIDOMWindow* aWindow)
{
  mWindow = do_GetWeakReference(aWindow);
  mClauses = new TextRangeArray();
  ClearInternal();
}

CompositionStringSynthesizer::~CompositionStringSynthesizer()
{
}

void
CompositionStringSynthesizer::ClearInternal()
{
  mString.Truncate();
  mClauses->Clear();
  mCaret.mRangeType = 0;
}

nsIWidget*
CompositionStringSynthesizer::GetWidget()
{
  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
  if (!window) {
    return nullptr;
  }
  nsIDocShell *docShell = window->GetDocShell();
  if (!docShell) {
    return nullptr;
  }
  nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell();
  if (!presShell) {
    return nullptr;
  }
  nsIFrame* frame = presShell->GetRootFrame();
  if (!frame) {
    return nullptr;
  }
  return frame->GetView()->GetNearestWidget(nullptr);
}

NS_IMETHODIMP
CompositionStringSynthesizer::SetString(const nsAString& aString)
{
  nsCOMPtr<nsIWidget> widget = GetWidget();
  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);

  mString = aString;
  return NS_OK;
}

NS_IMETHODIMP
CompositionStringSynthesizer::AppendClause(uint32_t aLength,
                                           uint32_t aAttribute)
{
  nsCOMPtr<nsIWidget> widget = GetWidget();
  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);

  switch (aAttribute) {
    case ATTR_RAWINPUT:
    case ATTR_SELECTEDRAWTEXT:
    case ATTR_CONVERTEDTEXT:
    case ATTR_SELECTEDCONVERTEDTEXT: {
      TextRange textRange;
      textRange.mStartOffset =
        mClauses->IsEmpty() ? 0 : mClauses->LastElement().mEndOffset;
      textRange.mEndOffset = textRange.mStartOffset + aLength;
      textRange.mRangeType = aAttribute;
      mClauses->AppendElement(textRange);
      return NS_OK;
    }
    default:
      return NS_ERROR_INVALID_ARG;
  }
}

NS_IMETHODIMP
CompositionStringSynthesizer::SetCaret(uint32_t aOffset, uint32_t aLength)
{
  nsCOMPtr<nsIWidget> widget = GetWidget();
  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);

  mCaret.mStartOffset = aOffset;
  mCaret.mEndOffset = mCaret.mStartOffset + aLength;
  mCaret.mRangeType = NS_TEXTRANGE_CARETPOSITION;
  return NS_OK;
}

NS_IMETHODIMP
CompositionStringSynthesizer::DispatchEvent(bool* aDefaultPrevented)
{
  NS_ENSURE_ARG_POINTER(aDefaultPrevented);
  nsCOMPtr<nsIWidget> widget = GetWidget();
  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);

  if (!nsContentUtils::IsCallerChrome()) {
    return NS_ERROR_DOM_SECURITY_ERR;
  }

  if (!mClauses->IsEmpty() &&
      mClauses->LastElement().mEndOffset != mString.Length()) {
    NS_WARNING("Sum of length of the all clauses must be same as the string "
               "length");
    ClearInternal();
    return NS_ERROR_ILLEGAL_VALUE;
  }
  if (mCaret.mRangeType == NS_TEXTRANGE_CARETPOSITION) {
    if (mCaret.mEndOffset > mString.Length()) {
      NS_WARNING("Caret position is out of the composition string");
      ClearInternal();
      return NS_ERROR_ILLEGAL_VALUE;
    }
    mClauses->AppendElement(mCaret);
  }

  WidgetTextEvent textEvent(true, NS_TEXT_TEXT, widget);
  textEvent.time = PR_IntervalNow();
  textEvent.theText = mString;
  if (!mClauses->IsEmpty()) {
    textEvent.mRanges = mClauses;
  }

  // XXX How should we set false for this on b2g?
  textEvent.mFlags.mIsSynthesizedForTests = true;

  nsEventStatus status = nsEventStatus_eIgnore;
  nsresult rv = widget->DispatchEvent(&textEvent, status);
  *aDefaultPrevented = (status == nsEventStatus_eConsumeNoDefault);

  ClearInternal();

  NS_ENSURE_SUCCESS(rv, rv);

  return NS_OK;
}

} // namespace dom
} // namespace mozilla