dom/base/CompositionStringSynthesizer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/CompositionStringSynthesizer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,160 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "CompositionStringSynthesizer.h"
    1.10 +#include "nsContentUtils.h"
    1.11 +#include "nsIDocShell.h"
    1.12 +#include "nsIFrame.h"
    1.13 +#include "nsIPresShell.h"
    1.14 +#include "nsIWidget.h"
    1.15 +#include "nsPIDOMWindow.h"
    1.16 +#include "nsView.h"
    1.17 +#include "mozilla/TextEvents.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace dom {
    1.21 +
    1.22 +NS_IMPL_ISUPPORTS(CompositionStringSynthesizer,
    1.23 +                  nsICompositionStringSynthesizer)
    1.24 +
    1.25 +CompositionStringSynthesizer::CompositionStringSynthesizer(
    1.26 +                                nsPIDOMWindow* aWindow)
    1.27 +{
    1.28 +  mWindow = do_GetWeakReference(aWindow);
    1.29 +  mClauses = new TextRangeArray();
    1.30 +  ClearInternal();
    1.31 +}
    1.32 +
    1.33 +CompositionStringSynthesizer::~CompositionStringSynthesizer()
    1.34 +{
    1.35 +}
    1.36 +
    1.37 +void
    1.38 +CompositionStringSynthesizer::ClearInternal()
    1.39 +{
    1.40 +  mString.Truncate();
    1.41 +  mClauses->Clear();
    1.42 +  mCaret.mRangeType = 0;
    1.43 +}
    1.44 +
    1.45 +nsIWidget*
    1.46 +CompositionStringSynthesizer::GetWidget()
    1.47 +{
    1.48 +  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
    1.49 +  if (!window) {
    1.50 +    return nullptr;
    1.51 +  }
    1.52 +  nsIDocShell *docShell = window->GetDocShell();
    1.53 +  if (!docShell) {
    1.54 +    return nullptr;
    1.55 +  }
    1.56 +  nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell();
    1.57 +  if (!presShell) {
    1.58 +    return nullptr;
    1.59 +  }
    1.60 +  nsIFrame* frame = presShell->GetRootFrame();
    1.61 +  if (!frame) {
    1.62 +    return nullptr;
    1.63 +  }
    1.64 +  return frame->GetView()->GetNearestWidget(nullptr);
    1.65 +}
    1.66 +
    1.67 +NS_IMETHODIMP
    1.68 +CompositionStringSynthesizer::SetString(const nsAString& aString)
    1.69 +{
    1.70 +  nsCOMPtr<nsIWidget> widget = GetWidget();
    1.71 +  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
    1.72 +
    1.73 +  mString = aString;
    1.74 +  return NS_OK;
    1.75 +}
    1.76 +
    1.77 +NS_IMETHODIMP
    1.78 +CompositionStringSynthesizer::AppendClause(uint32_t aLength,
    1.79 +                                           uint32_t aAttribute)
    1.80 +{
    1.81 +  nsCOMPtr<nsIWidget> widget = GetWidget();
    1.82 +  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
    1.83 +
    1.84 +  switch (aAttribute) {
    1.85 +    case ATTR_RAWINPUT:
    1.86 +    case ATTR_SELECTEDRAWTEXT:
    1.87 +    case ATTR_CONVERTEDTEXT:
    1.88 +    case ATTR_SELECTEDCONVERTEDTEXT: {
    1.89 +      TextRange textRange;
    1.90 +      textRange.mStartOffset =
    1.91 +        mClauses->IsEmpty() ? 0 : mClauses->LastElement().mEndOffset;
    1.92 +      textRange.mEndOffset = textRange.mStartOffset + aLength;
    1.93 +      textRange.mRangeType = aAttribute;
    1.94 +      mClauses->AppendElement(textRange);
    1.95 +      return NS_OK;
    1.96 +    }
    1.97 +    default:
    1.98 +      return NS_ERROR_INVALID_ARG;
    1.99 +  }
   1.100 +}
   1.101 +
   1.102 +NS_IMETHODIMP
   1.103 +CompositionStringSynthesizer::SetCaret(uint32_t aOffset, uint32_t aLength)
   1.104 +{
   1.105 +  nsCOMPtr<nsIWidget> widget = GetWidget();
   1.106 +  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
   1.107 +
   1.108 +  mCaret.mStartOffset = aOffset;
   1.109 +  mCaret.mEndOffset = mCaret.mStartOffset + aLength;
   1.110 +  mCaret.mRangeType = NS_TEXTRANGE_CARETPOSITION;
   1.111 +  return NS_OK;
   1.112 +}
   1.113 +
   1.114 +NS_IMETHODIMP
   1.115 +CompositionStringSynthesizer::DispatchEvent(bool* aDefaultPrevented)
   1.116 +{
   1.117 +  NS_ENSURE_ARG_POINTER(aDefaultPrevented);
   1.118 +  nsCOMPtr<nsIWidget> widget = GetWidget();
   1.119 +  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
   1.120 +
   1.121 +  if (!nsContentUtils::IsCallerChrome()) {
   1.122 +    return NS_ERROR_DOM_SECURITY_ERR;
   1.123 +  }
   1.124 +
   1.125 +  if (!mClauses->IsEmpty() &&
   1.126 +      mClauses->LastElement().mEndOffset != mString.Length()) {
   1.127 +    NS_WARNING("Sum of length of the all clauses must be same as the string "
   1.128 +               "length");
   1.129 +    ClearInternal();
   1.130 +    return NS_ERROR_ILLEGAL_VALUE;
   1.131 +  }
   1.132 +  if (mCaret.mRangeType == NS_TEXTRANGE_CARETPOSITION) {
   1.133 +    if (mCaret.mEndOffset > mString.Length()) {
   1.134 +      NS_WARNING("Caret position is out of the composition string");
   1.135 +      ClearInternal();
   1.136 +      return NS_ERROR_ILLEGAL_VALUE;
   1.137 +    }
   1.138 +    mClauses->AppendElement(mCaret);
   1.139 +  }
   1.140 +
   1.141 +  WidgetTextEvent textEvent(true, NS_TEXT_TEXT, widget);
   1.142 +  textEvent.time = PR_IntervalNow();
   1.143 +  textEvent.theText = mString;
   1.144 +  if (!mClauses->IsEmpty()) {
   1.145 +    textEvent.mRanges = mClauses;
   1.146 +  }
   1.147 +
   1.148 +  // XXX How should we set false for this on b2g?
   1.149 +  textEvent.mFlags.mIsSynthesizedForTests = true;
   1.150 +
   1.151 +  nsEventStatus status = nsEventStatus_eIgnore;
   1.152 +  nsresult rv = widget->DispatchEvent(&textEvent, status);
   1.153 +  *aDefaultPrevented = (status == nsEventStatus_eConsumeNoDefault);
   1.154 +
   1.155 +  ClearInternal();
   1.156 +
   1.157 +  NS_ENSURE_SUCCESS(rv, rv);
   1.158 +
   1.159 +  return NS_OK;
   1.160 +}
   1.161 +
   1.162 +} // namespace dom
   1.163 +} // namespace mozilla

mercurial