Bug 1686495 [wpt PR 27132] - Add tests for proposed WebDriver Shadow DOM support...
[gecko.git] / editor / libeditor / DeleteRangeTransaction.cpp
blob0980201c0b9f44b371ca4b50bf59edb297c3dca5
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/. */
6 #include "DeleteRangeTransaction.h"
8 #include "DeleteNodeTransaction.h"
9 #include "DeleteTextTransaction.h"
10 #include "mozilla/Assertions.h"
11 #include "mozilla/ContentIterator.h"
12 #include "mozilla/dom/Selection.h"
13 #include "mozilla/EditorBase.h"
14 #include "mozilla/mozalloc.h"
15 #include "mozilla/RangeBoundary.h"
16 #include "nsCOMPtr.h"
17 #include "nsDebug.h"
18 #include "nsError.h"
19 #include "nsIContent.h"
20 #include "nsINode.h"
21 #include "nsAString.h"
23 namespace mozilla {
25 using namespace dom;
27 DeleteRangeTransaction::DeleteRangeTransaction(EditorBase& aEditorBase,
28 const nsRange& aRangeToDelete)
29 : mEditorBase(&aEditorBase), mRangeToDelete(aRangeToDelete.CloneRange()) {}
31 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteRangeTransaction,
32 EditAggregateTransaction, mEditorBase,
33 mRangeToDelete)
35 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteRangeTransaction)
36 NS_INTERFACE_MAP_END_INHERITING(EditAggregateTransaction)
38 NS_IMETHODIMP DeleteRangeTransaction::DoTransaction() {
39 if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mRangeToDelete)) {
40 return NS_ERROR_NOT_AVAILABLE;
43 // Swap mRangeToDelete out into a stack variable, so we make sure to null it
44 // out on return from this function. Once this function returns, we no longer
45 // need mRangeToDelete, and keeping it alive in the long term slows down all
46 // DOM mutations because it's observing them.
47 RefPtr<nsRange> rangeToDelete;
48 rangeToDelete.swap(mRangeToDelete);
50 // build the child transactions
51 const RangeBoundary& startRef = rangeToDelete->StartRef();
52 const RangeBoundary& endRef = rangeToDelete->EndRef();
53 MOZ_ASSERT(startRef.IsSetAndValid());
54 MOZ_ASSERT(endRef.IsSetAndValid());
56 if (startRef.Container() == endRef.Container()) {
57 // the selection begins and ends in the same node
58 nsresult rv = CreateTxnsToDeleteBetween(startRef.AsRaw(), endRef.AsRaw());
59 if (NS_FAILED(rv)) {
60 NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteBetween() failed");
61 return rv;
63 } else {
64 // the selection ends in a different node from where it started. delete
65 // the relevant content in the start node
66 nsresult rv = CreateTxnsToDeleteContent(startRef.AsRaw(), nsIEditor::eNext);
67 if (NS_FAILED(rv)) {
68 NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteContent() failed");
69 return rv;
71 // delete the intervening nodes
72 rv = CreateTxnsToDeleteNodesBetween(rangeToDelete);
73 if (NS_FAILED(rv)) {
74 NS_WARNING(
75 "DeleteRangeTransaction::CreateTxnsToDeleteNodesBetween() failed");
76 return rv;
78 // delete the relevant content in the end node
79 rv = CreateTxnsToDeleteContent(endRef.AsRaw(), nsIEditor::ePrevious);
80 if (NS_FAILED(rv)) {
81 NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteContent() failed");
82 return rv;
86 // if we've successfully built this aggregate transaction, then do it.
87 nsresult rv = EditAggregateTransaction::DoTransaction();
88 if (NS_FAILED(rv)) {
89 NS_WARNING("EditAggregateTransaction::DoTransaction() failed");
90 return rv;
93 if (!mEditorBase->AllowsTransactionsToChangeSelection()) {
94 return NS_OK;
97 RefPtr<Selection> selection = mEditorBase->GetSelection();
98 if (NS_WARN_IF(!selection)) {
99 return NS_ERROR_NOT_INITIALIZED;
101 rv = selection->CollapseInLimiter(startRef.AsRaw());
102 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
103 "Selection::CollapseInLimiter() failed");
104 return rv;
107 NS_IMETHODIMP DeleteRangeTransaction::UndoTransaction() {
108 nsresult rv = EditAggregateTransaction::UndoTransaction();
109 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
110 "EditAggregateTransaction::UndoTransaction() failed");
111 return rv;
114 NS_IMETHODIMP DeleteRangeTransaction::RedoTransaction() {
115 nsresult rv = EditAggregateTransaction::RedoTransaction();
116 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
117 "EditAggregateTransaction::RedoTransaction() failed");
118 return rv;
121 nsresult DeleteRangeTransaction::CreateTxnsToDeleteBetween(
122 const RawRangeBoundary& aStart, const RawRangeBoundary& aEnd) {
123 if (NS_WARN_IF(!aStart.IsSetAndValid()) ||
124 NS_WARN_IF(!aEnd.IsSetAndValid()) ||
125 NS_WARN_IF(aStart.Container() != aEnd.Container())) {
126 return NS_ERROR_INVALID_ARG;
129 if (NS_WARN_IF(!mEditorBase)) {
130 return NS_ERROR_NOT_AVAILABLE;
133 // see what kind of node we have
134 if (Text* textNode = Text::FromNode(aStart.Container())) {
135 // if the node is a chardata node, then delete chardata content
136 int32_t numToDel;
137 if (aStart == aEnd) {
138 numToDel = 1;
139 } else {
140 numToDel = *aEnd.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets) -
141 *aStart.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets);
142 MOZ_DIAGNOSTIC_ASSERT(numToDel > 0);
145 RefPtr<DeleteTextTransaction> deleteTextTransaction =
146 DeleteTextTransaction::MaybeCreate(
147 *mEditorBase, *textNode,
148 *aStart.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets),
149 numToDel);
150 // If the text node isn't editable, it should be never undone/redone.
151 // So, the transaction shouldn't be recorded.
152 if (!deleteTextTransaction) {
153 NS_WARNING("DeleteTextTransaction::MaybeCreate() failed");
154 return NS_ERROR_FAILURE;
156 DebugOnly<nsresult> rvIgnored = AppendChild(deleteTextTransaction);
157 NS_WARNING_ASSERTION(
158 NS_SUCCEEDED(rvIgnored),
159 "DeleteRangeTransaction::AppendChild() failed, but ignored");
160 return NS_OK;
163 // Even if we detect invalid range, we should ignore it for removing
164 // specified range's nodes as far as possible.
165 // XXX This is super expensive. Probably, we should make
166 // DeleteNodeTransaction() can treat multiple siblings.
167 for (nsIContent* child = aStart.GetChildAtOffset();
168 child && child != aEnd.GetChildAtOffset();
169 child = child->GetNextSibling()) {
170 RefPtr<DeleteNodeTransaction> deleteNodeTransaction =
171 DeleteNodeTransaction::MaybeCreate(*mEditorBase, *child);
172 // XXX This is odd handling. Even if some children are not editable,
173 // editor should append transactions because they could be editable
174 // at undoing/redoing. Additionally, if the transaction needs to
175 // delete/restore all nodes, it should at undoing/redoing.
176 if (deleteNodeTransaction) {
177 DebugOnly<nsresult> rvIgnored = AppendChild(deleteNodeTransaction);
178 NS_WARNING_ASSERTION(
179 NS_SUCCEEDED(rvIgnored),
180 "DeleteRangeTransaction::AppendChild() failed, but ignored");
184 return NS_OK;
187 nsresult DeleteRangeTransaction::CreateTxnsToDeleteContent(
188 const RawRangeBoundary& aPoint, nsIEditor::EDirection aAction) {
189 if (NS_WARN_IF(!aPoint.IsSetAndValid())) {
190 return NS_ERROR_INVALID_ARG;
193 if (NS_WARN_IF(!mEditorBase)) {
194 return NS_ERROR_NOT_AVAILABLE;
197 Text* textNode = Text::FromNode(aPoint.Container());
198 if (!textNode) {
199 return NS_OK;
202 // If the node is a chardata node, then delete chardata content
203 uint32_t startOffset, numToDelete;
204 if (nsIEditor::eNext == aAction) {
205 startOffset = *aPoint.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets);
206 numToDelete = aPoint.Container()->Length() - startOffset;
207 } else {
208 startOffset = 0;
209 numToDelete = *aPoint.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets);
212 if (!numToDelete) {
213 return NS_OK;
216 RefPtr<DeleteTextTransaction> deleteTextTransaction =
217 DeleteTextTransaction::MaybeCreate(*mEditorBase, *textNode, startOffset,
218 numToDelete);
219 NS_WARNING_ASSERTION(deleteTextTransaction,
220 "DeleteTextTransaction::MaybeCreate() failed");
221 // If the text node isn't editable, it should be never undone/redone.
222 // So, the transaction shouldn't be recorded.
223 if (!deleteTextTransaction) {
224 return NS_ERROR_FAILURE;
226 DebugOnly<nsresult> rvIgnored = AppendChild(deleteTextTransaction);
227 NS_WARNING_ASSERTION(
228 NS_SUCCEEDED(rvIgnored),
229 "DeleteRangeTransaction::AppendChild() failed, but ignored");
230 return NS_OK;
233 nsresult DeleteRangeTransaction::CreateTxnsToDeleteNodesBetween(
234 nsRange* aRangeToDelete) {
235 if (NS_WARN_IF(!mEditorBase)) {
236 return NS_ERROR_NOT_AVAILABLE;
239 ContentSubtreeIterator subtreeIter;
240 nsresult rv = subtreeIter.Init(aRangeToDelete);
241 if (NS_FAILED(rv)) {
242 NS_WARNING("ContentSubtreeIterator::Init() failed");
243 return rv;
246 for (; !subtreeIter.IsDone(); subtreeIter.Next()) {
247 nsINode* node = subtreeIter.GetCurrentNode();
248 if (NS_WARN_IF(!node) || NS_WARN_IF(!node->IsContent())) {
249 return NS_ERROR_FAILURE;
252 RefPtr<DeleteNodeTransaction> deleteNodeTransaction =
253 DeleteNodeTransaction::MaybeCreate(*mEditorBase, *node->AsContent());
254 // XXX This is odd handling. Even if some nodes in the range are not
255 // editable, editor should append transactions because they could
256 // at undoing/redoing. Additionally, if the transaction needs to
257 // delete/restore all nodes, it should at undoing/redoing.
258 if (!deleteNodeTransaction) {
259 NS_WARNING("DeleteNodeTransaction::MaybeCreate() failed");
260 return NS_ERROR_FAILURE;
262 DebugOnly<nsresult> rvIgnored = AppendChild(deleteNodeTransaction);
263 NS_WARNING_ASSERTION(
264 NS_SUCCEEDED(rvIgnored),
265 "DeleteRangeTransaction::AppendChild() failed, but ignored");
267 return NS_OK;
270 } // namespace mozilla