NS_IMETHODIMP nsHtml5Parser::ParseFragment(const nsAString& aSourceBuffer, nsIContent* aTargetNode, nsIAtom* aContextLocalName, PRInt32 aContextNamespace, PRBool aQuirks) { nsIDocument* doc = aTargetNode->GetOwnerDoc(); NS_ENSURE_TRUE(doc, NS_ERROR_NOT_AVAILABLE); nsIURI* uri = doc->GetDocumentURI(); NS_ENSURE_TRUE(uri, NS_ERROR_NOT_AVAILABLE); Initialize(doc, uri, nsnull, nsnull); mExecutor->SetParser(this); mExecutor->SetNodeInfoManager(doc->NodeInfoManager()); nsIContent* target = aTargetNode; mTreeBuilder->setFragmentContext(aContextLocalName, aContextNamespace, &target, aQuirks); mExecutor->EnableFragmentMode(); NS_PRECONDITION(!mExecutor->HasStarted(), "Tried to start parse without initializing the parser."); mTreeBuilder->setScriptingEnabled(mExecutor->IsScriptEnabled()); mTokenizer->start(); mExecutor->Start(); // Don't call WillBuildModel in fragment case if (!aSourceBuffer.IsEmpty()) { PRBool lastWasCR = PR_FALSE; nsHtml5UTF16Buffer buffer(aSourceBuffer.Length()); memcpy(buffer.getBuffer(), aSourceBuffer.BeginReading(), aSourceBuffer.Length() * sizeof(PRUnichar)); buffer.setEnd(aSourceBuffer.Length()); while (buffer.hasMore()) { buffer.adjust(lastWasCR); lastWasCR = PR_FALSE; if (buffer.hasMore()) { lastWasCR = mTokenizer->tokenizeBuffer(&buffer); } } } mTokenizer->eof(); mTreeBuilder->StreamEnded(); mTreeBuilder->Flush(); mExecutor->FlushDocumentWrite(); mTokenizer->end(); mExecutor->DropParserAndPerfHint(); mExecutor->DropHeldElements(); mAtomTable.Clear(); return NS_OK; }
nsresult nsHtml5Parser::ParseHtml5Fragment(const nsAString& aSourceBuffer, nsIContent* aTargetNode, nsIAtom* aContextLocalName, PRInt32 aContextNamespace, bool aQuirks, bool aPreventScriptExecution) { NS_ENSURE_TRUE(aSourceBuffer.Length() <= PR_INT32_MAX, NS_ERROR_OUT_OF_MEMORY); nsIDocument* doc = aTargetNode->OwnerDoc(); nsIURI* uri = doc->GetDocumentURI(); NS_ENSURE_TRUE(uri, NS_ERROR_NOT_AVAILABLE); mExecutor->EnableFragmentMode(aPreventScriptExecution); Initialize(doc, uri, nsnull, nsnull); mExecutor->SetParser(this); mExecutor->SetNodeInfoManager(doc->NodeInfoManager()); nsIContent* target = aTargetNode; mTreeBuilder->setFragmentContext(aContextLocalName, aContextNamespace, &target, aQuirks); #ifdef DEBUG if (!aPreventScriptExecution) { NS_ASSERTION(!aTargetNode->IsInDoc(), "If script execution isn't prevented, " "the target node must not be in doc."); nsCOMPtr<nsIDOMDocumentFragment> domFrag = do_QueryInterface(aTargetNode); NS_ASSERTION(domFrag, "If script execution isn't prevented, must parse to DOM fragment."); } #endif NS_PRECONDITION(!mExecutor->HasStarted(), "Tried to start parse without initializing the parser."); mTreeBuilder->setScriptingEnabled(mExecutor->IsScriptEnabled()); mTokenizer->start(); mExecutor->Start(); // Don't call WillBuildModel in fragment case if (!aSourceBuffer.IsEmpty()) { bool lastWasCR = false; nsHtml5DependentUTF16Buffer buffer(aSourceBuffer); while (buffer.hasMore()) { buffer.adjust(lastWasCR); lastWasCR = false; if (buffer.hasMore()) { lastWasCR = mTokenizer->tokenizeBuffer(&buffer); if (mTreeBuilder->HasScript()) { // Flush on each script, because the execution prevention code // can handle at most one script per flush. mTreeBuilder->Flush(); // Move ops to the executor mExecutor->FlushDocumentWrite(); // run the ops } } } } mTokenizer->eof(); mTreeBuilder->StreamEnded(); mTreeBuilder->Flush(); mExecutor->FlushDocumentWrite(); mTokenizer->end(); mExecutor->DropParserAndPerfHint(); mExecutor->DropHeldElements(); mTreeBuilder->DropHandles(); mAtomTable.Clear(); return NS_OK; }