Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "DeleteTextTransaction.h"
7 :
8 : #include "mozilla/Assertions.h"
9 : #include "mozilla/EditorBase.h"
10 : #include "mozilla/SelectionState.h"
11 : #include "mozilla/dom/Selection.h"
12 : #include "nsDebug.h"
13 : #include "nsError.h"
14 : #include "nsIEditor.h"
15 : #include "nsISupportsImpl.h"
16 : #include "nsAString.h"
17 :
18 : namespace mozilla {
19 :
20 : using namespace dom;
21 :
22 0 : DeleteTextTransaction::DeleteTextTransaction(
23 : EditorBase& aEditorBase,
24 : nsGenericDOMDataNode& aCharData,
25 : uint32_t aOffset,
26 : uint32_t aNumCharsToDelete,
27 0 : RangeUpdater* aRangeUpdater)
28 : : mEditorBase(&aEditorBase)
29 : , mCharData(&aCharData)
30 : , mOffset(aOffset)
31 : , mNumCharsToDelete(aNumCharsToDelete)
32 0 : , mRangeUpdater(aRangeUpdater)
33 : {
34 0 : NS_ASSERTION(mCharData->Length() >= aOffset + aNumCharsToDelete,
35 : "Trying to delete more characters than in node");
36 0 : }
37 :
38 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTransaction, EditTransactionBase,
39 : mEditorBase,
40 : mCharData)
41 :
42 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTransaction)
43 0 : NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
44 :
45 : bool
46 0 : DeleteTextTransaction::CanDoIt() const
47 : {
48 0 : if (NS_WARN_IF(!mCharData) || NS_WARN_IF(!mEditorBase)) {
49 0 : return false;
50 : }
51 0 : return mEditorBase->IsModifiableNode(mCharData);
52 : }
53 :
54 : NS_IMETHODIMP
55 0 : DeleteTextTransaction::DoTransaction()
56 : {
57 0 : if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mCharData)) {
58 0 : return NS_ERROR_NOT_AVAILABLE;
59 : }
60 :
61 : // Get the text that we're about to delete
62 0 : nsresult rv = mCharData->SubstringData(mOffset, mNumCharsToDelete,
63 0 : mDeletedText);
64 0 : MOZ_ASSERT(NS_SUCCEEDED(rv));
65 0 : rv = mCharData->DeleteData(mOffset, mNumCharsToDelete);
66 0 : NS_ENSURE_SUCCESS(rv, rv);
67 :
68 0 : if (mRangeUpdater) {
69 0 : mRangeUpdater->SelAdjDeleteText(mCharData, mOffset, mNumCharsToDelete);
70 : }
71 :
72 : // Only set selection to deletion point if editor gives permission
73 0 : if (mEditorBase->GetShouldTxnSetSelection()) {
74 0 : RefPtr<Selection> selection = mEditorBase->GetSelection();
75 0 : NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
76 0 : rv = selection->Collapse(mCharData, mOffset);
77 0 : NS_ASSERTION(NS_SUCCEEDED(rv),
78 : "Selection could not be collapsed after undo of deletetext");
79 0 : NS_ENSURE_SUCCESS(rv, rv);
80 : }
81 : // Else do nothing - DOM Range gravity will adjust selection
82 0 : return NS_OK;
83 : }
84 :
85 : //XXX: We may want to store the selection state and restore it properly. Was
86 : // it an insertion point or an extended selection?
87 : NS_IMETHODIMP
88 0 : DeleteTextTransaction::UndoTransaction()
89 : {
90 0 : if (NS_WARN_IF(!mCharData)) {
91 0 : return NS_ERROR_NOT_INITIALIZED;
92 : }
93 0 : return mCharData->InsertData(mOffset, mDeletedText);
94 : }
95 :
96 : NS_IMETHODIMP
97 0 : DeleteTextTransaction::GetTxnDescription(nsAString& aString)
98 : {
99 0 : aString.AssignLiteral("DeleteTextTransaction: ");
100 0 : aString += mDeletedText;
101 0 : return NS_OK;
102 : }
103 :
104 : } // namespace mozilla
|