~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/xmlextras/base/src/nsDOMSerializer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
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
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
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"
 
48
#include "nsString.h"
 
49
#include "nsReadableUtils.h"
 
50
 
 
51
#include "nsIJSContextStack.h"
 
52
#include "nsIScriptSecurityManager.h"
 
53
#include "nsIURI.h"
 
54
 
 
55
nsDOMSerializer::nsDOMSerializer()
 
56
{
 
57
}
 
58
 
 
59
nsDOMSerializer::~nsDOMSerializer()
 
60
{
 
61
}
 
62
 
 
63
 
 
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)
 
69
NS_INTERFACE_MAP_END
 
70
 
 
71
 
 
72
NS_IMPL_ADDREF(nsDOMSerializer)
 
73
NS_IMPL_RELEASE(nsDOMSerializer)
 
74
 
 
75
 
 
76
static nsresult
 
77
SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
 
78
             nsIDocumentEncoder **aEncoder)
 
79
{
 
80
  *aEncoder = nsnull;
 
81
   
 
82
  nsresult rv;
 
83
  nsCOMPtr<nsIDocumentEncoder> encoder =
 
84
    do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "text/xml", &rv);
 
85
  if (NS_FAILED(rv))
 
86
    return rv;
 
87
 
 
88
  PRBool entireDocument = PR_TRUE;
 
89
  nsCOMPtr<nsIDocument> document(do_QueryInterface(aRoot));
 
90
  if (!document) {
 
91
    entireDocument = PR_FALSE;
 
92
    nsCOMPtr<nsIDOMDocument> domDoc;
 
93
    rv = aRoot->GetOwnerDocument(getter_AddRefs(domDoc));
 
94
    if (NS_FAILED(rv))
 
95
      return rv;
 
96
    document = do_QueryInterface(domDoc);
 
97
  }
 
98
 
 
99
  // This method will fail if no document
 
100
  rv = encoder->Init(document, NS_LITERAL_STRING("text/xml"),
 
101
                     nsIDocumentEncoder::OutputEncodeBasicEntities);
 
102
  if (NS_FAILED(rv))
 
103
    return rv;
 
104
 
 
105
  nsCAutoString charset(aCharset);
 
106
  if (charset.IsEmpty()) {
 
107
    charset = document->GetDocumentCharacterSet();
 
108
  }
 
109
  rv = encoder->SetCharset(charset);
 
110
  if (NS_FAILED(rv))
 
111
    return rv;
 
112
 
 
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);
 
117
  }
 
118
 
 
119
  if (NS_SUCCEEDED(rv)) {
 
120
    *aEncoder = encoder.get();
 
121
    NS_ADDREF(*aEncoder);
 
122
  }
 
123
 
 
124
  return rv;
 
125
}
 
126
 
 
127
static nsresult
 
128
CheckSameOrigin(nsIDOMNode *aRoot)
 
129
{
 
130
  // Get JSContext from stack.
 
131
  nsCOMPtr<nsIJSContextStack> stack =
 
132
    do_GetService("@mozilla.org/js/xpc/ContextStack;1");
 
133
 
 
134
  JSContext *cx = nsnull;
 
135
  nsresult rv = NS_OK;
 
136
 
 
137
  if (stack) {
 
138
    rv = stack->Peek(&cx);
 
139
    NS_ENSURE_SUCCESS(rv, rv);
 
140
  }
 
141
 
 
142
  if (cx) {
 
143
    // We're called from script, make sure the caller and the root are
 
144
    // from the same origin...
 
145
 
 
146
    nsCOMPtr<nsIDOMDocument> owner_doc(do_QueryInterface(aRoot));
 
147
 
 
148
    if (!owner_doc) {
 
149
      aRoot->GetOwnerDocument(getter_AddRefs(owner_doc));
 
150
    }
 
151
 
 
152
    nsCOMPtr<nsIDocument> doc(do_QueryInterface(owner_doc));
 
153
 
 
154
    if (doc) {
 
155
      nsCOMPtr<nsIURI> root_uri;
 
156
 
 
157
      nsIPrincipal *principal = doc->GetPrincipal();
 
158
 
 
159
      if (principal) {
 
160
        principal->GetURI(getter_AddRefs(root_uri));
 
161
      }
 
162
 
 
163
      if (root_uri) {
 
164
        nsCOMPtr<nsIScriptSecurityManager> secMan = 
 
165
          do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
 
166
        NS_ENSURE_SUCCESS(rv, rv);
 
167
 
 
168
        rv = secMan->CheckSameOrigin(cx, root_uri);
 
169
 
 
170
        if (NS_FAILED(rv)) {
 
171
          // The node that's being serialized comes from a different
 
172
          // origin than the calling script comes from...
 
173
 
 
174
          return rv;
 
175
        }
 
176
      }
 
177
    }      
 
178
  }
 
179
 
 
180
  return NS_OK;
 
181
}
 
182
 
 
183
NS_IMETHODIMP
 
184
nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval)
 
185
{
 
186
  NS_ENSURE_ARG_POINTER(aRoot);
 
187
  
 
188
  _retval.Truncate();
 
189
 
 
190
  nsresult rv = CheckSameOrigin(aRoot);
 
191
  if (NS_FAILED(rv))
 
192
    return rv;
 
193
 
 
194
  nsCOMPtr<nsIDocumentEncoder> encoder;
 
195
  rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder));
 
196
  if (NS_FAILED(rv))
 
197
    return rv;
 
198
 
 
199
  return encoder->EncodeToString(_retval);
 
200
}
 
201
 
 
202
NS_IMETHODIMP
 
203
nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot, 
 
204
                                   nsIOutputStream *aStream, 
 
205
                                   const nsACString& aCharset)
 
206
{
 
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.
 
211
 
 
212
  nsresult rv = CheckSameOrigin(aRoot);
 
213
  if (NS_FAILED(rv))
 
214
    return rv;
 
215
 
 
216
  nsCOMPtr<nsIDocumentEncoder> encoder;
 
217
  rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder));
 
218
  if (NS_FAILED(rv))
 
219
    return rv;
 
220
 
 
221
  return encoder->EncodeToStream(aStream);
 
222
}