summaryrefslogtreecommitdiffstats
path: root/dom/serializers/nsDOMSerializer.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/serializers/nsDOMSerializer.cpp
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/serializers/nsDOMSerializer.cpp')
-rw-r--r--dom/serializers/nsDOMSerializer.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/dom/serializers/nsDOMSerializer.cpp b/dom/serializers/nsDOMSerializer.cpp
new file mode 100644
index 0000000000..98d92e64ce
--- /dev/null
+++ b/dom/serializers/nsDOMSerializer.cpp
@@ -0,0 +1,112 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#include "nsDOMSerializer.h"
+
+#include "mozilla/Encoding.h"
+#include "mozilla/dom/Document.h"
+#include "nsIDocumentEncoder.h"
+#include "nsComponentManagerUtils.h"
+#include "nsContentCID.h"
+#include "nsContentUtils.h"
+#include "nsError.h"
+#include "nsINode.h"
+
+using namespace mozilla;
+
+nsDOMSerializer::nsDOMSerializer() = default;
+
+static already_AddRefed<nsIDocumentEncoder> SetUpEncoder(
+ nsINode& aRoot, const nsAString& aCharset, ErrorResult& aRv) {
+ nsCOMPtr<nsIDocumentEncoder> encoder =
+ do_createDocumentEncoder("application/xhtml+xml");
+ if (!encoder) {
+ aRv.Throw(NS_ERROR_FAILURE);
+ return nullptr;
+ }
+
+ dom::Document* doc = aRoot.OwnerDoc();
+ bool entireDocument = (doc == &aRoot);
+
+ // This method will fail if no document
+ nsresult rv = encoder->NativeInit(
+ doc, u"application/xhtml+xml"_ns,
+ nsIDocumentEncoder::OutputRaw |
+ nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
+
+ if (NS_FAILED(rv)) {
+ aRv.Throw(rv);
+ return nullptr;
+ }
+
+ NS_ConvertUTF16toUTF8 charset(aCharset);
+ if (charset.IsEmpty()) {
+ doc->GetDocumentCharacterSet()->Name(charset);
+ }
+ rv = encoder->SetCharset(charset);
+ if (NS_FAILED(rv)) {
+ aRv.Throw(rv);
+ return nullptr;
+ }
+
+ // 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_FAILED(rv)) {
+ aRv.Throw(rv);
+ return nullptr;
+ }
+
+ return encoder.forget();
+}
+
+void nsDOMSerializer::SerializeToString(nsINode& aRoot, nsAString& aStr,
+ ErrorResult& aRv) {
+ aStr.Truncate();
+
+ if (!nsContentUtils::CanCallerAccess(&aRoot)) {
+ aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
+ return;
+ }
+
+ nsCOMPtr<nsIDocumentEncoder> encoder = SetUpEncoder(aRoot, u""_ns, aRv);
+ if (aRv.Failed()) {
+ return;
+ }
+
+ nsresult rv = encoder->EncodeToString(aStr);
+ if (NS_FAILED(rv)) {
+ aRv.Throw(rv);
+ }
+}
+
+void nsDOMSerializer::SerializeToStream(nsINode& aRoot,
+ nsIOutputStream* aStream,
+ const nsAString& aCharset,
+ ErrorResult& aRv) {
+ if (NS_WARN_IF(!aStream)) {
+ aRv.Throw(NS_ERROR_INVALID_ARG);
+ return;
+ }
+
+ // The charset arg can be empty, in which case we get the document's
+ // charset and use that when serializing.
+
+ // No point doing a CanCallerAccess check, because we can only be
+ // called by system JS or C++.
+ nsCOMPtr<nsIDocumentEncoder> encoder = SetUpEncoder(aRoot, aCharset, aRv);
+ if (aRv.Failed()) {
+ return;
+ }
+
+ nsresult rv = encoder->EncodeToStream(aStream);
+ if (NS_FAILED(rv)) {
+ aRv.Throw(rv);
+ }
+}