Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Friends
SplitElementTxn Class Reference

A transaction that splits an element E into two identical nodes, E1 and E2 with the children of E divided between E1 and E2. More...

#include <SplitElementTxn.h>

Inheritance diagram for SplitElementTxn:
Inheritance graph
[legend]
Collaboration diagram for SplitElementTxn:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_IMETHOD Init (nsEditor *aEditor, nsIDOMNode *aNode, PRInt32 aOffset)
 initialize the transaction.
virtual ~SplitElementTxn ()
NS_IMETHOD DoTransaction (void)
NS_IMETHOD UndoTransaction (void)
NS_IMETHOD RedoTransaction (void)
NS_IMETHOD Merge (nsITransaction *aTransaction, PRBool *aDidMerge)
NS_IMETHOD GetTxnDescription (nsAString &aTxnDescription)
NS_IMETHOD GetNewNode (nsIDOMNode **aNewNode)
NS_IMETHOD GetIsTransient (PRBool *aIsTransient)
void doTransaction ()
 Executes the transaction.
void undoTransaction ()
 Restores the state to what it was before the transaction was executed.
void redoTransaction ()
 Executes the transaction again.
boolean merge (in nsITransaction aTransaction)
 Attempts to merge a transaction into "this" transaction.

Static Public Member Functions

static const nsIIDGetCID ()

Public Attributes

readonly attribute boolean isTransient
 The transaction's transient state.
readonly attribute DOMString txnDescription

Protected Member Functions

 SplitElementTxn ()

Protected Attributes

nsCOMPtr< nsIDOMNodemExistingRightNode
 the element to operate upon
PRInt32 mOffset
 the offset into mElement where the children of mElement are split.
nsCOMPtr< nsIDOMNodemNewLeftNode
 the element we create when splitting mElement
nsCOMPtr< nsIDOMNodemParent
 the parent shared by mExistingRightNode and mNewLeftNode
nsEditormEditor

Friends

class TransactionFactory

Detailed Description

A transaction that splits an element E into two identical nodes, E1 and E2 with the children of E divided between E1 and E2.

Definition at line 57 of file SplitElementTxn.h.


Constructor & Destructor Documentation

Definition at line 50 of file SplitElementTxn.cpp.

  : EditTxn()
{
}

Definition at line 68 of file SplitElementTxn.cpp.

{
}

Member Function Documentation

Executes the transaction.

Reimplemented from EditTxn.

Definition at line 72 of file SplitElementTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy) { printf("%p Do Split of node %p offset %d\n", this, mExistingRightNode.get(), mOffset); }
#endif

  NS_ASSERTION(mExistingRightNode && mEditor, "bad state");
  if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }

  // create a new node
  nsresult result = mExistingRightNode->CloneNode(PR_FALSE, getter_AddRefs(mNewLeftNode));
  NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element.");
  if (NS_FAILED(result)) return result;
  if (!mNewLeftNode) return NS_ERROR_NULL_POINTER;
  mEditor->MarkNodeDirty(mExistingRightNode);

#ifdef NS_DEBUG
  if (gNoisy) { printf("  created left node = %p\n", mNewLeftNode.get()); }
#endif

  // get the parent node
  result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
  if (NS_FAILED(result)) return result;
  if (!mParent) return NS_ERROR_NULL_POINTER;

  // insert the new node
  result = mEditor->SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
  if (NS_SUCCEEDED(result) && mNewLeftNode)
  {
    nsCOMPtr<nsISelection>selection;
    mEditor->GetSelection(getter_AddRefs(selection));
    if (NS_FAILED(result)) return result;
    if (!selection) return NS_ERROR_NULL_POINTER;
    result = selection->Collapse(mNewLeftNode, mOffset);
  }
  else {
    result = NS_ERROR_NOT_IMPLEMENTED;
  }
  return result;
}

Here is the call graph for this function:

static const nsIID& SplitElementTxn::GetCID ( ) [inline, static]

Reimplemented from EditTxn.

Definition at line 61 of file SplitElementTxn.h.

{ static const nsIID iid = SPLIT_ELEMENT_TXN_CID; return iid; }

Here is the caller graph for this function:

NS_IMETHODIMP EditTxn::GetIsTransient ( PRBool aIsTransient) [inherited]

Reimplemented in JoinElementTxn, SetDocTitleTxn, and EditAggregateTxn.

Definition at line 66 of file EditTxn.cpp.

{
  if (aIsTransient)
    *aIsTransient = PR_FALSE;
  return NS_OK;
}

Definition at line 229 of file SplitElementTxn.cpp.

{
  if (!aNewNode)
    return NS_ERROR_NULL_POINTER;
  if (!mNewLeftNode)
    return NS_ERROR_NOT_INITIALIZED;
  *aNewNode = mNewLeftNode;
  NS_ADDREF(*aNewNode);
  return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP SplitElementTxn::GetTxnDescription ( nsAString &  aTxnDescription)

Reimplemented from EditTxn.

Definition at line 223 of file SplitElementTxn.cpp.

{
  aString.AssignLiteral("SplitElementTxn");
  return NS_OK;
}
NS_IMETHODIMP SplitElementTxn::Init ( nsEditor aEditor,
nsIDOMNode aNode,
PRInt32  aOffset 
)

initialize the transaction.

Parameters:
aEditorthe provider of core editing operations
aNodethe node to split
aOffsetthe location within aNode to do the split. aOffset may refer to children of aNode, or content of aNode. The left node will have child|content 0..aOffset-1.

Definition at line 55 of file SplitElementTxn.cpp.

{
  NS_ASSERTION(aEditor && aNode, "bad args");
  if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; }

  mEditor = aEditor;
  mExistingRightNode = do_QueryInterface(aNode);
  mOffset = aOffset;
  return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP SplitElementTxn::Merge ( nsITransaction aTransaction,
PRBool aDidMerge 
)

Reimplemented from EditTxn.

Definition at line 216 of file SplitElementTxn.cpp.

{
  if (aDidMerge)
    *aDidMerge = PR_FALSE;
  return NS_OK;
}
boolean nsITransaction::merge ( in nsITransaction  aTransaction) [inherited]

Attempts to merge a transaction into "this" transaction.

Both transactions must be in their undo state, doTransaction() methods already called. The transaction manager calls this method to coalesce a new transaction with the transaction on the top of the undo stack. This method returns a boolean value that indicates the merge result. A true value indicates that the transactions were merged successfully, a false value if the merge was not possible or failed. If true, the transaction manager will Release() the new transacton instead of pushing it on the undo stack.

Parameters:
aTransactionthe previously executed transaction to merge.

Executes the transaction again.

Can only be called on a transaction that was previously undone.

In most cases, the redoTransaction() method will actually call the doTransaction() method to execute the transaction again.

Reimplemented from EditTxn.

Definition at line 147 of file SplitElementTxn.cpp.

{
  NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
  if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
    return NS_ERROR_NOT_INITIALIZED;
  }

#ifdef NS_DEBUG
  if (gNoisy) { 
    printf("%p Redo Split of existing node %p and new node %p offset %d\n", 
           this, mExistingRightNode.get(), mNewLeftNode.get(), mOffset); 
    if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
  }
#endif

  nsresult result;
  nsCOMPtr<nsIDOMNode>resultNode;
  // first, massage the existing node so it is in its post-split state
  nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mExistingRightNode);
  if (rightNodeAsText)
  {
    result = rightNodeAsText->DeleteData(0, mOffset);
#ifdef NS_DEBUG
    if (gNoisy) 
    { 
      printf("** after delete of text in right text node %p offset %d\n", rightNodeAsText.get(), mOffset);
      mEditor->DebugDumpContent();  // DEBUG
    }
#endif
  }
  else
  {
    nsCOMPtr<nsIDOMNode>child;
    nsCOMPtr<nsIDOMNode>nextSibling;
    result = mExistingRightNode->GetFirstChild(getter_AddRefs(child));
    PRInt32 i;
    for (i=0; i<mOffset; i++)
    {
      if (NS_FAILED(result)) {return result;}
      if (!child) {return NS_ERROR_NULL_POINTER;}
      child->GetNextSibling(getter_AddRefs(nextSibling));
      result = mExistingRightNode->RemoveChild(child, getter_AddRefs(resultNode));
      if (NS_SUCCEEDED(result)) 
      {
        result = mNewLeftNode->AppendChild(child, getter_AddRefs(resultNode));
#ifdef NS_DEBUG
        if (gNoisy) 
        { 
          printf("** move child node %p from right node %p to left node %p\n", child.get(), mExistingRightNode.get(), mNewLeftNode.get());
          if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
        }
#endif
      }
      child = do_QueryInterface(nextSibling);
    }
  }
  // second, re-insert the left node into the tree 
  result = mParent->InsertBefore(mNewLeftNode, mExistingRightNode, getter_AddRefs(resultNode));
#ifdef NS_DEBUG
  if (gNoisy) 
  { 
    printf("** reinsert left child node %p before right node %p\n", mNewLeftNode.get(), mExistingRightNode.get());
    if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
  }
#endif
  return result;
}

Here is the call graph for this function:

Restores the state to what it was before the transaction was executed.

Reimplemented from EditTxn.

Definition at line 113 of file SplitElementTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy) { 
    printf("%p Undo Split of existing node %p and new node %p offset %d\n", 
           this, mExistingRightNode.get(), mNewLeftNode.get(), mOffset); 
  }
#endif

  NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
  if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
    return NS_ERROR_NOT_INITIALIZED;
  }

  // this assumes Do inserted the new node in front of the prior existing node
  nsresult result = mEditor->JoinNodesImpl(mExistingRightNode, mNewLeftNode, mParent, PR_FALSE);
#ifdef NS_DEBUG
  if (gNoisy) 
  { 
    printf("** after join left child node %p into right node %p\n", mNewLeftNode.get(), mExistingRightNode.get());
    if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
  }
  if (NS_SUCCEEDED(result))
  {
    if (gNoisy) { printf("  left node = %p removed\n", mNewLeftNode.get()); }
  }
#endif

  return result;
}

Here is the call graph for this function:


Friends And Related Function Documentation

friend class TransactionFactory [friend]

Definition at line 109 of file SplitElementTxn.h.


Member Data Documentation

The transaction's transient state.

This attribute is checked by the transaction manager after the transaction's Execute() method is called. If the transient state is false, a reference to the transaction is held by the transaction manager so that the transactions' undoTransaction() and redoTransaction() methods can be called. If the transient state is true, the transaction manager returns immediately after the transaction's doTransaction() method is called, no references to the transaction are maintained. Transient transactions cannot be undone or redone by the transaction manager.

Definition at line 79 of file nsITransaction.idl.

Definition at line 107 of file SplitElementTxn.h.

the element to operate upon

Definition at line 94 of file SplitElementTxn.h.

the element we create when splitting mElement

Definition at line 103 of file SplitElementTxn.h.

the offset into mElement where the children of mElement are split.


mOffset is the index of the first child in the right node. -1 means the new node gets no children.

Definition at line 100 of file SplitElementTxn.h.

the parent shared by mExistingRightNode and mNewLeftNode

Definition at line 106 of file SplitElementTxn.h.

readonly attribute DOMString nsPIEditorTransaction::txnDescription [inherited]

Definition at line 50 of file nsPIEditorTransaction.idl.


The documentation for this class was generated from the following files: