Back to index

lightning-sunbird  0.9+nobinonly
DeleteElementTxn.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsReadableUtils.h"
00039 #include "nsCRT.h"
00040 
00041 #include "DeleteElementTxn.h"
00042 #include "nsSelectionState.h"
00043 #ifdef NS_DEBUG
00044 #include "nsIDOMElement.h"
00045 #endif
00046 
00047 #ifdef NS_DEBUG
00048 static PRBool gNoisy = PR_FALSE;
00049 #endif
00050 
00051 
00052 DeleteElementTxn::DeleteElementTxn()
00053 : EditTxn()
00054 ,mElement()
00055 ,mParent()
00056 ,mRefNode()
00057 ,mRangeUpdater(nsnull)
00058 {
00059 }
00060 
00061 NS_IMETHODIMP DeleteElementTxn::Init(nsIDOMNode *aElement,
00062                                      nsRangeUpdater *aRangeUpdater)
00063 {
00064   if (!aElement) return NS_ERROR_NULL_POINTER;
00065   mElement = do_QueryInterface(aElement);
00066   mRangeUpdater = aRangeUpdater;
00067   return NS_OK;
00068 }
00069 
00070 
00071 DeleteElementTxn::~DeleteElementTxn()
00072 {
00073 }
00074 
00075 NS_IMETHODIMP DeleteElementTxn::DoTransaction(void)
00076 {
00077 #ifdef NS_DEBUG
00078   if (gNoisy) { printf("%p Do Delete Element element = %p\n", this, mElement.get()); }
00079 #endif
00080 
00081   if (!mElement) return NS_ERROR_NOT_INITIALIZED;
00082 
00083   nsresult result = mElement->GetParentNode(getter_AddRefs(mParent));
00084   if (NS_FAILED(result)) { return result; }
00085   if (!mParent) { return NS_OK; }  // this is a no-op, there's no parent to delete mElement from
00086 
00087 #ifdef NS_DEBUG
00088   // begin debug output
00089   nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mElement);
00090   nsAutoString elementTag(NS_LITERAL_STRING("text node"));
00091   if (element)
00092     element->GetTagName(elementTag);
00093   nsCOMPtr<nsIDOMElement> parentElement = do_QueryInterface(mParent);
00094   nsAutoString parentElementTag(NS_LITERAL_STRING("text node"));
00095   if (parentElement)
00096     parentElement->GetTagName(parentElementTag);
00097   char *c, *p;
00098   c = ToNewCString(elementTag);
00099   p = ToNewCString(parentElementTag);
00100   if (c&&p)
00101   {
00102     if (gNoisy)
00103       printf("  DeleteElementTxn:  deleting child %s from parent %s\n", c, p); 
00104 
00105     NS_Free(c);
00106     NS_Free(p);
00107   }
00108   // end debug output
00109 #endif
00110 
00111   // remember which child mElement was (by remembering which child was next)
00112   result = mElement->GetNextSibling(getter_AddRefs(mRefNode));  // can return null mRefNode
00113 
00114   // give range updater a chance.  SelAdjDeleteNode() needs to be called *before*
00115   // we do the action, unlike some of the other nsRangeStore update methods.
00116   if (mRangeUpdater) 
00117     mRangeUpdater->SelAdjDeleteNode(mElement);
00118 
00119   nsCOMPtr<nsIDOMNode> resultNode;
00120   return mParent->RemoveChild(mElement, getter_AddRefs(resultNode));
00121 }
00122 
00123 NS_IMETHODIMP DeleteElementTxn::UndoTransaction(void)
00124 {
00125 #ifdef NS_DEBUG
00126   if (gNoisy) { printf("%p Undo Delete Element element = %p, parent = %p\n", this, mElement.get(), mParent.get()); }
00127 #endif
00128 
00129   if (!mParent) { return NS_OK; } // this is a legal state, the txn is a no-op
00130   if (!mElement) { return NS_ERROR_NULL_POINTER; }
00131 
00132 #ifdef NS_DEBUG
00133   // begin debug output
00134   nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mElement);
00135   nsAutoString elementTag(NS_LITERAL_STRING("text node"));
00136   if (element)
00137     element->GetTagName(elementTag);
00138   nsCOMPtr<nsIDOMElement> parentElement = do_QueryInterface(mParent);
00139   nsAutoString parentElementTag(NS_LITERAL_STRING("text node"));
00140   if (parentElement)
00141     parentElement->GetTagName(parentElementTag);
00142   char *c, *p;
00143   c = ToNewCString(elementTag);
00144   p = ToNewCString(parentElementTag);
00145   if (c&&p)
00146   {
00147     if (gNoisy)
00148       printf("  DeleteElementTxn:  inserting child %s back into parent %s\n", c, p); 
00149 
00150     NS_Free(c);
00151     NS_Free(p);
00152   }
00153   // end debug output
00154 #endif
00155 
00156   nsCOMPtr<nsIDOMNode> resultNode;
00157   return mParent->InsertBefore(mElement, mRefNode, getter_AddRefs(resultNode));
00158 }
00159 
00160 NS_IMETHODIMP DeleteElementTxn::RedoTransaction(void)
00161 {
00162 #ifdef NS_DEBUG
00163   if (gNoisy) { printf("%p Redo Delete Element element = %p, parent = %p\n", this, mElement.get(), mParent.get()); }
00164 #endif
00165 
00166   if (!mParent) { return NS_OK; } // this is a legal state, the txn is a no-op
00167   if (!mElement) { return NS_ERROR_NULL_POINTER; }
00168 
00169   if (mRangeUpdater) 
00170     mRangeUpdater->SelAdjDeleteNode(mElement);
00171 
00172   nsCOMPtr<nsIDOMNode> resultNode;
00173   return mParent->RemoveChild(mElement, getter_AddRefs(resultNode));
00174 }
00175 
00176 
00177 NS_IMETHODIMP DeleteElementTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00178 {
00179   if (aDidMerge)
00180     *aDidMerge = PR_FALSE;
00181   return NS_OK;
00182 }
00183 
00184 NS_IMETHODIMP DeleteElementTxn::GetTxnDescription(nsAString& aString)
00185 {
00186   aString.AssignLiteral("DeleteElementTxn");
00187   return NS_OK;
00188 }