michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsHtml5StringParser.h" michael@0: #include "nsHtml5TreeOpExecutor.h" michael@0: #include "nsHtml5TreeBuilder.h" michael@0: #include "nsHtml5Tokenizer.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMDocumentFragment.h" michael@0: #include "nsHtml5DependentUTF16Buffer.h" michael@0: michael@0: NS_IMPL_ISUPPORTS0(nsHtml5StringParser) michael@0: michael@0: nsHtml5StringParser::nsHtml5StringParser() michael@0: : mBuilder(new nsHtml5OplessBuilder()) michael@0: , mTreeBuilder(new nsHtml5TreeBuilder(mBuilder)) michael@0: , mTokenizer(new nsHtml5Tokenizer(mTreeBuilder, false)) michael@0: { michael@0: MOZ_COUNT_CTOR(nsHtml5StringParser); michael@0: mTokenizer->setInterner(&mAtomTable); michael@0: } michael@0: michael@0: nsHtml5StringParser::~nsHtml5StringParser() michael@0: { michael@0: MOZ_COUNT_DTOR(nsHtml5StringParser); michael@0: } michael@0: michael@0: nsresult michael@0: nsHtml5StringParser::ParseFragment(const nsAString& aSourceBuffer, michael@0: nsIContent* aTargetNode, michael@0: nsIAtom* aContextLocalName, michael@0: int32_t aContextNamespace, michael@0: bool aQuirks, michael@0: bool aPreventScriptExecution) michael@0: { michael@0: NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX, michael@0: NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: nsIDocument* doc = aTargetNode->OwnerDoc(); michael@0: nsIURI* uri = doc->GetDocumentURI(); michael@0: NS_ENSURE_TRUE(uri, NS_ERROR_NOT_AVAILABLE); michael@0: michael@0: mTreeBuilder->setFragmentContext(aContextLocalName, michael@0: aContextNamespace, michael@0: aTargetNode, michael@0: aQuirks); michael@0: michael@0: #ifdef DEBUG michael@0: if (!aPreventScriptExecution) { michael@0: NS_ASSERTION(!aTargetNode->IsInDoc(), michael@0: "If script execution isn't prevented, " michael@0: "the target node must not be in doc."); michael@0: nsCOMPtr domFrag = do_QueryInterface(aTargetNode); michael@0: NS_ASSERTION(domFrag, michael@0: "If script execution isn't prevented, must parse to DOM fragment."); michael@0: } michael@0: #endif michael@0: michael@0: mTreeBuilder->SetPreventScriptExecution(aPreventScriptExecution); michael@0: michael@0: return Tokenize(aSourceBuffer, doc, true); michael@0: } michael@0: michael@0: nsresult michael@0: nsHtml5StringParser::ParseDocument(const nsAString& aSourceBuffer, michael@0: nsIDocument* aTargetDoc, michael@0: bool aScriptingEnabledForNoscriptParsing) michael@0: { michael@0: MOZ_ASSERT(!aTargetDoc->GetFirstChild()); michael@0: michael@0: NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX, michael@0: NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: mTreeBuilder->setFragmentContext(nullptr, michael@0: kNameSpaceID_None, michael@0: nullptr, michael@0: false); michael@0: michael@0: mTreeBuilder->SetPreventScriptExecution(true); michael@0: michael@0: return Tokenize(aSourceBuffer, aTargetDoc, aScriptingEnabledForNoscriptParsing); michael@0: } michael@0: michael@0: nsresult michael@0: nsHtml5StringParser::Tokenize(const nsAString& aSourceBuffer, michael@0: nsIDocument* aDocument, michael@0: bool aScriptingEnabledForNoscriptParsing) { michael@0: michael@0: nsIURI* uri = aDocument->GetDocumentURI(); michael@0: michael@0: mBuilder->Init(aDocument, uri, nullptr, nullptr); michael@0: michael@0: mBuilder->SetParser(this); michael@0: mBuilder->SetNodeInfoManager(aDocument->NodeInfoManager()); michael@0: michael@0: // Mark the parser as *not* broken by passing NS_OK michael@0: nsresult rv = mBuilder->MarkAsBroken(NS_OK); michael@0: michael@0: mTreeBuilder->setScriptingEnabled(aScriptingEnabledForNoscriptParsing); michael@0: mTreeBuilder->setIsSrcdocDocument(aDocument->IsSrcdocDocument()); michael@0: mBuilder->Start(); michael@0: mTokenizer->start(); michael@0: if (!aSourceBuffer.IsEmpty()) { michael@0: bool lastWasCR = false; michael@0: nsHtml5DependentUTF16Buffer buffer(aSourceBuffer); michael@0: while (buffer.hasMore()) { michael@0: buffer.adjust(lastWasCR); michael@0: lastWasCR = false; michael@0: if (buffer.hasMore()) { michael@0: lastWasCR = mTokenizer->tokenizeBuffer(&buffer); michael@0: if (NS_FAILED(rv = mBuilder->IsBroken())) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: mTokenizer->eof(); michael@0: mTokenizer->end(); michael@0: mBuilder->Finish(); michael@0: mAtomTable.Clear(); michael@0: return rv; michael@0: }