|
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 <stdio.h> // for printf |
|
7 |
|
8 #include "InsertElementTxn.h" |
|
9 #include "nsAString.h" |
|
10 #include "nsDebug.h" // for NS_ENSURE_TRUE, etc |
|
11 #include "nsError.h" // for NS_ERROR_NULL_POINTER, etc |
|
12 #include "nsIContent.h" // for nsIContent |
|
13 #include "nsIEditor.h" // for nsIEditor |
|
14 #include "nsINode.h" // for nsINode |
|
15 #include "nsISelection.h" // for nsISelection |
|
16 #include "nsMemory.h" // for nsMemory |
|
17 #include "nsReadableUtils.h" // for ToNewCString |
|
18 #include "nsString.h" // for nsString |
|
19 |
|
20 InsertElementTxn::InsertElementTxn() |
|
21 : EditTxn() |
|
22 { |
|
23 } |
|
24 |
|
25 NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertElementTxn, EditTxn, |
|
26 mNode, |
|
27 mParent) |
|
28 |
|
29 NS_IMPL_ADDREF_INHERITED(InsertElementTxn, EditTxn) |
|
30 NS_IMPL_RELEASE_INHERITED(InsertElementTxn, EditTxn) |
|
31 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertElementTxn) |
|
32 NS_INTERFACE_MAP_END_INHERITING(EditTxn) |
|
33 |
|
34 NS_IMETHODIMP InsertElementTxn::Init(nsIDOMNode *aNode, |
|
35 nsIDOMNode *aParent, |
|
36 int32_t aOffset, |
|
37 nsIEditor *aEditor) |
|
38 { |
|
39 NS_ASSERTION(aNode && aParent && aEditor, "bad arg"); |
|
40 NS_ENSURE_TRUE(aNode && aParent && aEditor, NS_ERROR_NULL_POINTER); |
|
41 |
|
42 mNode = do_QueryInterface(aNode); |
|
43 mParent = do_QueryInterface(aParent); |
|
44 mOffset = aOffset; |
|
45 mEditor = aEditor; |
|
46 NS_ENSURE_TRUE(mNode && mParent && mEditor, NS_ERROR_INVALID_ARG); |
|
47 return NS_OK; |
|
48 } |
|
49 |
|
50 |
|
51 NS_IMETHODIMP InsertElementTxn::DoTransaction(void) |
|
52 { |
|
53 NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED); |
|
54 |
|
55 nsCOMPtr<nsINode> parent = do_QueryInterface(mParent); |
|
56 NS_ENSURE_STATE(parent); |
|
57 |
|
58 uint32_t count = parent->GetChildCount(); |
|
59 if (mOffset > int32_t(count) || mOffset == -1) { |
|
60 // -1 is sentinel value meaning "append at end" |
|
61 mOffset = count; |
|
62 } |
|
63 |
|
64 nsCOMPtr<nsIContent> refContent = parent->GetChildAt(mOffset); |
|
65 // note, it's ok for refNode to be null. that means append |
|
66 nsCOMPtr<nsIDOMNode> refNode = refContent ? refContent->AsDOMNode() : nullptr; |
|
67 |
|
68 mEditor->MarkNodeDirty(mNode); |
|
69 |
|
70 nsCOMPtr<nsIDOMNode> resultNode; |
|
71 nsresult result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode)); |
|
72 NS_ENSURE_SUCCESS(result, result); |
|
73 NS_ENSURE_TRUE(resultNode, NS_ERROR_NULL_POINTER); |
|
74 |
|
75 // only set selection to insertion point if editor gives permission |
|
76 bool bAdjustSelection; |
|
77 mEditor->ShouldTxnSetSelection(&bAdjustSelection); |
|
78 if (bAdjustSelection) |
|
79 { |
|
80 nsCOMPtr<nsISelection> selection; |
|
81 result = mEditor->GetSelection(getter_AddRefs(selection)); |
|
82 NS_ENSURE_SUCCESS(result, result); |
|
83 NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); |
|
84 // place the selection just after the inserted element |
|
85 selection->Collapse(mParent, mOffset+1); |
|
86 } |
|
87 else |
|
88 { |
|
89 // do nothing - dom range gravity will adjust selection |
|
90 } |
|
91 return result; |
|
92 } |
|
93 |
|
94 NS_IMETHODIMP InsertElementTxn::UndoTransaction(void) |
|
95 { |
|
96 NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED); |
|
97 |
|
98 nsCOMPtr<nsIDOMNode> resultNode; |
|
99 return mParent->RemoveChild(mNode, getter_AddRefs(resultNode)); |
|
100 } |
|
101 |
|
102 NS_IMETHODIMP InsertElementTxn::GetTxnDescription(nsAString& aString) |
|
103 { |
|
104 aString.AssignLiteral("InsertElementTxn"); |
|
105 return NS_OK; |
|
106 } |