1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DeleteTextTransaction.h"
#include "EditorBase.h"
#include "EditorDOMPoint.h"
#include "HTMLEditUtils.h"
#include "SelectionState.h"
#include "mozilla/Assertions.h"
#include "mozilla/TextEditor.h"
#include "mozilla/dom/Selection.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsISupportsImpl.h"
#include "nsAString.h"
namespace mozilla {
using namespace dom;
// static
already_AddRefed<DeleteTextTransaction> DeleteTextTransaction::MaybeCreate(
EditorBase& aEditorBase, Text& aTextNode, uint32_t aOffset,
uint32_t aLengthToDelete) {
RefPtr<DeleteTextTransaction> transaction =
aEditorBase.IsTextEditor()
? new DeleteTextTransaction(aEditorBase, aTextNode, aOffset,
aLengthToDelete)
: new DeleteTextFromTextNodeTransaction(aEditorBase, aTextNode,
aOffset, aLengthToDelete);
return transaction.forget();
}
// static
already_AddRefed<DeleteTextTransaction>
DeleteTextTransaction::MaybeCreateForPreviousCharacter(EditorBase& aEditorBase,
Text& aTextNode,
uint32_t aOffset) {
if (NS_WARN_IF(!aOffset)) {
return nullptr;
}
nsAutoString data;
aTextNode.GetData(data);
if (NS_WARN_IF(data.IsEmpty())) {
return nullptr;
}
uint32_t length = 1;
uint32_t offset = aOffset - 1;
if (offset && NS_IS_SURROGATE_PAIR(data[offset - 1], data[offset])) {
++length;
--offset;
}
return DeleteTextTransaction::MaybeCreate(aEditorBase, aTextNode, offset,
length);
}
// static
already_AddRefed<DeleteTextTransaction>
DeleteTextTransaction::MaybeCreateForNextCharacter(EditorBase& aEditorBase,
Text& aTextNode,
uint32_t aOffset) {
nsAutoString data;
aTextNode.GetData(data);
if (NS_WARN_IF(aOffset >= data.Length()) || NS_WARN_IF(data.IsEmpty())) {
return nullptr;
}
uint32_t length = 1;
if (aOffset + 1 < data.Length() &&
NS_IS_SURROGATE_PAIR(data[aOffset], data[aOffset + 1])) {
++length;
}
return DeleteTextTransaction::MaybeCreate(aEditorBase, aTextNode, aOffset,
length);
}
DeleteTextTransaction::DeleteTextTransaction(EditorBase& aEditorBase,
Text& aTextNode, uint32_t aOffset,
uint32_t aLengthToDelete)
: DeleteContentTransactionBase(aEditorBase),
mOffset(aOffset),
mLengthToDelete(aLengthToDelete) {
MOZ_ASSERT(aTextNode.TextDataLength() >= aOffset + aLengthToDelete);
}
std::ostream& operator<<(std::ostream& aStream,
const DeleteTextTransaction& aTransaction) {
const auto* transactionForHTMLEditor =
aTransaction.GetAsDeleteTextFromTextNodeTransaction();
if (transactionForHTMLEditor) {
return aStream << *transactionForHTMLEditor;
}
aStream << "{ mOffset=" << aTransaction.mOffset
<< ", mLengthToDelete=" << aTransaction.mLengthToDelete
<< ", mDeletedText=\""
<< NS_ConvertUTF16toUTF8(aTransaction.mDeletedText).get() << "\""
<< ", mEditorBase=" << aTransaction.mEditorBase.get() << " }";
return aStream;
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTransaction,
DeleteContentTransactionBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTransaction)
NS_INTERFACE_MAP_END_INHERITING(DeleteContentTransactionBase)
Text* DeleteTextTransaction::GetTextNode() const {
if (MOZ_UNLIKELY(!mEditorBase)) {
return nullptr;
}
if (TextEditor* const textEditor = mEditorBase->GetAsTextEditor()) {
return textEditor->GetTextNode();
}
MOZ_ASSERT(GetAsDeleteTextFromTextNodeTransaction());
return GetAsDeleteTextFromTextNodeTransaction()->mTextNode;
}
NS_IMETHODIMP DeleteTextTransaction::DoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteTextTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
if (NS_WARN_IF(!mEditorBase)) {
return NS_ERROR_NOT_AVAILABLE;
}
const RefPtr<Text> textNode = GetTextNode();
if (NS_WARN_IF(!textNode) ||
(mEditorBase->IsHTMLEditor() &&
NS_WARN_IF(!HTMLEditUtils::IsSimplyEditableNode(*textNode)))) {
return NS_ERROR_NOT_AVAILABLE;
}
// Get the text that we're about to delete
IgnoredErrorResult error;
textNode->SubstringData(mOffset, mLengthToDelete, mDeletedText, error);
if (MOZ_UNLIKELY(error.Failed())) {
NS_WARNING("Text::SubstringData() failed");
return error.StealNSResult();
}
const OwningNonNull<EditorBase> editorBase = *mEditorBase;
editorBase->DoDeleteText(*textNode, mOffset, mLengthToDelete, error);
if (MOZ_UNLIKELY(error.Failed())) {
NS_WARNING("EditorBase::DoDeleteText() failed");
return error.StealNSResult();
}
editorBase->RangeUpdaterRef().SelAdjDeleteText(*textNode, mOffset,
mLengthToDelete);
return NS_OK;
}
EditorDOMPoint DeleteTextTransaction::SuggestPointToPutCaret() const {
if (NS_WARN_IF(!mEditorBase)) {
return EditorDOMPoint();
}
Text* const textNode = GetTextNode();
if (NS_WARN_IF(!textNode) ||
(mEditorBase->IsHTMLEditor() &&
NS_WARN_IF(!HTMLEditUtils::IsSimplyEditableNode(*textNode)))) {
return EditorDOMPoint();
}
if (NS_WARN_IF(textNode->TextDataLength() < mOffset)) {
return EditorDOMPoint();
}
EditorDOMPoint candidatePoint(textNode, mOffset);
if (!candidatePoint.IsInNativeAnonymousSubtreeInTextControl() &&
!HTMLEditUtils::IsSimplyEditableNode(*textNode)) {
return EditorDOMPoint();
}
return candidatePoint;
}
// XXX: We may want to store the selection state and restore it properly. Was
// it an insertion point or an extended selection?
NS_IMETHODIMP DeleteTextTransaction::UndoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteTextTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
if (NS_WARN_IF(!mEditorBase)) {
return NS_ERROR_NOT_AVAILABLE;
}
const RefPtr<Text> textNode = GetTextNode();
if (NS_WARN_IF(!textNode) ||
(mEditorBase->IsHTMLEditor() &&
NS_WARN_IF(!HTMLEditUtils::IsSimplyEditableNode(*textNode)))) {
return NS_ERROR_NOT_AVAILABLE;
}
const OwningNonNull<EditorBase> editorBase = *mEditorBase;
IgnoredErrorResult error;
editorBase->DoInsertText(*textNode, mOffset, mDeletedText, error);
NS_WARNING_ASSERTION(!error.Failed(), "EditorBase::DoInsertText() failed");
return error.StealNSResult();
}
NS_IMETHODIMP DeleteTextTransaction::RedoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteTextTransaction::%s this=%s", this, __FUNCTION__,
!mEditorBase || mEditorBase->IsTextEditor()
? ToString(*this).c_str()
: ToString(*GetAsDeleteTextFromTextNodeTransaction()).c_str()));
nsresult rv = DoTransaction();
if (NS_FAILED(rv)) {
NS_WARNING("DeleteTextTransaction::DoTransaction() failed");
return rv;
}
if (!mEditorBase || !mEditorBase->AllowsTransactionsToChangeSelection()) {
return NS_OK;
}
const OwningNonNull<EditorBase> editorBase = *mEditorBase;
rv = editorBase->CollapseSelectionTo(SuggestPointToPutCaret());
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::CollapseSelectionTo() failed");
return rv;
}
return NS_OK;
}
/******************************************************************************
* mozilla::DeleteTextFromTextNodeTransaction
******************************************************************************/
DeleteTextFromTextNodeTransaction::DeleteTextFromTextNodeTransaction(
EditorBase& aEditorBase, Text& aTextNode, uint32_t aOffset,
uint32_t aLengthToDelete)
: DeleteTextTransaction(aEditorBase, aTextNode, aOffset, aLengthToDelete),
mTextNode(&aTextNode) {
MOZ_ASSERT(aEditorBase.IsHTMLEditor());
MOZ_ASSERT(mTextNode->TextDataLength() >= aOffset + aLengthToDelete);
}
std::ostream& operator<<(
std::ostream& aStream,
const DeleteTextFromTextNodeTransaction& aTransaction) {
aStream << "{ mTextNode=" << aTransaction.mTextNode.get();
if (aTransaction.mTextNode) {
aStream << " (" << *aTransaction.mTextNode << ")";
}
aStream << ", mOffset=" << aTransaction.mOffset
<< ", mLengthToDelete=" << aTransaction.mLengthToDelete
<< ", mDeletedText=\""
<< NS_ConvertUTF16toUTF8(aTransaction.mDeletedText).get() << "\""
<< ", mEditorBase=" << aTransaction.mEditorBase.get() << " }";
return aStream;
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextFromTextNodeTransaction,
DeleteTextTransaction, mTextNode)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextFromTextNodeTransaction)
NS_INTERFACE_MAP_END_INHERITING(DeleteTextTransaction)
} // namespace mozilla
|