Back to index

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

A transaction that creates a new node in the content tree. More...

#include <CreateElementTxn.h>

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

List of all members.

Public Types

enum  { eAppend = -1 }

Public Member Functions

NS_IMETHOD Init (nsEditor *aEditor, const nsAString &aTag, nsIDOMNode *aParent, PRUint32 aOffsetInParent)
 Initialize the transaction.
virtual ~CreateElementTxn ()
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 Attributes

nsEditormEditor
 the document into which the new node will be inserted
nsString mTag
 the tag (mapping to object type) for the new element
nsCOMPtr< nsIDOMNodemParent
 the node into which the new node will be inserted
PRUint32 mOffsetInParent
 the index in mParent for the new node
nsCOMPtr< nsIDOMNodemNewNode
 the new node to insert
nsCOMPtr< nsIDOMNodemRefNode
 the node we will insert mNewNode before.

Private Member Functions

 CreateElementTxn ()

Friends

class TransactionFactory

Detailed Description

A transaction that creates a new node in the content tree.

Definition at line 54 of file CreateElementTxn.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
eAppend 

Definition at line 60 of file CreateElementTxn.h.

{ eAppend=-1 };

Constructor & Destructor Documentation

Definition at line 54 of file CreateElementTxn.cpp.

  : EditTxn()
{
}

Definition at line 83 of file CreateElementTxn.cpp.

{
}

Member Function Documentation

Executes the transaction.

Reimplemented from EditTxn.

Definition at line 87 of file CreateElementTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy)
  {
    char* nodename = ToNewCString(mTag);
    printf("Do Create Element parent = %p <%s>, offset = %d\n", 
           mParent.get(), nodename, mOffsetInParent);
    nsMemory::Free(nodename);
  }
#endif

  NS_ASSERTION(mEditor && mParent, "bad state");
  if (!mEditor || !mParent) return NS_ERROR_NOT_INITIALIZED;
  // create a new node
  nsAutoString textNodeTag;
  nsresult result = nsEditor::GetTextNodeTag(textNodeTag);
  if (NS_FAILED(result)) { return result; }

  if (textNodeTag == mTag) 
  {
    nsCOMPtr<nsIDOMDocument>doc;
    result = mEditor->GetDocument(getter_AddRefs(doc));
    if (NS_FAILED(result)) return result;
    if (!doc) return NS_ERROR_NULL_POINTER;

    const nsString stringData;
    nsCOMPtr<nsIDOMText>newTextNode;
    result = doc->CreateTextNode(stringData, getter_AddRefs(newTextNode));
    if (NS_FAILED(result)) return result;
    if (!newTextNode) return NS_ERROR_NULL_POINTER;
    mNewNode = do_QueryInterface(newTextNode);
  }
  else 
  {
    nsCOMPtr<nsIContent> newContent;
 
    //new call to use instead to get proper HTML element, bug# 39919
    result = mEditor->CreateHTMLContent(mTag, getter_AddRefs(newContent));
    if (NS_FAILED(result)) return result;
    nsCOMPtr<nsIDOMElement>newElement = do_QueryInterface(newContent);
    if (!newElement) return NS_ERROR_NULL_POINTER;
    mNewNode = do_QueryInterface(newElement);
    // Try to insert formatting whitespace for the new node:
    mEditor->MarkNodeDirty(mNewNode);
  }
  NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewNode)), "could not create element.");
  if (!mNewNode) return NS_ERROR_NULL_POINTER;

#ifdef NS_DEBUG
  if (gNoisy) { printf("  newNode = %p\n", mNewNode.get()); }
#endif

  // insert the new node
  nsCOMPtr<nsIDOMNode> resultNode;
  if (CreateElementTxn::eAppend==(PRInt32)mOffsetInParent)
  {
    result = mParent->AppendChild(mNewNode, getter_AddRefs(resultNode));
  }
  else
  {
    nsCOMPtr<nsIDOMNodeList> childNodes;
    result = mParent->GetChildNodes(getter_AddRefs(childNodes));
    if ((NS_SUCCEEDED(result)) && (childNodes))
    {
      PRUint32 count;
      childNodes->GetLength(&count);
      if (mOffsetInParent>count)
        mOffsetInParent = count;
      result = childNodes->Item(mOffsetInParent, getter_AddRefs(mRefNode));
      if (NS_FAILED(result)) return result; // note, it's ok for mRefNode to be null.  that means append

      result = mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
      if (NS_FAILED(result)) return result; 

      // only set selection to insertion point if editor gives permission
      PRBool bAdjustSelection;
      mEditor->ShouldTxnSetSelection(&bAdjustSelection);
      if (bAdjustSelection)
      {
        nsCOMPtr<nsISelection> selection;
        result = mEditor->GetSelection(getter_AddRefs(selection));
        if (NS_FAILED(result)) return result;
        if (!selection) return NS_ERROR_NULL_POINTER;

        PRInt32 offset=0;
        result = nsEditor::GetChildOffset(mNewNode, mParent, offset);
        if (NS_FAILED(result)) return result;

        result = selection->Collapse(mParent, offset+1);
        NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
       }
      else
      {
        // do nothing - dom range gravity will adjust selection
      }
    }
  }
  return result;
}

Here is the call graph for this function:

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

Reimplemented from EditTxn.

Definition at line 58 of file CreateElementTxn.h.

{ static const nsIID iid = CREATE_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 238 of file CreateElementTxn.cpp.

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

Here is the call graph for this function:

NS_IMETHODIMP CreateElementTxn::GetTxnDescription ( nsAString &  aTxnDescription)

Reimplemented from EditTxn.

Definition at line 231 of file CreateElementTxn.cpp.

{
  aString.AssignLiteral("CreateElementTxn: ");
  aString += mTag;
  return NS_OK;
}
NS_IMETHODIMP CreateElementTxn::Init ( nsEditor aEditor,
const nsAString &  aTag,
nsIDOMNode aParent,
PRUint32  aOffsetInParent 
)

Initialize the transaction.

Parameters:
aEditorthe provider of basic editing functionality
aTagthe tag (P, HR, TABLE, etc.) for the new element
aParentthe node into which the new element will be inserted
aOffsetInParentthe location in aParent to insert the new element if eAppend, the new element is appended as the last child

Definition at line 59 of file CreateElementTxn.cpp.

{
  NS_ASSERTION(aEditor&&aParent, "null args");
  if (!aEditor || !aParent) { return NS_ERROR_NULL_POINTER; }

  mEditor = aEditor;
  mTag = aTag;
  mParent = do_QueryInterface(aParent);
  mOffsetInParent = aOffsetInParent;
#ifdef NS_DEBUG
  {
    nsCOMPtr<nsIDOMNodeList> testChildNodes;
    nsresult testResult = mParent->GetChildNodes(getter_AddRefs(testChildNodes));
    NS_ASSERTION(testChildNodes, "bad parent type, can't have children.");
    NS_ASSERTION(NS_SUCCEEDED(testResult), "bad result.");
  }
#endif
  return NS_OK;
}

Here is the call graph for this function:

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

Reimplemented from EditTxn.

Definition at line 224 of file CreateElementTxn.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 202 of file CreateElementTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy) { printf("Redo Create Element\n"); }
#endif

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

  // first, reset mNewNode so it has no attributes or content
  nsCOMPtr<nsIDOMCharacterData>nodeAsText = do_QueryInterface(mNewNode);
  if (nodeAsText)
  {
    nsAutoString nullString;
    nodeAsText->SetData(nullString);
  }
  
  // now, reinsert mNewNode
  nsCOMPtr<nsIDOMNode> resultNode;
  return mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
}

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 188 of file CreateElementTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy) { printf("Undo Create Element, mParent = %p, node = %p\n",
                        mParent.get(), mNewNode.get()); }
#endif

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

  nsCOMPtr<nsIDOMNode> resultNode;
  return mParent->RemoveChild(mNewNode, getter_AddRefs(resultNode));
}

Here is the call graph for this function:


Friends And Related Function Documentation

friend class TransactionFactory [friend]

Definition at line 113 of file CreateElementTxn.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.

the document into which the new node will be inserted

Definition at line 96 of file CreateElementTxn.h.

the new node to insert

Definition at line 108 of file CreateElementTxn.h.

the index in mParent for the new node

Definition at line 105 of file CreateElementTxn.h.

the node into which the new node will be inserted

Definition at line 102 of file CreateElementTxn.h.

the node we will insert mNewNode before.

We compute this ourselves.

Definition at line 111 of file CreateElementTxn.h.

the tag (mapping to object type) for the new element

Definition at line 99 of file CreateElementTxn.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: