nsresult NS_NewDOMDocumentType(nsIDOMDocumentType** aDocType, nsNodeInfoManager *aNodeInfoManager, nsIAtom *aName, const nsAString& aPublicId, const nsAString& aSystemId, const nsAString& aInternalSubset) { NS_ENSURE_ARG_POINTER(aDocType); mozilla::ErrorResult rv; *aDocType = NS_NewDOMDocumentType(aNodeInfoManager, aName, aPublicId, aSystemId, aInternalSubset, rv).take(); return rv.ErrorCode(); }
nsresult txMozillaXMLOutput::createResultDocument(const nsSubstring& aName, PRInt32 aNsID, nsIDOMDocument* aSourceDocument, nsIDOMDocument* aResultDocument) { nsresult rv; if (!aResultDocument) { // Create the document if (mOutputFormat.mMethod == eHTMLOutput) { rv = NS_NewHTMLDocument(getter_AddRefs(mDocument)); NS_ENSURE_SUCCESS(rv, rv); } else { // We should check the root name/namespace here and create the // appropriate document rv = NS_NewXMLDocument(getter_AddRefs(mDocument)); NS_ENSURE_SUCCESS(rv, rv); } // This should really be handled by nsIDocument::BeginLoad mDocument->SetReadyStateInternal(nsIDocument::READYSTATE_LOADING); nsCOMPtr<nsIDocument> source = do_QueryInterface(aSourceDocument); NS_ENSURE_STATE(source); PRBool hasHadScriptObject = PR_FALSE; nsIScriptGlobalObject* sgo = source->GetScriptHandlingObject(hasHadScriptObject); NS_ENSURE_STATE(sgo || !hasHadScriptObject); mDocument->SetScriptHandlingObject(sgo); } else { mDocument = do_QueryInterface(aResultDocument); } mCurrentNode = mDocument; mNodeInfoManager = mDocument->NodeInfoManager(); // Reset and set up the document URIUtils::ResetWithSource(mDocument, aSourceDocument); // Set the charset if (!mOutputFormat.mEncoding.IsEmpty()) { NS_LossyConvertUTF16toASCII charset(mOutputFormat.mEncoding); nsCAutoString canonicalCharset; nsCOMPtr<nsICharsetAlias> calias = do_GetService("@mozilla.org/intl/charsetalias;1"); if (calias && NS_SUCCEEDED(calias->GetPreferred(charset, canonicalCharset))) { mDocument->SetDocumentCharacterSetSource(kCharsetFromOtherComponent); mDocument->SetDocumentCharacterSet(canonicalCharset); } } // Set the mime-type if (!mOutputFormat.mMediaType.IsEmpty()) { mDocument->SetContentType(mOutputFormat.mMediaType); } else if (mOutputFormat.mMethod == eHTMLOutput) { mDocument->SetContentType(NS_LITERAL_STRING("text/html")); } else { mDocument->SetContentType(NS_LITERAL_STRING("application/xml")); } if (mOutputFormat.mMethod == eXMLOutput && mOutputFormat.mOmitXMLDeclaration != eTrue) { PRInt32 standalone; if (mOutputFormat.mStandalone == eNotSet) { standalone = -1; } else if (mOutputFormat.mStandalone == eFalse) { standalone = 0; } else { standalone = 1; } // Could use mOutputFormat.mVersion.get() when we support // versions > 1.0. static const PRUnichar kOneDotZero[] = { '1', '.', '0', '\0' }; mDocument->SetXMLDeclaration(kOneDotZero, mOutputFormat.mEncoding.get(), standalone); } // Set up script loader of the result document. nsScriptLoader *loader = mDocument->ScriptLoader(); if (mNotifier) { loader->AddObserver(mNotifier); } else { // Don't load scripts, we can't notify the caller when they're loaded. loader->SetEnabled(PR_FALSE); } if (mNotifier) { rv = mNotifier->SetOutputDocument(mDocument); NS_ENSURE_SUCCESS(rv, rv); } // Do this after calling OnDocumentCreated to ensure that the // PresShell/PresContext has been hooked up and get notified. nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(mDocument); if (htmlDoc) { htmlDoc->SetCompatibilityMode(eCompatibility_FullStandards); } // Add a doc-type if requested if (!mOutputFormat.mSystemId.IsEmpty()) { nsAutoString qName; if (mOutputFormat.mMethod == eHTMLOutput) { qName.AssignLiteral("html"); } else { qName.Assign(aName); } nsCOMPtr<nsIDOMDocumentType> documentType; nsresult rv = nsContentUtils::CheckQName(qName); if (NS_SUCCEEDED(rv)) { nsCOMPtr<nsIAtom> doctypeName = do_GetAtom(qName); if (!doctypeName) { return NS_ERROR_OUT_OF_MEMORY; } // Indicate that there is no internal subset (not just an empty one) nsAutoString voidString; voidString.SetIsVoid(PR_TRUE); rv = NS_NewDOMDocumentType(getter_AddRefs(documentType), mNodeInfoManager, nsnull, doctypeName, nsnull, nsnull, mOutputFormat.mPublicId, mOutputFormat.mSystemId, voidString); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIContent> docType = do_QueryInterface(documentType); rv = mDocument->AppendChildTo(docType, PR_TRUE); NS_ENSURE_SUCCESS(rv, rv); } } return NS_OK; }
nsresult DOMImplementation::CreateHTMLDocument(const nsAString& aTitle, nsIDocument** aDocument, nsIDOMDocument** aDOMDocument) { *aDocument = nullptr; *aDOMDocument = nullptr; NS_ENSURE_STATE(mOwner); nsCOMPtr<nsIDOMDocumentType> doctype; // Indicate that there is no internal subset (not just an empty one) nsresult rv = NS_NewDOMDocumentType(getter_AddRefs(doctype), mOwner->NodeInfoManager(), nsGkAtoms::html, // aName EmptyString(), // aPublicId EmptyString(), // aSystemId NullString()); // aInternalSubset NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIGlobalObject> scriptHandlingObject = do_QueryReferent(mScriptObject); NS_ENSURE_STATE(!mScriptObject || scriptHandlingObject); nsCOMPtr<nsIDOMDocument> document; rv = NS_NewDOMDocument(getter_AddRefs(document), EmptyString(), EmptyString(), doctype, mDocumentURI, mBaseURI, mOwner->NodePrincipal(), true, scriptHandlingObject, DocumentFlavorLegacyGuess); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDocument> doc = do_QueryInterface(document); nsCOMPtr<nsIContent> root; rv = doc->CreateElem(NS_LITERAL_STRING("html"), nullptr, kNameSpaceID_XHTML, getter_AddRefs(root)); NS_ENSURE_SUCCESS(rv, rv); rv = doc->AppendChildTo(root, false); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIContent> head; rv = doc->CreateElem(NS_LITERAL_STRING("head"), nullptr, kNameSpaceID_XHTML, getter_AddRefs(head)); NS_ENSURE_SUCCESS(rv, rv); rv = root->AppendChildTo(head, false); NS_ENSURE_SUCCESS(rv, rv); if (!DOMStringIsNull(aTitle)) { nsCOMPtr<nsIContent> title; rv = doc->CreateElem(NS_LITERAL_STRING("title"), nullptr, kNameSpaceID_XHTML, getter_AddRefs(title)); NS_ENSURE_SUCCESS(rv, rv); rv = head->AppendChildTo(title, false); NS_ENSURE_SUCCESS(rv, rv); nsRefPtr<nsTextNode> titleText = new nsTextNode(doc->NodeInfoManager()); rv = titleText->SetText(aTitle, false); NS_ENSURE_SUCCESS(rv, rv); rv = title->AppendChildTo(titleText, false); NS_ENSURE_SUCCESS(rv, rv); } nsCOMPtr<nsIContent> body; rv = doc->CreateElem(NS_LITERAL_STRING("body"), nullptr, kNameSpaceID_XHTML, getter_AddRefs(body)); NS_ENSURE_SUCCESS(rv, rv); rv = root->AppendChildTo(body, false); NS_ENSURE_SUCCESS(rv, rv); doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE); doc.forget(aDocument); document.forget(aDOMDocument); return NS_OK; }