Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/dom/events/   (Browser von der Mozilla Stiftung Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 132 kB image not shown  

Quelle  ContentEventHandler.cpp   Sprache: C

 
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "ContentEventHandler.h"

#include "mozilla/Assertions.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/ContentIterator.h"
#include "mozilla/IMEStateManager.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/Maybe.h"
#include "mozilla/MiscEvents.h"
#include "mozilla/PresShell.h"
#include "mozilla/RangeBoundary.h"
#include "mozilla/RangeUtils.h"
#include "mozilla/SelectionMovementUtils.h"
#include "mozilla/TextComposition.h"
#include "mozilla/TextEditor.h"
#include "mozilla/TextEvents.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLBRElement.h"
#include "mozilla/dom/HTMLUnknownElement.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/dom/StaticRange.h"
#include "mozilla/dom/Text.h"
#include "nsCaret.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsCopySupport.h"
#include "nsElementTable.h"
#include "nsFocusManager.h"
#include "nsFontMetrics.h"
#include "nsFrameSelection.h"
#include "nsHTMLTags.h"
#include "nsIFrame.h"
#include "nsLayoutUtils.h"
#include "nsPresContext.h"
#include "nsQueryObject.h"
#include "nsRange.h"
#include "nsTextFragment.h"
#include "nsTextFrame.h"
#include "nsView.h"
#include "mozilla/ViewportUtils.h"

#include <algorithm>

// Work around conflicting define in rpcndr.h
#if defined(small)
#  undef small
#endif  // defined(small)

#if defined(XP_WIN) && 0
#  define TRANSLATE_NEW_LINES
#endif

namespace mozilla {

using namespace dom;
using namespace widget;

/******************************************************************/
/* ContentEventHandler::SimpleRangeBase                           */
/******************************************************************/
template <>
ContentEventHandler::SimpleRangeBase<
    RefPtr<nsINode>, RangeBoundary>::SimpleRangeBase() = default;

template <>
ContentEventHandler::SimpleRangeBase<nsINode*,
                                     RawRangeBoundary>::SimpleRangeBase()
    : mRoot(nullptr) {
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
  mAssertNoGC.emplace();
#endif  // #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
}

template <>
template <typename OtherNodeType, typename OtherRangeBoundaryType>
ContentEventHandler::SimpleRangeBase<RefPtr<nsINode>, RangeBoundary>::
    SimpleRangeBase(
        const SimpleRangeBase<OtherNodeType, OtherRangeBoundaryType>& aOther)
    : mRoot(aOther.GetRoot()),
      mStart{aOther.Start().AsRaw()},
      mEnd{aOther.End().AsRaw()}
// Don't use the copy constructor of mAssertNoGC
{}

template <>
template <typename OtherNodeType, typename OtherRangeBoundaryType>
ContentEventHandler::SimpleRangeBase<nsINode*, RawRangeBoundary>::
    SimpleRangeBase(
        const SimpleRangeBase<OtherNodeType, OtherRangeBoundaryType>& aOther)
    : mRoot(aOther.GetRoot()),
      mStart{aOther.Start().AsRaw()},
      mEnd{aOther.End().AsRaw()} {
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
  mAssertNoGC.emplace();
#endif  // #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
}

template <>
ContentEventHandler::SimpleRangeBase<RefPtr<nsINode>, RangeBoundary>::
    SimpleRangeBase(
        SimpleRangeBase<RefPtr<nsINode>, RangeBoundary>&& aOther) noexcept
    : mRoot(std::move(aOther.GetRoot())),
      mStart(std::move(aOther.mStart)),
      mEnd(std::move(aOther.mEnd)) {}

template <>
ContentEventHandler::SimpleRangeBase<nsINode*, RawRangeBoundary>::
    SimpleRangeBase(
        SimpleRangeBase<nsINode*, RawRangeBoundary>&& aOther) noexcept
    : mRoot(std::move(aOther.GetRoot())),
      mStart(std::move(aOther.mStart)),
      mEnd(std::move(aOther.mEnd)) {
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
  mAssertNoGC.emplace();
#endif  // #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
}

#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
template <>
ContentEventHandler::SimpleRangeBase<
    RefPtr<nsINode>, RangeBoundary>::~SimpleRangeBase() = default;

template <>
ContentEventHandler::SimpleRangeBase<nsINode*,
                                     RawRangeBoundary>::~SimpleRangeBase() {
  MOZ_DIAGNOSTIC_ASSERT(!mMutationGuard.Mutated(0));
}
#endif  // #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED

template <typename NodeType, typename RangeBoundaryType>
void ContentEventHandler::SimpleRangeBase<
    NodeType, RangeBoundaryType>::AssertStartIsBeforeOrEqualToEnd() {
  MOZ_ASSERT(
      *nsContentUtils::ComparePoints(
          mStart.Container(),
          *mStart.Offset(
              RangeBoundaryType::OffsetFilter::kValidOrInvalidOffsets),
          mEnd.Container(),
          *mEnd.Offset(
              RangeBoundaryType::OffsetFilter::kValidOrInvalidOffsets)) <= 0);
}

template <typename NodeType, typename RangeBoundaryType>
nsresult
ContentEventHandler::SimpleRangeBase<NodeType, RangeBoundaryType>::SetStart(
    const RawRangeBoundary& aStart) {
  nsINode* newRoot = RangeUtils::ComputeRootNode(aStart.Container());
  if (!newRoot) {
    return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
  }

  if (!aStart.IsSetAndValid()) {
    return NS_ERROR_DOM_INDEX_SIZE_ERR;
  }

  // Collapse if not positioned yet, or if positioned in another document.
  if (!IsPositioned() || newRoot != mRoot) {
    mRoot = newRoot;
    mStart.CopyFrom(aStart, RangeBoundaryIsMutationObserved::Yes);
    mEnd.CopyFrom(aStart, RangeBoundaryIsMutationObserved::Yes);
    return NS_OK;
  }

  mStart.CopyFrom(aStart, RangeBoundaryIsMutationObserved::Yes);
  AssertStartIsBeforeOrEqualToEnd();
  return NS_OK;
}

template <typename NodeType, typename RangeBoundaryType>
nsresult
ContentEventHandler::SimpleRangeBase<NodeType, RangeBoundaryType>::SetEnd(
    const RawRangeBoundary& aEnd) {
  nsINode* newRoot = RangeUtils::ComputeRootNode(aEnd.Container());
  if (!newRoot) {
    return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
  }

  if (!aEnd.IsSetAndValid()) {
    return NS_ERROR_DOM_INDEX_SIZE_ERR;
  }

  // Collapse if not positioned yet, or if positioned in another document.
  if (!IsPositioned() || newRoot != mRoot) {
    mRoot = newRoot;
    mStart.CopyFrom(aEnd, RangeBoundaryIsMutationObserved::Yes);
    mEnd.CopyFrom(aEnd, RangeBoundaryIsMutationObserved::Yes);
    return NS_OK;
  }

  mEnd.CopyFrom(aEnd, RangeBoundaryIsMutationObserved::Yes);
  AssertStartIsBeforeOrEqualToEnd();
  return NS_OK;
}

template <typename NodeType, typename RangeBoundaryType>
nsresult
ContentEventHandler::SimpleRangeBase<NodeType, RangeBoundaryType>::SetEndAfter(
    nsINode* aEndContainer) {
  return SetEnd(RangeUtils::GetRawRangeBoundaryAfter(aEndContainer));
}

template <typename NodeType, typename RangeBoundaryType>
void ContentEventHandler::SimpleRangeBase<
    NodeType, RangeBoundaryType>::SetStartAndEnd(const nsRange* aRange) {
  DebugOnly<nsresult> rv =
      SetStartAndEnd(aRange->StartRef().AsRaw(), aRange->EndRef().AsRaw());
  MOZ_ASSERT(!aRange->IsPositioned() || NS_SUCCEEDED(rv));
}

template <typename NodeType, typename RangeBoundaryType>
nsresult ContentEventHandler::SimpleRangeBase<
    NodeType, RangeBoundaryType>::SetStartAndEnd(const RawRangeBoundary& aStart,
                                                 const RawRangeBoundary& aEnd) {
  nsINode* newStartRoot = RangeUtils::ComputeRootNode(aStart.Container());
  if (!newStartRoot) {
    return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
  }
  if (!aStart.IsSetAndValid()) {
    return NS_ERROR_DOM_INDEX_SIZE_ERR;
  }

  if (aStart.Container() == aEnd.Container()) {
    if (!aEnd.IsSetAndValid()) {
      return NS_ERROR_DOM_INDEX_SIZE_ERR;
    }
    MOZ_ASSERT(*aStart.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets) <=
               *aEnd.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets));
    mRoot = newStartRoot;
    mStart.CopyFrom(aStart, RangeBoundaryIsMutationObserved::Yes);
    mEnd.CopyFrom(aEnd, RangeBoundaryIsMutationObserved::Yes);
    return NS_OK;
  }

  nsINode* newEndRoot = RangeUtils::ComputeRootNode(aEnd.Container());
  if (!newEndRoot) {
    return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
  }
  if (!aEnd.IsSetAndValid()) {
    return NS_ERROR_DOM_INDEX_SIZE_ERR;
  }

  // If they have different root, this should be collapsed at the end point.
  if (newStartRoot != newEndRoot) {
    mRoot = newEndRoot;
    mStart.CopyFrom(aEnd, RangeBoundaryIsMutationObserved::Yes);
    mEnd.CopyFrom(aEnd, RangeBoundaryIsMutationObserved::Yes);
    return NS_OK;
  }

  // Otherwise, set the range as specified.
  mRoot = newStartRoot;
  mStart.CopyFrom(aStart, RangeBoundaryIsMutationObserved::Yes);
  mEnd.CopyFrom(aEnd, RangeBoundaryIsMutationObserved::Yes);
  AssertStartIsBeforeOrEqualToEnd();
  return NS_OK;
}

template <typename NodeType, typename RangeBoundaryType>
nsresult ContentEventHandler::SimpleRangeBase<NodeType, RangeBoundaryType>::
    SelectNodeContents(const nsINode* aNodeToSelectContents) {
  nsINode* const newRoot =
      RangeUtils::ComputeRootNode(const_cast<nsINode*>(aNodeToSelectContents));
  if (!newRoot) {
    return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
  }
  mRoot = newRoot;
  mStart =
      RangeBoundaryType(const_cast<nsINode*>(aNodeToSelectContents), nullptr);
  mEnd = RangeBoundaryType(const_cast<nsINode*>(aNodeToSelectContents),
                           aNodeToSelectContents->GetLastChild());
  return NS_OK;
}

/******************************************************************/
/* ContentEventHandler                                            */
/******************************************************************/

// NOTE
//
// ContentEventHandler *creates* ranges as following rules:
// 1. Start of range:
//   1.1. Cases: [textNode or text[Node or textNode[
//        When text node is start of a range, start node is the text node and
//        start offset is any number between 0 and the length of the text.
//   1.2. Case: [<element>:
//        When start of an element node is start of a range, start node is
//        parent of the element and start offset is the element's index in the
//        parent.
//   1.3. Case: <element/>[
//        When after an empty element node is start of a range, start node is
//        parent of the element and start offset is the element's index in the
//        parent + 1.
//   1.4. Case: <element>[
//        When start of a non-empty element is start of a range, start node is
//        the element and start offset is 0.
//   1.5. Case: <root>[
//        When start of a range is 0 and there are no nodes causing text,
//        start node is the root node and start offset is 0.
//   1.6. Case: [</root>
//        When start of a range is out of bounds, start node is the root node
//        and start offset is number of the children.
// 2. End of range:
//   2.1. Cases: ]textNode or text]Node or textNode]
//        When a text node is end of a range, end node is the text node and
//        end offset is any number between 0 and the length of the text.
//   2.2. Case: ]<element>
//        When before an element node (meaning before the open tag of the
//        element) is end of a range, end node is previous node causing text.
//        Note that this case shouldn't be handled directly.  If rule 2.1 and
//        2.3 are handled correctly, the loop with ContentIterator shouldn't
//        reach the element node since the loop should've finished already at
//        handling the last node which caused some text.
//   2.3. Case: <element>]
//        When a line break is caused before a non-empty element node and it's
//        end of a range, end node is the element and end offset is 0.
//        (i.e., including open tag of the element)
//   2.4. Cases: <element/>]
//        When after an empty element node is end of a range, end node is
//        parent of the element node and end offset is the element's index in
//        the parent + 1.  (i.e., including close tag of the element or empty
//        element)
//   2.5. Case: ]</root>
//        When end of a range is out of bounds, end node is the root node and
//        end offset is number of the children.
//
// ContentEventHandler *treats* ranges as following additional rules:
// 1. When the start node is an element node which doesn't have children,
//    it includes a line break caused before itself (i.e., includes its open
//    tag).  For example, if start position is { <br>, 0 }, the line break
//    caused by <br> should be included into the flatten text.
// 2. When the end node is an element node which doesn't have children,
//    it includes the end (i.e., includes its close tag except empty element).
//    Although, currently, any close tags don't cause line break, this also
//    includes its open tag.  For example, if end position is { <br>, 0 }, the
//    line break caused by the <br> should be included into the flatten text.

ContentEventHandler::ContentEventHandler(nsPresContext* aPresContext)
    : mDocument(aPresContext->Document()) {}

nsresult ContentEventHandler::InitBasic(bool aRequireFlush) {
  NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_AVAILABLE);
  if (aRequireFlush) {
    // If text frame which has overflowing selection underline is dirty,
    // we need to flush the pending reflow here.
    mDocument->FlushPendingNotifications(FlushType::Layout);
  }
  return NS_OK;
}

nsresult ContentEventHandler::InitRootContent(
    const Selection& aNormalSelection) {
  // Root content should be computed with normal selection because normal
  // selection is typically has at least one range but the other selections
  // not so.  If there is a range, computing its root is easy, but if
  // there are no ranges, we need to use ancestor limit instead.
  MOZ_ASSERT(aNormalSelection.Type() == SelectionType::eNormal);

  if (!aNormalSelection.RangeCount()) {
    // If there is no selection range, we should compute the selection root
    // from ancestor limiter or root content of the document.
    mRootElement = aNormalSelection.GetAncestorLimiter();
    if (!mRootElement) {
      mRootElement = mDocument->GetRootElement();
      if (NS_WARN_IF(!mRootElement)) {
        return NS_ERROR_NOT_AVAILABLE;
      }
    }
    return NS_OK;
  }

  RefPtr<const nsRange> range(aNormalSelection.GetRangeAt(0));
  if (NS_WARN_IF(!range)) {
    return NS_ERROR_UNEXPECTED;
  }

  // If there is a selection, we should retrieve the selection root from
  // the range since when the window is inactivated, the ancestor limiter
  // of selection was cleared by blur event handler of EditorBase but the
  // selection range still keeps storing the nodes.  If the active element of
  // the deactive window is <input> or <textarea>, we can compute the
  // selection root from them.
  nsCOMPtr<nsINode> startNode = range->GetStartContainer();
  nsINode* endNode = range->GetEndContainer();
  if (NS_WARN_IF(!startNode) || NS_WARN_IF(!endNode)) {
    return NS_ERROR_FAILURE;
  }

  // See bug 537041 comment 5, the range could have removed node.
  if (NS_WARN_IF(startNode->GetComposedDoc() != mDocument)) {
    return NS_ERROR_FAILURE;
  }

  NS_ASSERTION(startNode->GetComposedDoc() == endNode->GetComposedDoc(),
               "firstNormalSelectionRange crosses the document boundary");

  RefPtr<PresShell> presShell = mDocument->GetPresShell();
  mRootElement =
      Element::FromNodeOrNull(startNode->GetSelectionRootContent(presShell));
  if (NS_WARN_IF(!mRootElement)) {
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}

nsresult ContentEventHandler::InitCommon(EventMessage aEventMessage,
                                         SelectionType aSelectionType,
                                         bool aRequireFlush) {
  if (mSelection && mSelection->Type() == aSelectionType) {
    return NS_OK;
  }

  mSelection = nullptr;
  mRootElement = nullptr;
  mFirstSelectedSimpleRange.Clear();

  nsresult rv = InitBasic(aRequireFlush);
  NS_ENSURE_SUCCESS(rv, rv);

  RefPtr<nsFrameSelection> frameSel;
  if (PresShell* presShell = mDocument->GetPresShell()) {
    frameSel = presShell->GetLastFocusedFrameSelection();
  }
  if (NS_WARN_IF(!frameSel)) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  mSelection = frameSel->GetSelection(aSelectionType);
  if (NS_WARN_IF(!mSelection)) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  RefPtr<Selection> normalSelection;
  if (mSelection->Type() == SelectionType::eNormal) {
    normalSelection = mSelection;
  } else {
    normalSelection = &frameSel->NormalSelection();
    MOZ_ASSERT(normalSelection);
  }

  rv = InitRootContent(*normalSelection);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  if (mSelection->RangeCount()) {
    mFirstSelectedSimpleRange.SetStartAndEnd(mSelection->GetRangeAt(0));
    return NS_OK;
  }

  // Even if there are no selection ranges, it's usual case if aSelectionType
  // is a special selection or we're handling eQuerySelectedText.
  if (aSelectionType != SelectionType::eNormal ||
      aEventMessage == eQuerySelectedText) {
    MOZ_ASSERT(!mFirstSelectedSimpleRange.IsPositioned());
    return NS_OK;
  }

  // But otherwise, we need to assume that there is a selection range at the
  // beginning of the root content if aSelectionType is eNormal.
  rv = mFirstSelectedSimpleRange.CollapseTo(RawRangeBoundary(mRootElement, 0u));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return NS_ERROR_UNEXPECTED;
  }
  return NS_OK;
}

nsresult ContentEventHandler::Init(WidgetQueryContentEvent* aEvent) {
  NS_ASSERTION(aEvent, "aEvent must not be null");
  MOZ_ASSERT(aEvent->mMessage == eQuerySelectedText ||
             aEvent->mInput.mSelectionType == SelectionType::eNormal);

  if (NS_WARN_IF(!aEvent->mInput.IsValidOffset()) ||
      NS_WARN_IF(!aEvent->mInput.IsValidEventMessage(aEvent->mMessage))) {
    return NS_ERROR_FAILURE;
  }

  // Note that we should ignore WidgetQueryContentEvent::Input::mSelectionType
  // if the event isn't eQuerySelectedText.
  SelectionType selectionType = aEvent->mMessage == eQuerySelectedText
                                    ? aEvent->mInput.mSelectionType
                                    : SelectionType::eNormal;
  if (NS_WARN_IF(selectionType == SelectionType::eNone)) {
    return NS_ERROR_FAILURE;
  }

  nsresult rv = InitCommon(aEvent->mMessage, selectionType,
                           aEvent->AllowFlushingPendingNotifications());
  NS_ENSURE_SUCCESS(rv, rv);

  // Be aware, WidgetQueryContentEvent::mInput::mOffset should be made absolute
  // offset before sending it to ContentEventHandler because querying selection
  // every time may be expensive.  So, if the caller caches selection, it
  // should initialize the event with the cached value.
  if (aEvent->mInput.mRelativeToInsertionPoint) {
    MOZ_ASSERT(selectionType == SelectionType::eNormal);
    TextComposition* composition =
        IMEStateManager::GetTextCompositionFor(aEvent->mWidget);
    if (composition) {
      uint32_t compositionStart = composition->NativeOffsetOfStartComposition();
      if (NS_WARN_IF(!aEvent->mInput.MakeOffsetAbsolute(compositionStart))) {
        return NS_ERROR_FAILURE;
      }
    } else {
      LineBreakType lineBreakType = GetLineBreakType(aEvent);
      uint32_t selectionStart = 0;
      rv = GetStartOffset(mFirstSelectedSimpleRange, &selectionStart,
                          lineBreakType);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return NS_ERROR_FAILURE;
      }
      if (NS_WARN_IF(!aEvent->mInput.MakeOffsetAbsolute(selectionStart))) {
        return NS_ERROR_FAILURE;
      }
    }
  }

  // Ideally, we should emplace only when we return succeeded event.
  // However, we need to emplace here since it's hard to store the various
  // result.  Intead, `HandleQueryContentEvent()` will reset `mReply` if
  // corresponding handler returns error.
  aEvent->EmplaceReply();

  aEvent->mReply->mContentsRoot = mRootElement.get();
  aEvent->mReply->mIsEditableContent =
      mRootElement && mRootElement->IsEditable();

  nsRect r;
  nsIFrame* frame = nsCaret::GetGeometry(mSelection, &r);
  if (!frame) {
    frame = mRootElement->GetPrimaryFrame();
    if (NS_WARN_IF(!frame)) {
      return NS_ERROR_FAILURE;
    }
  }
  aEvent->mReply->mFocusedWidget = frame->GetNearestWidget();

  return NS_OK;
}

nsresult ContentEventHandler::Init(WidgetSelectionEvent* aEvent) {
  NS_ASSERTION(aEvent, "aEvent must not be null");

  nsresult rv = InitCommon(aEvent->mMessage);
  NS_ENSURE_SUCCESS(rv, rv);

  aEvent->mSucceeded = false;

  return NS_OK;
}

nsIContent* ContentEventHandler::GetFocusedContent() {
  nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow();
  nsCOMPtr<nsPIDOMWindowOuter> focusedWindow;
  return nsFocusManager::GetFocusedDescendant(
      window, nsFocusManager::eIncludeAllDescendants,
      getter_AddRefs(focusedWindow));
}

nsresult ContentEventHandler::QueryContentRect(
    nsIContent* aContent, WidgetQueryContentEvent* aEvent) {
  MOZ_ASSERT(aContent, "aContent must not be null");

  nsIFrame* frame = aContent->GetPrimaryFrame();
  NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);

  // get rect for first frame
  nsRect resultRect(nsPoint(0, 0), frame->GetRect().Size());
  nsresult rv = ConvertToRootRelativeOffset(frame, resultRect);
  NS_ENSURE_SUCCESS(rv, rv);

  nsPresContext* presContext = frame->PresContext();

  // account for any additional frames
  while ((frame = frame->GetNextContinuation())) {
    nsRect frameRect(nsPoint(0, 0), frame->GetRect().Size());
    rv = ConvertToRootRelativeOffset(frame, frameRect);
    NS_ENSURE_SUCCESS(rv, rv);
    resultRect.UnionRect(resultRect, frameRect);
  }

  aEvent->mReply->mRect = LayoutDeviceIntRect::FromAppUnitsToOutside(
      resultRect, presContext->AppUnitsPerDevPixel());
  // Returning empty rect may cause native IME confused, let's make sure to
  // return non-empty rect.
  EnsureNonEmptyRect(aEvent->mReply->mRect);

  return NS_OK;
}

// Editor places a padding <br> element under its root content if the editor
// doesn't have any text. This happens even for single line editors.
// When we get text content and when we change the selection,
// we don't want to include the padding <br> elements at the end.
static bool IsContentBR(const nsIContent& aContent) {
  const HTMLBRElement* brElement = HTMLBRElement::FromNode(aContent);
  return brElement && !brElement->IsPaddingForEmptyLastLine() &&
         !brElement->IsPaddingForEmptyEditor();
}

static bool IsPaddingBR(const nsIContent& aContent) {
  return aContent.IsHTMLElement(nsGkAtoms::br) && !IsContentBR(aContent);
}

static void ConvertToNativeNewlines(nsString& aString) {
#if defined(TRANSLATE_NEW_LINES)
  aString.ReplaceSubstring(u"\n"_ns, u"\r\n"_ns);
#endif
}

static void AppendString(nsString& aString, const Text& aTextNode) {
  const uint32_t oldXPLength = aString.Length();
  aTextNode.TextFragment().AppendTo(aString);
  if (aTextNode.HasFlag(NS_MAYBE_MASKED)) {
    TextEditor::MaskString(aString, aTextNode, oldXPLength, 0);
  }
}

static void AppendSubString(nsString& aString, const Text& aTextNode,
                            uint32_t aXPOffset, uint32_t aXPLength) {
  const uint32_t oldXPLength = aString.Length();
  aTextNode.TextFragment().AppendTo(aString, aXPOffset, aXPLength);
  if (aTextNode.HasFlag(NS_MAYBE_MASKED)) {
    TextEditor::MaskString(aString, aTextNode, oldXPLength, aXPOffset);
  }
}

#if defined(TRANSLATE_NEW_LINES)
template <typename StringType>
static uint32_t CountNewlinesInXPLength(const StringType& aString) {
  uint32_t count = 0;
  const auto* end = aString.EndReading();
  for (const auto* iter = aString.BeginReading(); iter < end; ++iter) {
    if (*iter == '\n') {
      count++;
    }
  }
  return count;
}

static uint32_t CountNewlinesInXPLength(const Text& aTextNode,
                                        uint32_t aXPLength) {
  const nsTextFragment& textFragment = aTextNode.TextFragment();
  // For automated tests, we should abort on debug build.
  MOZ_ASSERT(aXPLength == UINT32_MAX || aXPLength <= textFragment.GetLength(),
             "aXPLength is out-of-bounds");
  const uint32_t length = std::min(aXPLength, textFragment.GetLength());
  if (!length) {
    return 0;
  }
  if (textFragment.Is2b()) {
    nsDependentSubstring str(textFragment.Get2b(), length);
    return CountNewlinesInXPLength(str);
  }
  nsDependentCSubstring str(textFragment.Get1b(), length);
  return CountNewlinesInXPLength(str);
}

template <typename StringType>
static uint32_t CountNewlinesInNativeLength(const StringType& aString,
                                            uint32_t aNativeLength) {
  MOZ_ASSERT(
      (aNativeLength == UINT32_MAX || aNativeLength <= aString.Length() * 2),
      "aNativeLength is unexpected value");
  uint32_t count = 0;
  uint32_t nativeOffset = 0;
  const auto* end = aString.EndReading();
  for (const auto* iter = aString.BeginReading();
       iter < end && nativeOffset < aNativeLength; ++iter, ++nativeOffset) {
    if (*iter == '\n') {
      count++;
      nativeOffset++;
    }
  }
  return count;
}

static uint32_t CountNewlinesInNativeLength(const Text& aTextNode,
                                            uint32_t aNativeLength) {
  const nsTextFragment& textFragment = aTextNode.TextFragment();
  const uint32_t xpLength = textFragment.GetLength();
  if (!xpLength) {
    return 0;
  }
  if (textFragment.Is2b()) {
    nsDependentSubstring str(textFragment.Get2b(), xpLength);
    return CountNewlinesInNativeLength(str, aNativeLength);
  }
  nsDependentCSubstring str(textFragment.Get1b(), xpLength);
  return CountNewlinesInNativeLength(str, aNativeLength);
}
#endif

/* static */
uint32_t ContentEventHandler::GetNativeTextLength(const Text& aTextNode,
                                                  uint32_t aStartOffset,
                                                  uint32_t aEndOffset) {
  MOZ_ASSERT(aEndOffset >= aStartOffset,
             "aEndOffset must be equals or larger than aStartOffset");
  if (aStartOffset == aEndOffset) {
    return 0;
  }
  return GetTextLength(aTextNode, LINE_BREAK_TYPE_NATIVE, aEndOffset) -
         GetTextLength(aTextNode, LINE_BREAK_TYPE_NATIVE, aStartOffset);
}

/* static */
uint32_t ContentEventHandler::GetNativeTextLength(const Text& aTextNode,
                                                  uint32_t aMaxLength) {
  return GetTextLength(aTextNode, LINE_BREAK_TYPE_NATIVE, aMaxLength);
}

/* static inline */
uint32_t ContentEventHandler::GetBRLength(LineBreakType aLineBreakType) {
#if defined(TRANSLATE_NEW_LINES)
  // Length of \r\n
  return (aLineBreakType == LINE_BREAK_TYPE_NATIVE) ? 2 : 1;
#else
  return 1;
#endif
}

/* static */
uint32_t ContentEventHandler::GetTextLength(const Text& aTextNode,
                                            LineBreakType aLineBreakType,
                                            uint32_t aMaxLength) {
  const uint32_t textLengthDifference =
#if defined(TRANSLATE_NEW_LINES)
      // On Windows, the length of a native newline ("\r\n") is twice the length
      // of the XP newline ("\n"), so XP length is equal to the length of the
      // native offset plus the number of newlines encountered in the string.
      (aLineBreakType == LINE_BREAK_TYPE_NATIVE)
          ? CountNewlinesInXPLength(aTextNode, aMaxLength)
          : 0;
#else
      // On other platforms, the native and XP newlines are the same.
      0;
#endif

  const uint32_t length =
      std::min(aTextNode.TextFragment().GetLength(), aMaxLength);
  return length + textLengthDifference;
}

static uint32_t ConvertToXPOffset(const Text& aTextNode,
                                  uint32_t aNativeOffset) {
#if defined(TRANSLATE_NEW_LINES)
  // On Windows, the length of a native newline ("\r\n") is twice the length of
  // the XP newline ("\n"), so XP offset is equal to the length of the native
  // offset minus the number of newlines encountered in the string.
  return aNativeOffset - CountNewlinesInNativeLength(aTextNode, aNativeOffset);
#else
  // On other platforms, the native and XP newlines are the same.
  return aNativeOffset;
#endif
}

/* static */
uint32_t ContentEventHandler::GetNativeTextLength(const nsAString& aText) {
  const uint32_t textLengthDifference =
#if defined(TRANSLATE_NEW_LINES)
      // On Windows, the length of a native newline ("\r\n") is twice the length
      // of the XP newline ("\n"), so XP length is equal to the length of the
      // native offset plus the number of newlines encountered in the string.
      CountNewlinesInXPLength(aText);
#else
      // On other platforms, the native and XP newlines are the same.
      0;
#endif
  return aText.Length() + textLengthDifference;
}

/* static */
bool ContentEventHandler::ShouldBreakLineBefore(const nsIContent& aContent,
                                                const Element* aRootElement) {
  // We don't need to append linebreak at the start of the root element.
  if (&aContent == aRootElement) {
    return false;
  }

  // If it's not an HTML element (including other markup language's elements),
  // we shouldn't insert like break before that for now.  Becoming this is a
  // problem must be edge case.  E.g., when ContentEventHandler is used with
  // MathML or SVG elements.
  if (!aContent.IsHTMLElement()) {
    return false;
  }

  switch (
      nsHTMLTags::CaseSensitiveAtomTagToId(aContent.NodeInfo()->NameAtom())) {
    case eHTMLTag_br:
      // If the element is <br>, we need to check if the <br> is caused by web
      // content.  Otherwise, i.e., it's caused by internal reason of Gecko,
      // it shouldn't be exposed as a line break to flatten text.
      return IsContentBR(aContent);
    case eHTMLTag_a:
    case eHTMLTag_abbr:
    case eHTMLTag_acronym:
    case eHTMLTag_b:
    case eHTMLTag_bdi:
    case eHTMLTag_bdo:
    case eHTMLTag_big:
    case eHTMLTag_cite:
    case eHTMLTag_code:
    case eHTMLTag_data:
    case eHTMLTag_del:
    case eHTMLTag_dfn:
    case eHTMLTag_em:
    case eHTMLTag_font:
    case eHTMLTag_i:
    case eHTMLTag_ins:
    case eHTMLTag_kbd:
    case eHTMLTag_mark:
    case eHTMLTag_s:
    case eHTMLTag_samp:
    case eHTMLTag_small:
    case eHTMLTag_span:
    case eHTMLTag_strike:
    case eHTMLTag_strong:
    case eHTMLTag_sub:
    case eHTMLTag_sup:
    case eHTMLTag_time:
    case eHTMLTag_tt:
    case eHTMLTag_u:
    case eHTMLTag_var:
      // Note that ideally, we should refer the style of the primary frame of
      // aContent for deciding if it's an inline.  However, it's difficult
      // IMEContentObserver to notify IME of text change caused by style change.
      // Therefore, currently, we should check only from the tag for now.
      return false;
    case eHTMLTag_userdefined:
    case eHTMLTag_unknown:
      // If the element is unknown element, we shouldn't insert line breaks
      // before it since unknown elements should be ignored.
      return false;
    default:
      return true;
  }
}

nsresult ContentEventHandler::GenerateFlatTextContent(
    const Element* aElement, nsString& aString, LineBreakType aLineBreakType) {
  MOZ_ASSERT(aString.IsEmpty());

  UnsafeSimpleRange rawRange;
  nsresult rv = rawRange.SelectNodeContents(aElement);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }
  return GenerateFlatTextContent(rawRange, aString, aLineBreakType);
}

nsresult ContentEventHandler::GenerateFlatTextContent(const nsRange* aRange,
                                                      nsString& aString) {
  MOZ_ASSERT(aString.IsEmpty());

  if (NS_WARN_IF(!aRange)) {
    return NS_ERROR_FAILURE;
  }

  UnsafeSimpleRange rawRange;
  rawRange.SetStartAndEnd(aRange);

  return GenerateFlatTextContent(rawRange, aString, LINE_BREAK_TYPE_NATIVE);
}

template <typename NodeType, typename RangeBoundaryType>
nsresult ContentEventHandler::GenerateFlatTextContent(
    const SimpleRangeBase<NodeType, RangeBoundaryType>& aSimpleRange,
    nsString& aString, LineBreakType aLineBreakType) {
  MOZ_ASSERT(aString.IsEmpty());

  if (aSimpleRange.Collapsed()) {
    return NS_OK;
  }

  nsINode* startNode = aSimpleRange.GetStartContainer();
  nsINode* endNode = aSimpleRange.GetEndContainer();
  if (NS_WARN_IF(!startNode) || NS_WARN_IF(!endNode)) {
    return NS_ERROR_FAILURE;
  }

  if (startNode == endNode && startNode->IsText()) {
    AppendSubString(aString, *startNode->AsText(), aSimpleRange.StartOffset(),
                    aSimpleRange.EndOffset() - aSimpleRange.StartOffset());
    ConvertToNativeNewlines(aString);
    return NS_OK;
  }

  UnsafePreContentIterator preOrderIter;
  nsresult rv = preOrderIter.Init(aSimpleRange.Start().AsRaw(),
                                  aSimpleRange.End().AsRaw());
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }
  for (; !preOrderIter.IsDone(); preOrderIter.Next()) {
    nsINode* node = preOrderIter.GetCurrentNode();
    if (NS_WARN_IF(!node)) {
      break;
    }
    if (!node->IsContent()) {
      continue;
    }

    if (const Text* textNode = Text::FromNode(node)) {
      if (textNode == startNode) {
        AppendSubString(aString, *textNode, aSimpleRange.StartOffset(),
                        textNode->TextLength() - aSimpleRange.StartOffset());
      } else if (textNode == endNode) {
        AppendSubString(aString, *textNode, 0, aSimpleRange.EndOffset());
      } else {
        AppendString(aString, *textNode);
      }
    } else if (ShouldBreakLineBefore(*node->AsContent(), mRootElement)) {
      aString.Append(char16_t('\n'));
    }
  }
  if (aLineBreakType == LINE_BREAK_TYPE_NATIVE) {
    ConvertToNativeNewlines(aString);
  }
  return NS_OK;
}

static FontRange* AppendFontRange(nsTArray<FontRange>& aFontRanges,
                                  uint32_t aBaseOffset) {
  FontRange* fontRange = aFontRanges.AppendElement();
  fontRange->mStartOffset = aBaseOffset;
  return fontRange;
}

/* static */
uint32_t ContentEventHandler::GetTextLengthInRange(
    const Text& aTextNode, uint32_t aXPStartOffset, uint32_t aXPEndOffset,
    LineBreakType aLineBreakType) {
  return aLineBreakType == LINE_BREAK_TYPE_NATIVE
             ? GetNativeTextLength(aTextNode, aXPStartOffset, aXPEndOffset)
             : aXPEndOffset - aXPStartOffset;
}

/* static */
void ContentEventHandler::AppendFontRanges(FontRangeArray& aFontRanges,
                                           const Text& aTextNode,
                                           uint32_t aBaseOffset,
                                           uint32_t aXPStartOffset,
                                           uint32_t aXPEndOffset,
                                           LineBreakType aLineBreakType) {
  nsIFrame* frame = aTextNode.GetPrimaryFrame();
  if (!frame) {
    // It is a non-rendered content, create an empty range for it.
    AppendFontRange(aFontRanges, aBaseOffset);
    return;
  }

  uint32_t baseOffset = aBaseOffset;
#ifdef DEBUG
  {
    nsTextFrame* text = do_QueryFrame(frame);
    MOZ_ASSERT(text, "Not a text frame");
  }
#endif
  auto* curr = static_cast<nsTextFrame*>(frame);
  while (curr) {
    uint32_t frameXPStart = std::max(
        static_cast<uint32_t>(curr->GetContentOffset()), aXPStartOffset);
    uint32_t frameXPEnd =
        std::min(static_cast<uint32_t>(curr->GetContentEnd()), aXPEndOffset);
    if (frameXPStart >= frameXPEnd) {
      curr = curr->GetNextContinuation();
      continue;
    }

    gfxSkipCharsIterator iter = curr->EnsureTextRun(nsTextFrame::eInflated);
    gfxTextRun* textRun = curr->GetTextRun(nsTextFrame::eInflated);

    nsTextFrame* next = nullptr;
    if (frameXPEnd < aXPEndOffset) {
      next = curr->GetNextContinuation();
      while (next && next->GetTextRun(nsTextFrame::eInflated) == textRun) {
        frameXPEnd = std::min(static_cast<uint32_t>(next->GetContentEnd()),
                              aXPEndOffset);
        next =
            frameXPEnd < aXPEndOffset ? next->GetNextContinuation() : nullptr;
      }
    }

    gfxTextRun::Range skipRange(iter.ConvertOriginalToSkipped(frameXPStart),
                                iter.ConvertOriginalToSkipped(frameXPEnd));
    uint32_t lastXPEndOffset = frameXPStart;
    for (gfxTextRun::GlyphRunIterator runIter(textRun, skipRange);
         !runIter.AtEnd(); runIter.NextRun()) {
      gfxFont* font = runIter.GlyphRun()->mFont.get();
      uint32_t startXPOffset =
          iter.ConvertSkippedToOriginal(runIter.StringStart());
      // It is possible that the first glyph run has exceeded the frame,
      // because the whole frame is filled by skipped chars.
      if (startXPOffset >= frameXPEnd) {
        break;
      }

      if (startXPOffset > lastXPEndOffset) {
        // Create range for skipped leading chars.
        AppendFontRange(aFontRanges, baseOffset);
        baseOffset += GetTextLengthInRange(aTextNode, lastXPEndOffset,
                                           startXPOffset, aLineBreakType);
      }

      FontRange* fontRange = AppendFontRange(aFontRanges, baseOffset);
      fontRange->mFontName.Append(NS_ConvertUTF8toUTF16(font->GetName()));

      ParentLayerToScreenScale2D cumulativeResolution =
          ParentLayerToParentLayerScale(
              frame->PresShell()->GetCumulativeResolution()) *
          nsLayoutUtils::GetTransformToAncestorScaleCrossProcessForFrameMetrics(
              frame);
      float scale =
          std::max(cumulativeResolution.xScale, cumulativeResolution.yScale);

      fontRange->mFontSize = font->GetAdjustedSize() * scale;

      // The converted original offset may exceed the range,
      // hence we need to clamp it.
      uint32_t endXPOffset = iter.ConvertSkippedToOriginal(runIter.StringEnd());
      endXPOffset = std::min(frameXPEnd, endXPOffset);
      baseOffset += GetTextLengthInRange(aTextNode, startXPOffset, endXPOffset,
                                         aLineBreakType);
      lastXPEndOffset = endXPOffset;
    }
    if (lastXPEndOffset < frameXPEnd) {
      // Create range for skipped trailing chars. It also handles case
      // that the whole frame contains only skipped chars.
      AppendFontRange(aFontRanges, baseOffset);
      baseOffset += GetTextLengthInRange(aTextNode, lastXPEndOffset, frameXPEnd,
                                         aLineBreakType);
    }

    curr = next;
  }
}

nsresult ContentEventHandler::GenerateFlatFontRanges(
    const UnsafeSimpleRange& aSimpleRange, FontRangeArray& aFontRanges,
    uint32_t& aLength, LineBreakType aLineBreakType) {
  MOZ_ASSERT(aFontRanges.IsEmpty(), "aRanges must be empty array");

  if (aSimpleRange.Collapsed()) {
    return NS_OK;
  }

  nsINode* startNode = aSimpleRange.GetStartContainer();
  nsINode* endNode = aSimpleRange.GetEndContainer();
  if (NS_WARN_IF(!startNode) || NS_WARN_IF(!endNode)) {
    return NS_ERROR_FAILURE;
  }

  // baseOffset is the flattened offset of each content node.
  uint32_t baseOffset = 0;
  UnsafePreContentIterator preOrderIter;
  nsresult rv = preOrderIter.Init(aSimpleRange.Start().AsRaw(),
                                  aSimpleRange.End().AsRaw());
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }
  for (; !preOrderIter.IsDone(); preOrderIter.Next()) {
    nsINode* node = preOrderIter.GetCurrentNode();
    if (NS_WARN_IF(!node)) {
      break;
    }
    if (!node->IsContent()) {
      continue;
    }
    nsIContent* content = node->AsContent();

    if (const Text* textNode = Text::FromNode(content)) {
      const uint32_t startOffset =
          textNode != startNode ? 0 : aSimpleRange.StartOffset();
      const uint32_t endOffset = textNode != endNode ? textNode->TextLength()
                                                     : aSimpleRange.EndOffset();
      AppendFontRanges(aFontRanges, *textNode, baseOffset, startOffset,
                       endOffset, aLineBreakType);
      baseOffset += GetTextLengthInRange(*textNode, startOffset, endOffset,
                                         aLineBreakType);
    } else if (ShouldBreakLineBefore(*content, mRootElement)) {
      if (aFontRanges.IsEmpty()) {
        MOZ_ASSERT(baseOffset == 0);
        FontRange* fontRange = AppendFontRange(aFontRanges, baseOffset);
        if (nsIFrame* frame = content->GetPrimaryFrame()) {
          const nsFont& font = frame->GetParent()->StyleFont()->mFont;
          const StyleFontFamilyList& fontList = font.family.families;
          MOZ_ASSERT(!fontList.list.IsEmpty(), "Empty font family?");
          const StyleSingleFontFamily* fontName =
              fontList.list.IsEmpty() ? nullptr : &fontList.list.AsSpan()[0];
          nsAutoCString name;
          if (fontName) {
            fontName->AppendToString(name, false);
          }
          AppendUTF8toUTF16(name, fontRange->mFontName);

          ParentLayerToScreenScale2D cumulativeResolution =
              ParentLayerToParentLayerScale(
                  frame->PresShell()->GetCumulativeResolution()) *
              nsLayoutUtils::
                  GetTransformToAncestorScaleCrossProcessForFrameMetrics(frame);

          float scale = std::max(cumulativeResolution.xScale,
                                 cumulativeResolution.yScale);

          fontRange->mFontSize = frame->PresContext()->CSSPixelsToDevPixels(
              font.size.ToCSSPixels() * scale);
        }
      }
      baseOffset += GetBRLength(aLineBreakType);
    }
  }

  aLength = baseOffset;
  return NS_OK;
}

nsresult ContentEventHandler::ExpandToClusterBoundary(
    Text& aTextNode, bool aForward, uint32_t* aXPOffset) const {
  // XXX This method assumes that the frame boundaries must be cluster
  // boundaries. It's false, but no problem now, maybe.
  if (*aXPOffset == 0 || *aXPOffset == aTextNode.TextLength()) {
    return NS_OK;
  }

  NS_ASSERTION(*aXPOffset <= aTextNode.TextLength(), "offset is out of range.");

  MOZ_DIAGNOSTIC_ASSERT(mDocument->GetPresShell());
  CaretAssociationHint hint =
      aForward ? CaretAssociationHint::Before : CaretAssociationHint::After;
  nsIFrame* frame = SelectionMovementUtils::GetFrameForNodeOffset(
      &aTextNode, int32_t(*aXPOffset), hint);
  if (frame) {
    auto [startOffset, endOffset] = frame->GetOffsets();
    if (*aXPOffset == static_cast<uint32_t>(startOffset) ||
        *aXPOffset == static_cast<uint32_t>(endOffset)) {
      return NS_OK;
    }
    if (!frame->IsTextFrame()) {
      return NS_ERROR_FAILURE;
    }
    nsTextFrame* textFrame = static_cast<nsTextFrame*>(frame);
    int32_t newOffsetInFrame = *aXPOffset - startOffset;
    newOffsetInFrame += aForward ? -1 : 1;
    // PeekOffsetCharacter() should respect cluster but ignore user-select
    // style.  If it returns "FOUND", we should use the result.  Otherwise,
    // we shouldn't use the result because the offset was moved to reversed
    // direction.
    nsTextFrame::PeekOffsetCharacterOptions options;
    options.mRespectClusters = true;
    options.mIgnoreUserStyleAll = true;
    if (textFrame->PeekOffsetCharacter(aForward, &newOffsetInFrame, options) ==
        nsIFrame::FOUND) {
      *aXPOffset = startOffset + newOffsetInFrame;
      return NS_OK;
    }
  }

  // If the frame isn't available, we only can check surrogate pair...
  if (aTextNode.TextFragment().IsLowSurrogateFollowingHighSurrogateAt(
          *aXPOffset)) {
    *aXPOffset += aForward ? 1 : -1;
  }
  return NS_OK;
}

already_AddRefed<nsRange> ContentEventHandler::GetRangeFromFlatTextOffset(
    WidgetContentCommandEvent* aEvent, uint32_t aOffset, uint32_t aLength) {
  nsresult rv = InitCommon(aEvent->mMessage);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return nullptr;
  }

  Result<DOMRangeAndAdjustedOffsetInFlattenedText, nsresult> result =
      ConvertFlatTextOffsetToDOMRange(aOffset, aLength, LINE_BREAK_TYPE_NATIVE,
                                      false);
  if (NS_WARN_IF(result.isErr())) {
    return nullptr;
  }

  DOMRangeAndAdjustedOffsetInFlattenedText domRangeAndAdjustOffset =
      result.unwrap();

  return nsRange::Create(domRangeAndAdjustOffset.mRange.Start(),
                         domRangeAndAdjustOffset.mRange.End(), IgnoreErrors());
}

template <typename RangeType, typename TextNodeType>
Result<ContentEventHandler::DOMRangeAndAdjustedOffsetInFlattenedTextBase<
           RangeType, TextNodeType>,
       nsresult>
ContentEventHandler::ConvertFlatTextOffsetToDOMRangeBase(
    uint32_t aOffset, uint32_t aLength, LineBreakType aLineBreakType,
    bool aExpandToClusterBoundaries) {
  DOMRangeAndAdjustedOffsetInFlattenedTextBase<RangeType, TextNodeType> result;
  result.mAdjustedOffset = aOffset;

  // Special case like <br contenteditable>
  if (!mRootElement->HasChildren()) {
    nsresult rv = result.mRange.CollapseTo(RawRangeBoundary(mRootElement, 0u));
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return Err(rv);
    }
  }

  UnsafePreContentIterator preOrderIter;
  nsresult rv = preOrderIter.Init(mRootElement);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return Err(rv);
  }

  uint32_t offset = 0;
  uint32_t endOffset = aOffset + aLength;
  bool startSet = false;
  for (; !preOrderIter.IsDone(); preOrderIter.Next()) {
    nsINode* node = preOrderIter.GetCurrentNode();
    if (NS_WARN_IF(!node)) {
      break;
    }
    // FYI: mRootElement shouldn't cause any text. So, we can skip it simply.
    if (node == mRootElement || !node->IsContent()) {
      continue;
    }
    nsIContent* const content = node->AsContent();
    Text* const contentAsText = Text::FromNode(content);

    if (contentAsText) {
      result.mLastTextNode = contentAsText;
    }

    uint32_t textLength = contentAsText
                              ? GetTextLength(*contentAsText, aLineBreakType)
                              : (ShouldBreakLineBefore(*content, mRootElement)
                                     ? GetBRLength(aLineBreakType)
                                     : 0);
    if (!textLength) {
      continue;
    }

    // When the start offset is in between accumulated offset and the last
    // offset of the node, the node is the start node of the range.
    if (!startSet && aOffset <= offset + textLength) {
      nsINode* startNode = nullptr;
      Maybe<uint32_t> startNodeOffset;
      if (contentAsText) {
        // Rule #1.1: [textNode or text[Node or textNode[
        uint32_t xpOffset = aOffset - offset;
        if (aLineBreakType == LINE_BREAK_TYPE_NATIVE) {
          xpOffset = ConvertToXPOffset(*contentAsText, xpOffset);
        }

        if (aExpandToClusterBoundaries) {
          const uint32_t oldXPOffset = xpOffset;
          nsresult rv =
              ExpandToClusterBoundary(*contentAsText, false, &xpOffset);
          if (NS_WARN_IF(NS_FAILED(rv))) {
            return Err(rv);
          }
          // This is correct since a cluster shouldn't include line break.
          result.mAdjustedOffset -= (oldXPOffset - xpOffset);
        }
        startNode = contentAsText;
        startNodeOffset = Some(xpOffset);
      } else if (aOffset < offset + textLength) {
        // Rule #1.2 [<element>
        startNode = content->GetParent();
        if (NS_WARN_IF(!startNode)) {
          return Err(NS_ERROR_FAILURE);
        }
        startNodeOffset = startNode->ComputeIndexOf(content);
        if (NS_WARN_IF(startNodeOffset.isNothing())) {
          // The content is being removed from the parent!
          return Err(NS_ERROR_FAILURE);
        }
      } else if (!content->HasChildren()) {
        // Rule #1.3: <element/>[
        startNode = content->GetParent();
        if (NS_WARN_IF(!startNode)) {
          return Err(NS_ERROR_FAILURE);
        }
        startNodeOffset = startNode->ComputeIndexOf(content);
        if (NS_WARN_IF(startNodeOffset.isNothing())) {
          // The content is being removed from the parent!
          return Err(NS_ERROR_FAILURE);
        }
        MOZ_ASSERT(*startNodeOffset != UINT32_MAX);
        ++(*startNodeOffset);
      } else {
        // Rule #1.4: <element>[
        startNode = content;
        startNodeOffset = Some(0);
      }
      NS_ASSERTION(startNode, "startNode must not be nullptr");
      MOZ_ASSERT(startNodeOffset.isSome(),
                 "startNodeOffset must not be Nothing");
      rv = result.mRange.SetStart(startNode, *startNodeOffset);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return Err(rv);
      }
      startSet = true;

      if (!aLength) {
        rv = result.mRange.SetEnd(startNode, *startNodeOffset);
        if (NS_WARN_IF(NS_FAILED(rv))) {
          return Err(rv);
        }
        return result;
      }
    }

    // When the end offset is in the content, the node is the end node of the
    // range.
    if (endOffset <= offset + textLength) {
      MOZ_ASSERT(startSet, "The start of the range should've been set already");
      if (contentAsText) {
        // Rule #2.1: ]textNode or text]Node or textNode]
        uint32_t xpOffset = endOffset - offset;
        if (aLineBreakType == LINE_BREAK_TYPE_NATIVE) {
          const uint32_t xpOffsetCurrent =
              ConvertToXPOffset(*contentAsText, xpOffset);
          if (xpOffset && GetBRLength(aLineBreakType) > 1) {
            MOZ_ASSERT(GetBRLength(aLineBreakType) == 2);
            const uint32_t xpOffsetPre =
                ConvertToXPOffset(*contentAsText, xpOffset - 1);
            // If previous character's XP offset is same as current character's,
            // it means that the end offset is between \r and \n.  So, the
            // range end should be after the \n.
            if (xpOffsetPre == xpOffsetCurrent) {
              xpOffset = xpOffsetCurrent + 1;
            } else {
              xpOffset = xpOffsetCurrent;
            }
          }
        }
        if (aExpandToClusterBoundaries) {
          nsresult rv =
              ExpandToClusterBoundary(*contentAsText, true, &xpOffset);
          if (NS_WARN_IF(NS_FAILED(rv))) {
            return Err(rv);
          }
        }
        NS_ASSERTION(xpOffset <= INT32_MAX, "The end node offset is too large");
        nsresult rv = result.mRange.SetEnd(contentAsText, xpOffset);
        if (NS_WARN_IF(NS_FAILED(rv))) {
          return Err(rv);
        }
        return result;
      }

      if (endOffset == offset) {
        // Rule #2.2: ]<element>
        // NOTE: Please don't crash on release builds because it must be
        //       overreaction but we shouldn't allow this bug when some
        //       automated tests find this.
        MOZ_ASSERT(false,
                   "This case should've already been handled at "
                   "the last node which caused some text");
        return Err(NS_ERROR_FAILURE);
      }

      if (content->HasChildren() &&
          ShouldBreakLineBefore(*content, mRootElement)) {
        // Rule #2.3: </element>]
        rv = result.mRange.SetEnd(content, 0);
        if (NS_WARN_IF(NS_FAILED(rv))) {
          return Err(rv);
        }
        return result;
      }

      // Rule #2.4: <element/>]
      nsINode* endNode = content->GetParent();
      if (NS_WARN_IF(!endNode)) {
        return Err(NS_ERROR_FAILURE);
      }
      const Maybe<uint32_t> indexInParent = endNode->ComputeIndexOf(content);
      if (NS_WARN_IF(indexInParent.isNothing())) {
        // The content is being removed from the parent!
        return Err(NS_ERROR_FAILURE);
      }
      MOZ_ASSERT(*indexInParent != UINT32_MAX);
      rv = result.mRange.SetEnd(endNode, *indexInParent + 1);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return Err(rv);
      }
      return result;
    }

    offset += textLength;
  }

  if (!startSet) {
    if (!offset) {
      // Rule #1.5: <root>[</root>
      // When there are no nodes causing text, the start of the DOM range
      // should be start of the root node since clicking on such editor (e.g.,
      // <div contenteditable><span></span></div>) sets caret to the start of
      // the editor (i.e., before <span> in the example).
      rv = result.mRange.SetStart(mRootElement, 0);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return Err(rv);
      }
      if (!aLength) {
        rv = result.mRange.SetEnd(mRootElement, 0);
        if (NS_WARN_IF(NS_FAILED(rv))) {
          return Err(rv);
        }
        return result;
      }
    } else {
      // Rule #1.5: [</root>
      rv = result.mRange.SetStart(mRootElement, mRootElement->GetChildCount());
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return result;
      }
    }
    result.mAdjustedOffset = offset;
  }
  // Rule #2.5: ]</root>
  rv = result.mRange.SetEnd(mRootElement, mRootElement->GetChildCount());
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return Err(rv);
  }
  return result;
}

/* static */
LineBreakType ContentEventHandler::GetLineBreakType(
    WidgetQueryContentEvent* aEvent) {
  return GetLineBreakType(aEvent->mUseNativeLineBreak);
}

/* static */
LineBreakType ContentEventHandler::GetLineBreakType(
    WidgetSelectionEvent* aEvent) {
  return GetLineBreakType(aEvent->mUseNativeLineBreak);
}

/* static */
LineBreakType ContentEventHandler::GetLineBreakType(bool aUseNativeLineBreak) {
  return aUseNativeLineBreak ? LINE_BREAK_TYPE_NATIVE : LINE_BREAK_TYPE_XP;
}

nsresult ContentEventHandler::HandleQueryContentEvent(
    WidgetQueryContentEvent* aEvent) {
  nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
  switch (aEvent->mMessage) {
    case eQuerySelectedText:
      rv = OnQuerySelectedText(aEvent);
      break;
    case eQueryTextContent:
      rv = OnQueryTextContent(aEvent);
      break;
    case eQueryCaretRect:
      rv = OnQueryCaretRect(aEvent);
      break;
    case eQueryTextRect:
      rv = OnQueryTextRect(aEvent);
      break;
    case eQueryTextRectArray:
      rv = OnQueryTextRectArray(aEvent);
      break;
    case eQueryEditorRect:
      rv = OnQueryEditorRect(aEvent);
      break;
    case eQueryContentState:
      rv = OnQueryContentState(aEvent);
      break;
    case eQuerySelectionAsTransferable:
      rv = OnQuerySelectionAsTransferable(aEvent);
      break;
    case eQueryCharacterAtPoint:
      rv = OnQueryCharacterAtPoint(aEvent);
      break;
    case eQueryDOMWidgetHittest:
      rv = OnQueryDOMWidgetHittest(aEvent);
      break;
    case eQueryDropTargetHittest:
      rv = OnQueryDropTargetHittest(aEvent);
      break;
    default:
      break;
  }
  if (NS_FAILED(rv)) {
    aEvent->mReply.reset();  // Mark the query failed.
    return rv;
  }

  MOZ_ASSERT(aEvent->Succeeded());
  return NS_OK;
}

// Similar to nsFrameSelection::GetFrameForNodeOffset,
// but this is more flexible for OnQueryTextRect to use
static Result<nsIFrame*, nsresult> GetFrameForTextRect(const nsINode* aNode,
                                                       int32_t aNodeOffset,
                                                       bool aHint) {
  const nsIContent* content = nsIContent::FromNodeOrNull(aNode);
  if (NS_WARN_IF(!content)) {
    return Err(NS_ERROR_UNEXPECTED);
  }
  nsIFrame* frame = content->GetPrimaryFrame();
  // The node may be invisible, e.g., `display: none`, invisible text node
  // around block elements, etc.  Therefore, don't warn when we don't find
  // a primary frame.
  if (!frame) {
    return nullptr;
  }
  int32_t childNodeOffset = 0;
  nsIFrame* returnFrame = nullptr;
  nsresult rv = frame->GetChildFrameContainingOffset(
      aNodeOffset, aHint, &childNodeOffset, &returnFrame);
  if (NS_FAILED(rv)) {
    return Err(rv);
  }
  return returnFrame;
}

nsresult ContentEventHandler::OnQuerySelectedText(
    WidgetQueryContentEvent* aEvent) {
  nsresult rv = Init(aEvent);
  if (NS_FAILED(rv)) {
    return rv;
  }

  MOZ_ASSERT(aEvent->mReply->mOffsetAndData.isNothing());

  if (!mFirstSelectedSimpleRange.IsPositioned()) {
    MOZ_ASSERT(aEvent->mReply->mOffsetAndData.isNothing());
    MOZ_ASSERT_IF(mSelection, !mSelection->RangeCount());
    // This is special case that `mReply` is emplaced, but mOffsetAndData is
    // not emplaced but treated as succeeded because of no selection ranges
    // is a usual case.
    return NS_OK;
  }

  const UnsafeSimpleRange firstSelectedSimpleRange(mFirstSelectedSimpleRange);
  nsINode* const startNode = firstSelectedSimpleRange.GetStartContainer();
  nsINode* const endNode = firstSelectedSimpleRange.GetEndContainer();

  // Make sure the selection is within the root content range.
  if (!startNode->IsInclusiveDescendantOf(mRootElement) ||
      !endNode->IsInclusiveDescendantOf(mRootElement)) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  LineBreakType lineBreakType = GetLineBreakType(aEvent);
  uint32_t startOffset = 0;
  if (NS_WARN_IF(NS_FAILED(GetStartOffset(firstSelectedSimpleRange,
                                          &startOffset, lineBreakType)))) {
    return NS_ERROR_FAILURE;
  }

  const RawRangeBoundary anchorRef = mSelection->RangeCount() > 0
                                         ? mSelection->AnchorRef().AsRaw()
                                         : firstSelectedSimpleRange.Start();
  const RawRangeBoundary focusRef = mSelection->RangeCount() > 0
                                        ? mSelection->FocusRef().AsRaw()
                                        : firstSelectedSimpleRange.End();
  if (NS_WARN_IF(!anchorRef.IsSet()) || NS_WARN_IF(!focusRef.IsSet())) {
    return NS_ERROR_FAILURE;
  }

  if (mSelection->RangeCount()) {
    // If there is only one selection range, the anchor/focus node and offset
    // are the information of the range.  Therefore, we have the direction
    // information.
    if (mSelection->RangeCount() == 1) {
      // The selection's points should always be comparable, independent of the
      // selection (see nsISelectionController.idl).
      Maybe<int32_t> compare =
          nsContentUtils::ComparePoints(anchorRef, focusRef);
      if (compare.isNothing()) {
        return NS_ERROR_FAILURE;
      }

      aEvent->mReply->mReversed = compare.value() > 0;
    }
    // However, if there are 2 or more selection ranges, we have no information
    // of that.
    else {
      aEvent->mReply->mReversed = false;
    }

    nsString selectedString;
    if (!firstSelectedSimpleRange.Collapsed() &&
        NS_WARN_IF(NS_FAILED(GenerateFlatTextContent(
            firstSelectedSimpleRange, selectedString, lineBreakType)))) {
      return NS_ERROR_FAILURE;
    }
    aEvent->mReply->mOffsetAndData.emplace(startOffset, selectedString,
                                           OffsetAndDataFor::SelectedString);
  } else {
    NS_ASSERTION(anchorRef == focusRef,
                 "When mSelection doesn't have selection, "
                 "mFirstSelectedRawRange must be collapsed");

    aEvent->mReply->mReversed = false;
    aEvent->mReply->mOffsetAndData.emplace(startOffset, EmptyString(),
                                           OffsetAndDataFor::SelectedString);
  }

  Result<nsIFrame*, nsresult> frameForTextRectOrError = GetFrameForTextRect(
      focusRef.Container(),
      focusRef.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets).valueOr(0),
      true);
  if (NS_WARN_IF(frameForTextRectOrError.isErr()) ||
      !frameForTextRectOrError.inspect()) {
    aEvent->mReply->mWritingMode = WritingMode();
  } else {
    aEvent->mReply->mWritingMode =
        frameForTextRectOrError.inspect()->GetWritingMode();
  }

  MOZ_ASSERT(aEvent->Succeeded());
  return NS_OK;
}

nsresult ContentEventHandler::OnQueryTextContent(
    WidgetQueryContentEvent* aEvent) {
  nsresult rv = Init(aEvent);
  if (NS_FAILED(rv)) {
    return rv;
  }

  MOZ_ASSERT(aEvent->mReply->mOffsetAndData.isNothing());

  LineBreakType lineBreakType = GetLineBreakType(aEvent);

  Result<UnsafeDOMRangeAndAdjustedOffsetInFlattenedText, nsresult>
      domRangeAndAdjustedOffsetOrError = ConvertFlatTextOffsetToUnsafeDOMRange(
          aEvent->mInput.mOffset, aEvent->mInput.mLength, lineBreakType, false);
  if (MOZ_UNLIKELY(domRangeAndAdjustedOffsetOrError.isErr())) {
    NS_WARNING(
        "ContentEventHandler::ConvertFlatTextOffsetToDOMRangeBase() failed");
    return NS_ERROR_FAILURE;
  }
  const UnsafeDOMRangeAndAdjustedOffsetInFlattenedText
      domRangeAndAdjustedOffset = domRangeAndAdjustedOffsetOrError.unwrap();

  nsString textInRange;
  if (NS_WARN_IF(NS_FAILED(GenerateFlatTextContent(
          domRangeAndAdjustedOffset.mRange, textInRange, lineBreakType)))) {
    return NS_ERROR_FAILURE;
  }

  aEvent->mReply->mOffsetAndData.emplace(
      domRangeAndAdjustedOffset.mAdjustedOffset, textInRange,
      OffsetAndDataFor::EditorString);

  if (aEvent->mWithFontRanges) {
    uint32_t fontRangeLength;
    if (NS_WARN_IF(NS_FAILED(GenerateFlatFontRanges(
            domRangeAndAdjustedOffset.mRange, aEvent->mReply->mFontRanges,
            fontRangeLength, lineBreakType)))) {
      return NS_ERROR_FAILURE;
    }

    MOZ_ASSERT(fontRangeLength == aEvent->mReply->DataLength(),
               "Font ranges doesn't match the string");
  }

  MOZ_ASSERT(aEvent->Succeeded());
  return NS_OK;
}

void ContentEventHandler::EnsureNonEmptyRect(nsRect& aRect) const {
  // See the comment in ContentEventHandler.h why this doesn't set them to
  // one device pixel.
  aRect.height = std::max(1, aRect.height);
  aRect.width = std::max(1, aRect.width);
}

void ContentEventHandler::EnsureNonEmptyRect(LayoutDeviceIntRect& aRect) const {
  aRect.height = std::max(1, aRect.height);
  aRect.width = std::max(1, aRect.width);
}

template <typename NodeType, typename RangeBoundaryType>
ContentEventHandler::FrameAndNodeOffset
ContentEventHandler::GetFirstFrameInRangeForTextRect(
    const SimpleRangeBase<NodeType, RangeBoundaryType>& aSimpleRange) {
  RawNodePosition nodePosition;
  UnsafePreContentIterator preOrderIter;
  nsresult rv = preOrderIter.Init(aSimpleRange.Start().AsRaw(),
                                  aSimpleRange.End().AsRaw());
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return FrameAndNodeOffset();
  }
  for (; !preOrderIter.IsDone(); preOrderIter.Next()) {
    nsINode* node = preOrderIter.GetCurrentNode();
    if (NS_WARN_IF(!node)) {
      break;
    }

    auto* content = nsIContent::FromNode(node);
    if (MOZ_UNLIKELY(!content)) {
      continue;
    }

    // If the node is invisible (e.g., the node is or is in an invisible node or
    // it's a white-space only text node around a block boundary), we should
    // ignore it.
    if (!content->GetPrimaryFrame()) {
      continue;
    }

    if (auto* textNode = Text::FromNode(content)) {
      // If the range starts at the end of a text node, we need to find
      // next node which causes text.
      const uint32_t offsetInNode = textNode == aSimpleRange.GetStartContainer()
                                        ? aSimpleRange.StartOffset()
                                        : 0u;
      if (offsetInNode < textNode->TextDataLength()) {
        nodePosition = {textNode, offsetInNode};
        break;
      }
      continue;
    }

    // If the element node causes a line break before it, it's the first
    // node causing text.
    if (ShouldBreakLineBefore(*content, mRootElement) ||
        IsPaddingBR(*content)) {
      nodePosition = {content, 0u};
    }
  }

  if (!nodePosition.IsSetAndValid()) {
    return FrameAndNodeOffset();
  }

  Result<nsIFrame*, nsresult> firstFrameOrError = GetFrameForTextRect(
      nodePosition.Container(),
      *nodePosition.Offset(RawNodePosition::OffsetFilter::kValidOffsets), true);
  if (NS_WARN_IF(firstFrameOrError.isErr()) || !firstFrameOrError.inspect()) {
    return FrameAndNodeOffset();
  }
  return FrameAndNodeOffset(
      firstFrameOrError.inspect(),
      *nodePosition.Offset(RawNodePosition::OffsetFilter::kValidOffsets));
}

template <typename NodeType, typename RangeBoundaryType>
ContentEventHandler::FrameAndNodeOffset
ContentEventHandler::GetLastFrameInRangeForTextRect(
    const SimpleRangeBase<NodeType, RangeBoundaryType>& aSimpleRange) {
  RawNodePosition nodePosition;
  UnsafePreContentIterator preOrderIter;
  nsresult rv = preOrderIter.Init(aSimpleRange.Start().AsRaw(),
                                  aSimpleRange.End().AsRaw());
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return FrameAndNodeOffset();
  }

  const RangeBoundaryType& endPoint = aSimpleRange.End();
  MOZ_ASSERT(endPoint.IsSetAndValid());
  // If the end point is start of a text node or specified by its parent and
  // index, the node shouldn't be included into the range.  For example,
  // with this case, |<p>abc[<br>]def</p>|, the range ends at 3rd children of
  // <p> (see the range creation rules, "2.4. Cases: <element/>]"). This causes
  // following frames:
  // +----+-----+
  // | abc|[<br>|
  // +----+-----+
  // +----+
  // |]def|
  // +----+
  // So, if this method includes the 2nd text frame's rect to its result, the
  // caller will return too tall rect which includes 2 lines in this case isn't
  // expected by native IME  (e.g., popup of IME will be positioned at bottom
  // of "d" instead of right-bottom of "c").  Therefore, this method shouldn't
  // include the last frame when its content isn't really in aSimpleRange.
  nsINode* nextNodeOfRangeEnd = nullptr;
  if (endPoint.Container()->IsText()) {
    // Don't set nextNodeOfRangeEnd to the start node of aSimpleRange because if
    // the container of the end is same as start node of the range, the text
    // node shouldn't be next of range end even if the offset is 0.  This
    // could occur with empty text node.
    if (endPoint.IsStartOfContainer() &&
        aSimpleRange.GetStartContainer() != endPoint.Container()) {
      nextNodeOfRangeEnd = endPoint.Container();
    }
  } else if (endPoint.IsSetAndValid()) {
    nextNodeOfRangeEnd = endPoint.GetChildAtOffset();
  }

  for (preOrderIter.Last(); !preOrderIter.IsDone(); preOrderIter.Prev()) {
    nsINode* node = preOrderIter.GetCurrentNode();
    if (NS_WARN_IF(!node)) {
--> --------------------

--> maximum size reached

--> --------------------

Messung V0.5
C=89 H=98 G=93

¤ Dauer der Verarbeitung: 0.54 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.