LCOV - code coverage report
Current view: directory - editor/libeditor/base - JoinElementTxn.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 84 2 2.4 %
Date: 2012-06-02 Functions: 11 2 18.2 %

       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-1999
      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 "JoinElementTxn.h"
      39                 : #include "nsEditor.h"
      40                 : #include "nsIDOMNodeList.h"
      41                 : #include "nsIDOMCharacterData.h"
      42                 : 
      43                 : #ifdef NS_DEBUG
      44                 : static bool gNoisy = false;
      45                 : #endif
      46                 : 
      47               0 : JoinElementTxn::JoinElementTxn()
      48               0 :   : EditTxn()
      49                 : {
      50               0 : }
      51                 : 
      52            1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(JoinElementTxn)
      53                 : 
      54               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(JoinElementTxn, EditTxn)
      55               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mLeftNode)
      56               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mRightNode)
      57               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
      58               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
      59                 : 
      60               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(JoinElementTxn, EditTxn)
      61               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mLeftNode)
      62               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRightNode)
      63               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
      64               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
      65                 : 
      66               0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JoinElementTxn)
      67               0 : NS_INTERFACE_MAP_END_INHERITING(EditTxn)
      68                 : 
      69               0 : NS_IMETHODIMP JoinElementTxn::Init(nsEditor   *aEditor,
      70                 :                                    nsIDOMNode *aLeftNode,
      71                 :                                    nsIDOMNode *aRightNode)
      72                 : {
      73               0 :   NS_PRECONDITION((aEditor && aLeftNode && aRightNode), "null arg");
      74               0 :   if (!aEditor || !aLeftNode || !aRightNode) { return NS_ERROR_NULL_POINTER; }
      75               0 :   mEditor = aEditor;
      76               0 :   mLeftNode = do_QueryInterface(aLeftNode);
      77               0 :   nsCOMPtr<nsIDOMNode>leftParent;
      78               0 :   nsresult result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
      79               0 :   NS_ENSURE_SUCCESS(result, result);
      80               0 :   if (!mEditor->IsModifiableNode(leftParent)) {
      81               0 :     return NS_ERROR_FAILURE;
      82                 :   }
      83               0 :   mRightNode = do_QueryInterface(aRightNode);
      84               0 :   mOffset=0;
      85               0 :   return NS_OK;
      86                 : }
      87                 : 
      88                 : // After DoTransaction() and RedoTransaction(), the left node is removed from the content tree and right node remains.
      89               0 : NS_IMETHODIMP JoinElementTxn::DoTransaction(void)
      90                 : {
      91                 : #ifdef NS_DEBUG
      92               0 :   if (gNoisy)
      93                 :   {
      94                 :     printf("%p Do Join of %p and %p\n",
      95                 :            static_cast<void*>(this),
      96               0 :            static_cast<void*>(mLeftNode.get()),
      97               0 :            static_cast<void*>(mRightNode.get()));
      98                 :   }
      99                 : #endif
     100                 : 
     101               0 :   NS_PRECONDITION((mEditor && mLeftNode && mRightNode), "null arg");
     102               0 :   if (!mEditor || !mLeftNode || !mRightNode) { return NS_ERROR_NOT_INITIALIZED; }
     103                 : 
     104                 :   // get the parent node
     105               0 :   nsCOMPtr<nsIDOMNode>leftParent;
     106               0 :   nsresult result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
     107               0 :   NS_ENSURE_SUCCESS(result, result);
     108               0 :   NS_ENSURE_TRUE(leftParent, NS_ERROR_NULL_POINTER);
     109                 : 
     110                 :   // verify that mLeftNode and mRightNode have the same parent
     111               0 :   nsCOMPtr<nsIDOMNode>rightParent;
     112               0 :   result = mRightNode->GetParentNode(getter_AddRefs(rightParent));
     113               0 :   NS_ENSURE_SUCCESS(result, result);
     114               0 :   NS_ENSURE_TRUE(rightParent, NS_ERROR_NULL_POINTER);
     115                 : 
     116               0 :   if (leftParent==rightParent)
     117                 :   {
     118               0 :     mParent= do_QueryInterface(leftParent); // set this instance mParent. 
     119                 :                                             // Other methods will see a non-null mParent and know all is well
     120               0 :     nsCOMPtr<nsIDOMCharacterData> leftNodeAsText = do_QueryInterface(mLeftNode);
     121               0 :     if (leftNodeAsText) 
     122                 :     {
     123               0 :       leftNodeAsText->GetLength(&mOffset);
     124                 :     }
     125                 :     else 
     126                 :     {
     127               0 :       nsCOMPtr<nsIDOMNodeList> childNodes;
     128               0 :       result = mLeftNode->GetChildNodes(getter_AddRefs(childNodes));
     129               0 :       NS_ENSURE_SUCCESS(result, result);
     130               0 :       if (childNodes) 
     131                 :       {
     132               0 :         childNodes->GetLength(&mOffset);
     133                 :       }
     134                 :     }
     135               0 :     result = mEditor->JoinNodesImpl(mRightNode, mLeftNode, mParent, false);
     136                 : #ifdef NS_DEBUG
     137               0 :     if (NS_SUCCEEDED(result))
     138                 :     {
     139               0 :       if (gNoisy)
     140                 :       {
     141                 :         printf("  left node = %p removed\n",
     142               0 :                static_cast<void*>(mLeftNode.get()));
     143                 :       }
     144                 :     }
     145                 : #endif
     146                 :   }
     147                 :   else 
     148                 :   {
     149               0 :     NS_ASSERTION(false, "2 nodes do not have same parent");
     150               0 :     return NS_ERROR_INVALID_ARG;
     151                 :   }
     152               0 :   return result;
     153                 : }
     154                 : 
     155                 : //XXX: what if instead of split, we just deleted the unneeded children of mRight
     156                 : //     and re-inserted mLeft?
     157               0 : NS_IMETHODIMP JoinElementTxn::UndoTransaction(void)
     158                 : {
     159                 : #ifdef NS_DEBUG
     160               0 :   if (gNoisy)
     161                 :   {
     162                 :     printf("%p Undo Join, right node = %p\n",
     163                 :            static_cast<void*>(this),
     164               0 :            static_cast<void*>(mRightNode.get()));
     165                 :   }
     166                 : #endif
     167                 : 
     168               0 :   NS_ASSERTION(mRightNode && mLeftNode && mParent, "bad state");
     169               0 :   if (!mRightNode || !mLeftNode || !mParent) { return NS_ERROR_NOT_INITIALIZED; }
     170                 :   nsresult result;
     171               0 :   nsCOMPtr<nsIDOMNode>resultNode;
     172                 :   // first, massage the existing node so it is in its post-split state
     173               0 :   nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mRightNode);
     174               0 :   if (rightNodeAsText)
     175                 :   {
     176               0 :     result = rightNodeAsText->DeleteData(0, mOffset);
     177                 :   }
     178                 :   else
     179                 :   {
     180               0 :     nsCOMPtr<nsIDOMNode>child;
     181               0 :     result = mRightNode->GetFirstChild(getter_AddRefs(child));
     182               0 :     nsCOMPtr<nsIDOMNode>nextSibling;
     183                 :     PRUint32 i;
     184               0 :     for (i=0; i<mOffset; i++)
     185                 :     {
     186               0 :       if (NS_FAILED(result)) {return result;}
     187               0 :       if (!child) {return NS_ERROR_NULL_POINTER;}
     188               0 :       child->GetNextSibling(getter_AddRefs(nextSibling));
     189               0 :       result = mLeftNode->AppendChild(child, getter_AddRefs(resultNode));
     190               0 :       child = do_QueryInterface(nextSibling);
     191                 :     }
     192                 :   }
     193                 :   // second, re-insert the left node into the tree 
     194               0 :   result = mParent->InsertBefore(mLeftNode, mRightNode, getter_AddRefs(resultNode));
     195               0 :   return result;
     196                 : 
     197                 : }
     198                 : 
     199               0 : NS_IMETHODIMP JoinElementTxn::GetTxnDescription(nsAString& aString)
     200                 : {
     201               0 :   aString.AssignLiteral("JoinElementTxn");
     202               0 :   return NS_OK;
     203            4392 : }

Generated by: LCOV version 1.7