Back to index

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

A transaction that changes the document's title, which is a text node under the <title> tag in a page's <head> section provides default concrete behavior for all nsITransaction methods. More...

#include <SetDocTitleTxn.h>

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

List of all members.

Public Member Functions

virtual ~SetDocTitleTxn ()
NS_IMETHOD Init (nsIHTMLEditor *aEditor, const nsAString *aValue)
 Initialize the transaction.
NS_IMETHOD DoTransaction (void)
NS_IMETHOD UndoTransaction (void)
NS_IMETHOD RedoTransaction (void)
NS_IMETHOD Merge (nsITransaction *aTransaction, PRBool *aDidMerge)
NS_IMETHOD GetIsTransient (PRBool *aIsTransient)
NS_IMETHOD GetTxnDescription (nsAString &aTxnDescription)
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

nsIHTMLEditormEditor
 the editor that created this transaction
nsString mValue
 The new title string.
nsString mUndoValue
 The previous title string to use for undo.
PRPackedBool mIsTransient
 Set true if we dont' really change the title during Do()

Private Member Functions

 SetDocTitleTxn ()
nsresult SetDocTitle (const nsAString &aTitle)
nsresult SetDomTitle (const nsAString &aTitle)

Friends

class TransactionFactory

Detailed Description

A transaction that changes the document's title, which is a text node under the <title> tag in a page's <head> section provides default concrete behavior for all nsITransaction methods.

Definition at line 57 of file SetDocTitleTxn.h.


Constructor & Destructor Documentation

Definition at line 67 of file SetDocTitleTxn.cpp.

{
}

Definition at line 48 of file SetDocTitleTxn.cpp.


Member Function Documentation

Executes the transaction.

Reimplemented from EditTxn.

Definition at line 71 of file SetDocTitleTxn.cpp.

{
  nsresult res = SetDomTitle(mValue);
  if (NS_FAILED(res)) return res;

  return SetDocTitle(mValue);
}

Here is the call graph for this function:

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

Reimplemented from EditTxn.

Definition at line 61 of file SetDocTitleTxn.h.

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

Here is the caller graph for this function:

Reimplemented from EditTxn.

Definition at line 234 of file SetDocTitleTxn.cpp.

{
  if (!aIsTransient) return NS_ERROR_NULL_POINTER;  
  *aIsTransient = mIsTransient;
  return NS_OK;
}
NS_IMETHODIMP SetDocTitleTxn::GetTxnDescription ( nsAString &  aTxnDescription)

Reimplemented from EditTxn.

Definition at line 227 of file SetDocTitleTxn.cpp.

{
  aString.AssignLiteral("SetDocTitleTxn: ");
  aString += mValue;
  return NS_OK;
}
NS_IMETHODIMP SetDocTitleTxn::Init ( nsIHTMLEditor aEditor,
const nsAString *  aValue 
)

Initialize the transaction.

Parameters:
aEditorthe object providing core editing operations
aValuethe new value for document title

Definition at line 54 of file SetDocTitleTxn.cpp.

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

  mEditor = aEditor;
  mValue = *aValue;

  return NS_OK;
}
NS_IMETHODIMP SetDocTitleTxn::Merge ( nsITransaction aTransaction,
PRBool aDidMerge 
)

Reimplemented from EditTxn.

Definition at line 220 of file SetDocTitleTxn.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 84 of file SetDocTitleTxn.cpp.

{
  return SetDocTitle(mValue);
}

Here is the call graph for this function:

nsresult SetDocTitleTxn::SetDocTitle ( const nsAString &  aTitle) [private]

Definition at line 89 of file SetDocTitleTxn.cpp.

{
  NS_ASSERTION(mEditor, "bad state");
  if (!mEditor) return NS_ERROR_NOT_INITIALIZED;

  nsCOMPtr<nsIEditor> editor = do_QueryInterface(mEditor);
  if (!editor) return NS_ERROR_FAILURE;
  nsCOMPtr<nsIDOMDocument> domDoc;
  nsresult rv = editor->GetDocument(getter_AddRefs(domDoc));
  if (NS_FAILED(rv)) return rv;
  nsCOMPtr<nsIDOMHTMLDocument> HTMLDoc = do_QueryInterface(domDoc);
  if (!HTMLDoc) return NS_ERROR_FAILURE;

  return HTMLDoc->SetTitle(aTitle);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult SetDocTitleTxn::SetDomTitle ( const nsAString &  aTitle) [private]

Definition at line 105 of file SetDocTitleTxn.cpp.

{
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(mEditor);
  if (!editor) return NS_ERROR_FAILURE;
  nsCOMPtr<nsIDOMDocument> domDoc;
  nsresult res = editor->GetDocument(getter_AddRefs(domDoc));
  if (!domDoc) return NS_ERROR_FAILURE;

  nsCOMPtr<nsIDOMNodeList> titleList;
  res = domDoc->GetElementsByTagName(NS_LITERAL_STRING("title"), getter_AddRefs(titleList));
  if (NS_FAILED(res)) return res;

  // First assume we will NOT really do anything
  // (transaction will not be pushed on stack)
  mIsTransient = PR_TRUE;

  nsCOMPtr<nsIDOMNode>titleNode;
  if(titleList)
  {
    res = titleList->Item(0, getter_AddRefs(titleNode));
    if (NS_FAILED(res)) return res;
    if (titleNode)
    {
      // Delete existing child textnode of title node
      // (Note: all contents under a TITLE node are always in a single text node)
      nsCOMPtr<nsIDOMNode> child;
      res = titleNode->GetFirstChild(getter_AddRefs(child));
      if(NS_FAILED(res)) return res;
      if(child)
      {
        // Save current text as the undo value
        nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(child);
        if(textNode)
        {
          textNode->GetData(mUndoValue);

          // If title text is identical to what already exists,
          // quit now (mIsTransient is now TRUE)
          if (mUndoValue == aTitle)
            return NS_OK;
        }
        res = editor->DeleteNode(child);
        if(NS_FAILED(res)) return res;
      }
    }
  }

  // We didn't return above, thus we really will be changing the title
  mIsTransient = PR_FALSE;

  // Get the <HEAD> node, create a <TITLE> and insert it under the HEAD
  nsCOMPtr<nsIDOMNodeList> headList;
  res = domDoc->GetElementsByTagName(NS_LITERAL_STRING("head"),getter_AddRefs(headList));
  if (NS_FAILED(res)) return res;
  if (!headList) return NS_ERROR_FAILURE;
  
  nsCOMPtr<nsIDOMNode>headNode;
  headList->Item(0, getter_AddRefs(headNode));
  if (!headNode) return NS_ERROR_FAILURE;

  PRBool   newTitleNode = PR_FALSE;
  PRUint32 newTitleIndex = 0;

  if (!titleNode)
  {
    // Didn't find one above: Create a new one
    nsCOMPtr<nsIDOMElement>titleElement;
    res = domDoc->CreateElement(NS_LITERAL_STRING("title"), getter_AddRefs(titleElement));
    if (NS_FAILED(res)) return res;
    if (!titleElement) return NS_ERROR_FAILURE;

    titleNode = do_QueryInterface(titleElement);
    newTitleNode = PR_TRUE;

    // Get index so we append new title node 
    // after all existing HEAD children
    nsCOMPtr<nsIDOMNodeList> children;
    res = headNode->GetChildNodes(getter_AddRefs(children));
    if (NS_FAILED(res)) return res;
    if (children)
      children->GetLength(&newTitleIndex);
  }

  // Append a text node under the TITLE
  //  only if the title text isn't empty
  if (titleNode && !aTitle.IsEmpty())
  {
    nsCOMPtr<nsIDOMText> textNode;
    res = domDoc->CreateTextNode(aTitle, getter_AddRefs(textNode));
    if (NS_FAILED(res)) return res;
    nsCOMPtr<nsIDOMNode> newNode = do_QueryInterface(textNode);
    if (!newNode) return NS_ERROR_FAILURE;

    if (newTitleNode)
    {
      // Not undoable: We will insert newTitleNode below
      nsCOMPtr<nsIDOMNode> resultNode;
      res = titleNode->AppendChild(newNode, getter_AddRefs(resultNode));
    } 
    else 
    {
      // This is an undoable transaction
      res = editor->InsertNode(newNode, titleNode, 0);
    }
    if (NS_FAILED(res)) return res;
  }

  if (newTitleNode)
  {
    // Undoable transaction to insert title+text together
    res = editor->InsertNode(titleNode, headNode, newTitleIndex);
  }
  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from EditTxn.

Definition at line 79 of file SetDocTitleTxn.cpp.

{
  return SetDocTitle(mUndoValue);
}

Here is the call graph for this function:


Friends And Related Function Documentation

friend class TransactionFactory [friend]

Definition at line 104 of file SetDocTitleTxn.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 editor that created this transaction

Definition at line 93 of file SetDocTitleTxn.h.

Set true if we dont' really change the title during Do()

Definition at line 102 of file SetDocTitleTxn.h.

The previous title string to use for undo.

Definition at line 99 of file SetDocTitleTxn.h.

The new title string.

Definition at line 96 of file SetDocTitleTxn.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: