2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-01-07 20:56:20 +03:00
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
#include "DeleteTextTransaction.h"
|
2016-07-08 07:10:13 +03:00
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2016-07-08 07:10:13 +03:00
|
|
|
#include "mozilla/EditorBase.h"
|
2017-12-15 14:43:26 +03:00
|
|
|
#include "mozilla/EditorDOMPoint.h"
|
2016-07-09 05:34:41 +03:00
|
|
|
#include "mozilla/SelectionState.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsAString.h"
|
1999-01-07 20:56:20 +03:00
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
namespace mozilla {
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
using namespace dom;
|
|
|
|
|
2017-12-15 14:43:26 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<DeleteTextTransaction> DeleteTextTransaction::MaybeCreate(
|
2019-07-22 06:53:29 +03:00
|
|
|
EditorBase& aEditorBase, Text& aTextNode, uint32_t aOffset,
|
2017-12-15 14:43:26 +03:00
|
|
|
uint32_t aLengthToDelete) {
|
|
|
|
RefPtr<DeleteTextTransaction> transaction = new DeleteTextTransaction(
|
2019-07-22 06:53:29 +03:00
|
|
|
aEditorBase, aTextNode, aOffset, aLengthToDelete);
|
2017-12-15 14:43:26 +03:00
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<DeleteTextTransaction>
|
|
|
|
DeleteTextTransaction::MaybeCreateForPreviousCharacter(EditorBase& aEditorBase,
|
2019-07-22 06:53:29 +03:00
|
|
|
Text& aTextNode,
|
2017-12-15 14:43:26 +03:00
|
|
|
uint32_t aOffset) {
|
|
|
|
if (NS_WARN_IF(!aOffset)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString data;
|
2019-07-22 06:53:29 +03:00
|
|
|
aTextNode.GetData(data);
|
2017-12-15 14:43:26 +03:00
|
|
|
if (NS_WARN_IF(data.IsEmpty())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t length = 1;
|
|
|
|
uint32_t offset = aOffset - 1;
|
|
|
|
if (offset && NS_IS_LOW_SURROGATE(data[offset]) &&
|
|
|
|
NS_IS_HIGH_SURROGATE(data[offset - 1])) {
|
|
|
|
++length;
|
|
|
|
--offset;
|
|
|
|
}
|
2019-07-22 06:53:29 +03:00
|
|
|
return DeleteTextTransaction::MaybeCreate(aEditorBase, aTextNode, offset,
|
2017-12-15 14:43:26 +03:00
|
|
|
length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<DeleteTextTransaction>
|
|
|
|
DeleteTextTransaction::MaybeCreateForNextCharacter(EditorBase& aEditorBase,
|
2019-07-22 06:53:29 +03:00
|
|
|
Text& aTextNode,
|
2017-12-15 14:43:26 +03:00
|
|
|
uint32_t aOffset) {
|
|
|
|
nsAutoString data;
|
2019-07-22 06:53:29 +03:00
|
|
|
aTextNode.GetData(data);
|
2017-12-15 14:43:26 +03:00
|
|
|
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_HIGH_SURROGATE(data[aOffset]) &&
|
|
|
|
NS_IS_LOW_SURROGATE(data[aOffset + 1])) {
|
|
|
|
++length;
|
|
|
|
}
|
2019-07-22 06:53:29 +03:00
|
|
|
return DeleteTextTransaction::MaybeCreate(aEditorBase, aTextNode, aOffset,
|
2017-12-15 14:43:26 +03:00
|
|
|
length);
|
|
|
|
}
|
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
DeleteTextTransaction::DeleteTextTransaction(EditorBase& aEditorBase,
|
2019-07-22 06:53:29 +03:00
|
|
|
Text& aTextNode, uint32_t aOffset,
|
2017-12-15 14:43:26 +03:00
|
|
|
uint32_t aLengthToDelete)
|
2017-03-21 13:00:36 +03:00
|
|
|
: mEditorBase(&aEditorBase),
|
2019-07-22 06:53:29 +03:00
|
|
|
mTextNode(&aTextNode),
|
2014-08-20 16:25:16 +04:00
|
|
|
mOffset(aOffset),
|
2017-12-15 14:43:26 +03:00
|
|
|
mLengthToDelete(aLengthToDelete) {
|
2019-07-22 06:53:29 +03:00
|
|
|
NS_ASSERTION(mTextNode->Length() >= aOffset + aLengthToDelete,
|
2014-08-20 16:25:16 +04:00
|
|
|
"Trying to delete more characters than in node");
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTransaction, EditTransactionBase,
|
2019-07-22 06:53:29 +03:00
|
|
|
mEditorBase, mTextNode)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTransaction)
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2017-03-10 07:38:28 +03:00
|
|
|
bool DeleteTextTransaction::CanDoIt() const {
|
2019-07-22 06:53:29 +03:00
|
|
|
if (NS_WARN_IF(!mTextNode) || NS_WARN_IF(!mEditorBase)) {
|
2017-03-10 07:38:28 +03:00
|
|
|
return false;
|
2007-06-28 06:48:16 +04:00
|
|
|
}
|
2019-07-22 06:53:29 +03:00
|
|
|
return mEditorBase->IsModifiableNode(*mTextNode);
|
1999-01-07 20:56:20 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 06:53:29 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
2012-06-08 13:58:29 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 09:20:22 +03:00
|
|
|
DeleteTextTransaction::DoTransaction() {
|
2019-07-22 06:53:29 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode)) {
|
2017-03-21 13:00:36 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2003-05-24 01:43:10 +04:00
|
|
|
|
2014-08-20 16:25:16 +04:00
|
|
|
// Get the text that we're about to delete
|
2018-03-19 22:18:07 +03:00
|
|
|
ErrorResult err;
|
2019-07-22 06:53:29 +03:00
|
|
|
mTextNode->SubstringData(mOffset, mLengthToDelete, mDeletedText, err);
|
2018-03-19 22:18:07 +03:00
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
|
|
|
}
|
|
|
|
|
2019-07-22 06:53:29 +03:00
|
|
|
RefPtr<EditorBase> editorBase = mEditorBase;
|
|
|
|
RefPtr<Text> textNode = mTextNode;
|
|
|
|
editorBase->DoDeleteText(*textNode, mOffset, mLengthToDelete, err);
|
2018-03-19 22:18:07 +03:00
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
2012-06-08 13:58:29 +04:00
|
|
|
}
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2019-07-22 06:53:29 +03:00
|
|
|
editorBase->RangeUpdaterRef().SelAdjDeleteText(textNode, mOffset,
|
|
|
|
mLengthToDelete);
|
2017-12-15 14:43:26 +03:00
|
|
|
|
2019-07-22 06:53:29 +03:00
|
|
|
if (!editorBase->AllowsTransactionsToChangeSelection()) {
|
2018-08-01 14:53:57 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-22 06:53:29 +03:00
|
|
|
RefPtr<Selection> selection = editorBase->GetSelection();
|
2018-08-01 14:53:57 +03:00
|
|
|
if (NS_WARN_IF(!selection)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
ErrorResult error;
|
2019-07-22 06:53:29 +03:00
|
|
|
selection->Collapse(EditorRawDOMPoint(textNode, mOffset), error);
|
2018-08-01 14:53:57 +03:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
1999-02-22 18:53:31 +03:00
|
|
|
}
|
2012-06-08 13:58:29 +04:00
|
|
|
return NS_OK;
|
1999-01-07 20:56:20 +03:00
|
|
|
}
|
|
|
|
|
2014-08-20 16:25:16 +04:00
|
|
|
// XXX: We may want to store the selection state and restore it properly. Was
|
|
|
|
// it an insertion point or an extended selection?
|
2019-07-22 06:53:29 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
2012-06-08 13:58:29 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 09:20:22 +03:00
|
|
|
DeleteTextTransaction::UndoTransaction() {
|
2019-07-22 06:53:29 +03:00
|
|
|
if (NS_WARN_IF(!mTextNode) || NS_WARN_IF(!mEditorBase)) {
|
2017-03-21 13:00:36 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2019-07-22 06:53:29 +03:00
|
|
|
RefPtr<EditorBase> editorBase = mEditorBase;
|
|
|
|
RefPtr<Text> textNode = mTextNode;
|
|
|
|
ErrorResult error;
|
|
|
|
editorBase->DoInsertText(*textNode, mOffset, mDeletedText, error);
|
|
|
|
return error.StealNSResult();
|
1999-01-07 20:56:20 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
} // namespace mozilla
|