Bug 1247796. Use keyboardFocusIndicatorColor for ActiveBorder system color keyword...
[gecko.git] / editor / libeditor / InsertNodeTxn.cpp
blobbb9d793602fa9e10e7a47129a4ae032efc33fbf6
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)
24 : EditTxn()
25 , mNode(&aNode)
26 , mParent(&aParent)
27 , mOffset(aOffset)
28 , mEditor(aEditor)
32 InsertNodeTxn::~InsertNodeTxn()
36 NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertNodeTxn, EditTxn,
37 mNode,
38 mParent)
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)
45 NS_IMETHODIMP
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"
53 mOffset = count;
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));
61 ErrorResult rv;
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);
71 } else {
72 // Do nothing - DOM Range gravity will adjust selection
74 return NS_OK;
77 NS_IMETHODIMP
78 InsertNodeTxn::UndoTransaction()
80 MOZ_ASSERT(mNode && mParent);
82 ErrorResult rv;
83 mParent->RemoveChild(*mNode, rv);
84 return rv.StealNSResult();
87 NS_IMETHODIMP
88 InsertNodeTxn::GetTxnDescription(nsAString& aString)
90 aString.AssignLiteral("InsertNodeTxn");
91 return NS_OK;