1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/nsDOMSerializer.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,148 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsDOMSerializer.h" 1.10 + 1.11 +#include "nsIDocument.h" 1.12 +#include "nsIDocumentEncoder.h" 1.13 +#include "nsIDOMDocument.h" 1.14 +#include "nsComponentManagerUtils.h" 1.15 +#include "nsContentCID.h" 1.16 +#include "nsContentUtils.h" 1.17 +#include "nsError.h" 1.18 +#include "nsINode.h" 1.19 + 1.20 +using namespace mozilla; 1.21 + 1.22 +nsDOMSerializer::nsDOMSerializer() 1.23 +{ 1.24 + SetIsDOMBinding(); 1.25 +} 1.26 + 1.27 +nsDOMSerializer::~nsDOMSerializer() 1.28 +{ 1.29 +} 1.30 + 1.31 +// QueryInterface implementation for nsDOMSerializer 1.32 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMSerializer) 1.33 + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 1.34 + NS_INTERFACE_MAP_ENTRY(nsISupports) 1.35 + NS_INTERFACE_MAP_ENTRY(nsIDOMSerializer) 1.36 +NS_INTERFACE_MAP_END 1.37 + 1.38 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsDOMSerializer, mOwner) 1.39 + 1.40 +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMSerializer) 1.41 +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMSerializer) 1.42 + 1.43 + 1.44 +static nsresult 1.45 +SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset, 1.46 + nsIDocumentEncoder **aEncoder) 1.47 +{ 1.48 + *aEncoder = nullptr; 1.49 + 1.50 + nsresult rv; 1.51 + nsCOMPtr<nsIDocumentEncoder> encoder = 1.52 + do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "application/xhtml+xml", &rv); 1.53 + if (NS_FAILED(rv)) 1.54 + return rv; 1.55 + 1.56 + bool entireDocument = true; 1.57 + nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(aRoot)); 1.58 + if (!domDoc) { 1.59 + entireDocument = false; 1.60 + rv = aRoot->GetOwnerDocument(getter_AddRefs(domDoc)); 1.61 + if (NS_FAILED(rv)) 1.62 + return rv; 1.63 + } 1.64 + 1.65 + // This method will fail if no document 1.66 + rv = encoder->Init(domDoc, NS_LITERAL_STRING("application/xhtml+xml"), 1.67 + nsIDocumentEncoder::OutputRaw | 1.68 + nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration); 1.69 + 1.70 + if (NS_FAILED(rv)) 1.71 + return rv; 1.72 + 1.73 + nsAutoCString charset(aCharset); 1.74 + if (charset.IsEmpty()) { 1.75 + nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc); 1.76 + NS_ASSERTION(doc, "Need a document"); 1.77 + charset = doc->GetDocumentCharacterSet(); 1.78 + } 1.79 + rv = encoder->SetCharset(charset); 1.80 + if (NS_FAILED(rv)) 1.81 + return rv; 1.82 + 1.83 + // If we are working on the entire document we do not need to 1.84 + // specify which part to serialize 1.85 + if (!entireDocument) { 1.86 + rv = encoder->SetNode(aRoot); 1.87 + } 1.88 + 1.89 + if (NS_SUCCEEDED(rv)) { 1.90 + *aEncoder = encoder.get(); 1.91 + NS_ADDREF(*aEncoder); 1.92 + } 1.93 + 1.94 + return rv; 1.95 +} 1.96 + 1.97 +void 1.98 +nsDOMSerializer::SerializeToString(nsINode& aRoot, nsAString& aStr, 1.99 + ErrorResult& rv) 1.100 +{ 1.101 + rv = nsDOMSerializer::SerializeToString(aRoot.AsDOMNode(), aStr); 1.102 +} 1.103 + 1.104 +NS_IMETHODIMP 1.105 +nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval) 1.106 +{ 1.107 + NS_ENSURE_ARG_POINTER(aRoot); 1.108 + 1.109 + _retval.Truncate(); 1.110 + 1.111 + if (!nsContentUtils::CanCallerAccess(aRoot)) { 1.112 + return NS_ERROR_DOM_SECURITY_ERR; 1.113 + } 1.114 + 1.115 + nsCOMPtr<nsIDocumentEncoder> encoder; 1.116 + nsresult rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder)); 1.117 + if (NS_FAILED(rv)) 1.118 + return rv; 1.119 + 1.120 + return encoder->EncodeToString(_retval); 1.121 +} 1.122 + 1.123 +void 1.124 +nsDOMSerializer::SerializeToStream(nsINode& aRoot, nsIOutputStream* aStream, 1.125 + const nsAString& aCharset, ErrorResult& rv) 1.126 +{ 1.127 + rv = nsDOMSerializer::SerializeToStream(aRoot.AsDOMNode(), aStream, 1.128 + NS_ConvertUTF16toUTF8(aCharset)); 1.129 +} 1.130 + 1.131 +NS_IMETHODIMP 1.132 +nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot, 1.133 + nsIOutputStream *aStream, 1.134 + const nsACString& aCharset) 1.135 +{ 1.136 + NS_ENSURE_ARG_POINTER(aRoot); 1.137 + NS_ENSURE_ARG_POINTER(aStream); 1.138 + // The charset arg can be empty, in which case we get the document's 1.139 + // charset and use that when serializing. 1.140 + 1.141 + if (!nsContentUtils::CanCallerAccess(aRoot)) { 1.142 + return NS_ERROR_DOM_SECURITY_ERR; 1.143 + } 1.144 + 1.145 + nsCOMPtr<nsIDocumentEncoder> encoder; 1.146 + nsresult rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder)); 1.147 + if (NS_FAILED(rv)) 1.148 + return rv; 1.149 + 1.150 + return encoder->EncodeToStream(aStream); 1.151 +}