LCOV - code coverage report
Current view: directory - editor/libeditor/base - SetDocTitleTxn.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 85 0 0.0 %
Date: 2012-06-02 Functions: 8 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-1999
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Pierre Phaneuf <pp@ludusdesign.com>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      27                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #include "SetDocTitleTxn.h"
      40                 : #include "nsIDOMNode.h"
      41                 : #include "nsIDOMNodeList.h"
      42                 : #include "nsIDOMDocument.h"
      43                 : #include "nsIDOMHTMLDocument.h"
      44                 : #include "nsIDOMText.h"
      45                 : #include "nsIDOMElement.h"
      46                 : 
      47                 : // note that aEditor is not refcounted
      48               0 : SetDocTitleTxn::SetDocTitleTxn()
      49                 :   : EditTxn()
      50               0 : , mIsTransient(false)
      51                 : {
      52               0 : }
      53                 : 
      54               0 : NS_IMETHODIMP SetDocTitleTxn::Init(nsIHTMLEditor *aEditor,
      55                 :                                    const nsAString *aValue)
      56                 : 
      57                 : {
      58               0 :   NS_ASSERTION(aEditor && aValue, "null args");
      59               0 :   if (!aEditor || !aValue) { return NS_ERROR_NULL_POINTER; }
      60                 : 
      61               0 :   mEditor = aEditor;
      62               0 :   mValue = *aValue;
      63                 : 
      64               0 :   return NS_OK;
      65                 : }
      66                 : 
      67               0 : NS_IMETHODIMP SetDocTitleTxn::DoTransaction(void)
      68                 : {
      69               0 :   return SetDomTitle(mValue);
      70                 : }
      71                 : 
      72               0 : NS_IMETHODIMP SetDocTitleTxn::UndoTransaction(void)
      73                 : {
      74                 :   // No extra work required; the DOM changes alone are enough
      75               0 :   return NS_OK;
      76                 : }
      77                 : 
      78               0 : NS_IMETHODIMP SetDocTitleTxn::RedoTransaction(void)
      79                 : {
      80                 :   // No extra work required; the DOM changes alone are enough
      81               0 :   return NS_OK;
      82                 : }
      83                 : 
      84               0 : nsresult SetDocTitleTxn::SetDomTitle(const nsAString& aTitle)
      85                 : {
      86               0 :   nsCOMPtr<nsIEditor> editor = do_QueryInterface(mEditor);
      87               0 :   NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
      88               0 :   nsCOMPtr<nsIDOMDocument> domDoc;
      89               0 :   nsresult res = editor->GetDocument(getter_AddRefs(domDoc));
      90               0 :   NS_ENSURE_TRUE(domDoc, NS_ERROR_FAILURE);
      91                 : 
      92               0 :   nsCOMPtr<nsIDOMNodeList> titleList;
      93               0 :   res = domDoc->GetElementsByTagName(NS_LITERAL_STRING("title"), getter_AddRefs(titleList));
      94               0 :   NS_ENSURE_SUCCESS(res, res);
      95                 : 
      96                 :   // First assume we will NOT really do anything
      97                 :   // (transaction will not be pushed on stack)
      98               0 :   mIsTransient = true;
      99                 : 
     100               0 :   nsCOMPtr<nsIDOMNode>titleNode;
     101               0 :   if(titleList)
     102                 :   {
     103               0 :     res = titleList->Item(0, getter_AddRefs(titleNode));
     104               0 :     NS_ENSURE_SUCCESS(res, res);
     105               0 :     if (titleNode)
     106                 :     {
     107                 :       // Delete existing child textnode of title node
     108                 :       // (Note: all contents under a TITLE node are always in a single text node)
     109               0 :       nsCOMPtr<nsIDOMNode> child;
     110               0 :       res = titleNode->GetFirstChild(getter_AddRefs(child));
     111               0 :       if(NS_FAILED(res)) return res;
     112               0 :       if(child)
     113                 :       {
     114                 :         // Save current text as the undo value
     115               0 :         nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(child);
     116               0 :         if(textNode)
     117                 :         {
     118               0 :           textNode->GetData(mUndoValue);
     119                 : 
     120                 :           // If title text is identical to what already exists,
     121                 :           // quit now (mIsTransient is now TRUE)
     122               0 :           if (mUndoValue == aTitle)
     123               0 :             return NS_OK;
     124                 :         }
     125               0 :         res = editor->DeleteNode(child);
     126               0 :         if(NS_FAILED(res)) return res;
     127                 :       }
     128                 :     }
     129                 :   }
     130                 : 
     131                 :   // We didn't return above, thus we really will be changing the title
     132               0 :   mIsTransient = false;
     133                 : 
     134                 :   // Get the <HEAD> node, create a <TITLE> and insert it under the HEAD
     135               0 :   nsCOMPtr<nsIDOMNodeList> headList;
     136               0 :   res = domDoc->GetElementsByTagName(NS_LITERAL_STRING("head"),getter_AddRefs(headList));
     137               0 :   NS_ENSURE_SUCCESS(res, res);
     138               0 :   NS_ENSURE_TRUE(headList, NS_ERROR_FAILURE);
     139                 :   
     140               0 :   nsCOMPtr<nsIDOMNode>headNode;
     141               0 :   headList->Item(0, getter_AddRefs(headNode));
     142               0 :   NS_ENSURE_TRUE(headNode, NS_ERROR_FAILURE);
     143                 : 
     144               0 :   bool     newTitleNode = false;
     145               0 :   PRUint32 newTitleIndex = 0;
     146                 : 
     147               0 :   if (!titleNode)
     148                 :   {
     149                 :     // Didn't find one above: Create a new one
     150               0 :     nsCOMPtr<nsIDOMElement>titleElement;
     151               0 :     res = domDoc->CreateElement(NS_LITERAL_STRING("title"), getter_AddRefs(titleElement));
     152               0 :     NS_ENSURE_SUCCESS(res, res);
     153               0 :     NS_ENSURE_TRUE(titleElement, NS_ERROR_FAILURE);
     154                 : 
     155               0 :     titleNode = do_QueryInterface(titleElement);
     156               0 :     newTitleNode = true;
     157                 : 
     158                 :     // Get index so we append new title node 
     159                 :     // after all existing HEAD children
     160               0 :     nsCOMPtr<nsIDOMNodeList> children;
     161               0 :     res = headNode->GetChildNodes(getter_AddRefs(children));
     162               0 :     NS_ENSURE_SUCCESS(res, res);
     163               0 :     if (children)
     164               0 :       children->GetLength(&newTitleIndex);
     165                 :   }
     166                 : 
     167                 :   // Append a text node under the TITLE
     168                 :   //  only if the title text isn't empty
     169               0 :   if (titleNode && !aTitle.IsEmpty())
     170                 :   {
     171               0 :     nsCOMPtr<nsIDOMText> textNode;
     172               0 :     res = domDoc->CreateTextNode(aTitle, getter_AddRefs(textNode));
     173               0 :     NS_ENSURE_SUCCESS(res, res);
     174               0 :     nsCOMPtr<nsIDOMNode> newNode = do_QueryInterface(textNode);
     175               0 :     NS_ENSURE_TRUE(newNode, NS_ERROR_FAILURE);
     176                 : 
     177               0 :     if (newTitleNode)
     178                 :     {
     179                 :       // Not undoable: We will insert newTitleNode below
     180               0 :       nsCOMPtr<nsIDOMNode> resultNode;
     181               0 :       res = titleNode->AppendChild(newNode, getter_AddRefs(resultNode));
     182                 :     } 
     183                 :     else 
     184                 :     {
     185                 :       // This is an undoable transaction
     186               0 :       res = editor->InsertNode(newNode, titleNode, 0);
     187                 :     }
     188               0 :     NS_ENSURE_SUCCESS(res, res);
     189                 :   }
     190                 : 
     191               0 :   if (newTitleNode)
     192                 :   {
     193                 :     // Undoable transaction to insert title+text together
     194               0 :     res = editor->InsertNode(titleNode, headNode, newTitleIndex);
     195                 :   }
     196               0 :   return res;
     197                 : }
     198                 : 
     199               0 : NS_IMETHODIMP SetDocTitleTxn::GetTxnDescription(nsAString& aString)
     200                 : {
     201               0 :   aString.AssignLiteral("SetDocTitleTxn: ");
     202               0 :   aString += mValue;
     203               0 :   return NS_OK;
     204                 : }
     205                 : 
     206               0 : NS_IMETHODIMP SetDocTitleTxn::GetIsTransient(bool *aIsTransient)
     207                 : {
     208               0 :   NS_ENSURE_TRUE(aIsTransient, NS_ERROR_NULL_POINTER);  
     209               0 :   *aIsTransient = mIsTransient;
     210               0 :   return NS_OK;
     211                 : }
     212                 : 

Generated by: LCOV version 1.7