2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsDOMSerializer.h"
|
2013-03-17 00:55:15 -07:00
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocumentEncoder.h"
|
2013-08-21 23:30:55 -07:00
|
|
|
#include "nsIDOMDocument.h"
|
2013-03-17 00:55:15 -07:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2012-12-25 15:06:15 -08:00
|
|
|
#include "nsINode.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-03 17:26:16 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDOMSerializer::nsDOMSerializer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMSerializer::~nsDOMSerializer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryInterface implementation for nsDOMSerializer
|
2012-12-03 17:26:16 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMSerializer)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMSerializer)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-29 01:57:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsDOMSerializer, mOwner)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-03 17:26:16 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMSerializer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMSerializer)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
|
|
|
|
nsIDocumentEncoder **aEncoder)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aEncoder = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder =
|
2009-09-28 00:59:52 -07:00
|
|
|
do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "application/xhtml+xml", &rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool entireDocument = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(aRoot));
|
|
|
|
if (!domDoc) {
|
2011-10-17 07:59:28 -07:00
|
|
|
entireDocument = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = aRoot->GetOwnerDocument(getter_AddRefs(domDoc));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method will fail if no document
|
2009-09-28 00:59:52 -07:00
|
|
|
rv = encoder->Init(domDoc, NS_LITERAL_STRING("application/xhtml+xml"),
|
|
|
|
nsIDocumentEncoder::OutputRaw |
|
|
|
|
nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString charset(aCharset);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (charset.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
|
|
|
|
NS_ASSERTION(doc, "Need a document");
|
|
|
|
charset = doc->GetDocumentCharacterSet();
|
|
|
|
}
|
|
|
|
rv = encoder->SetCharset(charset);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// If we are working on the entire document we do not need to
|
|
|
|
// specify which part to serialize
|
|
|
|
if (!entireDocument) {
|
|
|
|
rv = encoder->SetNode(aRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2015-03-31 07:03:49 -07:00
|
|
|
encoder.forget(aEncoder);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-12-03 17:26:16 -08:00
|
|
|
void
|
|
|
|
nsDOMSerializer::SerializeToString(nsINode& aRoot, nsAString& aStr,
|
|
|
|
ErrorResult& rv)
|
|
|
|
{
|
|
|
|
rv = nsDOMSerializer::SerializeToString(aRoot.AsDOMNode(), aStr);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRoot);
|
|
|
|
|
|
|
|
_retval.Truncate();
|
|
|
|
|
|
|
|
if (!nsContentUtils::CanCallerAccess(aRoot)) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder;
|
|
|
|
nsresult rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return encoder->EncodeToString(_retval);
|
|
|
|
}
|
|
|
|
|
2012-12-03 17:26:16 -08:00
|
|
|
void
|
|
|
|
nsDOMSerializer::SerializeToStream(nsINode& aRoot, nsIOutputStream* aStream,
|
|
|
|
const nsAString& aCharset, ErrorResult& rv)
|
|
|
|
{
|
|
|
|
rv = nsDOMSerializer::SerializeToStream(aRoot.AsDOMNode(), aStream,
|
|
|
|
NS_ConvertUTF16toUTF8(aCharset));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot,
|
|
|
|
nsIOutputStream *aStream,
|
|
|
|
const nsACString& aCharset)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRoot);
|
|
|
|
NS_ENSURE_ARG_POINTER(aStream);
|
|
|
|
// The charset arg can be empty, in which case we get the document's
|
|
|
|
// charset and use that when serializing.
|
|
|
|
|
|
|
|
if (!nsContentUtils::CanCallerAccess(aRoot)) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder;
|
|
|
|
nsresult rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return encoder->EncodeToStream(aStream);
|
|
|
|
}
|