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 "InsertNodeTxn.h"
8 #include "mozilla/dom/Selection.h" // for Selection
10 #include "nsAString.h"
11 #include "nsDebug.h" // for NS_ENSURE_TRUE, etc
12 #include "nsEditor.h" // for nsEditor
13 #include "nsError.h" // for NS_ERROR_NULL_POINTER, etc
14 #include "nsIContent.h" // for nsIContent
15 #include "nsMemory.h" // for nsMemory
16 #include "nsReadableUtils.h" // for ToNewCString
17 #include "nsString.h" // for nsString
19 using namespace mozilla
;
20 using namespace mozilla::dom
;
22 InsertNodeTxn::InsertNodeTxn(nsIContent
& aNode
, nsINode
& aParent
,
23 int32_t aOffset
, nsEditor
& aEditor
)
32 InsertNodeTxn::~InsertNodeTxn()
36 NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertNodeTxn
, EditTxn
,
40 NS_IMPL_ADDREF_INHERITED(InsertNodeTxn
, EditTxn
)
41 NS_IMPL_RELEASE_INHERITED(InsertNodeTxn
, EditTxn
)
42 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertNodeTxn
)
43 NS_INTERFACE_MAP_END_INHERITING(EditTxn
)
46 InsertNodeTxn::DoTransaction()
48 MOZ_ASSERT(mNode
&& mParent
);
50 uint32_t count
= mParent
->GetChildCount();
51 if (mOffset
> static_cast<int32_t>(count
) || mOffset
== -1) {
52 // -1 is sentinel value meaning "append at end"
56 // Note, it's ok for ref to be null. That means append.
57 nsCOMPtr
<nsIContent
> ref
= mParent
->GetChildAt(mOffset
);
59 mEditor
.MarkNodeDirty(GetAsDOMNode(mNode
));
62 mParent
->InsertBefore(*mNode
, ref
, rv
);
63 NS_ENSURE_TRUE(!rv
.Failed(), rv
.StealNSResult());
65 // Only set selection to insertion point if editor gives permission
66 if (mEditor
.GetShouldTxnSetSelection()) {
67 RefPtr
<Selection
> selection
= mEditor
.GetSelection();
68 NS_ENSURE_TRUE(selection
, NS_ERROR_NULL_POINTER
);
69 // Place the selection just after the inserted element
70 selection
->Collapse(mParent
, mOffset
+ 1);
72 // Do nothing - DOM Range gravity will adjust selection
78 InsertNodeTxn::UndoTransaction()
80 MOZ_ASSERT(mNode
&& mParent
);
83 mParent
->RemoveChild(*mNode
, rv
);
84 return rv
.StealNSResult();
88 InsertNodeTxn::GetTxnDescription(nsAString
& aString
)
90 aString
.AssignLiteral("InsertNodeTxn");