2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2006-04-20 07:37:16 +04:00
|
|
|
#include "nsIDocumentEncoder.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;
|
|
|
|
|
2020-02-21 13:41:47 +03:00
|
|
|
nsDOMSerializer::nsDOMSerializer() = default;
|
2006-04-20 07:36:50 +04:00
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
static already_AddRefed<nsIDocumentEncoder> SetUpEncoder(
|
|
|
|
nsINode& aRoot, const nsAString& aCharset, ErrorResult& aRv) {
|
2018-11-05 03:41:05 +03:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder =
|
|
|
|
do_createDocumentEncoder("application/xhtml+xml");
|
|
|
|
if (!encoder) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2018-04-09 23:30:32 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-30 07:10:52 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* doc = aRoot.OwnerDoc();
|
2018-04-09 23:30:32 +03:00
|
|
|
bool entireDocument = (doc == &aRoot);
|
2006-04-20 07:37:16 +04:00
|
|
|
|
|
|
|
// This method will fail if no document
|
2018-11-05 03:41:05 +03:00
|
|
|
nsresult rv = encoder->NativeInit(
|
2020-07-01 11:29:29 +03:00
|
|
|
doc, u"application/xhtml+xml"_ns,
|
2018-04-09 23:30:32 +03:00
|
|
|
nsIDocumentEncoder::OutputRaw |
|
|
|
|
nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
|
2009-09-28 11:59:52 +04:00
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2006-04-20 07:37:16 +04:00
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
NS_ConvertUTF16toUTF8 charset(aCharset);
|
2006-04-20 07:38:51 +04:00
|
|
|
if (charset.IsEmpty()) {
|
2017-06-18 14:37:50 +03:00
|
|
|
doc->GetDocumentCharacterSet()->Name(charset);
|
2006-04-20 07:37:16 +04:00
|
|
|
}
|
|
|
|
rv = encoder->SetCharset(charset);
|
2018-04-09 23:30:32 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2006-04-20 07:37:16 +04:00
|
|
|
|
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) {
|
2018-05-30 05:58:49 +03:00
|
|
|
rv = encoder->SetNode(&aRoot);
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
return encoder.forget();
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
void nsDOMSerializer::SerializeToString(nsINode& aRoot, nsAString& aStr,
|
2018-04-09 23:30:32 +03:00
|
|
|
ErrorResult& aRv) {
|
|
|
|
aStr.Truncate();
|
2006-04-20 07:38:02 +04:00
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
if (!nsContentUtils::CanCallerAccess(&aRoot)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2006-06-16 00:30:44 +04:00
|
|
|
}
|
2006-04-20 07:38:01 +04:00
|
|
|
|
2020-09-23 18:17:15 +03:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder = SetUpEncoder(aRoot, u""_ns, aRv);
|
2018-04-09 23:30:32 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2006-04-20 07:37:16 +04:00
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
nsresult rv = encoder->EncodeToString(aStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
void nsDOMSerializer::SerializeToStream(nsINode& aRoot,
|
|
|
|
nsIOutputStream* aStream,
|
2018-04-09 23:30:32 +03:00
|
|
|
const nsAString& aCharset,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (NS_WARN_IF(!aStream)) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-04 05:26:16 +04:00
|
|
|
|
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
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
// No point doing a CanCallerAccess check, because we can only be
|
|
|
|
// called by system JS or C++.
|
2018-04-09 23:30:32 +03:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder = SetUpEncoder(aRoot, aCharset, aRv);
|
|
|
|
if (aRv.Failed()) {
|
2018-04-09 23:30:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2006-04-20 07:36:50 +04:00
|
|
|
|
2018-04-09 23:30:32 +03:00
|
|
|
nsresult rv = encoder->EncodeToStream(aStream);
|
2018-04-09 23:30:32 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
2006-04-20 07:36:50 +04:00
|
|
|
}
|