/* -*- 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/. */
nsAutoCString utf8str; // Convert from UTF16 to UTF8 using fallible allocations if (!AppendUTF16toUTF8(aStr, utf8str, mozilla::fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return nullptr;
}
// The new stream holds a reference to the buffer
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), utf8str,
NS_ASSIGNMENT_DEPEND); if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv); return nullptr;
}
already_AddRefed<Document> DOMParser::ParseFromSafeString(const nsAString& aStr,
SupportedType aType,
ErrorResult& aRv) { // Create the new document with the same principal as `mOwner`, even if it is // the system principal. This will ensure that nodes from the returned // document are in the same DocGroup as the owner global's document, allowing // nodes to be adopted.
nsCOMPtr<nsIPrincipal> docPrincipal = mPrincipal; if (mOwner && mOwner->PrincipalOrNull()) {
mPrincipal = mOwner->PrincipalOrNull();
}
// For now, we can only create XML documents. // XXXsmaug Should we create an HTMLDocument (in XHTML mode) // for "application/xhtml+xml"? if (aType != SupportedType::Text_xml &&
aType != SupportedType::Application_xml &&
aType != SupportedType::Application_xhtml_xml && !svg) {
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); return nullptr;
}
// Put the nsCOMPtr out here so we hold a ref to the stream as needed
nsCOMPtr<nsIInputStream> stream = aStream; if (!NS_InputStreamIsBuffered(stream)) {
nsCOMPtr<nsIInputStream> bufferedStream;
nsresult rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
stream.forget(), 4096); if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv); return nullptr;
}
if (!DOMStringIsNull(aCharset)) {
parserChannel->SetContentCharset(NS_ConvertUTF16toUTF8(aCharset));
}
// Tell the document to start loading
nsCOMPtr<nsIStreamListener> listener;
// Keep the XULXBL state in sync with the HTML case if (mForceEnableXULXBL) {
document->ForceEnableXULXBL();
}
if (mForceEnableDTD) {
document->ForceSkipDTDSecurityChecks();
}
// Have to pass false for reset here, else the reset will remove // our event listener. Should that listener addition move to later // than this call?
nsresult rv =
document->StartDocumentLoad(kLoadAsData, parserChannel, nullptr, nullptr,
getter_AddRefs(listener), false);
if (NS_FAILED(rv) || !listener) {
aRv.Throw(NS_ERROR_FAILURE); return nullptr;
}
// Now start pumping data to the listener
nsresult status;
rv = listener->OnStartRequest(parserChannel); if (NS_FAILED(rv)) parserChannel->Cancel(rv);
parserChannel->GetStatus(&status);
if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(status)) {
rv = listener->OnDataAvailable(parserChannel, stream, 0, aContentLength); if (NS_FAILED(rv)) parserChannel->Cancel(rv);
parserChannel->GetStatus(&status);
}
rv = listener->OnStopRequest(parserChannel, status); // Failure returned from OnStopRequest does not affect the final status of // the channel, so we do not need to call Cancel(rv) as we do above.
if (NS_FAILED(rv)) {
aRv.Throw(NS_ERROR_FAILURE); return nullptr;
}
return document.forget();
}
/*static */
already_AddRefed<DOMParser> DOMParser::Constructor(const GlobalObject& aOwner,
ErrorResult& rv) {
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIPrincipal> docPrincipal = aOwner.GetSubjectPrincipal();
nsCOMPtr<nsIURI> documentURI; if (docPrincipal->IsSystemPrincipal()) {
docPrincipal = NullPrincipal::Create(OriginAttributes());
documentURI = docPrincipal->GetURI();
} else { // Grab document URI off the window our constructor was called on. // Error out if anything untoward happens.
nsCOMPtr<nsPIDOMWindowInner> window =
do_QueryInterface(aOwner.GetAsSupports()); if (!window) {
rv.Throw(NS_ERROR_UNEXPECTED); return nullptr;
}
documentURI = window->GetDocumentURI();
}
if (!documentURI) {
rv.Throw(NS_ERROR_UNEXPECTED); return nullptr;
}
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aOwner.GetAsSupports());
MOZ_ASSERT(global);
RefPtr<DOMParser> domParser = new DOMParser(global, docPrincipal, documentURI); return domParser.forget();
}
RefPtr<DOMParser> domParser = new DOMParser(nullptr, docPrincipal, documentURI); return domParser.forget();
}
already_AddRefed<Document> DOMParser::SetUpDocument(DocumentFlavor aFlavor,
ErrorResult& aRv) { // We should really just use mOwner here, but Document gets confused // if we pass it a scriptHandlingObject that doesn't QI to // nsIScriptGlobalObject, and test_isequalnode.js (an xpcshell test without // a window global) breaks. The correct solution is just to wean Document off // of nsIScriptGlobalObject, but that's a yak to shave another day.
nsCOMPtr<nsIScriptGlobalObject> scriptHandlingObject =
do_QueryInterface(mOwner);
// Try to inherit a style backend.
NS_ASSERTION(mPrincipal, "Must have principal by now");
NS_ASSERTION(mDocumentURI, "Must have document URI by now");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.