Source code

Revision control

Other Tools

/* -*- 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/EditorUtils.h"
#include "mozilla/IMEStateManager.h"
#include "mozilla/PresShell.h"
#include "mozilla/RangeUtils.h"
#include "mozilla/TextComposition.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/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 "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>
namespace mozilla {
using namespace dom;
using namespace widget;
/******************************************************************/
/* ContentEventHandler::RawRange */
/******************************************************************/
void ContentEventHandler::RawRange::AssertStartIsBeforeOrEqualToEnd() {
MOZ_ASSERT(*nsContentUtils::ComparePoints(
mStart.Container(),
static_cast<int32_t>(*mStart.Offset(
NodePosition::OffsetFilter::kValidOrInvalidOffsets)),
mEnd.Container(),
static_cast<int32_t>(*mEnd.Offset(
NodePosition::OffsetFilter::kValidOrInvalidOffsets))) <=
0);
}
nsresult ContentEventHandler::RawRange::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 = mEnd = aStart;
return NS_OK;
}
mStart = aStart;
AssertStartIsBeforeOrEqualToEnd();
return NS_OK;
}
nsresult ContentEventHandler::RawRange::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 = mEnd = aEnd;
return NS_OK;
}
mEnd = aEnd;
AssertStartIsBeforeOrEqualToEnd();
return NS_OK;
}
nsresult ContentEventHandler::RawRange::SetEndAfter(nsINode* aEndContainer) {
return SetEnd(RangeUtils::GetRawRangeBoundaryAfter(aEndContainer));
}
void ContentEventHandler::RawRange::SetStartAndEnd(const nsRange* aRange) {
DebugOnly<nsresult> rv =
SetStartAndEnd(aRange->StartRef().AsRaw(), aRange->EndRef().AsRaw());
MOZ_ASSERT(!aRange->IsPositioned() || NS_SUCCEEDED(rv));
}
nsresult ContentEventHandler::RawRange::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 = aStart;
mEnd = aEnd;
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 = mEnd = aEnd;
return NS_OK;
}
// Otherwise, set the range as specified.
mRoot = newStartRoot;
mStart = aStart;
mEnd = aEnd;
AssertStartIsBeforeOrEqualToEnd();
return NS_OK;
}
nsresult ContentEventHandler::RawRange::SelectNodeContents(
nsINode* aNodeToSelectContents) {
nsINode* newRoot = RangeUtils::ComputeRootNode(aNodeToSelectContents);
if (!newRoot) {
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
}
mRoot = newRoot;
mStart = RawRangeBoundary(aNodeToSelectContents, nullptr);
mEnd = RawRangeBoundary(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.
mRootContent = aNormalSelection.GetAncestorLimiter();
if (!mRootContent) {
mRootContent = mDocument->GetRootElement();
if (NS_WARN_IF(!mRootContent)) {
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();
mRootContent = startNode->GetSelectionRootContent(presShell);
if (NS_WARN_IF(!mRootContent)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
nsresult ContentEventHandler::InitCommon(SelectionType aSelectionType,
bool aRequireFlush) {
if (mSelection && mSelection->Type() == aSelectionType) {
return NS_OK;
}
mSelection = nullptr;
mRootContent = nullptr;
mFirstSelectedRawRange.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->GetSelection(SelectionType::eNormal);
if (NS_WARN_IF(!normalSelection)) {
return NS_ERROR_NOT_AVAILABLE;
}
}
rv = InitRootContent(*normalSelection);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (mSelection->RangeCount()) {
mFirstSelectedRawRange.SetStartAndEnd(mSelection->GetRangeAt(0));
return NS_OK;
}
// Even if there are no selection ranges, it's usual case if aSelectionType
// is a special selection.
if (aSelectionType != SelectionType::eNormal) {
MOZ_ASSERT(!mFirstSelectedRawRange.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 = mFirstSelectedRawRange.CollapseTo(RawRangeBoundary(mRootContent, 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(selectionType, aEvent->NeedsToFlushLayout());
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);
RefPtr<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(mFirstSelectedRawRange, &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 = mRootContent.get();
aEvent->mReply->mHasSelection = !mSelection->IsCollapsed();
nsRect r;
nsIFrame* frame = nsCaret::GetGeometry(mSelection, &r);
if (!frame) {
frame = mRootContent->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();
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(XP_WIN)
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)) {
EditorUtils::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)) {
EditorUtils::MaskString(aString, aTextNode, oldXPLength, aXPOffset);
}
}
#if defined(XP_WIN)
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());
uint32_t newlines = 0;
for (uint32_t i = 0; i < length; ++i) {
if (textFragment.CharAt(i) == '\n') {
++newlines;
}
}
return newlines;
}
static uint32_t CountNewlinesInNativeLength(const Text& aTextNode,
uint32_t aNativeLength) {
const nsTextFragment& textFragment = aTextNode.TextFragment();
// For automated tests, we should abort on debug build.
MOZ_ASSERT((aNativeLength == UINT32_MAX ||
aNativeLength <= textFragment.GetLength() * 2),
"aNativeLength is unexpected value");
const uint32_t xpLength = textFragment.GetLength();
uint32_t newlines = 0;
for (uint32_t i = 0, nativeOffset = 0;
i < xpLength && nativeOffset < aNativeLength; ++i, ++nativeOffset) {
// For automated tests, we should abort on debug build.
MOZ_ASSERT(i < xpLength, "i is out-of-bounds");
if (textFragment.CharAt(i) == '\n') {
++newlines;
++nativeOffset;
}
}
return newlines;
}
#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(XP_WIN)
// 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(XP_WIN)
// 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(XP_WIN)
// 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 */
bool ContentEventHandler::ShouldBreakLineBefore(
const nsIContent& aContent, const nsINode* aRootNode /* = nullptr */) {
// We don't need to append linebreak at the start of the root element.
if (&aContent == aRootNode) {
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;
}
// 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.
if (aContent.IsHTMLElement(nsGkAtoms::br)) {
return IsContentBR(aContent);
}
// 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.
if (aContent.IsAnyOfHTMLElements(
nsGkAtoms::a, nsGkAtoms::abbr, nsGkAtoms::acronym, nsGkAtoms::b,
nsGkAtoms::bdi, nsGkAtoms::bdo, nsGkAtoms::big, nsGkAtoms::cite,
nsGkAtoms::code, nsGkAtoms::data, nsGkAtoms::del, nsGkAtoms::dfn,
nsGkAtoms::em, nsGkAtoms::font, nsGkAtoms::i, nsGkAtoms::ins,
nsGkAtoms::kbd, nsGkAtoms::mark, nsGkAtoms::s, nsGkAtoms::samp,
nsGkAtoms::small, nsGkAtoms::span, nsGkAtoms::strike,
nsGkAtoms::strong, nsGkAtoms::sub, nsGkAtoms::sup, nsGkAtoms::time,
nsGkAtoms::tt, nsGkAtoms::u, nsGkAtoms::var)) {
return false;
}
// If the element is unknown element, we shouldn't insert line breaks before
// it since unknown elements should be ignored.
RefPtr<HTMLUnknownElement> unknownHTMLElement =
do_QueryObject(const_cast<nsIContent*>(&aContent));
return !unknownHTMLElement;
}
nsresult ContentEventHandler::GenerateFlatTextContent(
nsIContent* aContent, nsString& aString, LineBreakType aLineBreakType) {
MOZ_ASSERT(aString.IsEmpty());
RawRange rawRange;
nsresult rv = rawRange.SelectNodeContents(aContent);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return GenerateFlatTextContent(rawRange, aString, aLineBreakType);
}
nsresult ContentEventHandler::GenerateFlatTextContent(
const RawRange& aRawRange, nsString& aString,
LineBreakType aLineBreakType) {
MOZ_ASSERT(aString.IsEmpty());
if (aRawRange.Collapsed()) {
return NS_OK;
}
nsINode* startNode = aRawRange.GetStartContainer();
nsINode* endNode = aRawRange.GetEndContainer();
if (NS_WARN_IF(!startNode) || NS_WARN_IF(!endNode)) {
return NS_ERROR_FAILURE;
}
if (startNode == endNode && startNode->IsText()) {
AppendSubString(aString, *startNode->AsText(), aRawRange.StartOffset(),
aRawRange.EndOffset() - aRawRange.StartOffset());
ConvertToNativeNewlines(aString);
return NS_OK;
}
PreContentIterator preOrderIter;
nsresult rv =
preOrderIter.Init(aRawRange.Start().AsRaw(), aRawRange.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, aRawRange.StartOffset(),
textNode->TextLength() - aRawRange.StartOffset());
} else if (textNode == endNode) {
AppendSubString(aString, *textNode, 0, aRawRange.EndOffset());
} else {
AppendString(aString, *textNode);
}
} else if (ShouldBreakLineBefore(*node->AsContent(), mRootContent)) {
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));
gfxTextRun::GlyphRunIterator runIter(textRun, skipRange);
uint32_t lastXPEndOffset = frameXPStart;
while (runIter.NextRun()) {
gfxFont* font = runIter.GetGlyphRun()->mFont.get();
uint32_t startXPOffset =
iter.ConvertSkippedToOriginal(runIter.GetStringStart());
// 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.GetStringEnd());
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 RawRange& aRawRange, FontRangeArray& aFontRanges, uint32_t& aLength,
LineBreakType aLineBreakType) {
MOZ_ASSERT(aFontRanges.IsEmpty(), "aRanges must be empty array");
if (aRawRange.Collapsed()) {
return NS_OK;
}
nsINode* startNode = aRawRange.GetStartContainer();
nsINode* endNode = aRawRange.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;
PreContentIterator preOrderIter;
nsresult rv =
preOrderIter.Init(aRawRange.Start().AsRaw(), aRawRange.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 : aRawRange.StartOffset();
const uint32_t endOffset =
textNode != endNode ? textNode->TextLength() : aRawRange.EndOffset();
AppendFontRanges(aFontRanges, *textNode, baseOffset, startOffset,
endOffset, aLineBreakType);
baseOffset += GetTextLengthInRange(*textNode, startOffset, endOffset,
aLineBreakType);
} else if (ShouldBreakLineBefore(*content, mRootContent)) {
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());
int32_t offsetInFrame;
CaretAssociationHint hint =
aForward ? CARET_ASSOCIATE_BEFORE : CARET_ASSOCIATE_AFTER;
nsIFrame* frame = nsFrameSelection::GetFrameForNodeOffset(
&aTextNode, int32_t(*aXPOffset), hint, &offsetInFrame);
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;
}
nsresult ContentEventHandler::SetRawRangeFromFlatTextOffset(
RawRange* aRawRange, uint32_t aOffset, uint32_t aLength,
LineBreakType aLineBreakType, bool aExpandToClusterBoundaries,
uint32_t* aNewOffset, Text** aLastTextNode) {
if (aNewOffset) {
*aNewOffset = aOffset;
}
if (aLastTextNode) {
*aLastTextNode = nullptr;
}
// Special case like <br contenteditable>
if (!mRootContent->HasChildren()) {
nsresult rv = aRawRange->CollapseTo(RawRangeBoundary(mRootContent, 0u));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
PreContentIterator preOrderIter;
nsresult rv = preOrderIter.Init(mRootContent);
if (NS_WARN_IF(NS_FAILED(rv))) {
return 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: mRootContent shouldn't cause any text. So, we can skip it simply.
if (node == mRootContent || !node->IsContent()) {
continue;
}
nsIContent* const content = node->AsContent();
Text* const contentAsText = Text::FromNode(content);
if (aLastTextNode && contentAsText) {
NS_IF_RELEASE(*aLastTextNode);
NS_ADDREF(*aLastTextNode = contentAsText);
}
uint32_t textLength = contentAsText
? GetTextLength(*contentAsText, aLineBreakType)
: (ShouldBreakLineBefore(*content, mRootContent)
? 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;
int32_t startNodeOffset = -1;
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 rv;
}
if (aNewOffset) {
// This is correct since a cluster shouldn't include line break.
*aNewOffset -= (oldXPOffset - xpOffset);
}
}
startNode = contentAsText;
startNodeOffset = static_cast<int32_t>(xpOffset);
} else if (aOffset < offset + textLength) {
// Rule #1.2 [<element>
startNode = content->GetParent();
if (NS_WARN_IF(!startNode)) {
return NS_ERROR_FAILURE;
}
startNodeOffset = startNode->ComputeIndexOf(content);
if (NS_WARN_IF(startNodeOffset == -1)) {
// The content is being removed from the parent!
return NS_ERROR_FAILURE;
}
} else if (!content->HasChildren()) {
// Rule #1.3: <element/>[
startNode = content->GetParent();
if (NS_WARN_IF(!startNode)) {
return NS_ERROR_FAILURE;
}
startNodeOffset = startNode->ComputeIndexOf(content) + 1;
if (NS_WARN_IF(startNodeOffset == 0)) {
// The content is being removed from the parent!
return NS_ERROR_FAILURE;
}
} else {
// Rule #1.4: <element>[
startNode = content;
startNodeOffset = 0;
}
NS_ASSERTION(startNode, "startNode must not be nullptr");
NS_ASSERTION(startNodeOffset >= 0,
"startNodeOffset must not be negative");
rv = aRawRange->SetStart(startNode,
static_cast<uint32_t>(startNodeOffset));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
startSet = true;
if (!aLength) {
rv = aRawRange->SetEnd(startNode,
static_cast<uint32_t>(startNodeOffset));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
}
// 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 rv;
}
}
NS_ASSERTION(xpOffset <= INT32_MAX, "The end node offset is too large");
nsresult rv = aRawRange->SetEnd(contentAsText, xpOffset);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
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 NS_ERROR_FAILURE;
}
if (content->HasChildren() &&
ShouldBreakLineBefore(*content, mRootContent)) {
// Rule #2.3: </element>]
rv = aRawRange->SetEnd(content, 0);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
// Rule #2.4: <element/>]
nsINode* endNode = content->GetParent();
if (NS_WARN_IF(!endNode)) {
return NS_ERROR_FAILURE;
}
int32_t indexInParent = endNode->ComputeIndexOf(content);
if (NS_WARN_IF(indexInParent == -1)) {
// The content is being removed from the parent!
return NS_ERROR_FAILURE;
}
rv = aRawRange->SetEnd(endNode, indexInParent + 1);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
offset += textLength;
}
if (!startSet) {
MOZ_ASSERT(!mRootContent->IsText());
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 = aRawRange->SetStart(mRootContent, 0);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (!aLength) {
rv = aRawRange->SetEnd(mRootContent, 0);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
} else {
// Rule #1.5: [</root>
rv = aRawRange->SetStart(mRootContent, mRootContent->GetChildCount());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
if (aNewOffset) {
*aNewOffset = offset;
}
}
// Rule #2.5: ]</root>
rv = aRawRange->SetEnd(mRootContent, mRootContent->GetChildCount());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
/* 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;
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 nsresult GetFrameForTextRect(const nsINode* aNode, int32_t aNodeOffset,
bool aHint, nsIFrame** aReturnFrame) {
NS_ENSURE_TRUE(aNode && aNode->IsContent(), NS_ERROR_UNEXPECTED);
nsIFrame* frame = aNode->AsContent()->GetPrimaryFrame();
NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
int32_t childNodeOffset = 0;
return frame->GetChildFrameContainingOffset(aNodeOffset, aHint,
&childNodeOffset, aReturnFrame);
}
nsresult ContentEventHandler::OnQuerySelectedText(
WidgetQueryContentEvent* aEvent) {
nsresult rv = Init(aEvent);
if (NS_FAILED(rv)) {
return rv;
}
MOZ_ASSERT(aEvent->mReply->mOffsetAndData.isNothing());
if (!mFirstSelectedRawRange.IsPositioned()) {
MOZ_ASSERT(aEvent->mInput.mSelectionType != SelectionType::eNormal);
MOZ_ASSERT(aEvent->mReply->mOffsetAndData.isNothing());
MOZ_ASSERT(!aEvent->mReply->mHasSelection);
// 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;
}
nsINode* const startNode = mFirstSelectedRawRange.GetStartContainer();
nsINode* const endNode = mFirstSelectedRawRange.GetEndContainer();
// Make sure the selection is within the root content range.
if (!startNode->IsInclusiveDescendantOf(mRootContent) ||
!endNode->IsInclusiveDescendantOf(mRootContent)) {
return NS_ERROR_NOT_AVAILABLE;
}
LineBreakType lineBreakType = GetLineBreakType(aEvent);
uint32_t startOffset = 0;
if (NS_WARN_IF(NS_FAILED(GetStartOffset(mFirstSelectedRawRange, &startOffset,
lineBreakType)))) {
return NS_ERROR_FAILURE;
}
const RangeBoundary& anchorRef = mSelection->RangeCount() > 0
? mSelection->AnchorRef()
: mFirstSelectedRawRange.Start();
const RangeBoundary& focusRef = mSelection->RangeCount() > 0
? mSelection->FocusRef()
: mFirstSelectedRawRange.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 (!mFirstSelectedRawRange.Collapsed() &&
NS_WARN_IF(NS_FAILED(GenerateFlatTextContent(
mFirstSelectedRawRange, 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);
}
nsIFrame* frame = nullptr;
rv = GetFrameForTextRect(
focusRef.Container(),
focusRef.Offset(RangeBoundary::OffsetFilter::kValidOffsets).valueOr(0),
true, &frame);
if (NS_SUCCEEDED(rv) && frame) {
aEvent->mReply->mWritingMode = frame->GetWritingMode();
} else {
aEvent->mReply->mWritingMode = WritingMode();
}
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);
RawRange rawRange;
uint32_t startOffset = 0;
if (NS_WARN_IF(NS_FAILED(SetRawRangeFromFlatTextOffset(
&rawRange, aEvent->mInput.mOffset, aEvent->mInput.mLength,
lineBreakType, false, &startOffset)))) {
return NS_ERROR_FAILURE;
}
nsString textInRange;
if (NS_WARN_IF(NS_FAILED(
GenerateFlatTextContent(rawRange, textInRange, lineBreakType)))) {
return NS_ERROR_FAILURE;
}
aEvent->mReply->mOffsetAndData.emplace(startOffset, textInRange,
OffsetAndDataFor::EditorString);
if (aEvent->mWithFontRanges) {
uint32_t fontRangeLength;
if (NS_WARN_IF(NS_FAILED(
GenerateFlatFontRanges(rawRange, 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);
}
ContentEventHandler::FrameAndNodeOffset
ContentEventHandler::GetFirstFrameInRangeForTextRect(
const RawRange& aRawRange) {
NodePosition nodePosition;
PreContentIterator preOrderIter;
nsresult rv =
preOrderIter.Init(aRawRange.Start().AsRaw(), aRawRange.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;
}
if (!node->IsContent()) {
continue;
}
if (node->IsText()) {
// If the range starts at the end of a text node, we need to find
// next node which causes text.
int32_t offsetInNode =
node == aRawRange.GetStartContainer() ? aRawRange.StartOffset() : 0;
if (static_cast<uint32_t>(offsetInNode) < node->Length()) {
nodePosition = {node, offsetInNode};
break;
}
continue;
}
// If the element node causes a line break before it, it's the first
// node causing text.
if (ShouldBreakLineBefore(*node->AsContent(), mRootContent) ||
IsPaddingBR(*node->AsContent())) {
nodePosition = {node, 0};
}
}
if (!nodePosition.IsSetAndValid()) {
return FrameAndNodeOffset();
}
nsIFrame* firstFrame = nullptr;
GetFrameForTextRect(
nodePosition.Container(),
*nodePosition.Offset(NodePosition::OffsetFilter::kValidOffsets), true,
&