editor/libeditor/base/DeleteTextTxn.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:49b24cd9dc58
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 "DeleteTextTxn.h"
7 #include "mozilla/Assertions.h"
8 #include "mozilla/dom/Selection.h"
9 #include "nsAutoPtr.h"
10 #include "nsDebug.h"
11 #include "nsEditor.h"
12 #include "nsError.h"
13 #include "nsIEditor.h"
14 #include "nsISelection.h"
15 #include "nsISupportsImpl.h"
16 #include "nsSelectionState.h"
17 #include "nsAString.h"
18
19 using namespace mozilla;
20 using namespace mozilla::dom;
21
22 DeleteTextTxn::DeleteTextTxn() :
23 EditTxn(),
24 mEditor(nullptr),
25 mCharData(),
26 mOffset(0),
27 mNumCharsToDelete(0),
28 mRangeUpdater(nullptr)
29 {
30 }
31
32 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTxn, EditTxn,
33 mCharData)
34
35 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTxn)
36 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
37
38 NS_IMETHODIMP
39 DeleteTextTxn::Init(nsEditor* aEditor,
40 nsIDOMCharacterData* aCharData,
41 uint32_t aOffset,
42 uint32_t aNumCharsToDelete,
43 nsRangeUpdater* aRangeUpdater)
44 {
45 MOZ_ASSERT(aEditor && aCharData);
46
47 mEditor = aEditor;
48 mCharData = aCharData;
49
50 // do nothing if the node is read-only
51 if (!mEditor->IsModifiableNode(mCharData)) {
52 return NS_ERROR_FAILURE;
53 }
54
55 mOffset = aOffset;
56 mNumCharsToDelete = aNumCharsToDelete;
57 #ifdef DEBUG
58 uint32_t length;
59 mCharData->GetLength(&length);
60 NS_ASSERTION(length >= aOffset + aNumCharsToDelete,
61 "Trying to delete more characters than in node");
62 #endif
63 mDeletedText.Truncate();
64 mRangeUpdater = aRangeUpdater;
65 return NS_OK;
66 }
67
68 NS_IMETHODIMP
69 DeleteTextTxn::DoTransaction()
70 {
71 MOZ_ASSERT(mEditor && mCharData);
72
73 // get the text that we're about to delete
74 nsresult res = mCharData->SubstringData(mOffset, mNumCharsToDelete,
75 mDeletedText);
76 MOZ_ASSERT(NS_SUCCEEDED(res));
77 res = mCharData->DeleteData(mOffset, mNumCharsToDelete);
78 NS_ENSURE_SUCCESS(res, res);
79
80 if (mRangeUpdater) {
81 mRangeUpdater->SelAdjDeleteText(mCharData, mOffset, mNumCharsToDelete);
82 }
83
84 // only set selection to deletion point if editor gives permission
85 bool bAdjustSelection;
86 mEditor->ShouldTxnSetSelection(&bAdjustSelection);
87 if (bAdjustSelection) {
88 nsRefPtr<Selection> selection = mEditor->GetSelection();
89 NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
90 res = selection->Collapse(mCharData, mOffset);
91 NS_ASSERTION(NS_SUCCEEDED(res),
92 "selection could not be collapsed after undo of deletetext.");
93 NS_ENSURE_SUCCESS(res, res);
94 }
95 // else do nothing - dom range gravity will adjust selection
96 return NS_OK;
97 }
98
99 //XXX: we may want to store the selection state and restore it properly
100 // was it an insertion point or an extended selection?
101 NS_IMETHODIMP
102 DeleteTextTxn::UndoTransaction()
103 {
104 MOZ_ASSERT(mEditor && mCharData);
105
106 return mCharData->InsertData(mOffset, mDeletedText);
107 }
108
109 NS_IMETHODIMP
110 DeleteTextTxn::GetTxnDescription(nsAString& aString)
111 {
112 aString.AssignLiteral("DeleteTextTxn: ");
113 aString += mDeletedText;
114 return NS_OK;
115 }

mercurial