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 "nsReadableUtils.h"
39 : #include "nsCRT.h"
40 :
41 : #include "DeleteElementTxn.h"
42 : #include "nsSelectionState.h"
43 : #ifdef NS_DEBUG
44 : #include "nsIDOMElement.h"
45 : #endif
46 :
47 : #ifdef NS_DEBUG
48 : static bool gNoisy = false;
49 : #endif
50 :
51 :
52 0 : DeleteElementTxn::DeleteElementTxn()
53 : : EditTxn()
54 : ,mElement()
55 : ,mParent()
56 : ,mRefNode()
57 0 : ,mRangeUpdater(nsnull)
58 : {
59 0 : }
60 :
61 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(DeleteElementTxn)
62 :
63 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DeleteElementTxn, EditTxn)
64 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mElement)
65 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
66 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mRefNode)
67 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
68 :
69 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DeleteElementTxn, EditTxn)
70 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mElement)
71 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
72 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRefNode)
73 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
74 :
75 0 : NS_IMPL_ADDREF_INHERITED(DeleteElementTxn, EditTxn)
76 0 : NS_IMPL_RELEASE_INHERITED(DeleteElementTxn, EditTxn)
77 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteElementTxn)
78 0 : NS_INTERFACE_MAP_END_INHERITING(EditTxn)
79 :
80 0 : NS_IMETHODIMP DeleteElementTxn::Init(nsIEditor *aEditor,
81 : nsIDOMNode *aElement,
82 : nsRangeUpdater *aRangeUpdater)
83 : {
84 0 : NS_ENSURE_TRUE(aEditor && aElement, NS_ERROR_NULL_POINTER);
85 0 : mEditor = aEditor;
86 0 : mElement = do_QueryInterface(aElement);
87 0 : nsresult result = mElement->GetParentNode(getter_AddRefs(mParent));
88 0 : if (NS_FAILED(result)) { return result; }
89 :
90 : // do nothing if the parent is read-only
91 0 : if (mParent && !mEditor->IsModifiableNode(mParent)) {
92 0 : return NS_ERROR_FAILURE;
93 : }
94 :
95 0 : mRangeUpdater = aRangeUpdater;
96 0 : return NS_OK;
97 : }
98 :
99 :
100 0 : NS_IMETHODIMP DeleteElementTxn::DoTransaction(void)
101 : {
102 : #ifdef NS_DEBUG
103 0 : if (gNoisy)
104 : {
105 : printf("%p Do Delete Element element = %p\n",
106 : static_cast<void*>(this),
107 0 : static_cast<void*>(mElement.get()));
108 : }
109 : #endif
110 :
111 0 : NS_ENSURE_TRUE(mElement, NS_ERROR_NOT_INITIALIZED);
112 :
113 0 : if (!mParent) { return NS_OK; } // this is a no-op, there's no parent to delete mElement from
114 :
115 : #ifdef NS_DEBUG
116 : // begin debug output
117 0 : nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mElement);
118 0 : nsAutoString elementTag(NS_LITERAL_STRING("text node"));
119 0 : if (element)
120 0 : element->GetTagName(elementTag);
121 0 : nsCOMPtr<nsIDOMElement> parentElement = do_QueryInterface(mParent);
122 0 : nsAutoString parentElementTag(NS_LITERAL_STRING("text node"));
123 0 : if (parentElement)
124 0 : parentElement->GetTagName(parentElementTag);
125 : char *c, *p;
126 0 : c = ToNewCString(elementTag);
127 0 : p = ToNewCString(parentElementTag);
128 0 : if (c&&p)
129 : {
130 0 : if (gNoisy)
131 0 : printf(" DeleteElementTxn: deleting child %s from parent %s\n", c, p);
132 :
133 0 : NS_Free(c);
134 0 : NS_Free(p);
135 : }
136 : // end debug output
137 : #endif
138 :
139 : // remember which child mElement was (by remembering which child was next)
140 0 : mElement->GetNextSibling(getter_AddRefs(mRefNode)); // can return null mRefNode
141 :
142 : // give range updater a chance. SelAdjDeleteNode() needs to be called *before*
143 : // we do the action, unlike some of the other nsRangeStore update methods.
144 0 : if (mRangeUpdater)
145 0 : mRangeUpdater->SelAdjDeleteNode(mElement);
146 :
147 0 : nsCOMPtr<nsIDOMNode> resultNode;
148 0 : return mParent->RemoveChild(mElement, getter_AddRefs(resultNode));
149 : }
150 :
151 0 : NS_IMETHODIMP DeleteElementTxn::UndoTransaction(void)
152 : {
153 : #ifdef NS_DEBUG
154 0 : if (gNoisy)
155 : {
156 : printf("%p Undo Delete Element element = %p, parent = %p\n",
157 : static_cast<void*>(this),
158 0 : static_cast<void*>(mElement.get()),
159 0 : static_cast<void*>(mParent.get()));
160 : }
161 : #endif
162 :
163 0 : if (!mParent) { return NS_OK; } // this is a legal state, the txn is a no-op
164 0 : if (!mElement) { return NS_ERROR_NULL_POINTER; }
165 :
166 : #ifdef NS_DEBUG
167 : // begin debug output
168 0 : nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mElement);
169 0 : nsAutoString elementTag(NS_LITERAL_STRING("text node"));
170 0 : if (element)
171 0 : element->GetTagName(elementTag);
172 0 : nsCOMPtr<nsIDOMElement> parentElement = do_QueryInterface(mParent);
173 0 : nsAutoString parentElementTag(NS_LITERAL_STRING("text node"));
174 0 : if (parentElement)
175 0 : parentElement->GetTagName(parentElementTag);
176 : char *c, *p;
177 0 : c = ToNewCString(elementTag);
178 0 : p = ToNewCString(parentElementTag);
179 0 : if (c&&p)
180 : {
181 0 : if (gNoisy)
182 0 : printf(" DeleteElementTxn: inserting child %s back into parent %s\n", c, p);
183 :
184 0 : NS_Free(c);
185 0 : NS_Free(p);
186 : }
187 : // end debug output
188 : #endif
189 :
190 0 : nsCOMPtr<nsIDOMNode> resultNode;
191 0 : return mParent->InsertBefore(mElement, mRefNode, getter_AddRefs(resultNode));
192 : }
193 :
194 0 : NS_IMETHODIMP DeleteElementTxn::RedoTransaction(void)
195 : {
196 : #ifdef NS_DEBUG
197 0 : if (gNoisy)
198 : {
199 : printf("%p Redo Delete Element element = %p, parent = %p\n",
200 : static_cast<void*>(this),
201 0 : static_cast<void*>(mElement.get()),
202 0 : static_cast<void*>(mParent.get()));
203 : }
204 : #endif
205 :
206 0 : if (!mParent) { return NS_OK; } // this is a legal state, the txn is a no-op
207 0 : if (!mElement) { return NS_ERROR_NULL_POINTER; }
208 :
209 0 : if (mRangeUpdater)
210 0 : mRangeUpdater->SelAdjDeleteNode(mElement);
211 :
212 0 : nsCOMPtr<nsIDOMNode> resultNode;
213 0 : return mParent->RemoveChild(mElement, getter_AddRefs(resultNode));
214 : }
215 :
216 0 : NS_IMETHODIMP DeleteElementTxn::GetTxnDescription(nsAString& aString)
217 : {
218 0 : aString.AssignLiteral("DeleteElementTxn");
219 0 : return NS_OK;
220 4392 : }
|