Back to index

lightning-sunbird  0.9+nobinonly
DeleteRangeTxn.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  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "DeleteRangeTxn.h"
00040 #include "nsIDOMRange.h"
00041 #include "nsIDOMCharacterData.h"
00042 #include "nsIDOMNodeList.h"
00043 #include "nsISelection.h"
00044 #include "DeleteTextTxn.h"
00045 #include "DeleteElementTxn.h"
00046 #include "TransactionFactory.h"
00047 #include "nsIContentIterator.h"
00048 #include "nsIContent.h"
00049 #include "nsComponentManagerUtils.h"
00050 
00051 #ifdef NS_DEBUG
00052 static PRBool gNoisy = PR_FALSE;
00053 #endif
00054 
00055 // note that aEditor is not refcounted
00056 DeleteRangeTxn::DeleteRangeTxn()
00057 : EditAggregateTxn()
00058 ,mRange()
00059 ,mStartParent()
00060 ,mStartOffset(0)
00061 ,mEndParent()
00062 ,mCommonParent()
00063 ,mEndOffset(0)
00064 ,mEditor(nsnull)
00065 ,mRangeUpdater(nsnull)
00066 {
00067 }
00068 
00069 NS_IMETHODIMP DeleteRangeTxn::Init(nsIEditor *aEditor, 
00070                                    nsIDOMRange *aRange,
00071                                    nsRangeUpdater *aRangeUpdater)
00072 {
00073   NS_ASSERTION(aEditor && aRange, "bad state");
00074   if (!aEditor || !aRange) { return NS_ERROR_NOT_INITIALIZED; }
00075 
00076   mEditor = aEditor;
00077   mRange  = do_QueryInterface(aRange);
00078   mRangeUpdater = aRangeUpdater;
00079   
00080   nsresult result = aRange->GetStartContainer(getter_AddRefs(mStartParent));
00081   NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartParent failed.");
00082   result = aRange->GetEndContainer(getter_AddRefs(mEndParent));
00083   NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndParent failed.");
00084   result = aRange->GetStartOffset(&mStartOffset);
00085   NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartOffset failed.");
00086   result = aRange->GetEndOffset(&mEndOffset);
00087   NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndOffset failed.");
00088   result = aRange->GetCommonAncestorContainer(getter_AddRefs(mCommonParent));
00089   NS_ASSERTION((NS_SUCCEEDED(result)), "GetCommonParent failed.");
00090 
00091 #ifdef NS_DEBUG
00092   {
00093     PRUint32 count;
00094     nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(mStartParent);
00095     if (textNode)
00096       textNode->GetLength(&count);
00097     else
00098     {
00099       nsCOMPtr<nsIDOMNodeList> children;
00100       result = mStartParent->GetChildNodes(getter_AddRefs(children));
00101       NS_ASSERTION(((NS_SUCCEEDED(result)) && children), "bad start child list");
00102       children->GetLength(&count);
00103     }
00104     NS_ASSERTION(mStartOffset<=(PRInt32)count, "bad start offset");
00105 
00106     textNode = do_QueryInterface(mEndParent);
00107     if (textNode)
00108       textNode->GetLength(&count);
00109     else
00110     {
00111       nsCOMPtr<nsIDOMNodeList> children;
00112       result = mEndParent->GetChildNodes(getter_AddRefs(children));
00113       NS_ASSERTION(((NS_SUCCEEDED(result)) && children), "bad end child list");
00114       children->GetLength(&count);
00115     }
00116     NS_ASSERTION(mEndOffset<=(PRInt32)count, "bad end offset");
00117 
00118 #ifdef NS_DEBUG
00119     if (gNoisy)
00120     {
00121       printf ("DeleteRange: %d of %p to %d of %p\n", 
00122                mStartOffset, (void *)mStartParent, mEndOffset, (void *)mEndParent);
00123     }         
00124 #endif
00125   }
00126 #endif
00127   return result;
00128 
00129 }
00130 
00131 DeleteRangeTxn::~DeleteRangeTxn()
00132 {
00133 }
00134 
00135 NS_IMETHODIMP DeleteRangeTxn::DoTransaction(void)
00136 {
00137 #ifdef NS_DEBUG
00138   if (gNoisy) { printf("Do Delete Range\n"); }
00139 #endif
00140 
00141   if (!mStartParent || !mEndParent || !mCommonParent || !mEditor) 
00142     return NS_ERROR_NOT_INITIALIZED;
00143 
00144   nsresult result; 
00145   // build the child transactions
00146 
00147   if (mStartParent==mEndParent)
00148   { // the selection begins and ends in the same node
00149     result = CreateTxnsToDeleteBetween(mStartParent, mStartOffset, mEndOffset);
00150   }
00151   else
00152   { // the selection ends in a different node from where it started
00153     // delete the relevant content in the start node
00154     result = CreateTxnsToDeleteContent(mStartParent, mStartOffset, nsIEditor::eNext);
00155     if (NS_SUCCEEDED(result))
00156     {
00157       // delete the intervening nodes
00158       result = CreateTxnsToDeleteNodesBetween();
00159       if (NS_SUCCEEDED(result))
00160       {
00161         // delete the relevant content in the end node
00162         result = CreateTxnsToDeleteContent(mEndParent, mEndOffset, nsIEditor::ePrevious);
00163       }
00164     }
00165   }
00166 
00167   // if we've successfully built this aggregate transaction, then do it.
00168   if (NS_SUCCEEDED(result)) {
00169     result = EditAggregateTxn::DoTransaction();
00170   }
00171 
00172   if (NS_FAILED(result)) return result;
00173   
00174   // only set selection to deletion point if editor gives permission
00175   PRBool bAdjustSelection;
00176   mEditor->ShouldTxnSetSelection(&bAdjustSelection);
00177   if (bAdjustSelection)
00178   {
00179     nsCOMPtr<nsISelection> selection;
00180     result = mEditor->GetSelection(getter_AddRefs(selection));
00181     if (NS_FAILED(result)) return result;
00182     if (!selection) return NS_ERROR_NULL_POINTER;
00183     result = selection->Collapse(mStartParent, mStartOffset);
00184   }
00185   else
00186   {
00187     // do nothing - dom range gravity will adjust selection
00188   }
00189 
00190   return result;
00191 }
00192 
00193 NS_IMETHODIMP DeleteRangeTxn::UndoTransaction(void)
00194 {
00195 #ifdef NS_DEBUG
00196   if (gNoisy) { printf("Undo Delete Range\n"); }
00197 #endif
00198 
00199   if (!mStartParent || !mEndParent || !mCommonParent || !mEditor) 
00200     return NS_ERROR_NOT_INITIALIZED;
00201 
00202   return EditAggregateTxn::UndoTransaction();
00203 }
00204 
00205 NS_IMETHODIMP DeleteRangeTxn::RedoTransaction(void)
00206 {
00207 #ifdef NS_DEBUG
00208   if (gNoisy) { printf("Redo Delete Range\n"); }
00209 #endif
00210 
00211   if (!mStartParent || !mEndParent || !mCommonParent || !mEditor) 
00212     return NS_ERROR_NOT_INITIALIZED;
00213 
00214   return EditAggregateTxn::RedoTransaction();
00215 }
00216 
00217 NS_IMETHODIMP DeleteRangeTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00218 {
00219   if (aDidMerge)
00220     *aDidMerge = PR_FALSE;
00221   return NS_OK;
00222 }
00223 
00224 NS_IMETHODIMP DeleteRangeTxn::GetTxnDescription(nsAString& aString)
00225 {
00226   aString.AssignLiteral("DeleteRangeTxn");
00227   return NS_OK;
00228 }
00229 
00230 NS_IMETHODIMP 
00231 DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent, 
00232                                           PRUint32    aStartOffset, 
00233                                           PRUint32    aEndOffset)
00234 {
00235   nsresult result;
00236   // see what kind of node we have
00237   nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(aStartParent);
00238   if (textNode)
00239   { // if the node is a text node, then delete text content
00240     DeleteTextTxn *txn;
00241     result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
00242     if (NS_FAILED(result)) return result;
00243     if (!txn) return NS_ERROR_NULL_POINTER;
00244 
00245     PRInt32 numToDel;
00246     if (aStartOffset==aEndOffset)
00247       numToDel = 1;
00248     else
00249       numToDel = aEndOffset-aStartOffset;
00250     txn->Init(mEditor, textNode, aStartOffset, numToDel, mRangeUpdater);
00251     AppendChild(txn);
00252     NS_RELEASE(txn);
00253   }
00254   else
00255   {
00256     nsCOMPtr<nsIDOMNodeList> children;
00257     result = aStartParent->GetChildNodes(getter_AddRefs(children));
00258     if (NS_FAILED(result)) return result;
00259     if (!children) return NS_ERROR_NULL_POINTER;
00260 
00261 #ifdef DEBUG
00262     PRUint32 childCount;
00263     children->GetLength(&childCount);
00264     NS_ASSERTION(aEndOffset<=childCount, "bad aEndOffset");
00265 #endif
00266     PRUint32 i;
00267     for (i=aStartOffset; i<aEndOffset; i++)
00268     {
00269       nsCOMPtr<nsIDOMNode> child;
00270       result = children->Item(i, getter_AddRefs(child));
00271       if (NS_FAILED(result)) return result;
00272       if (!child) return NS_ERROR_NULL_POINTER;
00273 
00274       DeleteElementTxn *txn;
00275       result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
00276       if (NS_FAILED(result)) return result;
00277       if (!txn) return NS_ERROR_NULL_POINTER;
00278 
00279       txn->Init(child, mRangeUpdater);
00280       AppendChild(txn);
00281       NS_RELEASE(txn);
00282     }
00283   }
00284   return result;
00285 }
00286 
00287 NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteContent(nsIDOMNode *aParent, 
00288                                                         PRUint32    aOffset, 
00289                                                         nsIEditor::EDirection aAction)
00290 {
00291   nsresult result = NS_OK;
00292   // see what kind of node we have
00293   nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(aParent);
00294   if (textNode)
00295   { // if the node is a text node, then delete text content
00296     PRUint32 start, numToDelete;
00297     if (nsIEditor::eNext == aAction)
00298     {
00299       start=aOffset;
00300       textNode->GetLength(&numToDelete);
00301       numToDelete -= aOffset;
00302     }
00303     else
00304     {
00305       start=0;
00306       numToDelete=aOffset;
00307     }
00308     
00309     if (numToDelete)
00310     {
00311       DeleteTextTxn *txn;
00312       result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
00313       if (NS_FAILED(result)) return result;
00314       if (!txn) return NS_ERROR_NULL_POINTER;
00315 
00316       txn->Init(mEditor, textNode, start, numToDelete, mRangeUpdater);
00317       AppendChild(txn);
00318       NS_RELEASE(txn);
00319     }
00320   }
00321 
00322   return result;
00323 }
00324 
00325 NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteNodesBetween()
00326 {
00327   nsCOMPtr<nsIContentIterator> iter = do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1");
00328   if (!iter) return NS_ERROR_NULL_POINTER;
00329 
00330   nsresult result = iter->Init(mRange);
00331   if (NS_FAILED(result)) return result;
00332 
00333   while (!iter->IsDone())
00334   {
00335     nsCOMPtr<nsIDOMNode> node = do_QueryInterface(iter->GetCurrentNode());
00336     if (!node)
00337       return NS_ERROR_NULL_POINTER;
00338 
00339     DeleteElementTxn *txn;
00340     result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
00341     if (NS_FAILED(result)) return result;
00342     if (!txn) return NS_ERROR_NULL_POINTER;
00343 
00344     txn->Init(node, mRangeUpdater);
00345     AppendChild(txn);
00346     NS_RELEASE(txn);
00347     iter->Next();
00348   }
00349   return result;
00350 }
00351