Back to index

lightning-sunbird  0.9+nobinonly
JoinElementTxn.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 "JoinElementTxn.h"
00039 #include "nsEditor.h"
00040 #include "nsIDOMNodeList.h"
00041 #include "nsIDOMCharacterData.h"
00042 
00043 #ifdef NS_DEBUG
00044 static PRBool gNoisy = PR_FALSE;
00045 #endif
00046 
00047 JoinElementTxn::JoinElementTxn()
00048   : EditTxn()
00049 {
00050 }
00051 
00052 NS_IMETHODIMP JoinElementTxn::Init(nsEditor   *aEditor,
00053                                    nsIDOMNode *aLeftNode,
00054                                    nsIDOMNode *aRightNode)
00055 {
00056   NS_PRECONDITION((aEditor && aLeftNode && aRightNode), "null arg");
00057   if (!aEditor || !aLeftNode || !aRightNode) { return NS_ERROR_NULL_POINTER; }
00058   mEditor = aEditor;
00059   mLeftNode = do_QueryInterface(aLeftNode);
00060   mRightNode = do_QueryInterface(aRightNode);
00061   mOffset=0;
00062   return NS_OK;
00063 }
00064 
00065 JoinElementTxn::~JoinElementTxn()
00066 {
00067 }
00068 
00069 // After DoTransaction() and RedoTransaction(), the left node is removed from the content tree and right node remains.
00070 NS_IMETHODIMP JoinElementTxn::DoTransaction(void)
00071 {
00072 #ifdef NS_DEBUG
00073   if (gNoisy) { printf("%p Do Join of %p and %p\n", this, mLeftNode.get(), mRightNode.get()); }
00074 #endif
00075 
00076   NS_PRECONDITION((mEditor && mLeftNode && mRightNode), "null arg");
00077   if (!mEditor || !mLeftNode || !mRightNode) { return NS_ERROR_NOT_INITIALIZED; }
00078 
00079   // get the parent node
00080   nsCOMPtr<nsIDOMNode>leftParent;
00081   nsresult result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
00082   if (NS_FAILED(result)) return result;
00083   if (!leftParent) return NS_ERROR_NULL_POINTER;
00084 
00085   // verify that mLeftNode and mRightNode have the same parent
00086   nsCOMPtr<nsIDOMNode>rightParent;
00087   result = mRightNode->GetParentNode(getter_AddRefs(rightParent));
00088   if (NS_FAILED(result)) return result;
00089   if (!rightParent) return NS_ERROR_NULL_POINTER;
00090 
00091   if (leftParent==rightParent)
00092   {
00093     mParent= do_QueryInterface(leftParent); // set this instance mParent. 
00094                                             // Other methods will see a non-null mParent and know all is well
00095     nsCOMPtr<nsIDOMCharacterData> leftNodeAsText = do_QueryInterface(mLeftNode);
00096     if (leftNodeAsText) 
00097     {
00098       leftNodeAsText->GetLength(&mOffset);
00099     }
00100     else 
00101     {
00102       nsCOMPtr<nsIDOMNodeList> childNodes;
00103       result = mLeftNode->GetChildNodes(getter_AddRefs(childNodes));
00104       if (NS_FAILED(result)) return result;
00105       if (childNodes) 
00106       {
00107         childNodes->GetLength(&mOffset);
00108       }
00109     }
00110     result = mEditor->JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE);
00111 #ifdef NS_DEBUG
00112     if (NS_SUCCEEDED(result))
00113     {
00114       if (gNoisy) { printf("  left node = %p removed\n", mLeftNode.get()); }
00115     }
00116 #endif
00117   }
00118   else 
00119   {
00120     NS_ASSERTION(PR_FALSE, "2 nodes do not have same parent");
00121     return NS_ERROR_INVALID_ARG;
00122   }
00123   return result;
00124 }
00125 
00126 //XXX: what if instead of split, we just deleted the unneeded children of mRight
00127 //     and re-inserted mLeft?
00128 NS_IMETHODIMP JoinElementTxn::UndoTransaction(void)
00129 {
00130 #ifdef NS_DEBUG
00131   if (gNoisy) { printf("%p Undo Join, right node = %p\n", this, mRightNode.get()); }
00132 #endif
00133 
00134   NS_ASSERTION(mRightNode && mLeftNode && mParent, "bad state");
00135   if (!mRightNode || !mLeftNode || !mParent) { return NS_ERROR_NOT_INITIALIZED; }
00136   nsresult result;
00137   nsCOMPtr<nsIDOMNode>resultNode;
00138   // first, massage the existing node so it is in its post-split state
00139   nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mRightNode);
00140   if (rightNodeAsText)
00141   {
00142     result = rightNodeAsText->DeleteData(0, mOffset);
00143   }
00144   else
00145   {
00146     nsCOMPtr<nsIDOMNode>child;
00147     result = mRightNode->GetFirstChild(getter_AddRefs(child));
00148     nsCOMPtr<nsIDOMNode>nextSibling;
00149     PRUint32 i;
00150     for (i=0; i<mOffset; i++)
00151     {
00152       if (NS_FAILED(result)) {return result;}
00153       if (!child) {return NS_ERROR_NULL_POINTER;}
00154       child->GetNextSibling(getter_AddRefs(nextSibling));
00155       result = mLeftNode->AppendChild(child, getter_AddRefs(resultNode));
00156       child = do_QueryInterface(nextSibling);
00157     }
00158   }
00159   // second, re-insert the left node into the tree 
00160   result = mParent->InsertBefore(mLeftNode, mRightNode, getter_AddRefs(resultNode));
00161   return result;
00162 
00163 }
00164 
00165 NS_IMETHODIMP JoinElementTxn::GetIsTransient(PRBool *aIsTransient)
00166 {
00167   if (aIsTransient)
00168     *aIsTransient = PR_FALSE;
00169   return NS_OK;
00170 }
00171 
00172 nsresult JoinElementTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00173 {
00174   if (aDidMerge)
00175     *aDidMerge = PR_FALSE;
00176   return NS_OK;
00177 }
00178 
00179 NS_IMETHODIMP JoinElementTxn::GetTxnDescription(nsAString& aString)
00180 {
00181   aString.AssignLiteral("JoinElementTxn");
00182   return NS_OK;
00183 }