2012-05-29 15:52:43 +00:00
|
|
|
/* 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/. */
|
2012-01-20 11:16:27 +00:00
|
|
|
|
|
|
|
#include "nsHtml5StringParser.h"
|
|
|
|
#include "nsHtml5TreeOpExecutor.h"
|
|
|
|
#include "nsHtml5TreeBuilder.h"
|
|
|
|
#include "nsHtml5Tokenizer.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMDocumentFragment.h"
|
|
|
|
#include "nsHtml5DependentUTF16Buffer.h"
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(nsHtml5StringParser)
|
|
|
|
|
|
|
|
nsHtml5StringParser::nsHtml5StringParser()
|
2012-01-20 12:03:49 +00:00
|
|
|
: mExecutor(new nsHtml5TreeOpExecutor(true))
|
2012-07-30 14:20:58 +00:00
|
|
|
, mTreeBuilder(new nsHtml5TreeBuilder(mExecutor, nullptr))
|
2012-01-20 11:16:27 +00:00
|
|
|
, mTokenizer(new nsHtml5Tokenizer(mTreeBuilder, false))
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsHtml5StringParser);
|
2012-05-18 17:30:49 +00:00
|
|
|
mAtomTable.Init();
|
2012-01-20 11:16:27 +00:00
|
|
|
mTokenizer->setInterner(&mAtomTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5StringParser::~nsHtml5StringParser()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsHtml5StringParser);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-20 12:03:49 +00:00
|
|
|
nsHtml5StringParser::ParseFragment(const nsAString& aSourceBuffer,
|
|
|
|
nsIContent* aTargetNode,
|
|
|
|
nsIAtom* aContextLocalName,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aContextNamespace,
|
2012-01-20 12:03:49 +00:00
|
|
|
bool aQuirks,
|
|
|
|
bool aPreventScriptExecution)
|
2012-01-20 11:16:27 +00:00
|
|
|
{
|
2012-09-28 06:57:33 +00:00
|
|
|
NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX,
|
2012-01-20 12:03:49 +00:00
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
2012-01-20 11:16:27 +00:00
|
|
|
|
2012-01-20 12:03:49 +00:00
|
|
|
nsIDocument* doc = aTargetNode->OwnerDoc();
|
2012-01-20 11:16:27 +00:00
|
|
|
nsIURI* uri = doc->GetDocumentURI();
|
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsIContent* target = aTargetNode;
|
|
|
|
mTreeBuilder->setFragmentContext(aContextLocalName,
|
|
|
|
aContextNamespace,
|
|
|
|
&target,
|
|
|
|
aQuirks);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (!aPreventScriptExecution) {
|
|
|
|
NS_ASSERTION(!aTargetNode->IsInDoc(),
|
2012-01-20 12:03:49 +00:00
|
|
|
"If script execution isn't prevented, "
|
|
|
|
"the target node must not be in doc.");
|
2012-01-20 11:16:27 +00:00
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> domFrag = do_QueryInterface(aTargetNode);
|
|
|
|
NS_ASSERTION(domFrag,
|
2012-01-20 12:03:49 +00:00
|
|
|
"If script execution isn't prevented, must parse to DOM fragment.");
|
2012-01-20 11:16:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-20 07:05:39 +00:00
|
|
|
mTreeBuilder->SetPreventScriptExecution(aPreventScriptExecution);
|
2012-01-20 12:03:49 +00:00
|
|
|
|
|
|
|
Tokenize(aSourceBuffer, doc, true);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5StringParser::ParseDocument(const nsAString& aSourceBuffer,
|
2012-02-27 11:57:48 +00:00
|
|
|
nsIDocument* aTargetDoc,
|
|
|
|
bool aScriptingEnabledForNoscriptParsing)
|
2012-01-20 12:03:49 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aTargetDoc->GetFirstChild());
|
|
|
|
|
2012-09-28 06:57:33 +00:00
|
|
|
NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX,
|
2012-01-20 12:03:49 +00:00
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mTreeBuilder->setFragmentContext(nullptr,
|
2012-01-20 12:03:49 +00:00
|
|
|
kNameSpaceID_None,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr,
|
2012-01-20 12:03:49 +00:00
|
|
|
false);
|
|
|
|
|
2012-06-20 07:05:39 +00:00
|
|
|
mTreeBuilder->SetPreventScriptExecution(true);
|
2012-01-20 12:03:49 +00:00
|
|
|
|
2012-02-27 11:57:48 +00:00
|
|
|
Tokenize(aSourceBuffer, aTargetDoc, aScriptingEnabledForNoscriptParsing);
|
2012-01-20 12:03:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5StringParser::Tokenize(const nsAString& aSourceBuffer,
|
|
|
|
nsIDocument* aDocument,
|
|
|
|
bool aScriptingEnabledForNoscriptParsing) {
|
|
|
|
|
|
|
|
nsIURI* uri = aDocument->GetDocumentURI();
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mExecutor->Init(aDocument, uri, nullptr, nullptr);
|
2012-01-20 12:03:49 +00:00
|
|
|
|
|
|
|
mExecutor->SetParser(this);
|
|
|
|
mExecutor->SetNodeInfoManager(aDocument->NodeInfoManager());
|
|
|
|
|
2012-01-20 11:16:27 +00:00
|
|
|
NS_PRECONDITION(!mExecutor->HasStarted(),
|
|
|
|
"Tried to start parse without initializing the parser.");
|
2012-01-20 12:03:49 +00:00
|
|
|
mTreeBuilder->setScriptingEnabled(aScriptingEnabledForNoscriptParsing);
|
2012-01-20 11:16:27 +00:00
|
|
|
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()) {
|
2012-06-20 07:05:39 +00:00
|
|
|
// If we come here, we are in createContextualFragment() or in the
|
|
|
|
// upcoming document.parse(). It's unclear if it's really necessary
|
|
|
|
// to flush here, but let's do so for consistency with other flushes
|
|
|
|
// to avoid different code paths on the executor side.
|
2012-01-20 11:16:27 +00:00
|
|
|
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();
|
|
|
|
mExecutor->Reset();
|
|
|
|
}
|