content/base/src/nsDOMSerializer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsDOMSerializer.h"
michael@0 7
michael@0 8 #include "nsIDocument.h"
michael@0 9 #include "nsIDocumentEncoder.h"
michael@0 10 #include "nsIDOMDocument.h"
michael@0 11 #include "nsComponentManagerUtils.h"
michael@0 12 #include "nsContentCID.h"
michael@0 13 #include "nsContentUtils.h"
michael@0 14 #include "nsError.h"
michael@0 15 #include "nsINode.h"
michael@0 16
michael@0 17 using namespace mozilla;
michael@0 18
michael@0 19 nsDOMSerializer::nsDOMSerializer()
michael@0 20 {
michael@0 21 SetIsDOMBinding();
michael@0 22 }
michael@0 23
michael@0 24 nsDOMSerializer::~nsDOMSerializer()
michael@0 25 {
michael@0 26 }
michael@0 27
michael@0 28 // QueryInterface implementation for nsDOMSerializer
michael@0 29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMSerializer)
michael@0 30 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
michael@0 31 NS_INTERFACE_MAP_ENTRY(nsISupports)
michael@0 32 NS_INTERFACE_MAP_ENTRY(nsIDOMSerializer)
michael@0 33 NS_INTERFACE_MAP_END
michael@0 34
michael@0 35 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsDOMSerializer, mOwner)
michael@0 36
michael@0 37 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMSerializer)
michael@0 38 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMSerializer)
michael@0 39
michael@0 40
michael@0 41 static nsresult
michael@0 42 SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
michael@0 43 nsIDocumentEncoder **aEncoder)
michael@0 44 {
michael@0 45 *aEncoder = nullptr;
michael@0 46
michael@0 47 nsresult rv;
michael@0 48 nsCOMPtr<nsIDocumentEncoder> encoder =
michael@0 49 do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "application/xhtml+xml", &rv);
michael@0 50 if (NS_FAILED(rv))
michael@0 51 return rv;
michael@0 52
michael@0 53 bool entireDocument = true;
michael@0 54 nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(aRoot));
michael@0 55 if (!domDoc) {
michael@0 56 entireDocument = false;
michael@0 57 rv = aRoot->GetOwnerDocument(getter_AddRefs(domDoc));
michael@0 58 if (NS_FAILED(rv))
michael@0 59 return rv;
michael@0 60 }
michael@0 61
michael@0 62 // This method will fail if no document
michael@0 63 rv = encoder->Init(domDoc, NS_LITERAL_STRING("application/xhtml+xml"),
michael@0 64 nsIDocumentEncoder::OutputRaw |
michael@0 65 nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
michael@0 66
michael@0 67 if (NS_FAILED(rv))
michael@0 68 return rv;
michael@0 69
michael@0 70 nsAutoCString charset(aCharset);
michael@0 71 if (charset.IsEmpty()) {
michael@0 72 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
michael@0 73 NS_ASSERTION(doc, "Need a document");
michael@0 74 charset = doc->GetDocumentCharacterSet();
michael@0 75 }
michael@0 76 rv = encoder->SetCharset(charset);
michael@0 77 if (NS_FAILED(rv))
michael@0 78 return rv;
michael@0 79
michael@0 80 // If we are working on the entire document we do not need to
michael@0 81 // specify which part to serialize
michael@0 82 if (!entireDocument) {
michael@0 83 rv = encoder->SetNode(aRoot);
michael@0 84 }
michael@0 85
michael@0 86 if (NS_SUCCEEDED(rv)) {
michael@0 87 *aEncoder = encoder.get();
michael@0 88 NS_ADDREF(*aEncoder);
michael@0 89 }
michael@0 90
michael@0 91 return rv;
michael@0 92 }
michael@0 93
michael@0 94 void
michael@0 95 nsDOMSerializer::SerializeToString(nsINode& aRoot, nsAString& aStr,
michael@0 96 ErrorResult& rv)
michael@0 97 {
michael@0 98 rv = nsDOMSerializer::SerializeToString(aRoot.AsDOMNode(), aStr);
michael@0 99 }
michael@0 100
michael@0 101 NS_IMETHODIMP
michael@0 102 nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval)
michael@0 103 {
michael@0 104 NS_ENSURE_ARG_POINTER(aRoot);
michael@0 105
michael@0 106 _retval.Truncate();
michael@0 107
michael@0 108 if (!nsContentUtils::CanCallerAccess(aRoot)) {
michael@0 109 return NS_ERROR_DOM_SECURITY_ERR;
michael@0 110 }
michael@0 111
michael@0 112 nsCOMPtr<nsIDocumentEncoder> encoder;
michael@0 113 nsresult rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder));
michael@0 114 if (NS_FAILED(rv))
michael@0 115 return rv;
michael@0 116
michael@0 117 return encoder->EncodeToString(_retval);
michael@0 118 }
michael@0 119
michael@0 120 void
michael@0 121 nsDOMSerializer::SerializeToStream(nsINode& aRoot, nsIOutputStream* aStream,
michael@0 122 const nsAString& aCharset, ErrorResult& rv)
michael@0 123 {
michael@0 124 rv = nsDOMSerializer::SerializeToStream(aRoot.AsDOMNode(), aStream,
michael@0 125 NS_ConvertUTF16toUTF8(aCharset));
michael@0 126 }
michael@0 127
michael@0 128 NS_IMETHODIMP
michael@0 129 nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot,
michael@0 130 nsIOutputStream *aStream,
michael@0 131 const nsACString& aCharset)
michael@0 132 {
michael@0 133 NS_ENSURE_ARG_POINTER(aRoot);
michael@0 134 NS_ENSURE_ARG_POINTER(aStream);
michael@0 135 // The charset arg can be empty, in which case we get the document's
michael@0 136 // charset and use that when serializing.
michael@0 137
michael@0 138 if (!nsContentUtils::CanCallerAccess(aRoot)) {
michael@0 139 return NS_ERROR_DOM_SECURITY_ERR;
michael@0 140 }
michael@0 141
michael@0 142 nsCOMPtr<nsIDocumentEncoder> encoder;
michael@0 143 nsresult rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder));
michael@0 144 if (NS_FAILED(rv))
michael@0 145 return rv;
michael@0 146
michael@0 147 return encoder->EncodeToStream(aStream);
michael@0 148 }

mercurial