Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/nsDOMSerializer.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsDOMSerializer.h"
8
9
#include "mozilla/Encoding.h"
10
#include "nsIDocument.h"
11
#include "nsIDocumentEncoder.h"
12
#include "nsComponentManagerUtils.h"
13
#include "nsContentCID.h"
14
#include "nsContentUtils.h"
15
#include "nsError.h"
16
#include "nsINode.h"
17
18
using namespace mozilla;
19
20
nsDOMSerializer::nsDOMSerializer()
21
0
{
22
0
}
23
24
static already_AddRefed<nsIDocumentEncoder>
25
SetUpEncoder(nsINode& aRoot, const nsAString& aCharset, ErrorResult& aRv)
26
0
{
27
0
  nsresult rv;
28
0
  nsCOMPtr<nsIDocumentEncoder> encoder =
29
0
    do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "application/xhtml+xml", &rv);
30
0
  if (NS_FAILED(rv)) {
31
0
    aRv.Throw(rv);
32
0
    return nullptr;
33
0
  }
34
0
35
0
  nsIDocument* doc = aRoot.OwnerDoc();
36
0
  bool entireDocument = (doc == &aRoot);
37
0
38
0
  // This method will fail if no document
39
0
  rv = encoder->
40
0
    NativeInit(doc, NS_LITERAL_STRING("application/xhtml+xml"),
41
0
               nsIDocumentEncoder::OutputRaw |
42
0
               nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
43
0
44
0
  if (NS_FAILED(rv)) {
45
0
    aRv.Throw(rv);
46
0
    return nullptr;
47
0
  }
48
0
49
0
  NS_ConvertUTF16toUTF8 charset(aCharset);
50
0
  if (charset.IsEmpty()) {
51
0
    doc->GetDocumentCharacterSet()->Name(charset);
52
0
  }
53
0
  rv = encoder->SetCharset(charset);
54
0
  if (NS_FAILED(rv)) {
55
0
    aRv.Throw(rv);
56
0
    return nullptr;
57
0
  }
58
0
59
0
  // If we are working on the entire document we do not need to
60
0
  // specify which part to serialize
61
0
  if (!entireDocument) {
62
0
    rv = encoder->SetNode(&aRoot);
63
0
  }
64
0
65
0
  if (NS_FAILED(rv)) {
66
0
    aRv.Throw(rv);
67
0
    return nullptr;
68
0
  }
69
0
70
0
  return encoder.forget();
71
0
}
72
73
void
74
nsDOMSerializer::SerializeToString(nsINode& aRoot, nsAString& aStr,
75
                                   ErrorResult& aRv)
76
0
{
77
0
  aStr.Truncate();
78
0
79
0
  if (!nsContentUtils::CanCallerAccess(&aRoot)) {
80
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
81
0
    return;
82
0
  }
83
0
84
0
  nsCOMPtr<nsIDocumentEncoder> encoder =
85
0
    SetUpEncoder(aRoot, EmptyString(), aRv);
86
0
  if (aRv.Failed()) {
87
0
    return;
88
0
  }
89
0
90
0
  nsresult rv = encoder->EncodeToString(aStr);
91
0
  if (NS_FAILED(rv)) {
92
0
    aRv.Throw(rv);
93
0
  }
94
0
}
95
96
void
97
nsDOMSerializer::SerializeToStream(nsINode& aRoot, nsIOutputStream* aStream,
98
                                   const nsAString& aCharset,
99
                                   ErrorResult& aRv)
100
0
{
101
0
  if (NS_WARN_IF(!aStream)) {
102
0
    aRv.Throw(NS_ERROR_INVALID_ARG);
103
0
    return;
104
0
  }
105
0
106
0
  // The charset arg can be empty, in which case we get the document's
107
0
  // charset and use that when serializing.
108
0
109
0
  // No point doing a CanCallerAccess check, because we can only be
110
0
  // called by system JS or C++.
111
0
  nsCOMPtr<nsIDocumentEncoder> encoder =
112
0
    SetUpEncoder(aRoot, aCharset, aRv);
113
0
  if (aRv.Failed()) {
114
0
    return;
115
0
  }
116
0
117
0
  nsresult rv = encoder->EncodeToStream(aStream);
118
0
  if (NS_FAILED(rv)) {
119
0
    aRv.Throw(rv);
120
0
  }
121
0
}