Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / base / DOMParser.cpp
blobb3cf6ba04bd12640723a9b74e3d5e56c408d2868
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/. */
7 #include "mozilla/dom/DOMParser.h"
9 #include "nsNetUtil.h"
10 #include "nsDOMString.h"
11 #include "MainThreadUtils.h"
12 #include "SystemPrincipal.h"
13 #include "nsIScriptGlobalObject.h"
14 #include "nsIStreamListener.h"
15 #include "nsStringStream.h"
16 #include "nsCRT.h"
17 #include "nsStreamUtils.h"
18 #include "nsContentUtils.h"
19 #include "nsDOMJSUtils.h"
20 #include "nsError.h"
21 #include "nsPIDOMWindow.h"
22 #include "mozilla/BasePrincipal.h"
23 #include "mozilla/LoadInfo.h"
24 #include "mozilla/NullPrincipal.h"
25 #include "mozilla/dom/BindingUtils.h"
26 #include "mozilla/dom/Document.h"
27 #include "mozilla/dom/ScriptSettings.h"
29 using namespace mozilla;
30 using namespace mozilla::dom;
32 DOMParser::DOMParser(nsIGlobalObject* aOwner, nsIPrincipal* aDocPrincipal,
33 nsIURI* aDocumentURI, nsIURI* aBaseURI)
34 : mOwner(aOwner),
35 mPrincipal(aDocPrincipal),
36 mDocumentURI(aDocumentURI),
37 mBaseURI(aBaseURI),
38 mForceEnableXULXBL(false),
39 mForceEnableDTD(false) {
40 MOZ_ASSERT(aDocPrincipal);
41 MOZ_ASSERT(aDocumentURI);
44 DOMParser::~DOMParser() = default;
46 // QueryInterface implementation for DOMParser
47 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMParser)
48 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
49 NS_INTERFACE_MAP_ENTRY(nsISupports)
50 NS_INTERFACE_MAP_END
52 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMParser, mOwner)
54 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMParser)
55 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMParser)
57 already_AddRefed<Document> DOMParser::ParseFromString(const nsAString& aStr,
58 SupportedType aType,
59 ErrorResult& aRv) {
60 if (aType == SupportedType::Text_html) {
61 nsCOMPtr<Document> document = SetUpDocument(DocumentFlavorHTML, aRv);
62 if (NS_WARN_IF(aRv.Failed())) {
63 return nullptr;
66 // Keep the XULXBL state in sync with the XML case.
67 if (mForceEnableXULXBL) {
68 document->ForceEnableXULXBL();
71 if (mForceEnableDTD) {
72 document->ForceSkipDTDSecurityChecks();
75 nsresult rv = nsContentUtils::ParseDocumentHTML(aStr, document, false);
76 if (NS_WARN_IF(NS_FAILED(rv))) {
77 aRv.Throw(rv);
78 return nullptr;
81 return document.forget();
84 nsAutoCString utf8str;
85 // Convert from UTF16 to UTF8 using fallible allocations
86 if (!AppendUTF16toUTF8(aStr, utf8str, mozilla::fallible)) {
87 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
88 return nullptr;
91 // The new stream holds a reference to the buffer
92 nsCOMPtr<nsIInputStream> stream;
93 nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), utf8str,
94 NS_ASSIGNMENT_DEPEND);
95 if (NS_WARN_IF(NS_FAILED(rv))) {
96 aRv.Throw(rv);
97 return nullptr;
100 return ParseFromStream(stream, u"UTF-8"_ns, utf8str.Length(), aType, aRv);
103 already_AddRefed<Document> DOMParser::ParseFromSafeString(const nsAString& aStr,
104 SupportedType aType,
105 ErrorResult& aRv) {
106 // Create the new document with the same principal as `mOwner`, even if it is
107 // the system principal. This will ensure that nodes from the returned
108 // document are in the same DocGroup as the owner global's document, allowing
109 // nodes to be adopted.
110 nsCOMPtr<nsIPrincipal> docPrincipal = mPrincipal;
111 if (mOwner && mOwner->PrincipalOrNull()) {
112 mPrincipal = mOwner->PrincipalOrNull();
115 RefPtr<Document> ret = ParseFromString(aStr, aType, aRv);
116 mPrincipal = docPrincipal;
117 return ret.forget();
120 already_AddRefed<Document> DOMParser::ParseFromBuffer(const Uint8Array& aBuf,
121 SupportedType aType,
122 ErrorResult& aRv) {
123 return aBuf.ProcessFixedData([&](const Span<uint8_t>& aData) {
124 return ParseFromBuffer(aData, aType, aRv);
128 already_AddRefed<Document> DOMParser::ParseFromBuffer(Span<const uint8_t> aBuf,
129 SupportedType aType,
130 ErrorResult& aRv) {
131 // The new stream holds a reference to the buffer
132 nsCOMPtr<nsIInputStream> stream;
133 nsresult rv = NS_NewByteInputStream(
134 getter_AddRefs(stream),
135 Span(reinterpret_cast<const char*>(aBuf.Elements()), aBuf.Length()),
136 NS_ASSIGNMENT_DEPEND);
137 if (NS_FAILED(rv)) {
138 aRv.Throw(rv);
139 return nullptr;
142 return ParseFromStream(stream, VoidString(), aBuf.Length(), aType, aRv);
145 already_AddRefed<Document> DOMParser::ParseFromStream(nsIInputStream* aStream,
146 const nsAString& aCharset,
147 int32_t aContentLength,
148 SupportedType aType,
149 ErrorResult& aRv) {
150 bool svg = (aType == SupportedType::Image_svg_xml);
152 // For now, we can only create XML documents.
153 // XXXsmaug Should we create an HTMLDocument (in XHTML mode)
154 // for "application/xhtml+xml"?
155 if (aType != SupportedType::Text_xml &&
156 aType != SupportedType::Application_xml &&
157 aType != SupportedType::Application_xhtml_xml && !svg) {
158 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
159 return nullptr;
162 // Put the nsCOMPtr out here so we hold a ref to the stream as needed
163 nsCOMPtr<nsIInputStream> stream = aStream;
164 if (!NS_InputStreamIsBuffered(stream)) {
165 nsCOMPtr<nsIInputStream> bufferedStream;
166 nsresult rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
167 stream.forget(), 4096);
168 if (NS_WARN_IF(NS_FAILED(rv))) {
169 aRv.Throw(rv);
170 return nullptr;
173 stream = bufferedStream;
176 nsCOMPtr<Document> document =
177 SetUpDocument(svg ? DocumentFlavorSVG : DocumentFlavorLegacyGuess, aRv);
178 if (NS_WARN_IF(aRv.Failed())) {
179 return nullptr;
182 // Create a fake channel
183 nsCOMPtr<nsIChannel> parserChannel;
184 NS_NewInputStreamChannel(getter_AddRefs(parserChannel), mDocumentURI,
185 nullptr, // aStream
186 mPrincipal, nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL,
187 nsIContentPolicy::TYPE_OTHER, GetEnumString(aType));
188 if (NS_WARN_IF(!parserChannel)) {
189 aRv.Throw(NS_ERROR_UNEXPECTED);
190 return nullptr;
193 if (!DOMStringIsNull(aCharset)) {
194 parserChannel->SetContentCharset(NS_ConvertUTF16toUTF8(aCharset));
197 // Tell the document to start loading
198 nsCOMPtr<nsIStreamListener> listener;
200 // Keep the XULXBL state in sync with the HTML case
201 if (mForceEnableXULXBL) {
202 document->ForceEnableXULXBL();
205 if (mForceEnableDTD) {
206 document->ForceSkipDTDSecurityChecks();
209 // Have to pass false for reset here, else the reset will remove
210 // our event listener. Should that listener addition move to later
211 // than this call?
212 nsresult rv =
213 document->StartDocumentLoad(kLoadAsData, parserChannel, nullptr, nullptr,
214 getter_AddRefs(listener), false);
216 if (NS_FAILED(rv) || !listener) {
217 aRv.Throw(NS_ERROR_FAILURE);
218 return nullptr;
221 // Now start pumping data to the listener
222 nsresult status;
224 rv = listener->OnStartRequest(parserChannel);
225 if (NS_FAILED(rv)) parserChannel->Cancel(rv);
226 parserChannel->GetStatus(&status);
228 if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(status)) {
229 rv = listener->OnDataAvailable(parserChannel, stream, 0, aContentLength);
230 if (NS_FAILED(rv)) parserChannel->Cancel(rv);
231 parserChannel->GetStatus(&status);
234 rv = listener->OnStopRequest(parserChannel, status);
235 // Failure returned from OnStopRequest does not affect the final status of
236 // the channel, so we do not need to call Cancel(rv) as we do above.
238 if (NS_FAILED(rv)) {
239 aRv.Throw(NS_ERROR_FAILURE);
240 return nullptr;
243 return document.forget();
246 /*static */
247 already_AddRefed<DOMParser> DOMParser::Constructor(const GlobalObject& aOwner,
248 ErrorResult& rv) {
249 MOZ_ASSERT(NS_IsMainThread());
250 nsCOMPtr<nsIPrincipal> docPrincipal = aOwner.GetSubjectPrincipal();
251 nsCOMPtr<nsIURI> documentURI;
252 nsIURI* baseURI = nullptr;
253 if (docPrincipal->IsSystemPrincipal()) {
254 docPrincipal = NullPrincipal::Create(OriginAttributes());
255 documentURI = docPrincipal->GetURI();
256 } else {
257 // Grab document and base URIs off the window our constructor was
258 // called on. Error out if anything untoward happens.
259 nsCOMPtr<nsPIDOMWindowInner> window =
260 do_QueryInterface(aOwner.GetAsSupports());
261 if (!window) {
262 rv.Throw(NS_ERROR_UNEXPECTED);
263 return nullptr;
266 baseURI = window->GetDocBaseURI();
267 documentURI = window->GetDocumentURI();
270 if (!documentURI) {
271 rv.Throw(NS_ERROR_UNEXPECTED);
272 return nullptr;
275 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aOwner.GetAsSupports());
276 MOZ_ASSERT(global);
277 RefPtr<DOMParser> domParser =
278 new DOMParser(global, docPrincipal, documentURI, baseURI);
279 return domParser.forget();
282 // static
283 already_AddRefed<DOMParser> DOMParser::CreateWithoutGlobal(ErrorResult& aRv) {
284 nsCOMPtr<nsIPrincipal> docPrincipal =
285 NullPrincipal::Create(OriginAttributes());
287 nsCOMPtr<nsIURI> documentURI = docPrincipal->GetURI();
288 if (!documentURI) {
289 aRv.Throw(NS_ERROR_UNEXPECTED);
290 return nullptr;
293 RefPtr<DOMParser> domParser =
294 new DOMParser(nullptr, docPrincipal, documentURI, nullptr);
295 return domParser.forget();
298 already_AddRefed<Document> DOMParser::SetUpDocument(DocumentFlavor aFlavor,
299 ErrorResult& aRv) {
300 // We should really just use mOwner here, but Document gets confused
301 // if we pass it a scriptHandlingObject that doesn't QI to
302 // nsIScriptGlobalObject, and test_isequalnode.js (an xpcshell test without
303 // a window global) breaks. The correct solution is just to wean Document off
304 // of nsIScriptGlobalObject, but that's a yak to shave another day.
305 nsCOMPtr<nsIScriptGlobalObject> scriptHandlingObject =
306 do_QueryInterface(mOwner);
308 // Try to inherit a style backend.
309 NS_ASSERTION(mPrincipal, "Must have principal by now");
310 NS_ASSERTION(mDocumentURI, "Must have document URI by now");
312 nsCOMPtr<Document> doc;
313 nsresult rv = NS_NewDOMDocument(getter_AddRefs(doc), u""_ns, u""_ns, nullptr,
314 mDocumentURI, mBaseURI, mPrincipal, true,
315 scriptHandlingObject, aFlavor);
316 if (NS_WARN_IF(NS_FAILED(rv))) {
317 aRv.Throw(rv);
318 return nullptr;
321 return doc.forget();