Bug 1845715 - Check for failure when getting RegExp match result template r=iain
[gecko.git] / parser / html / nsHtml5Parser.cpp
blobf1916ca2d44dbd98284760de78047e96e5e4c158
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=2 et tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsHtml5Parser.h"
9 #include "mozilla/AutoRestore.h"
10 #include "mozilla/UniquePtr.h"
11 #include "nsCRT.h"
12 #include "nsContentUtils.h" // for kLoadAsData
13 #include "nsHtml5AtomTable.h"
14 #include "nsHtml5DependentUTF16Buffer.h"
15 #include "nsHtml5Tokenizer.h"
16 #include "nsHtml5TreeBuilder.h"
17 #include "nsNetUtil.h"
19 NS_INTERFACE_TABLE_HEAD(nsHtml5Parser)
20 NS_INTERFACE_TABLE(nsHtml5Parser, nsIParser, nsISupportsWeakReference)
21 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(nsHtml5Parser)
22 NS_INTERFACE_MAP_END
24 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHtml5Parser)
25 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHtml5Parser)
27 NS_IMPL_CYCLE_COLLECTION_CLASS(nsHtml5Parser)
29 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsHtml5Parser)
30 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExecutor)
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(GetStreamParser())
32 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
34 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsHtml5Parser)
35 NS_IMPL_CYCLE_COLLECTION_UNLINK(mExecutor)
36 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
37 tmp->DropStreamParser();
38 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
40 nsHtml5Parser::nsHtml5Parser()
41 : mLastWasCR(false),
42 mDocWriteSpeculativeLastWasCR(false),
43 mBlocked(0),
44 mDocWriteSpeculatorActive(false),
45 mScriptNestingLevel(0),
46 mDocumentClosed(false),
47 mInDocumentWrite(false),
48 mInsertionPointPermanentlyUndefined(false),
49 mFirstBuffer(new nsHtml5OwningUTF16Buffer((void*)nullptr)),
50 mLastBuffer(mFirstBuffer),
51 mExecutor(new nsHtml5TreeOpExecutor()),
52 mTreeBuilder(new nsHtml5TreeBuilder(mExecutor, nullptr, false)),
53 mTokenizer(new nsHtml5Tokenizer(mTreeBuilder.get(), false)),
54 mRootContextLineNumber(1),
55 mReturnToStreamParserPermitted(false) {
56 mTokenizer->setInterner(&mAtomTable);
59 nsHtml5Parser::~nsHtml5Parser() {
60 mTokenizer->end();
61 if (mDocWriteSpeculativeTokenizer) {
62 mDocWriteSpeculativeTokenizer->end();
66 NS_IMETHODIMP_(void)
67 nsHtml5Parser::SetContentSink(nsIContentSink* aSink) {
68 NS_ASSERTION(aSink == static_cast<nsIContentSink*>(mExecutor),
69 "Attempt to set a foreign sink.");
72 NS_IMETHODIMP_(nsIContentSink*)
73 nsHtml5Parser::GetContentSink() {
74 return static_cast<nsIContentSink*>(mExecutor);
77 NS_IMETHODIMP_(void)
78 nsHtml5Parser::GetCommand(nsCString& aCommand) {
79 aCommand.AssignLiteral("view");
82 NS_IMETHODIMP_(void)
83 nsHtml5Parser::SetCommand(const char* aCommand) {
84 NS_ASSERTION(!strcmp(aCommand, "view") || !strcmp(aCommand, "view-source") ||
85 !strcmp(aCommand, "external-resource") ||
86 !strcmp(aCommand, "import") ||
87 !strcmp(aCommand, kLoadAsData),
88 "Unsupported parser command");
91 NS_IMETHODIMP_(void)
92 nsHtml5Parser::SetCommand(eParserCommands aParserCommand) {
93 NS_ASSERTION(aParserCommand == eViewNormal,
94 "Parser command was not eViewNormal.");
97 void nsHtml5Parser::SetDocumentCharset(NotNull<const Encoding*> aEncoding,
98 int32_t aCharsetSource,
99 bool aForceAutoDetection) {
100 MOZ_ASSERT(!mExecutor->HasStarted(), "Document charset set too late.");
101 MOZ_ASSERT(GetStreamParser(), "Setting charset on a script-only parser.");
102 GetStreamParser()->SetDocumentCharset(
103 aEncoding, (nsCharsetSource)aCharsetSource, aForceAutoDetection);
104 mExecutor->SetDocumentCharsetAndSource(aEncoding,
105 (nsCharsetSource)aCharsetSource);
108 nsresult nsHtml5Parser::GetChannel(nsIChannel** aChannel) {
109 if (GetStreamParser()) {
110 return GetStreamParser()->GetChannel(aChannel);
111 } else {
112 return NS_ERROR_NOT_AVAILABLE;
116 nsIStreamListener* nsHtml5Parser::GetStreamListener() {
117 return mStreamListener;
120 NS_IMETHODIMP
121 nsHtml5Parser::ContinueInterruptedParsing() {
122 MOZ_ASSERT_UNREACHABLE("Don't call. For interface compat only.");
123 return NS_ERROR_NOT_IMPLEMENTED;
126 NS_IMETHODIMP_(void)
127 nsHtml5Parser::BlockParser() { mBlocked++; }
129 NS_IMETHODIMP_(void)
130 nsHtml5Parser::UnblockParser() {
131 MOZ_DIAGNOSTIC_ASSERT(mBlocked > 0);
132 if (MOZ_LIKELY(mBlocked > 0)) {
133 mBlocked--;
135 if (MOZ_LIKELY(mBlocked == 0) && mExecutor) {
136 mExecutor->ContinueInterruptedParsingAsync();
140 NS_IMETHODIMP_(void)
141 nsHtml5Parser::ContinueInterruptedParsingAsync() {
142 if (mExecutor) {
143 mExecutor->ContinueInterruptedParsingAsync();
147 NS_IMETHODIMP_(bool)
148 nsHtml5Parser::IsParserEnabled() { return !mBlocked; }
150 NS_IMETHODIMP_(bool)
151 nsHtml5Parser::IsComplete() { return mExecutor->IsComplete(); }
153 NS_IMETHODIMP
154 nsHtml5Parser::Parse(nsIURI* aURL) {
156 * Do NOT cause WillBuildModel to be called synchronously from here!
157 * The document won't be ready for it until OnStartRequest!
159 MOZ_ASSERT(!mExecutor->HasStarted(),
160 "Tried to start parse without initializing the parser.");
161 MOZ_ASSERT(GetStreamParser(),
162 "Can't call this Parse() variant on script-created parser");
164 GetStreamParser()->SetViewSourceTitle(aURL); // In case we're viewing source
165 mExecutor->SetStreamParser(GetStreamParser());
166 mExecutor->SetParser(this);
167 return NS_OK;
170 nsresult nsHtml5Parser::Parse(const nsAString& aSourceBuffer, void* aKey,
171 bool aLastCall) {
172 nsresult rv;
173 if (NS_FAILED(rv = mExecutor->IsBroken())) {
174 return rv;
176 if (aSourceBuffer.Length() > INT32_MAX) {
177 return mExecutor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
180 // Maintain a reference to ourselves so we don't go away
181 // till we're completely done. The old parser grips itself in this method.
182 nsCOMPtr<nsIParser> kungFuDeathGrip(this);
184 // Gripping the other objects just in case, since the other old grip
185 // required grips to these, too.
186 RefPtr<nsHtml5StreamParser> streamKungFuDeathGrip(GetStreamParser());
187 mozilla::Unused << streamKungFuDeathGrip; // Not used within function
188 RefPtr<nsHtml5TreeOpExecutor> executor(mExecutor);
190 MOZ_RELEASE_ASSERT(executor->HasStarted());
192 // Return early if the parser has processed EOF
193 if (executor->IsComplete()) {
194 return NS_OK;
197 if (aLastCall && aSourceBuffer.IsEmpty() && !aKey) {
198 // document.close()
199 NS_ASSERTION(!GetStreamParser(),
200 "Had stream parser but got document.close().");
201 if (mDocumentClosed) {
202 // already closed
203 return NS_OK;
205 mDocumentClosed = true;
206 if (!mBlocked && !mInDocumentWrite) {
207 return ParseUntilBlocked();
209 return NS_OK;
212 // If we got this far, we are dealing with a document.write or
213 // document.writeln call--not document.close().
215 MOZ_RELEASE_ASSERT(
216 IsInsertionPointDefined(),
217 "Doc.write reached parser with undefined insertion point.");
219 MOZ_RELEASE_ASSERT(!(GetStreamParser() && !aKey),
220 "Got a null key in a non-script-created parser");
222 // XXX is this optimization bogus?
223 if (aSourceBuffer.IsEmpty()) {
224 return NS_OK;
227 // This guard is here to prevent document.close from tokenizing synchronously
228 // while a document.write (that wrote the script that called document.close!)
229 // is still on the call stack.
230 mozilla::AutoRestore<bool> guard(mInDocumentWrite);
231 mInDocumentWrite = true;
233 // The script is identified by aKey. If there's nothing in the buffer
234 // chain for that key, we'll insert at the head of the queue.
235 // When the script leaves something in the queue, a zero-length
236 // key-holder "buffer" is inserted in the queue. If the same script
237 // leaves something in the chain again, it will be inserted immediately
238 // before the old key holder belonging to the same script.
240 // We don't do the actual data insertion yet in the hope that the data gets
241 // tokenized and there no data or less data to copy to the heap after
242 // tokenization. Also, this way, we avoid inserting one empty data buffer
243 // per document.write, which matters for performance when the parser isn't
244 // blocked and a badly-authored script calls document.write() once per
245 // input character. (As seen in a benchmark!)
247 // The insertion into the input stream happens conceptually before anything
248 // gets tokenized. To make sure multi-level document.write works right,
249 // it's necessary to establish the location of our parser key up front
250 // in case this is the first write with this key.
252 // In a document.open() case, the first write level has a null key, so that
253 // case is handled separately, because normal buffers containing data
254 // have null keys.
256 // These don't need to be owning references, because they always point to
257 // the buffer queue and buffers can't be removed from the buffer queue
258 // before document.write() returns. The buffer queue clean-up happens the
259 // next time ParseUntilBlocked() is called.
260 // However, they are made owning just in case the reasoning above is flawed
261 // and a flaw would lead to worse problems with plain pointers. If this
262 // turns out to be a perf problem, it's worthwhile to consider making
263 // prevSearchbuf a plain pointer again.
264 RefPtr<nsHtml5OwningUTF16Buffer> prevSearchBuf;
265 RefPtr<nsHtml5OwningUTF16Buffer> firstLevelMarker;
267 if (aKey) {
268 if (mFirstBuffer == mLastBuffer) {
269 nsHtml5OwningUTF16Buffer* keyHolder = new nsHtml5OwningUTF16Buffer(aKey);
270 keyHolder->next = mLastBuffer;
271 mFirstBuffer = keyHolder;
272 } else if (mFirstBuffer->key != aKey) {
273 prevSearchBuf = mFirstBuffer;
274 for (;;) {
275 if (prevSearchBuf->next == mLastBuffer) {
276 // key was not found
277 nsHtml5OwningUTF16Buffer* keyHolder =
278 new nsHtml5OwningUTF16Buffer(aKey);
279 keyHolder->next = mFirstBuffer;
280 mFirstBuffer = keyHolder;
281 prevSearchBuf = nullptr;
282 break;
284 if (prevSearchBuf->next->key == aKey) {
285 // found a key holder
286 break;
288 prevSearchBuf = prevSearchBuf->next;
290 } // else mFirstBuffer is the keyholder
292 // prevSearchBuf is the previous buffer before the keyholder or null if
293 // there isn't one.
294 } else {
295 // We have a first-level write in the document.open() case. We insert before
296 // mLastBuffer, effectively, by making mLastBuffer be a new sentinel object
297 // and redesignating the previous mLastBuffer as our firstLevelMarker. We
298 // need to put a marker there, because otherwise additional document.writes
299 // from nested event loops would insert in the wrong place. Sigh.
300 mLastBuffer->next = new nsHtml5OwningUTF16Buffer((void*)nullptr);
301 firstLevelMarker = mLastBuffer;
302 mLastBuffer = mLastBuffer->next;
305 nsHtml5DependentUTF16Buffer stackBuffer(aSourceBuffer);
307 while (!mBlocked && stackBuffer.hasMore()) {
308 stackBuffer.adjust(mLastWasCR);
309 mLastWasCR = false;
310 if (stackBuffer.hasMore()) {
311 int32_t lineNumberSave;
312 bool inRootContext = (!GetStreamParser() && !aKey);
313 if (inRootContext) {
314 mTokenizer->setLineNumber(mRootContextLineNumber);
315 } else {
316 // we aren't the root context, so save the line number on the
317 // *stack* so that we can restore it.
318 lineNumberSave = mTokenizer->getLineNumber();
321 if (!mTokenizer->EnsureBufferSpace(stackBuffer.getLength())) {
322 return executor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
324 mLastWasCR = mTokenizer->tokenizeBuffer(&stackBuffer);
325 if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
326 return executor->MarkAsBroken(rv);
329 if (inRootContext) {
330 mRootContextLineNumber = mTokenizer->getLineNumber();
331 } else {
332 mTokenizer->setLineNumber(lineNumberSave);
335 if (mTreeBuilder->HasScript()) {
336 auto r = mTreeBuilder->Flush(); // Move ops to the executor
337 if (r.isErr()) {
338 return executor->MarkAsBroken(r.unwrapErr());
340 rv = executor->FlushDocumentWrite(); // run the ops
341 NS_ENSURE_SUCCESS(rv, rv);
342 // Flushing tree ops can cause all sorts of things.
343 // Return early if the parser got terminated.
344 if (executor->IsComplete()) {
345 return NS_OK;
348 // Ignore suspension requests
352 RefPtr<nsHtml5OwningUTF16Buffer> heapBuffer;
353 if (stackBuffer.hasMore()) {
354 // The buffer wasn't tokenized to completion. Create a copy of the tail
355 // on the heap.
356 heapBuffer = stackBuffer.FalliblyCopyAsOwningBuffer();
357 if (!heapBuffer) {
358 // Allocation failed. The parser is now broken.
359 return executor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
363 if (heapBuffer) {
364 // We have something to insert before the keyholder holding in the non-null
365 // aKey case and we have something to swap into firstLevelMarker in the
366 // null aKey case.
367 if (aKey) {
368 NS_ASSERTION(mFirstBuffer != mLastBuffer, "Where's the keyholder?");
369 // the key holder is still somewhere further down the list from
370 // prevSearchBuf (which may be null)
371 if (mFirstBuffer->key == aKey) {
372 NS_ASSERTION(
373 !prevSearchBuf,
374 "Non-null prevSearchBuf when mFirstBuffer is the key holder?");
375 heapBuffer->next = mFirstBuffer;
376 mFirstBuffer = heapBuffer;
377 } else {
378 if (!prevSearchBuf) {
379 prevSearchBuf = mFirstBuffer;
381 // We created a key holder earlier, so we will find it without walking
382 // past the end of the list.
383 while (prevSearchBuf->next->key != aKey) {
384 prevSearchBuf = prevSearchBuf->next;
386 heapBuffer->next = prevSearchBuf->next;
387 prevSearchBuf->next = heapBuffer;
389 } else {
390 NS_ASSERTION(firstLevelMarker, "How come we don't have a marker.");
391 firstLevelMarker->Swap(heapBuffer);
395 if (!mBlocked) { // buffer was tokenized to completion
396 NS_ASSERTION(!stackBuffer.hasMore(),
397 "Buffer wasn't tokenized to completion?");
398 // Scripting semantics require a forced tree builder flush here
399 auto r = mTreeBuilder->Flush(); // Move ops to the executor
400 if (r.isErr()) {
401 return executor->MarkAsBroken(r.unwrapErr());
403 rv = executor->FlushDocumentWrite(); // run the ops
404 NS_ENSURE_SUCCESS(rv, rv);
405 } else if (stackBuffer.hasMore()) {
406 // The buffer wasn't tokenized to completion. Tokenize the untokenized
407 // content in order to preload stuff. This content will be retokenized
408 // later for normal parsing.
409 if (!mDocWriteSpeculatorActive) {
410 mDocWriteSpeculatorActive = true;
411 if (!mDocWriteSpeculativeTreeBuilder) {
412 // Lazily initialize if uninitialized
413 mDocWriteSpeculativeTreeBuilder =
414 mozilla::MakeUnique<nsHtml5TreeBuilder>(nullptr,
415 executor->GetStage(), true);
416 mDocWriteSpeculativeTreeBuilder->setScriptingEnabled(
417 mTreeBuilder->isScriptingEnabled());
418 mDocWriteSpeculativeTokenizer = mozilla::MakeUnique<nsHtml5Tokenizer>(
419 mDocWriteSpeculativeTreeBuilder.get(), false);
420 mDocWriteSpeculativeTokenizer->setInterner(&mAtomTable);
421 mDocWriteSpeculativeTokenizer->start();
423 mDocWriteSpeculativeTokenizer->resetToDataState();
424 mDocWriteSpeculativeTreeBuilder->loadState(mTreeBuilder.get());
425 mDocWriteSpeculativeLastWasCR = false;
428 // Note that with multilevel document.write if we didn't just activate the
429 // speculator, it's possible that the speculator is now in the wrong state.
430 // That's OK for the sake of simplicity. The worst that can happen is
431 // that the speculative loads aren't exactly right. The content will be
432 // reparsed anyway for non-preload purposes.
434 // The buffer position for subsequent non-speculative parsing now lives
435 // in heapBuffer, so it's ok to let the buffer position of stackBuffer
436 // to be overwritten and not restored below.
437 while (stackBuffer.hasMore()) {
438 stackBuffer.adjust(mDocWriteSpeculativeLastWasCR);
439 if (stackBuffer.hasMore()) {
440 if (!mDocWriteSpeculativeTokenizer->EnsureBufferSpace(
441 stackBuffer.getLength())) {
442 return executor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
444 mDocWriteSpeculativeLastWasCR =
445 mDocWriteSpeculativeTokenizer->tokenizeBuffer(&stackBuffer);
446 nsresult rv;
447 if (NS_FAILED((rv = mDocWriteSpeculativeTreeBuilder->IsBroken()))) {
448 return executor->MarkAsBroken(rv);
453 auto r = mDocWriteSpeculativeTreeBuilder->Flush();
454 if (r.isErr()) {
455 return executor->MarkAsBroken(r.unwrapErr());
457 mDocWriteSpeculativeTreeBuilder->DropHandles();
458 executor->FlushSpeculativeLoads();
461 return NS_OK;
464 NS_IMETHODIMP
465 nsHtml5Parser::Terminate() {
466 // Prevent a second call to DidBuildModel via document.close()
467 mDocumentClosed = true;
468 // We should only call DidBuildModel once, so don't do anything if this is
469 // the second time that Terminate has been called.
470 if (mExecutor->IsComplete()) {
471 return NS_OK;
473 // XXX - [ until we figure out a way to break parser-sink circularity ]
474 // Hack - Hold a reference until we are completely done...
475 nsCOMPtr<nsIParser> kungFuDeathGrip(this);
476 RefPtr<nsHtml5StreamParser> streamParser(GetStreamParser());
477 RefPtr<nsHtml5TreeOpExecutor> executor(mExecutor);
478 if (streamParser) {
479 streamParser->Terminate();
481 return executor->DidBuildModel(true);
484 bool nsHtml5Parser::IsInsertionPointDefined() {
485 return !mExecutor->IsFlushing() && !mInsertionPointPermanentlyUndefined &&
486 (!GetStreamParser() || mScriptNestingLevel != 0);
489 void nsHtml5Parser::IncrementScriptNestingLevel() { ++mScriptNestingLevel; }
491 void nsHtml5Parser::DecrementScriptNestingLevel() { --mScriptNestingLevel; }
493 bool nsHtml5Parser::HasNonzeroScriptNestingLevel() const {
494 return mScriptNestingLevel != 0;
497 void nsHtml5Parser::MarkAsNotScriptCreated(const char* aCommand) {
498 MOZ_ASSERT(!mStreamListener, "Must not call this twice.");
499 eParserMode mode = NORMAL;
500 if (!nsCRT::strcmp(aCommand, "view-source")) {
501 mode = VIEW_SOURCE_HTML;
502 } else if (!nsCRT::strcmp(aCommand, "view-source-xml")) {
503 mode = VIEW_SOURCE_XML;
504 } else if (!nsCRT::strcmp(aCommand, "view-source-plain")) {
505 mode = VIEW_SOURCE_PLAIN;
506 } else if (!nsCRT::strcmp(aCommand, "plain-text")) {
507 mode = PLAIN_TEXT;
508 } else if (!nsCRT::strcmp(aCommand, kLoadAsData)) {
509 mode = LOAD_AS_DATA;
511 #ifdef DEBUG
512 else {
513 NS_ASSERTION(!nsCRT::strcmp(aCommand, "view") ||
514 !nsCRT::strcmp(aCommand, "external-resource") ||
515 !nsCRT::strcmp(aCommand, "import"),
516 "Unsupported parser command!");
518 #endif
519 mStreamListener =
520 new nsHtml5StreamListener(new nsHtml5StreamParser(mExecutor, this, mode));
523 bool nsHtml5Parser::IsScriptCreated() { return !GetStreamParser(); }
525 /* End nsIParser */
527 // not from interface
528 nsresult nsHtml5Parser::ParseUntilBlocked() {
529 nsresult rv = mExecutor->IsBroken();
530 NS_ENSURE_SUCCESS(rv, rv);
531 if (mBlocked || mInsertionPointPermanentlyUndefined ||
532 mExecutor->IsComplete()) {
533 return NS_OK;
535 NS_ASSERTION(mExecutor->HasStarted(), "Bad life cycle.");
536 NS_ASSERTION(!mInDocumentWrite,
537 "ParseUntilBlocked entered while in doc.write!");
539 mDocWriteSpeculatorActive = false;
541 for (;;) {
542 if (!mFirstBuffer->hasMore()) {
543 if (mFirstBuffer == mLastBuffer) {
544 if (mExecutor->IsComplete()) {
545 // something like cache manisfests stopped the parse in mid-flight
546 return NS_OK;
548 if (mDocumentClosed) {
549 PermanentlyUndefineInsertionPoint();
550 nsresult rv;
551 MOZ_RELEASE_ASSERT(
552 !GetStreamParser(),
553 "This should only happen with script-created parser.");
554 if (NS_SUCCEEDED((rv = mExecutor->IsBroken()))) {
555 mTokenizer->eof();
556 if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
557 mExecutor->MarkAsBroken(rv);
558 } else {
559 mTreeBuilder->StreamEnded();
562 auto r = mTreeBuilder->Flush();
563 if (r.isErr()) {
564 return mExecutor->MarkAsBroken(r.unwrapErr());
566 mExecutor->FlushDocumentWrite();
567 // The below call does memory cleanup, so call it even if the
568 // parser has been marked as broken.
569 mTokenizer->end();
570 return rv;
572 // never release the last buffer.
573 NS_ASSERTION(!mLastBuffer->getStart() && !mLastBuffer->getEnd(),
574 "Sentinel buffer had its indeces changed.");
575 if (GetStreamParser()) {
576 if (mReturnToStreamParserPermitted &&
577 !mExecutor->IsScriptExecuting()) {
578 auto r = mTreeBuilder->Flush();
579 if (r.isErr()) {
580 return mExecutor->MarkAsBroken(r.unwrapErr());
582 mReturnToStreamParserPermitted = false;
583 GetStreamParser()->ContinueAfterScriptsOrEncodingCommitment(
584 mTokenizer.get(), mTreeBuilder.get(), mLastWasCR);
586 } else {
587 // Script-created parser
588 auto r = mTreeBuilder->Flush();
589 if (r.isErr()) {
590 return mExecutor->MarkAsBroken(r.unwrapErr());
592 // No need to flush the executor, because the executor is already
593 // in a flush
594 NS_ASSERTION(mExecutor->IsInFlushLoop(),
595 "How did we come here without being in the flush loop?");
597 return NS_OK; // no more data for now but expecting more
599 mFirstBuffer = mFirstBuffer->next;
600 continue;
603 if (mBlocked || mExecutor->IsComplete()) {
604 return NS_OK;
607 // now we have a non-empty buffer
608 mFirstBuffer->adjust(mLastWasCR);
609 mLastWasCR = false;
610 if (mFirstBuffer->hasMore()) {
611 bool inRootContext = (!GetStreamParser() && !mFirstBuffer->key);
612 if (inRootContext) {
613 mTokenizer->setLineNumber(mRootContextLineNumber);
615 if (!mTokenizer->EnsureBufferSpace(mFirstBuffer->getLength())) {
616 return mExecutor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
618 mLastWasCR = mTokenizer->tokenizeBuffer(mFirstBuffer);
619 nsresult rv;
620 if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
621 return mExecutor->MarkAsBroken(rv);
623 if (inRootContext) {
624 mRootContextLineNumber = mTokenizer->getLineNumber();
626 if (mTreeBuilder->HasScript()) {
627 auto r = mTreeBuilder->Flush();
628 if (r.isErr()) {
629 return mExecutor->MarkAsBroken(r.unwrapErr());
631 rv = mExecutor->FlushDocumentWrite();
632 NS_ENSURE_SUCCESS(rv, rv);
634 if (mBlocked) {
635 return NS_OK;
641 nsresult nsHtml5Parser::StartExecutor() {
642 MOZ_ASSERT(!GetStreamParser(),
643 "Had stream parser but document.write started life cycle.");
644 // This is part of the setup document.open() does.
645 RefPtr<nsHtml5TreeOpExecutor> executor(mExecutor);
646 executor->SetParser(this);
647 mTreeBuilder->setScriptingEnabled(executor->IsScriptEnabled());
649 mTreeBuilder->setIsSrcdocDocument(false);
651 mTokenizer->start();
652 executor->Start();
655 * We know we're in document.open(), so our document must already
656 * have a script global andthe WillBuildModel call is safe.
658 return executor->WillBuildModel();
661 nsresult nsHtml5Parser::Initialize(mozilla::dom::Document* aDoc, nsIURI* aURI,
662 nsISupports* aContainer,
663 nsIChannel* aChannel) {
664 return mExecutor->Init(aDoc, aURI, aContainer, aChannel);
667 void nsHtml5Parser::StartTokenizer(bool aScriptingEnabled) {
668 bool isSrcdoc = false;
669 nsCOMPtr<nsIChannel> channel;
670 nsresult rv = GetChannel(getter_AddRefs(channel));
671 if (NS_SUCCEEDED(rv)) {
672 isSrcdoc = NS_IsSrcdocChannel(channel);
674 mTreeBuilder->setIsSrcdocDocument(isSrcdoc);
676 mTreeBuilder->SetPreventScriptExecution(!aScriptingEnabled);
677 mTreeBuilder->setScriptingEnabled(aScriptingEnabled);
678 mTokenizer->start();
681 void nsHtml5Parser::InitializeDocWriteParserState(
682 nsAHtml5TreeBuilderState* aState, int32_t aLine) {
683 mTokenizer->resetToDataState();
684 mTokenizer->setLineNumber(aLine);
685 mTreeBuilder->loadState(aState);
686 mLastWasCR = false;
687 mReturnToStreamParserPermitted = true;
690 void nsHtml5Parser::ContinueAfterFailedCharsetSwitch() {
691 MOZ_ASSERT(
692 GetStreamParser(),
693 "Tried to continue after failed charset switch without a stream parser");
694 GetStreamParser()->ContinueAfterFailedCharsetSwitch();