2006-04-20 07:37:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-04-20 07:36:50 +04:00
|
|
|
|
|
|
|
#include "nsDOMSerializer.h"
|
2013-03-17 11:55:15 +04:00
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
2006-04-20 07:37:16 +04:00
|
|
|
#include "nsIDocumentEncoder.h"
|
2013-08-22 10:30:55 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
2013-03-17 11:55:15 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2006-04-20 07:39:12 +04:00
|
|
|
#include "nsContentCID.h"
|
2006-04-26 13:19:48 +04:00
|
|
|
#include "nsContentUtils.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2012-12-26 03:06:15 +04:00
|
|
|
#include "nsINode.h"
|
2006-04-20 07:38:01 +04:00
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2006-04-20 07:36:50 +04:00
|
|
|
nsDOMSerializer::nsDOMSerializer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMSerializer::~nsDOMSerializer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2006-04-20 07:37:17 +04:00
|
|
|
// QueryInterface implementation for nsDOMSerializer
|
2012-12-04 05:26:16 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMSerializer)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2006-04-20 07:37:17 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMSerializer)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-29 12:57:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsDOMSerializer, mOwner)
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2006-04-20 07:37:17 +04:00
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMSerializer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMSerializer)
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2006-04-20 07:37:17 +04:00
|
|
|
|
2006-04-20 07:36:50 +04:00
|
|
|
|
2006-04-20 07:38:51 +04:00
|
|
|
static nsresult
|
|
|
|
SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
|
|
|
|
nsIDocumentEncoder **aEncoder)
|
2006-04-20 07:36:50 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aEncoder = nullptr;
|
2006-04-20 07:37:44 +04:00
|
|
|
|
2006-04-20 07:37:16 +04:00
|
|
|
nsresult rv;
|
2006-04-20 07:38:51 +04:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder =
|
2009-09-28 11:59:52 +04:00
|
|
|
do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "application/xhtml+xml", &rv);
|
2006-04-20 07:37:16 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool entireDocument = true;
|
2006-05-01 09:25:52 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(aRoot));
|
|
|
|
if (!domDoc) {
|
2011-10-17 18:59:28 +04:00
|
|
|
entireDocument = false;
|
2006-04-20 07:37:16 +04:00
|
|
|
rv = aRoot->GetOwnerDocument(getter_AddRefs(domDoc));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method will fail if no document
|
2009-09-28 11:59:52 +04:00
|
|
|
rv = encoder->Init(domDoc, NS_LITERAL_STRING("application/xhtml+xml"),
|
|
|
|
nsIDocumentEncoder::OutputRaw |
|
|
|
|
nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
|
|
|
|
|
2006-04-20 07:37:16 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString charset(aCharset);
|
2006-04-20 07:38:51 +04:00
|
|
|
if (charset.IsEmpty()) {
|
2006-05-01 09:25:52 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
|
|
|
|
NS_ASSERTION(doc, "Need a document");
|
|
|
|
charset = doc->GetDocumentCharacterSet();
|
2006-04-20 07:37:16 +04:00
|
|
|
}
|
|
|
|
rv = encoder->SetCharset(charset);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2006-04-20 07:38:51 +04:00
|
|
|
// If we are working on the entire document we do not need to
|
|
|
|
// specify which part to serialize
|
2006-04-20 07:37:16 +04:00
|
|
|
if (!entireDocument) {
|
2006-04-20 07:38:19 +04:00
|
|
|
rv = encoder->SetNode(aRoot);
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|
|
|
|
|
2006-04-20 07:37:16 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2015-03-31 17:03:49 +03:00
|
|
|
encoder.forget(aEncoder);
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|
|
|
|
|
2006-04-20 07:37:55 +04:00
|
|
|
return rv;
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
void
|
|
|
|
nsDOMSerializer::SerializeToString(nsINode& aRoot, nsAString& aStr,
|
|
|
|
ErrorResult& rv)
|
|
|
|
{
|
|
|
|
rv = nsDOMSerializer::SerializeToString(aRoot.AsDOMNode(), aStr);
|
|
|
|
}
|
|
|
|
|
2006-04-20 07:38:02 +04:00
|
|
|
NS_IMETHODIMP
|
2006-04-20 07:38:51 +04:00
|
|
|
nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval)
|
2006-04-20 07:38:02 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRoot);
|
|
|
|
|
2006-04-20 07:38:51 +04:00
|
|
|
_retval.Truncate();
|
2006-04-20 07:38:02 +04:00
|
|
|
|
2006-06-16 00:30:44 +04:00
|
|
|
if (!nsContentUtils::CanCallerAccess(aRoot)) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
2006-04-20 07:38:01 +04:00
|
|
|
|
2006-04-20 07:37:16 +04:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder;
|
2006-06-16 00:30:44 +04:00
|
|
|
nsresult rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder));
|
2006-04-20 07:37:16 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2006-04-20 07:38:51 +04:00
|
|
|
return encoder->EncodeToString(_retval);
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
void
|
|
|
|
nsDOMSerializer::SerializeToStream(nsINode& aRoot, nsIOutputStream* aStream,
|
|
|
|
const nsAString& aCharset, ErrorResult& rv)
|
|
|
|
{
|
|
|
|
rv = nsDOMSerializer::SerializeToStream(aRoot.AsDOMNode(), aStream,
|
|
|
|
NS_ConvertUTF16toUTF8(aCharset));
|
|
|
|
}
|
|
|
|
|
2006-04-20 07:36:50 +04:00
|
|
|
NS_IMETHODIMP
|
2006-04-20 07:38:02 +04:00
|
|
|
nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot,
|
|
|
|
nsIOutputStream *aStream,
|
2006-04-20 07:38:51 +04:00
|
|
|
const nsACString& aCharset)
|
2006-04-20 07:36:50 +04:00
|
|
|
{
|
2006-04-20 07:38:02 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aRoot);
|
|
|
|
NS_ENSURE_ARG_POINTER(aStream);
|
2006-11-28 07:01:46 +03:00
|
|
|
// The charset arg can be empty, in which case we get the document's
|
2006-04-20 07:37:44 +04:00
|
|
|
// charset and use that when serializing.
|
2006-04-20 07:38:02 +04:00
|
|
|
|
2006-06-16 00:30:44 +04:00
|
|
|
if (!nsContentUtils::CanCallerAccess(aRoot)) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
2006-04-20 07:38:02 +04:00
|
|
|
|
2006-04-20 07:37:16 +04:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder;
|
2006-06-16 00:30:44 +04:00
|
|
|
nsresult rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder));
|
2006-04-20 07:37:16 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2006-04-20 07:36:50 +04:00
|
|
|
|
2006-04-20 07:38:02 +04:00
|
|
|
return encoder->EncodeToStream(aStream);
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|