Source code

Revision control

Copy as Markdown

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/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 =
Element::FromNodeOrNull(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()) {