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 "SplitElementTxn.h"
39 : #include "nsEditor.h"
40 : #include "nsIDOMNode.h"
41 : #include "nsISelection.h"
42 : #include "nsIDOMCharacterData.h"
43 :
44 : #ifdef NS_DEBUG
45 : static bool gNoisy = false;
46 : #endif
47 :
48 :
49 : // note that aEditor is not refcounted
50 0 : SplitElementTxn::SplitElementTxn()
51 0 : : EditTxn()
52 : {
53 0 : }
54 :
55 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(SplitElementTxn)
56 :
57 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SplitElementTxn, EditTxn)
58 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
59 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mNewLeftNode)
60 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
61 :
62 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SplitElementTxn, EditTxn)
63 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
64 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mNewLeftNode)
65 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
66 :
67 0 : NS_IMPL_ADDREF_INHERITED(SplitElementTxn, EditTxn)
68 0 : NS_IMPL_RELEASE_INHERITED(SplitElementTxn, EditTxn)
69 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SplitElementTxn)
70 0 : NS_INTERFACE_MAP_END_INHERITING(EditTxn)
71 :
72 0 : NS_IMETHODIMP SplitElementTxn::Init(nsEditor *aEditor,
73 : nsIDOMNode *aNode,
74 : PRInt32 aOffset)
75 : {
76 0 : NS_ASSERTION(aEditor && aNode, "bad args");
77 0 : if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; }
78 :
79 0 : mEditor = aEditor;
80 0 : mExistingRightNode = do_QueryInterface(aNode);
81 0 : mOffset = aOffset;
82 0 : return NS_OK;
83 : }
84 :
85 0 : NS_IMETHODIMP SplitElementTxn::DoTransaction(void)
86 : {
87 : #ifdef NS_DEBUG
88 0 : if (gNoisy)
89 : {
90 : printf("%p Do Split of node %p offset %d\n",
91 : static_cast<void*>(this),
92 0 : static_cast<void*>(mExistingRightNode.get()),
93 0 : mOffset);
94 : }
95 : #endif
96 :
97 0 : NS_ASSERTION(mExistingRightNode && mEditor, "bad state");
98 0 : if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
99 :
100 : // create a new node
101 0 : nsresult result = mExistingRightNode->CloneNode(false, 1, getter_AddRefs(mNewLeftNode));
102 0 : NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element.");
103 0 : NS_ENSURE_SUCCESS(result, result);
104 0 : NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NULL_POINTER);
105 0 : mEditor->MarkNodeDirty(mExistingRightNode);
106 :
107 : #ifdef NS_DEBUG
108 0 : if (gNoisy)
109 : {
110 : printf(" created left node = %p\n",
111 0 : static_cast<void*>(mNewLeftNode.get()));
112 : }
113 : #endif
114 :
115 : // get the parent node
116 0 : result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
117 0 : NS_ENSURE_SUCCESS(result, result);
118 0 : NS_ENSURE_TRUE(mParent, NS_ERROR_NULL_POINTER);
119 :
120 : // insert the new node
121 0 : result = mEditor->SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
122 0 : if (mNewLeftNode) {
123 : bool bAdjustSelection;
124 0 : mEditor->ShouldTxnSetSelection(&bAdjustSelection);
125 0 : if (bAdjustSelection)
126 : {
127 0 : nsCOMPtr<nsISelection> selection;
128 0 : result = mEditor->GetSelection(getter_AddRefs(selection));
129 0 : NS_ENSURE_SUCCESS(result, result);
130 0 : NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
131 0 : result = selection->Collapse(mNewLeftNode, mOffset);
132 : }
133 : else
134 : {
135 : // do nothing - dom range gravity will adjust selection
136 : }
137 : }
138 0 : return result;
139 : }
140 :
141 0 : NS_IMETHODIMP SplitElementTxn::UndoTransaction(void)
142 : {
143 : #ifdef NS_DEBUG
144 0 : if (gNoisy) {
145 : printf("%p Undo Split of existing node %p and new node %p offset %d\n",
146 : static_cast<void*>(this),
147 0 : static_cast<void*>(mExistingRightNode.get()),
148 0 : static_cast<void*>(mNewLeftNode.get()),
149 0 : mOffset);
150 : }
151 : #endif
152 :
153 0 : NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
154 0 : if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
155 0 : return NS_ERROR_NOT_INITIALIZED;
156 : }
157 :
158 : // this assumes Do inserted the new node in front of the prior existing node
159 0 : nsresult result = mEditor->JoinNodesImpl(mExistingRightNode, mNewLeftNode, mParent, false);
160 : #ifdef NS_DEBUG
161 0 : if (gNoisy)
162 : {
163 : printf("** after join left child node %p into right node %p\n",
164 0 : static_cast<void*>(mNewLeftNode.get()),
165 0 : static_cast<void*>(mExistingRightNode.get()));
166 0 : if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
167 : }
168 0 : if (NS_SUCCEEDED(result))
169 : {
170 0 : if (gNoisy)
171 : {
172 : printf(" left node = %p removed\n",
173 0 : static_cast<void*>(mNewLeftNode.get()));
174 : }
175 : }
176 : #endif
177 :
178 0 : return result;
179 : }
180 :
181 : /* redo cannot simply resplit the right node, because subsequent transactions
182 : * on the redo stack may depend on the left node existing in its previous state.
183 : */
184 0 : NS_IMETHODIMP SplitElementTxn::RedoTransaction(void)
185 : {
186 0 : NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
187 0 : if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
188 0 : return NS_ERROR_NOT_INITIALIZED;
189 : }
190 :
191 : #ifdef NS_DEBUG
192 0 : if (gNoisy) {
193 : printf("%p Redo Split of existing node %p and new node %p offset %d\n",
194 : static_cast<void*>(this),
195 0 : static_cast<void*>(mExistingRightNode.get()),
196 0 : static_cast<void*>(mNewLeftNode.get()),
197 0 : mOffset);
198 0 : if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
199 : }
200 : #endif
201 :
202 : nsresult result;
203 0 : nsCOMPtr<nsIDOMNode>resultNode;
204 : // first, massage the existing node so it is in its post-split state
205 0 : nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mExistingRightNode);
206 0 : if (rightNodeAsText)
207 : {
208 0 : result = rightNodeAsText->DeleteData(0, mOffset);
209 : #ifdef NS_DEBUG
210 0 : if (gNoisy)
211 : {
212 : printf("** after delete of text in right text node %p offset %d\n",
213 0 : static_cast<void*>(rightNodeAsText.get()),
214 0 : mOffset);
215 0 : mEditor->DebugDumpContent(); // DEBUG
216 : }
217 : #endif
218 : }
219 : else
220 : {
221 0 : nsCOMPtr<nsIDOMNode>child;
222 0 : nsCOMPtr<nsIDOMNode>nextSibling;
223 0 : result = mExistingRightNode->GetFirstChild(getter_AddRefs(child));
224 : PRInt32 i;
225 0 : for (i=0; i<mOffset; i++)
226 : {
227 0 : if (NS_FAILED(result)) {return result;}
228 0 : if (!child) {return NS_ERROR_NULL_POINTER;}
229 0 : child->GetNextSibling(getter_AddRefs(nextSibling));
230 0 : result = mExistingRightNode->RemoveChild(child, getter_AddRefs(resultNode));
231 0 : if (NS_SUCCEEDED(result))
232 : {
233 0 : result = mNewLeftNode->AppendChild(child, getter_AddRefs(resultNode));
234 : #ifdef NS_DEBUG
235 0 : if (gNoisy)
236 : {
237 : printf("** move child node %p from right node %p to left node %p\n",
238 0 : static_cast<void*>(child.get()),
239 0 : static_cast<void*>(mExistingRightNode.get()),
240 0 : static_cast<void*>(mNewLeftNode.get()));
241 0 : if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
242 : }
243 : #endif
244 : }
245 0 : child = do_QueryInterface(nextSibling);
246 : }
247 : }
248 : // second, re-insert the left node into the tree
249 0 : result = mParent->InsertBefore(mNewLeftNode, mExistingRightNode, getter_AddRefs(resultNode));
250 : #ifdef NS_DEBUG
251 0 : if (gNoisy)
252 : {
253 : printf("** reinsert left child node %p before right node %p\n",
254 0 : static_cast<void*>(mNewLeftNode.get()),
255 0 : static_cast<void*>(mExistingRightNode.get()));
256 0 : if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
257 : }
258 : #endif
259 0 : return result;
260 : }
261 :
262 :
263 0 : NS_IMETHODIMP SplitElementTxn::GetTxnDescription(nsAString& aString)
264 : {
265 0 : aString.AssignLiteral("SplitElementTxn");
266 0 : return NS_OK;
267 : }
268 :
269 0 : NS_IMETHODIMP SplitElementTxn::GetNewNode(nsIDOMNode **aNewNode)
270 : {
271 0 : NS_ENSURE_TRUE(aNewNode, NS_ERROR_NULL_POINTER);
272 0 : NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NOT_INITIALIZED);
273 0 : *aNewNode = mNewLeftNode;
274 0 : NS_ADDREF(*aNewNode);
275 0 : return NS_OK;
276 4392 : }
|