LCOV - code coverage report
Current view: directory - editor/libeditor/base - nsSelectionState.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 314 0 0.0 %
Date: 2012-06-02 Functions: 34 0 0.0 %

       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
      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 "nsSelectionState.h"
      39                 : #include "nsIDOMCharacterData.h"
      40                 : #include "nsIDOMNode.h"
      41                 : #include "nsRange.h"
      42                 : #include "nsISelection.h"
      43                 : #include "nsEditor.h"
      44                 : #include "nsEditorUtils.h"
      45                 : 
      46                 : 
      47                 : /***************************************************************************
      48                 :  * class for recording selection info.  stores selection as collection of
      49                 :  * { {startnode, startoffset} , {endnode, endoffset} } tuples.  Can't store
      50                 :  * ranges since dom gravity will possibly change the ranges.
      51                 :  */
      52               0 : nsSelectionState::nsSelectionState() : mArray(){}
      53                 : 
      54               0 : nsSelectionState::~nsSelectionState() 
      55                 : {
      56               0 :   MakeEmpty();
      57               0 : }
      58                 : 
      59                 : void
      60               0 : nsSelectionState::DoTraverse(nsCycleCollectionTraversalCallback &cb)
      61                 : {
      62               0 :   for (PRUint32 i = 0, iEnd = mArray.Length(); i < iEnd; ++i)
      63                 :   {
      64               0 :     nsRangeStore &item = mArray[i];
      65               0 :     NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
      66                 :                                        "selection state mArray[i].startNode");
      67               0 :     cb.NoteXPCOMChild(item.startNode);
      68               0 :     NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
      69                 :                                        "selection state mArray[i].endNode");
      70               0 :     cb.NoteXPCOMChild(item.endNode);
      71                 :   }
      72               0 : }
      73                 : 
      74                 : nsresult  
      75               0 : nsSelectionState::SaveSelection(nsISelection *aSel)
      76                 : {
      77               0 :   NS_ENSURE_TRUE(aSel, NS_ERROR_NULL_POINTER);
      78               0 :   PRInt32 i,rangeCount, arrayCount = mArray.Length();
      79               0 :   aSel->GetRangeCount(&rangeCount);
      80                 :   
      81                 :   // if we need more items in the array, new them
      82               0 :   if (arrayCount<rangeCount)
      83                 :   {
      84               0 :     PRInt32 count = rangeCount-arrayCount;
      85               0 :     for (i=0; i<count; i++)
      86                 :     {
      87               0 :       mArray.AppendElement();
      88                 :     }
      89                 :   }
      90                 :   
      91                 :   // else if we have too many, delete them
      92               0 :   else if (arrayCount>rangeCount)
      93                 :   {
      94               0 :     for (i = arrayCount-1; i >= rangeCount; i--)
      95                 :     {
      96               0 :       mArray.RemoveElementAt(i);
      97                 :     }
      98                 :   }
      99                 :   
     100                 :   // now store the selection ranges
     101               0 :   nsresult res = NS_OK;
     102               0 :   for (i=0; i<rangeCount; i++)
     103                 :   {
     104               0 :     nsCOMPtr<nsIDOMRange> range;
     105               0 :     res = aSel->GetRangeAt(i, getter_AddRefs(range));
     106               0 :     mArray[i].StoreRange(range);
     107                 :   }
     108                 :   
     109               0 :   return res;
     110                 : }
     111                 : 
     112                 : nsresult  
     113               0 : nsSelectionState::RestoreSelection(nsISelection *aSel)
     114                 : {
     115               0 :   NS_ENSURE_TRUE(aSel, NS_ERROR_NULL_POINTER);
     116                 :   nsresult res;
     117               0 :   PRUint32 i, arrayCount = mArray.Length();
     118                 : 
     119                 :   // clear out selection
     120               0 :   aSel->RemoveAllRanges();
     121                 :   
     122                 :   // set the selection ranges anew
     123               0 :   for (i=0; i<arrayCount; i++)
     124                 :   {
     125               0 :     nsRefPtr<nsRange> range;
     126               0 :     mArray[i].GetRange(getter_AddRefs(range));
     127               0 :     NS_ENSURE_TRUE(range, NS_ERROR_UNEXPECTED);
     128                 :    
     129               0 :     res = aSel->AddRange(range);
     130               0 :     if(NS_FAILED(res)) return res;
     131                 : 
     132                 :   }
     133               0 :   return NS_OK;
     134                 : }
     135                 : 
     136                 : bool
     137               0 : nsSelectionState::IsCollapsed()
     138                 : {
     139               0 :   if (1 != mArray.Length()) return false;
     140               0 :   nsRefPtr<nsRange> range;
     141               0 :   mArray[0].GetRange(getter_AddRefs(range));
     142               0 :   NS_ENSURE_TRUE(range, false);
     143               0 :   bool bIsCollapsed = false;
     144               0 :   range->GetCollapsed(&bIsCollapsed);
     145               0 :   return bIsCollapsed;
     146                 : }
     147                 : 
     148                 : bool
     149               0 : nsSelectionState::IsEqual(nsSelectionState *aSelState)
     150                 : {
     151               0 :   NS_ENSURE_TRUE(aSelState, false);
     152               0 :   PRUint32 i, myCount = mArray.Length(), itsCount = aSelState->mArray.Length();
     153               0 :   if (myCount != itsCount) return false;
     154               0 :   if (myCount < 1) return false;
     155                 : 
     156               0 :   for (i=0; i<myCount; i++)
     157                 :   {
     158               0 :     nsRefPtr<nsRange> myRange, itsRange;
     159               0 :     mArray[i].GetRange(getter_AddRefs(myRange));
     160               0 :     aSelState->mArray[i].GetRange(getter_AddRefs(itsRange));
     161               0 :     NS_ENSURE_TRUE(myRange && itsRange, false);
     162                 :   
     163                 :     PRInt16 compResult;
     164                 :     nsresult rv;
     165               0 :     rv = myRange->CompareBoundaryPoints(nsIDOMRange::START_TO_START, itsRange, &compResult);
     166               0 :     if (NS_FAILED(rv) || compResult) return false;
     167               0 :     rv = myRange->CompareBoundaryPoints(nsIDOMRange::END_TO_END, itsRange, &compResult);
     168               0 :     if (NS_FAILED(rv) || compResult) return false;
     169                 :   }
     170                 :   // if we got here, they are equal
     171               0 :   return true;
     172                 : }
     173                 : 
     174                 : void     
     175               0 : nsSelectionState::MakeEmpty()
     176                 : {
     177                 :   // free any items in the array
     178               0 :   mArray.Clear();
     179               0 : }
     180                 : 
     181                 : bool     
     182               0 : nsSelectionState::IsEmpty()
     183                 : {
     184               0 :   return mArray.IsEmpty();
     185                 : }
     186                 : 
     187                 : /***************************************************************************
     188                 :  * nsRangeUpdater:  class for updating nsIDOMRanges in response to editor actions.
     189                 :  */
     190                 : 
     191               0 : nsRangeUpdater::nsRangeUpdater() : mArray(), mLock(false) {}
     192                 : 
     193               0 : nsRangeUpdater::~nsRangeUpdater()
     194                 : {
     195                 :   // nothing to do, we don't own the items in our array.
     196               0 : }
     197                 :   
     198                 : void 
     199               0 : nsRangeUpdater::RegisterRangeItem(nsRangeStore *aRangeItem)
     200                 : {
     201               0 :   if (!aRangeItem) return;
     202               0 :   if (mArray.Contains(aRangeItem))
     203                 :   {
     204               0 :     NS_ERROR("tried to register an already registered range");
     205               0 :     return;  // don't register it again.  It would get doubly adjusted.
     206                 :   }
     207               0 :   mArray.AppendElement(aRangeItem);
     208                 : }
     209                 : 
     210                 : void 
     211               0 : nsRangeUpdater::DropRangeItem(nsRangeStore *aRangeItem)
     212                 : {
     213               0 :   if (!aRangeItem) return;
     214               0 :   mArray.RemoveElement(aRangeItem);
     215                 : }
     216                 : 
     217                 : nsresult 
     218               0 : nsRangeUpdater::RegisterSelectionState(nsSelectionState &aSelState)
     219                 : {
     220               0 :   PRUint32 i, theCount = aSelState.mArray.Length();
     221               0 :   if (theCount < 1) return NS_ERROR_FAILURE;
     222                 : 
     223               0 :   for (i=0; i<theCount; i++)
     224                 :   {
     225               0 :     RegisterRangeItem(&aSelState.mArray[i]);
     226                 :   }
     227                 : 
     228               0 :   return NS_OK;
     229                 : }
     230                 : 
     231                 : nsresult 
     232               0 : nsRangeUpdater::DropSelectionState(nsSelectionState &aSelState)
     233                 : {
     234               0 :   PRUint32 i, theCount = aSelState.mArray.Length();
     235               0 :   if (theCount < 1) return NS_ERROR_FAILURE;
     236                 : 
     237               0 :   for (i=0; i<theCount; i++)
     238                 :   {
     239               0 :     DropRangeItem(&aSelState.mArray[i]);
     240                 :   }
     241                 : 
     242               0 :   return NS_OK;
     243                 : }
     244                 : 
     245                 : // gravity methods:
     246                 : 
     247                 : nsresult
     248               0 : nsRangeUpdater::SelAdjCreateNode(nsIDOMNode *aParent, PRInt32 aPosition)
     249                 : {
     250               0 :   if (mLock) return NS_OK;  // lock set by Will/DidReplaceParent, etc...
     251               0 :   NS_ENSURE_TRUE(aParent, NS_ERROR_NULL_POINTER);
     252               0 :   PRUint32 i, count = mArray.Length();
     253               0 :   if (!count) {
     254               0 :     return NS_OK;
     255                 :   }
     256                 : 
     257                 :   nsRangeStore *item;
     258                 :   
     259               0 :   for (i=0; i<count; i++)
     260                 :   {
     261               0 :     item = mArray[i];
     262               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     263                 :     
     264               0 :     if ((item->startNode.get() == aParent) && (item->startOffset > aPosition))
     265               0 :       item->startOffset++;
     266               0 :     if ((item->endNode.get() == aParent) && (item->endOffset > aPosition))
     267               0 :       item->endOffset++;
     268                 :   }
     269               0 :   return NS_OK;
     270                 : }
     271                 : 
     272                 : nsresult
     273               0 : nsRangeUpdater::SelAdjInsertNode(nsIDOMNode *aParent, PRInt32 aPosition)
     274                 : {
     275               0 :   return SelAdjCreateNode(aParent, aPosition);
     276                 : }
     277                 : 
     278                 : 
     279                 : nsresult
     280               0 : nsRangeUpdater::SelAdjDeleteNode(nsIDOMNode *aNode)
     281                 : {
     282               0 :   if (mLock) return NS_OK;  // lock set by Will/DidReplaceParent, etc...
     283               0 :   NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
     284               0 :   PRUint32 i, count = mArray.Length();
     285               0 :   if (!count) {
     286               0 :     return NS_OK;
     287                 :   }
     288                 : 
     289               0 :   nsCOMPtr<nsIDOMNode> parent;
     290               0 :   PRInt32 offset = 0;
     291                 :   
     292               0 :   nsresult res = nsEditor::GetNodeLocation(aNode, address_of(parent), &offset);
     293               0 :   NS_ENSURE_SUCCESS(res, res);
     294                 :   
     295                 :   // check for range endpoints that are after aNode and in the same parent
     296                 :   nsRangeStore *item;
     297               0 :   for (i=0; i<count; i++)
     298                 :   {
     299               0 :     item = mArray[i];
     300               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     301                 :     
     302               0 :     if ((item->startNode.get() == parent) && (item->startOffset > offset))
     303               0 :       item->startOffset--;
     304               0 :     if ((item->endNode.get() == parent) && (item->endOffset > offset))
     305               0 :       item->endOffset--;
     306                 :       
     307                 :     // check for range endpoints that are in aNode
     308               0 :     if (item->startNode == aNode)
     309                 :     {
     310               0 :       item->startNode   = parent;
     311               0 :       item->startOffset = offset;
     312                 :     }
     313               0 :     if (item->endNode == aNode)
     314                 :     {
     315               0 :       item->endNode   = parent;
     316               0 :       item->endOffset = offset;
     317                 :     }
     318                 : 
     319                 :     // check for range endpoints that are in descendants of aNode
     320               0 :     nsCOMPtr<nsIDOMNode> oldStart;
     321               0 :     if (nsEditorUtils::IsDescendantOf(item->startNode, aNode))
     322                 :     {
     323               0 :       oldStart = item->startNode;  // save for efficiency hack below.
     324               0 :       item->startNode   = parent;
     325               0 :       item->startOffset = offset;
     326                 :     }
     327                 : 
     328                 :     // avoid having to call IsDescendantOf() for common case of range startnode == range endnode.
     329               0 :     if ((item->endNode == oldStart) || nsEditorUtils::IsDescendantOf(item->endNode, aNode))
     330                 :     {
     331               0 :       item->endNode   = parent;
     332               0 :       item->endOffset = offset;
     333                 :     }
     334                 :   }
     335               0 :   return NS_OK;
     336                 : }
     337                 : 
     338                 : 
     339                 : nsresult
     340               0 : nsRangeUpdater::SelAdjSplitNode(nsIDOMNode *aOldRightNode, PRInt32 aOffset, nsIDOMNode *aNewLeftNode)
     341                 : {
     342               0 :   if (mLock) return NS_OK;  // lock set by Will/DidReplaceParent, etc...
     343               0 :   NS_ENSURE_TRUE(aOldRightNode && aNewLeftNode, NS_ERROR_NULL_POINTER);
     344               0 :   PRUint32 i, count = mArray.Length();
     345               0 :   if (!count) {
     346               0 :     return NS_OK;
     347                 :   }
     348                 : 
     349               0 :   nsCOMPtr<nsIDOMNode> parent;
     350                 :   PRInt32 offset;
     351               0 :   nsresult result = nsEditor::GetNodeLocation(aOldRightNode, address_of(parent), &offset);
     352               0 :   NS_ENSURE_SUCCESS(result, result);
     353                 :   
     354                 :   // first part is same as inserting aNewLeftnode
     355               0 :   result = SelAdjInsertNode(parent,offset-1);
     356               0 :   NS_ENSURE_SUCCESS(result, result);
     357                 : 
     358                 :   // next step is to check for range enpoints inside aOldRightNode
     359                 :   nsRangeStore *item;
     360                 :   
     361               0 :   for (i=0; i<count; i++)
     362                 :   {
     363               0 :     item = mArray[i];
     364               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     365                 :     
     366               0 :     if (item->startNode.get() == aOldRightNode)
     367                 :     {
     368               0 :       if (item->startOffset > aOffset)
     369                 :       {
     370               0 :         item->startOffset -= aOffset;
     371                 :       }
     372                 :       else
     373                 :       {
     374               0 :         item->startNode = aNewLeftNode;
     375                 :       }
     376                 :     }
     377               0 :     if (item->endNode.get() == aOldRightNode)
     378                 :     {
     379               0 :       if (item->endOffset > aOffset)
     380                 :       {
     381               0 :         item->endOffset -= aOffset;
     382                 :       }
     383                 :       else
     384                 :       {
     385               0 :         item->endNode = aNewLeftNode;
     386                 :       }
     387                 :     }
     388                 :   }
     389               0 :   return NS_OK;
     390                 : }
     391                 : 
     392                 : 
     393                 : nsresult
     394               0 : nsRangeUpdater::SelAdjJoinNodes(nsIDOMNode *aLeftNode, 
     395                 :                                   nsIDOMNode *aRightNode, 
     396                 :                                   nsIDOMNode *aParent, 
     397                 :                                   PRInt32 aOffset,
     398                 :                                   PRInt32 aOldLeftNodeLength)
     399                 : {
     400               0 :   if (mLock) return NS_OK;  // lock set by Will/DidReplaceParent, etc...
     401               0 :   NS_ENSURE_TRUE(aLeftNode && aRightNode && aParent, NS_ERROR_NULL_POINTER);
     402               0 :   PRUint32 i, count = mArray.Length();
     403               0 :   if (!count) {
     404               0 :     return NS_OK;
     405                 :   }
     406                 : 
     407                 :   nsRangeStore *item;
     408                 : 
     409               0 :   for (i=0; i<count; i++)
     410                 :   {
     411               0 :     item = mArray[i];
     412               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     413                 :     
     414               0 :     if (item->startNode.get() == aParent)
     415                 :     {
     416                 :       // adjust start point in aParent
     417               0 :       if (item->startOffset > aOffset)
     418                 :       {
     419               0 :         item->startOffset--;
     420                 :       }
     421               0 :       else if (item->startOffset == aOffset)
     422                 :       {
     423                 :         // join keeps right hand node
     424               0 :         item->startNode = aRightNode;
     425               0 :         item->startOffset = aOldLeftNodeLength;
     426                 :       }
     427                 :     }
     428               0 :     else if (item->startNode.get() == aRightNode)
     429                 :     {
     430                 :       // adjust start point in aRightNode
     431               0 :       item->startOffset += aOldLeftNodeLength;
     432                 :     }
     433               0 :     else if (item->startNode.get() == aLeftNode)
     434                 :     {
     435                 :       // adjust start point in aLeftNode
     436               0 :       item->startNode = aRightNode;
     437                 :     }
     438                 : 
     439               0 :     if (item->endNode.get() == aParent)
     440                 :     {
     441                 :       // adjust end point in aParent
     442               0 :       if (item->endOffset > aOffset)
     443                 :       {
     444               0 :         item->endOffset--;
     445                 :       }
     446               0 :       else if (item->endOffset == aOffset)
     447                 :       {
     448                 :         // join keeps right hand node
     449               0 :         item->endNode = aRightNode;
     450               0 :         item->endOffset = aOldLeftNodeLength;
     451                 :       }
     452                 :     }
     453               0 :     else if (item->endNode.get() == aRightNode)
     454                 :     {
     455                 :       // adjust end point in aRightNode
     456               0 :        item->endOffset += aOldLeftNodeLength;
     457                 :     }
     458               0 :     else if (item->endNode.get() == aLeftNode)
     459                 :     {
     460                 :       // adjust end point in aLeftNode
     461               0 :       item->endNode = aRightNode;
     462                 :     }
     463                 :   }
     464                 :   
     465               0 :   return NS_OK;
     466                 : }
     467                 : 
     468                 : 
     469                 : nsresult
     470               0 : nsRangeUpdater::SelAdjInsertText(nsIDOMCharacterData *aTextNode, PRInt32 aOffset, const nsAString &aString)
     471                 : {
     472               0 :   if (mLock) return NS_OK;  // lock set by Will/DidReplaceParent, etc...
     473                 : 
     474               0 :   PRUint32 count = mArray.Length();
     475               0 :   if (!count) {
     476               0 :     return NS_OK;
     477                 :   }
     478               0 :   nsCOMPtr<nsIDOMNode> node(do_QueryInterface(aTextNode));
     479               0 :   NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
     480                 :   
     481               0 :   PRUint32 len=aString.Length(), i;
     482                 :   nsRangeStore *item;
     483               0 :   for (i=0; i<count; i++)
     484                 :   {
     485               0 :     item = mArray[i];
     486               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     487                 :     
     488               0 :     if ((item->startNode.get() == node) && (item->startOffset > aOffset))
     489               0 :       item->startOffset += len;
     490               0 :     if ((item->endNode.get() == node) && (item->endOffset > aOffset))
     491               0 :       item->endOffset += len;
     492                 :   }
     493               0 :   return NS_OK;
     494                 : }
     495                 : 
     496                 : 
     497                 : nsresult
     498               0 : nsRangeUpdater::SelAdjDeleteText(nsIDOMCharacterData *aTextNode, PRInt32 aOffset, PRInt32 aLength)
     499                 : {
     500               0 :   if (mLock) return NS_OK;  // lock set by Will/DidReplaceParent, etc...
     501                 : 
     502               0 :   PRUint32 i, count = mArray.Length();
     503               0 :   if (!count) {
     504               0 :     return NS_OK;
     505                 :   }
     506                 :   nsRangeStore *item;
     507               0 :   nsCOMPtr<nsIDOMNode> node(do_QueryInterface(aTextNode));
     508               0 :   NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
     509                 :   
     510               0 :   for (i=0; i<count; i++)
     511                 :   {
     512               0 :     item = mArray[i];
     513               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     514                 :     
     515               0 :     if ((item->startNode.get() == node) && (item->startOffset > aOffset))
     516                 :     {
     517               0 :       item->startOffset -= aLength;
     518               0 :       if (item->startOffset < 0) item->startOffset = 0;
     519                 :     }
     520               0 :     if ((item->endNode.get() == node) && (item->endOffset > aOffset))
     521                 :     {
     522               0 :       item->endOffset -= aLength;
     523               0 :       if (item->endOffset < 0) item->endOffset = 0;
     524                 :     }
     525                 :   }
     526               0 :   return NS_OK;
     527                 : }
     528                 : 
     529                 : 
     530                 : nsresult
     531               0 : nsRangeUpdater::WillReplaceContainer()
     532                 : {
     533               0 :   if (mLock) return NS_ERROR_UNEXPECTED;  
     534               0 :   mLock = true;
     535               0 :   return NS_OK;
     536                 : }
     537                 : 
     538                 : 
     539                 : nsresult
     540               0 : nsRangeUpdater::DidReplaceContainer(nsIDOMNode *aOriginalNode, nsIDOMNode *aNewNode)
     541                 : {
     542               0 :   NS_ENSURE_TRUE(mLock, NS_ERROR_UNEXPECTED);  
     543               0 :   mLock = false;
     544                 : 
     545               0 :   NS_ENSURE_TRUE(aOriginalNode && aNewNode, NS_ERROR_NULL_POINTER);
     546               0 :   PRUint32 i, count = mArray.Length();
     547               0 :   if (!count) {
     548               0 :     return NS_OK;
     549                 :   }
     550                 : 
     551                 :   nsRangeStore *item;
     552                 :   
     553               0 :   for (i=0; i<count; i++)
     554                 :   {
     555               0 :     item = mArray[i];
     556               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     557                 :     
     558               0 :     if (item->startNode.get() == aOriginalNode)
     559               0 :       item->startNode = aNewNode;
     560               0 :     if (item->endNode.get() == aOriginalNode)
     561               0 :       item->endNode = aNewNode;
     562                 :   }
     563               0 :   return NS_OK;
     564                 : }
     565                 : 
     566                 : 
     567                 : nsresult
     568               0 : nsRangeUpdater::WillRemoveContainer()
     569                 : {
     570               0 :   if (mLock) return NS_ERROR_UNEXPECTED;  
     571               0 :   mLock = true;
     572               0 :   return NS_OK;
     573                 : }
     574                 : 
     575                 : 
     576                 : nsresult
     577               0 : nsRangeUpdater::DidRemoveContainer(nsIDOMNode *aNode, nsIDOMNode *aParent, PRInt32 aOffset, PRUint32 aNodeOrigLen)
     578                 : {
     579               0 :   NS_ENSURE_TRUE(mLock, NS_ERROR_UNEXPECTED);  
     580               0 :   mLock = false;
     581                 : 
     582               0 :   NS_ENSURE_TRUE(aNode && aParent, NS_ERROR_NULL_POINTER);
     583               0 :   PRUint32 i, count = mArray.Length();
     584               0 :   if (!count) {
     585               0 :     return NS_OK;
     586                 :   }
     587                 : 
     588                 :   nsRangeStore *item;
     589                 :   
     590               0 :   for (i=0; i<count; i++)
     591                 :   {
     592               0 :     item = mArray[i];
     593               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     594                 :     
     595               0 :     if (item->startNode.get() == aNode)
     596                 :     {
     597               0 :       item->startNode = aParent;
     598               0 :       item->startOffset += aOffset;
     599                 :     }
     600               0 :     else if ((item->startNode.get() == aParent) && (item->startOffset > aOffset))
     601               0 :       item->startOffset += (PRInt32)aNodeOrigLen-1;
     602                 :       
     603               0 :     if (item->endNode.get() == aNode)
     604                 :     {
     605               0 :       item->endNode = aParent;
     606               0 :       item->endOffset += aOffset;
     607                 :     }
     608               0 :     else if ((item->endNode.get() == aParent) && (item->endOffset > aOffset))
     609               0 :       item->endOffset += (PRInt32)aNodeOrigLen-1;
     610                 :   }
     611               0 :   return NS_OK;
     612                 : }
     613                 : 
     614                 : 
     615                 : nsresult
     616               0 : nsRangeUpdater::WillInsertContainer()
     617                 : {
     618               0 :   if (mLock) return NS_ERROR_UNEXPECTED;  
     619               0 :   mLock = true;
     620               0 :   return NS_OK;
     621                 : }
     622                 : 
     623                 : 
     624                 : nsresult
     625               0 : nsRangeUpdater::DidInsertContainer()
     626                 : {
     627               0 :   NS_ENSURE_TRUE(mLock, NS_ERROR_UNEXPECTED);  
     628               0 :   mLock = false;
     629               0 :   return NS_OK;
     630                 : }
     631                 : 
     632                 : 
     633                 : nsresult
     634               0 : nsRangeUpdater::WillMoveNode()
     635                 : {
     636               0 :   if (mLock) return NS_ERROR_UNEXPECTED;  
     637               0 :   mLock = true;
     638               0 :   return NS_OK;
     639                 : }
     640                 : 
     641                 : 
     642                 : nsresult
     643               0 : nsRangeUpdater::DidMoveNode(nsIDOMNode *aOldParent, PRInt32 aOldOffset, nsIDOMNode *aNewParent, PRInt32 aNewOffset)
     644                 : {
     645               0 :   NS_ENSURE_TRUE(mLock, NS_ERROR_UNEXPECTED);  
     646               0 :   mLock = false;
     647                 : 
     648               0 :   NS_ENSURE_TRUE(aOldParent && aNewParent, NS_ERROR_NULL_POINTER);
     649               0 :   PRUint32 i, count = mArray.Length();
     650               0 :   if (!count) {
     651               0 :     return NS_OK;
     652                 :   }
     653                 : 
     654                 :   nsRangeStore *item;
     655                 :   
     656               0 :   for (i=0; i<count; i++)
     657                 :   {
     658               0 :     item = mArray[i];
     659               0 :     NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
     660                 :     
     661                 :     // like a delete in aOldParent
     662               0 :     if ((item->startNode.get() == aOldParent) && (item->startOffset > aOldOffset))
     663               0 :       item->startOffset--;
     664               0 :     if ((item->endNode.get() == aOldParent) && (item->endOffset > aOldOffset))
     665               0 :       item->endOffset--;
     666                 :       
     667                 :     // and like an insert in aNewParent
     668               0 :     if ((item->startNode.get() == aNewParent) && (item->startOffset > aNewOffset))
     669               0 :       item->startOffset++;
     670               0 :     if ((item->endNode.get() == aNewParent) && (item->endOffset > aNewOffset))
     671               0 :       item->endOffset++;
     672                 :   }
     673               0 :   return NS_OK;
     674                 : }
     675                 : 
     676                 : 
     677                 : 
     678                 : /***************************************************************************
     679                 :  * helper class for nsSelectionState.  nsRangeStore stores range endpoints.
     680                 :  */
     681                 : 
     682                 :   // DEBUG: PRInt32 nsRangeStore::n = 0;
     683                 : 
     684               0 : nsRangeStore::nsRangeStore() 
     685                 : { 
     686                 :   // DEBUG: n++;  printf("range store alloc count=%d\n", n); 
     687               0 : }
     688               0 : nsRangeStore::~nsRangeStore()
     689                 : {
     690                 :   // DEBUG: n--;  printf("range store alloc count=%d\n", n); 
     691               0 : }
     692                 : 
     693               0 : nsresult nsRangeStore::StoreRange(nsIDOMRange *aRange)
     694                 : {
     695               0 :   NS_ENSURE_TRUE(aRange, NS_ERROR_NULL_POINTER);
     696               0 :   aRange->GetStartContainer(getter_AddRefs(startNode));
     697               0 :   aRange->GetEndContainer(getter_AddRefs(endNode));
     698               0 :   aRange->GetStartOffset(&startOffset);
     699               0 :   aRange->GetEndOffset(&endOffset);
     700               0 :   return NS_OK;
     701                 : }
     702                 : 
     703               0 : nsresult nsRangeStore::GetRange(nsRange** outRange)
     704                 : {
     705                 :   return nsRange::CreateRange(startNode, startOffset, endNode, endOffset,
     706               0 :                               outRange);
     707                 : }

Generated by: LCOV version 1.7