parser/html/nsHtml5StringParser.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "nsHtml5StringParser.h"
michael@0 6 #include "nsHtml5TreeOpExecutor.h"
michael@0 7 #include "nsHtml5TreeBuilder.h"
michael@0 8 #include "nsHtml5Tokenizer.h"
michael@0 9 #include "nsIContent.h"
michael@0 10 #include "nsIDocument.h"
michael@0 11 #include "nsIDOMDocumentFragment.h"
michael@0 12 #include "nsHtml5DependentUTF16Buffer.h"
michael@0 13
michael@0 14 NS_IMPL_ISUPPORTS0(nsHtml5StringParser)
michael@0 15
michael@0 16 nsHtml5StringParser::nsHtml5StringParser()
michael@0 17 : mBuilder(new nsHtml5OplessBuilder())
michael@0 18 , mTreeBuilder(new nsHtml5TreeBuilder(mBuilder))
michael@0 19 , mTokenizer(new nsHtml5Tokenizer(mTreeBuilder, false))
michael@0 20 {
michael@0 21 MOZ_COUNT_CTOR(nsHtml5StringParser);
michael@0 22 mTokenizer->setInterner(&mAtomTable);
michael@0 23 }
michael@0 24
michael@0 25 nsHtml5StringParser::~nsHtml5StringParser()
michael@0 26 {
michael@0 27 MOZ_COUNT_DTOR(nsHtml5StringParser);
michael@0 28 }
michael@0 29
michael@0 30 nsresult
michael@0 31 nsHtml5StringParser::ParseFragment(const nsAString& aSourceBuffer,
michael@0 32 nsIContent* aTargetNode,
michael@0 33 nsIAtom* aContextLocalName,
michael@0 34 int32_t aContextNamespace,
michael@0 35 bool aQuirks,
michael@0 36 bool aPreventScriptExecution)
michael@0 37 {
michael@0 38 NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX,
michael@0 39 NS_ERROR_OUT_OF_MEMORY);
michael@0 40
michael@0 41 nsIDocument* doc = aTargetNode->OwnerDoc();
michael@0 42 nsIURI* uri = doc->GetDocumentURI();
michael@0 43 NS_ENSURE_TRUE(uri, NS_ERROR_NOT_AVAILABLE);
michael@0 44
michael@0 45 mTreeBuilder->setFragmentContext(aContextLocalName,
michael@0 46 aContextNamespace,
michael@0 47 aTargetNode,
michael@0 48 aQuirks);
michael@0 49
michael@0 50 #ifdef DEBUG
michael@0 51 if (!aPreventScriptExecution) {
michael@0 52 NS_ASSERTION(!aTargetNode->IsInDoc(),
michael@0 53 "If script execution isn't prevented, "
michael@0 54 "the target node must not be in doc.");
michael@0 55 nsCOMPtr<nsIDOMDocumentFragment> domFrag = do_QueryInterface(aTargetNode);
michael@0 56 NS_ASSERTION(domFrag,
michael@0 57 "If script execution isn't prevented, must parse to DOM fragment.");
michael@0 58 }
michael@0 59 #endif
michael@0 60
michael@0 61 mTreeBuilder->SetPreventScriptExecution(aPreventScriptExecution);
michael@0 62
michael@0 63 return Tokenize(aSourceBuffer, doc, true);
michael@0 64 }
michael@0 65
michael@0 66 nsresult
michael@0 67 nsHtml5StringParser::ParseDocument(const nsAString& aSourceBuffer,
michael@0 68 nsIDocument* aTargetDoc,
michael@0 69 bool aScriptingEnabledForNoscriptParsing)
michael@0 70 {
michael@0 71 MOZ_ASSERT(!aTargetDoc->GetFirstChild());
michael@0 72
michael@0 73 NS_ENSURE_TRUE(aSourceBuffer.Length() <= INT32_MAX,
michael@0 74 NS_ERROR_OUT_OF_MEMORY);
michael@0 75
michael@0 76 mTreeBuilder->setFragmentContext(nullptr,
michael@0 77 kNameSpaceID_None,
michael@0 78 nullptr,
michael@0 79 false);
michael@0 80
michael@0 81 mTreeBuilder->SetPreventScriptExecution(true);
michael@0 82
michael@0 83 return Tokenize(aSourceBuffer, aTargetDoc, aScriptingEnabledForNoscriptParsing);
michael@0 84 }
michael@0 85
michael@0 86 nsresult
michael@0 87 nsHtml5StringParser::Tokenize(const nsAString& aSourceBuffer,
michael@0 88 nsIDocument* aDocument,
michael@0 89 bool aScriptingEnabledForNoscriptParsing) {
michael@0 90
michael@0 91 nsIURI* uri = aDocument->GetDocumentURI();
michael@0 92
michael@0 93 mBuilder->Init(aDocument, uri, nullptr, nullptr);
michael@0 94
michael@0 95 mBuilder->SetParser(this);
michael@0 96 mBuilder->SetNodeInfoManager(aDocument->NodeInfoManager());
michael@0 97
michael@0 98 // Mark the parser as *not* broken by passing NS_OK
michael@0 99 nsresult rv = mBuilder->MarkAsBroken(NS_OK);
michael@0 100
michael@0 101 mTreeBuilder->setScriptingEnabled(aScriptingEnabledForNoscriptParsing);
michael@0 102 mTreeBuilder->setIsSrcdocDocument(aDocument->IsSrcdocDocument());
michael@0 103 mBuilder->Start();
michael@0 104 mTokenizer->start();
michael@0 105 if (!aSourceBuffer.IsEmpty()) {
michael@0 106 bool lastWasCR = false;
michael@0 107 nsHtml5DependentUTF16Buffer buffer(aSourceBuffer);
michael@0 108 while (buffer.hasMore()) {
michael@0 109 buffer.adjust(lastWasCR);
michael@0 110 lastWasCR = false;
michael@0 111 if (buffer.hasMore()) {
michael@0 112 lastWasCR = mTokenizer->tokenizeBuffer(&buffer);
michael@0 113 if (NS_FAILED(rv = mBuilder->IsBroken())) {
michael@0 114 break;
michael@0 115 }
michael@0 116 }
michael@0 117 }
michael@0 118 }
michael@0 119 mTokenizer->eof();
michael@0 120 mTokenizer->end();
michael@0 121 mBuilder->Finish();
michael@0 122 mAtomTable.Clear();
michael@0 123 return rv;
michael@0 124 }

mercurial