2012-05-29 19:52:43 +04: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 15:16:27 +04:00
|
|
|
|
|
|
|
#include "nsHtml5StringParser.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "nsHtml5DependentUTF16Buffer.h"
|
2012-01-20 15:16:27 +04:00
|
|
|
#include "nsHtml5Tokenizer.h"
|
2018-03-16 18:26:06 +03:00
|
|
|
#include "nsHtml5TreeBuilder.h"
|
|
|
|
#include "nsHtml5TreeOpExecutor.h"
|
2012-01-20 15:16:27 +04:00
|
|
|
#include "nsIContent.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2018-04-18 04:13:03 +03:00
|
|
|
#include "mozilla/dom/DocumentFragment.h"
|
2012-01-20 15:16:27 +04:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
using mozilla::dom::Document;
|
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(nsHtml5StringParser)
|
|
|
|
|
|
|
|
nsHtml5StringParser::nsHtml5StringParser()
|
2014-03-05 23:38:50 +04:00
|
|
|
: mBuilder(new nsHtml5OplessBuilder()),
|
|
|
|
mTreeBuilder(new nsHtml5TreeBuilder(mBuilder)),
|
2020-02-28 23:49:06 +03:00
|
|
|
mTokenizer(new nsHtml5Tokenizer(mTreeBuilder.get(), false)) {
|
2012-01-20 15:16:27 +04:00
|
|
|
mTokenizer->setInterner(&mAtomTable);
|
|
|
|
}
|
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
nsHtml5StringParser::~nsHtml5StringParser() {}
|
2012-01-20 15:16:27 +04:00
|
|
|
|
2012-01-20 16:03:49 +04:00
|
|
|
nsresult nsHtml5StringParser::ParseFragment(const nsAString& aSourceBuffer,
|
|
|
|
nsIContent* aTargetNode,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aContextLocalName,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aContextNamespace,
|
2012-01-20 16:03:49 +04:00
|
|
|
bool aQuirks,
|
|
|
|
bool aPreventScriptExecution) {
|
2018-03-16 18:26:06 +03:00
|
|
|
NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX, NS_ERROR_OUT_OF_MEMORY);
|
2012-01-20 15:16:27 +04:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aTargetNode->OwnerDoc();
|
2012-01-20 15:16:27 +04:00
|
|
|
nsIURI* uri = doc->GetDocumentURI();
|
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->setFragmentContext(aContextLocalName, aContextNamespace,
|
|
|
|
aTargetNode, aQuirks);
|
2012-01-20 15:16:27 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (!aPreventScriptExecution) {
|
2016-03-31 13:58:25 +03:00
|
|
|
NS_ASSERTION(!aTargetNode->IsInUncomposedDoc(),
|
2012-01-20 16:03:49 +04:00
|
|
|
"If script execution isn't prevented, "
|
|
|
|
"the target node must not be in doc.");
|
2018-03-16 18:26:06 +03:00
|
|
|
NS_ASSERTION(
|
2018-04-18 04:13:03 +03:00
|
|
|
aTargetNode->NodeType() == nsINode::DOCUMENT_FRAGMENT_NODE,
|
2012-01-20 16:03:49 +04:00
|
|
|
"If script execution isn't prevented, must parse to DOM fragment.");
|
2012-01-20 15:16:27 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-20 11:05:39 +04:00
|
|
|
mTreeBuilder->SetPreventScriptExecution(aPreventScriptExecution);
|
2012-01-20 16:03:49 +04:00
|
|
|
|
2014-03-05 23:38:50 +04:00
|
|
|
return Tokenize(aSourceBuffer, doc, true);
|
2012-01-20 16:03:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsHtml5StringParser::ParseDocument(
|
2019-01-02 16:05:23 +03:00
|
|
|
const nsAString& aSourceBuffer, Document* aTargetDoc,
|
2012-02-27 15:57:48 +04:00
|
|
|
bool aScriptingEnabledForNoscriptParsing) {
|
2012-01-20 16:03:49 +04:00
|
|
|
MOZ_ASSERT(!aTargetDoc->GetFirstChild());
|
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX, NS_ERROR_OUT_OF_MEMORY);
|
2012-01-20 16:03:49 +04:00
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->setFragmentContext(nullptr, kNameSpaceID_None, nullptr, false);
|
2012-01-20 16:03:49 +04:00
|
|
|
|
2012-06-20 11:05:39 +04:00
|
|
|
mTreeBuilder->SetPreventScriptExecution(true);
|
2012-01-20 16:03:49 +04:00
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
return Tokenize(aSourceBuffer, aTargetDoc,
|
|
|
|
aScriptingEnabledForNoscriptParsing);
|
2012-01-20 16:03:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsHtml5StringParser::Tokenize(
|
2019-01-02 16:05:23 +03:00
|
|
|
const nsAString& aSourceBuffer, Document* aDocument,
|
2018-03-16 18:26:06 +03:00
|
|
|
bool aScriptingEnabledForNoscriptParsing) {
|
2012-01-20 16:03:49 +04:00
|
|
|
nsIURI* uri = aDocument->GetDocumentURI();
|
|
|
|
|
2014-03-05 23:38:50 +04:00
|
|
|
mBuilder->Init(aDocument, uri, nullptr, nullptr);
|
|
|
|
|
|
|
|
mBuilder->SetParser(this);
|
|
|
|
mBuilder->SetNodeInfoManager(aDocument->NodeInfoManager());
|
2012-01-20 16:03:49 +04:00
|
|
|
|
2014-03-05 23:38:50 +04:00
|
|
|
// Mark the parser as *not* broken by passing NS_OK
|
|
|
|
nsresult rv = mBuilder->MarkAsBroken(NS_OK);
|
2012-01-20 16:03:49 +04:00
|
|
|
|
|
|
|
mTreeBuilder->setScriptingEnabled(aScriptingEnabledForNoscriptParsing);
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->setIsSrcdocDocument(aDocument->IsSrcdocDocument());
|
2014-03-05 23:38:50 +04:00
|
|
|
mBuilder->Start();
|
2012-01-20 15:16:27 +04:00
|
|
|
mTokenizer->start();
|
|
|
|
if (!aSourceBuffer.IsEmpty()) {
|
|
|
|
bool lastWasCR = false;
|
|
|
|
nsHtml5DependentUTF16Buffer buffer(aSourceBuffer);
|
|
|
|
while (buffer.hasMore()) {
|
|
|
|
buffer.adjust(lastWasCR);
|
|
|
|
lastWasCR = false;
|
|
|
|
if (buffer.hasMore()) {
|
2015-08-25 18:05:45 +03:00
|
|
|
if (!mTokenizer->EnsureBufferSpace(buffer.getLength())) {
|
|
|
|
rv = mBuilder->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-20 15:16:27 +04:00
|
|
|
lastWasCR = mTokenizer->tokenizeBuffer(&buffer);
|
2014-03-05 23:38:50 +04:00
|
|
|
if (NS_FAILED(rv = mBuilder->IsBroken())) {
|
|
|
|
break;
|
2012-01-20 15:16:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-25 18:05:45 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mTokenizer->eof();
|
|
|
|
}
|
2012-01-20 15:16:27 +04:00
|
|
|
mTokenizer->end();
|
2014-03-05 23:38:50 +04:00
|
|
|
mBuilder->Finish();
|
2012-01-20 15:16:27 +04:00
|
|
|
mAtomTable.Clear();
|
2014-03-05 23:38:50 +04:00
|
|
|
return rv;
|
2012-01-20 15:16:27 +04:00
|
|
|
}
|