1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
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
15
* The Original Code is mozilla.org code.
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.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* 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 NPL, 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 NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#include "nsDOMSerializer.h"
40
#include "nsIDOMNode.h"
41
#include "nsIDOMClassInfo.h"
42
#include "nsIOutputStream.h"
43
#include "nsIDocument.h"
44
#include "nsIDOMDocument.h"
45
#include "nsIDocumentEncoder.h"
46
#include "nsIComponentManager.h"
47
#include "nsIContentSerializer.h"
49
#include "nsReadableUtils.h"
51
#include "nsIJSContextStack.h"
52
#include "nsIScriptSecurityManager.h"
55
nsDOMSerializer::nsDOMSerializer()
59
nsDOMSerializer::~nsDOMSerializer()
64
// QueryInterface implementation for nsDOMSerializer
65
NS_INTERFACE_MAP_BEGIN(nsDOMSerializer)
66
NS_INTERFACE_MAP_ENTRY(nsISupports)
67
NS_INTERFACE_MAP_ENTRY(nsIDOMSerializer)
68
NS_INTERFACE_MAP_ENTRY_EXTERNAL_DOM_CLASSINFO(XMLSerializer)
72
NS_IMPL_ADDREF(nsDOMSerializer)
73
NS_IMPL_RELEASE(nsDOMSerializer)
77
SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
78
nsIDocumentEncoder **aEncoder)
83
nsCOMPtr<nsIDocumentEncoder> encoder =
84
do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "text/xml", &rv);
88
PRBool entireDocument = PR_TRUE;
89
nsCOMPtr<nsIDocument> document(do_QueryInterface(aRoot));
91
entireDocument = PR_FALSE;
92
nsCOMPtr<nsIDOMDocument> domDoc;
93
rv = aRoot->GetOwnerDocument(getter_AddRefs(domDoc));
96
document = do_QueryInterface(domDoc);
99
// This method will fail if no document
100
rv = encoder->Init(document, NS_LITERAL_STRING("text/xml"),
101
nsIDocumentEncoder::OutputEncodeBasicEntities);
105
nsCAutoString charset(aCharset);
106
if (charset.IsEmpty()) {
107
charset = document->GetDocumentCharacterSet();
109
rv = encoder->SetCharset(charset);
113
// If we are working on the entire document we do not need to
114
// specify which part to serialize
115
if (!entireDocument) {
116
rv = encoder->SetNode(aRoot);
119
if (NS_SUCCEEDED(rv)) {
120
*aEncoder = encoder.get();
121
NS_ADDREF(*aEncoder);
128
CheckSameOrigin(nsIDOMNode *aRoot)
130
// Get JSContext from stack.
131
nsCOMPtr<nsIJSContextStack> stack =
132
do_GetService("@mozilla.org/js/xpc/ContextStack;1");
134
JSContext *cx = nsnull;
138
rv = stack->Peek(&cx);
139
NS_ENSURE_SUCCESS(rv, rv);
143
// We're called from script, make sure the caller and the root are
144
// from the same origin...
146
nsCOMPtr<nsIDOMDocument> owner_doc(do_QueryInterface(aRoot));
149
aRoot->GetOwnerDocument(getter_AddRefs(owner_doc));
152
nsCOMPtr<nsIDocument> doc(do_QueryInterface(owner_doc));
155
nsCOMPtr<nsIURI> root_uri;
157
nsIPrincipal *principal = doc->GetPrincipal();
160
principal->GetURI(getter_AddRefs(root_uri));
164
nsCOMPtr<nsIScriptSecurityManager> secMan =
165
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
166
NS_ENSURE_SUCCESS(rv, rv);
168
rv = secMan->CheckSameOrigin(cx, root_uri);
171
// The node that's being serialized comes from a different
172
// origin than the calling script comes from...
184
nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval)
186
NS_ENSURE_ARG_POINTER(aRoot);
190
nsresult rv = CheckSameOrigin(aRoot);
194
nsCOMPtr<nsIDocumentEncoder> encoder;
195
rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder));
199
return encoder->EncodeToString(_retval);
203
nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot,
204
nsIOutputStream *aStream,
205
const nsACString& aCharset)
207
NS_ENSURE_ARG_POINTER(aRoot);
208
NS_ENSURE_ARG_POINTER(aStream);
209
// The charset arg can be null, in which case we get the document's
210
// charset and use that when serializing.
212
nsresult rv = CheckSameOrigin(aRoot);
216
nsCOMPtr<nsIDocumentEncoder> encoder;
217
rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder));
221
return encoder->EncodeToStream(aStream);