LCOV - code coverage report
Current view: directory - content/base/src - nsDOMSerializer.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 61 53 86.9 %
Date: 2012-06-02 Functions: 9 9 100.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
      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 the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * 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 "nsDOMSerializer.h"
      39                 : #include "nsIDOMNode.h"
      40                 : #include "nsDOMClassInfoID.h"
      41                 : #include "nsIOutputStream.h"
      42                 : #include "nsINode.h"
      43                 : #include "nsIDocument.h"
      44                 : #include "nsIDOMDocument.h"
      45                 : #include "nsIDocumentEncoder.h"
      46                 : #include "nsIContentSerializer.h"
      47                 : #include "nsString.h"
      48                 : #include "nsReadableUtils.h"
      49                 : #include "nsContentCID.h"
      50                 : #include "nsContentUtils.h"
      51                 : #include "nsDOMError.h"
      52                 : 
      53             216 : nsDOMSerializer::nsDOMSerializer()
      54                 : {
      55             216 : }
      56                 : 
      57             432 : nsDOMSerializer::~nsDOMSerializer()
      58                 : {
      59             864 : }
      60                 : 
      61                 : DOMCI_DATA(XMLSerializer, nsDOMSerializer)
      62                 : 
      63                 : // QueryInterface implementation for nsDOMSerializer
      64            1513 : NS_INTERFACE_MAP_BEGIN(nsDOMSerializer)
      65            1513 :   NS_INTERFACE_MAP_ENTRY(nsISupports)
      66            1297 :   NS_INTERFACE_MAP_ENTRY(nsIDOMSerializer)
      67             865 :   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(XMLSerializer)
      68             649 : NS_INTERFACE_MAP_END
      69                 : 
      70                 : 
      71             864 : NS_IMPL_ADDREF(nsDOMSerializer)
      72             864 : NS_IMPL_RELEASE(nsDOMSerializer)
      73                 : 
      74                 : 
      75                 : static nsresult
      76             215 : SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
      77                 :              nsIDocumentEncoder **aEncoder)
      78                 : {
      79             215 :   *aEncoder = nsnull;
      80                 :    
      81                 :   nsresult rv;
      82                 :   nsCOMPtr<nsIDocumentEncoder> encoder =
      83             430 :     do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "application/xhtml+xml", &rv);
      84             215 :   if (NS_FAILED(rv))
      85               0 :     return rv;
      86                 : 
      87             215 :   bool entireDocument = true;
      88             430 :   nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(aRoot));
      89             215 :   if (!domDoc) {
      90             152 :     entireDocument = false;
      91             152 :     rv = aRoot->GetOwnerDocument(getter_AddRefs(domDoc));
      92             152 :     if (NS_FAILED(rv))
      93               0 :       return rv;
      94                 :   }
      95                 : 
      96                 :   // This method will fail if no document
      97             430 :   rv = encoder->Init(domDoc, NS_LITERAL_STRING("application/xhtml+xml"),
      98                 :                      nsIDocumentEncoder::OutputRaw |
      99             215 :                      nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
     100                 : 
     101             215 :   if (NS_FAILED(rv))
     102               0 :     return rv;
     103                 : 
     104             430 :   nsCAutoString charset(aCharset);
     105             215 :   if (charset.IsEmpty()) {
     106             396 :     nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
     107             198 :     NS_ASSERTION(doc, "Need a document");
     108             198 :     charset = doc->GetDocumentCharacterSet();
     109                 :   }
     110             215 :   rv = encoder->SetCharset(charset);
     111             215 :   if (NS_FAILED(rv))
     112               0 :     return rv;
     113                 : 
     114                 :   // If we are working on the entire document we do not need to
     115                 :   // specify which part to serialize
     116             215 :   if (!entireDocument) {
     117             152 :     rv = encoder->SetNode(aRoot);
     118                 :   }
     119                 : 
     120             215 :   if (NS_SUCCEEDED(rv)) {
     121             215 :     *aEncoder = encoder.get();
     122             215 :     NS_ADDREF(*aEncoder);
     123                 :   }
     124                 : 
     125             215 :   return rv;
     126                 : }
     127                 : 
     128                 : NS_IMETHODIMP
     129              46 : nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval)
     130                 : {
     131              46 :   NS_ENSURE_ARG_POINTER(aRoot);
     132                 :   
     133              46 :   _retval.Truncate();
     134                 : 
     135              46 :   if (!nsContentUtils::CanCallerAccess(aRoot)) {
     136               0 :     return NS_ERROR_DOM_SECURITY_ERR;
     137                 :   }
     138                 : 
     139              92 :   nsCOMPtr<nsIDocumentEncoder> encoder;
     140              46 :   nsresult rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder));
     141              46 :   if (NS_FAILED(rv))
     142               0 :     return rv;
     143                 : 
     144              46 :   return encoder->EncodeToString(_retval);
     145                 : }
     146                 : 
     147                 : NS_IMETHODIMP
     148             169 : nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot, 
     149                 :                                    nsIOutputStream *aStream, 
     150                 :                                    const nsACString& aCharset)
     151                 : {
     152             169 :   NS_ENSURE_ARG_POINTER(aRoot);
     153             169 :   NS_ENSURE_ARG_POINTER(aStream);
     154                 :   // The charset arg can be empty, in which case we get the document's
     155                 :   // charset and use that when serializing.
     156                 : 
     157             169 :   if (!nsContentUtils::CanCallerAccess(aRoot)) {
     158               0 :     return NS_ERROR_DOM_SECURITY_ERR;
     159                 :   }
     160                 : 
     161             338 :   nsCOMPtr<nsIDocumentEncoder> encoder;
     162             169 :   nsresult rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder));
     163             169 :   if (NS_FAILED(rv))
     164               0 :     return rv;
     165                 : 
     166             169 :   return encoder->EncodeToStream(aStream);
     167                 : }

Generated by: LCOV version 1.7