2012-05-29 08:52:43 -07: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 03:16:27 -08: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()
|
2014-03-05 11:38:50 -08:00
|
|
|
: mBuilder(new nsHtml5OplessBuilder())
|
|
|
|
, mTreeBuilder(new nsHtml5TreeBuilder(mBuilder))
|
2012-01-20 03:16:27 -08:00
|
|
|
, mTokenizer(new nsHtml5Tokenizer(mTreeBuilder, false))
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsHtml5StringParser);
|
|
|
|
mTokenizer->setInterner(&mAtomTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5StringParser::~nsHtml5StringParser()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsHtml5StringParser);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-20 04:03:49 -08:00
|
|
|
nsHtml5StringParser::ParseFragment(const nsAString& aSourceBuffer,
|
|
|
|
nsIContent* aTargetNode,
|
|
|
|
nsIAtom* aContextLocalName,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aContextNamespace,
|
2012-01-20 04:03:49 -08:00
|
|
|
bool aQuirks,
|
|
|
|
bool aPreventScriptExecution)
|
2012-01-20 03:16:27 -08:00
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX,
|
2012-01-20 04:03:49 -08:00
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
2012-01-20 03:16:27 -08:00
|
|
|
|
2012-01-20 04:03:49 -08:00
|
|
|
nsIDocument* doc = aTargetNode->OwnerDoc();
|
2012-01-20 03:16:27 -08:00
|
|
|
nsIURI* uri = doc->GetDocumentURI();
|
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
mTreeBuilder->setFragmentContext(aContextLocalName,
|
|
|
|
aContextNamespace,
|
2014-03-05 11:38:50 -08:00
|
|
|
aTargetNode,
|
2012-01-20 03:16:27 -08:00
|
|
|
aQuirks);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (!aPreventScriptExecution) {
|
|
|
|
NS_ASSERTION(!aTargetNode->IsInDoc(),
|
2012-01-20 04:03:49 -08:00
|
|
|
"If script execution isn't prevented, "
|
|
|
|
"the target node must not be in doc.");
|
2012-01-20 03:16:27 -08:00
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> domFrag = do_QueryInterface(aTargetNode);
|
|
|
|
NS_ASSERTION(domFrag,
|
2012-01-20 04:03:49 -08:00
|
|
|
"If script execution isn't prevented, must parse to DOM fragment.");
|
2012-01-20 03:16:27 -08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-20 00:05:39 -07:00
|
|
|
mTreeBuilder->SetPreventScriptExecution(aPreventScriptExecution);
|
2012-01-20 04:03:49 -08:00
|
|
|
|
2014-03-05 11:38:50 -08:00
|
|
|
return Tokenize(aSourceBuffer, doc, true);
|
2012-01-20 04:03:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5StringParser::ParseDocument(const nsAString& aSourceBuffer,
|
2012-02-27 03:57:48 -08:00
|
|
|
nsIDocument* aTargetDoc,
|
|
|
|
bool aScriptingEnabledForNoscriptParsing)
|
2012-01-20 04:03:49 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aTargetDoc->GetFirstChild());
|
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX,
|
2012-01-20 04:03:49 -08:00
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mTreeBuilder->setFragmentContext(nullptr,
|
2012-01-20 04:03:49 -08:00
|
|
|
kNameSpaceID_None,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2012-01-20 04:03:49 -08:00
|
|
|
false);
|
|
|
|
|
2012-06-20 00:05:39 -07:00
|
|
|
mTreeBuilder->SetPreventScriptExecution(true);
|
2012-01-20 04:03:49 -08:00
|
|
|
|
2014-03-05 11:38:50 -08:00
|
|
|
return Tokenize(aSourceBuffer, aTargetDoc, aScriptingEnabledForNoscriptParsing);
|
2012-01-20 04:03:49 -08:00
|
|
|
}
|
|
|
|
|
2014-03-05 11:38:50 -08:00
|
|
|
nsresult
|
2012-01-20 04:03:49 -08:00
|
|
|
nsHtml5StringParser::Tokenize(const nsAString& aSourceBuffer,
|
|
|
|
nsIDocument* aDocument,
|
|
|
|
bool aScriptingEnabledForNoscriptParsing) {
|
|
|
|
|
|
|
|
nsIURI* uri = aDocument->GetDocumentURI();
|
|
|
|
|
2014-03-05 11:38:50 -08:00
|
|
|
mBuilder->Init(aDocument, uri, nullptr, nullptr);
|
|
|
|
|
|
|
|
mBuilder->SetParser(this);
|
|
|
|
mBuilder->SetNodeInfoManager(aDocument->NodeInfoManager());
|
2012-01-20 04:03:49 -08:00
|
|
|
|
2014-03-05 11:38:50 -08:00
|
|
|
// Mark the parser as *not* broken by passing NS_OK
|
|
|
|
nsresult rv = mBuilder->MarkAsBroken(NS_OK);
|
2012-01-20 04:03:49 -08:00
|
|
|
|
|
|
|
mTreeBuilder->setScriptingEnabled(aScriptingEnabledForNoscriptParsing);
|
2013-06-28 20:13:23 -07:00
|
|
|
mTreeBuilder->setIsSrcdocDocument(aDocument->IsSrcdocDocument());
|
2014-03-05 11:38:50 -08:00
|
|
|
mBuilder->Start();
|
2012-01-20 03:16:27 -08:00
|
|
|
mTokenizer->start();
|
|
|
|
if (!aSourceBuffer.IsEmpty()) {
|
|
|
|
bool lastWasCR = false;
|
|
|
|
nsHtml5DependentUTF16Buffer buffer(aSourceBuffer);
|
|
|
|
while (buffer.hasMore()) {
|
|
|
|
buffer.adjust(lastWasCR);
|
|
|
|
lastWasCR = false;
|
|
|
|
if (buffer.hasMore()) {
|
|
|
|
lastWasCR = mTokenizer->tokenizeBuffer(&buffer);
|
2014-03-05 11:38:50 -08:00
|
|
|
if (NS_FAILED(rv = mBuilder->IsBroken())) {
|
|
|
|
break;
|
2012-01-20 03:16:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mTokenizer->eof();
|
|
|
|
mTokenizer->end();
|
2014-03-05 11:38:50 -08:00
|
|
|
mBuilder->Finish();
|
2012-01-20 03:16:27 -08:00
|
|
|
mAtomTable.Clear();
|
2014-03-05 11:38:50 -08:00
|
|
|
return rv;
|
2012-01-20 03:16:27 -08:00
|
|
|
}
|