1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998-1999
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "InsertElementTxn.h"
39 : #include "nsISelection.h"
40 : #include "nsIContent.h"
41 : #include "nsIDOMNodeList.h"
42 : #include "nsReadableUtils.h"
43 :
44 : #ifdef NS_DEBUG
45 : static bool gNoisy = false;
46 : #endif
47 :
48 :
49 0 : InsertElementTxn::InsertElementTxn()
50 0 : : EditTxn()
51 : {
52 0 : }
53 :
54 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(InsertElementTxn)
55 :
56 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(InsertElementTxn, EditTxn)
57 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mNode)
58 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
59 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
60 :
61 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(InsertElementTxn, EditTxn)
62 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mNode)
63 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
64 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
65 :
66 0 : NS_IMPL_ADDREF_INHERITED(InsertElementTxn, EditTxn)
67 0 : NS_IMPL_RELEASE_INHERITED(InsertElementTxn, EditTxn)
68 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertElementTxn)
69 0 : NS_INTERFACE_MAP_END_INHERITING(EditTxn)
70 :
71 0 : NS_IMETHODIMP InsertElementTxn::Init(nsIDOMNode *aNode,
72 : nsIDOMNode *aParent,
73 : PRInt32 aOffset,
74 : nsIEditor *aEditor)
75 : {
76 0 : NS_ASSERTION(aNode && aParent && aEditor, "bad arg");
77 0 : NS_ENSURE_TRUE(aNode && aParent && aEditor, NS_ERROR_NULL_POINTER);
78 :
79 0 : mNode = do_QueryInterface(aNode);
80 0 : mParent = do_QueryInterface(aParent);
81 0 : mOffset = aOffset;
82 0 : mEditor = aEditor;
83 0 : NS_ENSURE_TRUE(mNode && mParent && mEditor, NS_ERROR_INVALID_ARG);
84 0 : return NS_OK;
85 : }
86 :
87 :
88 0 : NS_IMETHODIMP InsertElementTxn::DoTransaction(void)
89 : {
90 : #ifdef NS_DEBUG
91 0 : if (gNoisy)
92 : {
93 0 : nsCOMPtr<nsIContent>nodeAsContent = do_QueryInterface(mNode);
94 0 : nsCOMPtr<nsIContent>parentAsContent = do_QueryInterface(mParent);
95 0 : nsString namestr;
96 0 : mNode->GetNodeName(namestr);
97 0 : char* nodename = ToNewCString(namestr);
98 : printf("%p Do Insert Element of %p <%s> into parent %p at offset %d\n",
99 : static_cast<void*>(this),
100 0 : static_cast<void*>(nodeAsContent.get()),
101 : nodename,
102 0 : static_cast<void*>(parentAsContent.get()),
103 0 : mOffset);
104 0 : nsMemory::Free(nodename);
105 : }
106 : #endif
107 :
108 0 : NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED);
109 :
110 0 : nsCOMPtr<nsIDOMNodeList> childNodes;
111 0 : nsresult result = mParent->GetChildNodes(getter_AddRefs(childNodes));
112 0 : NS_ENSURE_SUCCESS(result, result);
113 0 : nsCOMPtr<nsIDOMNode>refNode;
114 0 : if (childNodes)
115 : {
116 : PRUint32 count;
117 0 : childNodes->GetLength(&count);
118 0 : if (mOffset>(PRInt32)count) mOffset = count;
119 : // -1 is sentinel value meaning "append at end"
120 0 : if (mOffset == -1) mOffset = count;
121 0 : result = childNodes->Item(mOffset, getter_AddRefs(refNode));
122 0 : NS_ENSURE_SUCCESS(result, result);
123 : // note, it's ok for mRefNode to be null. that means append
124 : }
125 :
126 0 : mEditor->MarkNodeDirty(mNode);
127 :
128 0 : nsCOMPtr<nsIDOMNode> resultNode;
129 0 : result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode));
130 0 : NS_ENSURE_SUCCESS(result, result);
131 0 : NS_ENSURE_TRUE(resultNode, NS_ERROR_NULL_POINTER);
132 :
133 : // only set selection to insertion point if editor gives permission
134 : bool bAdjustSelection;
135 0 : mEditor->ShouldTxnSetSelection(&bAdjustSelection);
136 0 : if (bAdjustSelection)
137 : {
138 0 : nsCOMPtr<nsISelection> selection;
139 0 : result = mEditor->GetSelection(getter_AddRefs(selection));
140 0 : NS_ENSURE_SUCCESS(result, result);
141 0 : NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
142 : // place the selection just after the inserted element
143 0 : selection->Collapse(mParent, mOffset+1);
144 : }
145 : else
146 : {
147 : // do nothing - dom range gravity will adjust selection
148 : }
149 0 : return result;
150 : }
151 :
152 0 : NS_IMETHODIMP InsertElementTxn::UndoTransaction(void)
153 : {
154 : #ifdef NS_DEBUG
155 0 : if (gNoisy)
156 : {
157 : printf("%p Undo Insert Element of %p into parent %p at offset %d\n",
158 : static_cast<void*>(this),
159 0 : static_cast<void*>(mNode.get()),
160 0 : static_cast<void*>(mParent.get()),
161 0 : mOffset);
162 : }
163 : #endif
164 :
165 0 : NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED);
166 :
167 0 : nsCOMPtr<nsIDOMNode> resultNode;
168 0 : return mParent->RemoveChild(mNode, getter_AddRefs(resultNode));
169 : }
170 :
171 0 : NS_IMETHODIMP InsertElementTxn::GetTxnDescription(nsAString& aString)
172 : {
173 0 : aString.AssignLiteral("InsertElementTxn");
174 0 : return NS_OK;
175 4392 : }
|