Back to index

lightning-sunbird  0.9+nobinonly
InsertElementTxn.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 "InsertElementTxn.h"
00039 #include "nsISelection.h"
00040 #include "nsIContent.h"
00041 #include "nsIDOMNodeList.h"
00042 #include "nsReadableUtils.h"
00043 
00044 #ifdef NS_DEBUG
00045 static PRBool gNoisy = PR_FALSE;
00046 #endif
00047 
00048 
00049 InsertElementTxn::InsertElementTxn()
00050   : EditTxn()
00051 {
00052 }
00053 
00054 NS_IMETHODIMP InsertElementTxn::Init(nsIDOMNode *aNode,
00055                                      nsIDOMNode *aParent,
00056                                      PRInt32     aOffset,
00057                                      nsIEditor  *aEditor)
00058 {
00059   NS_ASSERTION(aNode && aParent && aEditor, "bad arg");
00060   if (!aNode || !aParent || !aEditor)
00061     return NS_ERROR_NULL_POINTER;
00062 
00063   mNode = do_QueryInterface(aNode);
00064   mParent = do_QueryInterface(aParent);
00065   mOffset = aOffset;
00066   mEditor = aEditor;
00067   if (!mNode || !mParent || !mEditor)
00068     return NS_ERROR_INVALID_ARG;
00069   return NS_OK;
00070 }
00071 
00072 
00073 InsertElementTxn::~InsertElementTxn()
00074 {
00075 }
00076 
00077 NS_IMETHODIMP InsertElementTxn::DoTransaction(void)
00078 {
00079 #ifdef NS_DEBUG
00080   if (gNoisy) 
00081   { 
00082     nsCOMPtr<nsIContent>nodeAsContent = do_QueryInterface(mNode);
00083     nsCOMPtr<nsIContent>parentAsContent = do_QueryInterface(mParent);
00084     nsString namestr;
00085     mNode->GetNodeName(namestr);
00086     char* nodename = ToNewCString(namestr);
00087     printf("%p Do Insert Element of %p <%s> into parent %p at offset %d\n", 
00088            this, nodeAsContent.get(), nodename,
00089            parentAsContent.get(), mOffset); 
00090     nsMemory::Free(nodename);
00091   }
00092 #endif
00093 
00094   if (!mNode || !mParent) return NS_ERROR_NOT_INITIALIZED;
00095 
00096   nsCOMPtr<nsIDOMNodeList> childNodes;
00097   nsresult result = mParent->GetChildNodes(getter_AddRefs(childNodes));
00098   if (NS_FAILED(result)) return result;
00099   nsCOMPtr<nsIDOMNode>refNode;
00100   if (childNodes)
00101   {
00102     PRUint32 count;
00103     childNodes->GetLength(&count);
00104     if (mOffset>(PRInt32)count) mOffset = count;
00105     // -1 is sentinel value meaning "append at end"
00106     if (mOffset == -1) mOffset = count;
00107     result = childNodes->Item(mOffset, getter_AddRefs(refNode));
00108     if (NS_FAILED(result)) return result; 
00109     // note, it's ok for mRefNode to be null.  that means append
00110   }
00111 
00112   mEditor->MarkNodeDirty(mNode);
00113 
00114   nsCOMPtr<nsIDOMNode> resultNode;
00115   result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode));
00116   if (NS_FAILED(result)) return result;
00117   if (!resultNode) return NS_ERROR_NULL_POINTER;
00118 
00119   // only set selection to insertion point if editor gives permission
00120   PRBool bAdjustSelection;
00121   mEditor->ShouldTxnSetSelection(&bAdjustSelection);
00122   if (bAdjustSelection)
00123   {
00124     nsCOMPtr<nsISelection> selection;
00125     result = mEditor->GetSelection(getter_AddRefs(selection));
00126     if (NS_FAILED(result)) return result;
00127     if (!selection) return NS_ERROR_NULL_POINTER;
00128     // place the selection just after the inserted element
00129     selection->Collapse(mParent, mOffset+1);
00130   }
00131   else
00132   {
00133     // do nothing - dom range gravity will adjust selection
00134   }
00135   return result;
00136 }
00137 
00138 NS_IMETHODIMP InsertElementTxn::UndoTransaction(void)
00139 {
00140 #ifdef NS_DEBUG
00141   if (gNoisy) { printf("%p Undo Insert Element of %p into parent %p at offset %d\n", 
00142                        this, mNode.get(), mParent.get(), mOffset); }
00143 #endif
00144 
00145   if (!mNode || !mParent) return NS_ERROR_NOT_INITIALIZED;
00146 
00147   nsCOMPtr<nsIDOMNode> resultNode;
00148   return mParent->RemoveChild(mNode, getter_AddRefs(resultNode));
00149 }
00150 
00151 NS_IMETHODIMP InsertElementTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00152 {
00153   if (aDidMerge)
00154     *aDidMerge = PR_FALSE;
00155   return NS_OK;
00156 }
00157 
00158 NS_IMETHODIMP InsertElementTxn::GetTxnDescription(nsAString& aString)
00159 {
00160   aString.AssignLiteral("InsertElementTxn");
00161   return NS_OK;
00162 }