Backed out 2 changesets (bug 1886730) for causing detekt & klint failures CLOSED...
[gecko.git] / parser / html / nsHtml5Parser.cpp
blob04097e56d931cbe3f7ab528c6ee4432c792c8acf
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::IsParserClosed() { return mDocumentClosed; }
153 NS_IMETHODIMP_(bool)
154 nsHtml5Parser::IsComplete() { return mExecutor->IsComplete(); }
156 NS_IMETHODIMP
157 nsHtml5Parser::Parse(nsIURI* aURL) {
159 * Do NOT cause WillBuildModel to be called synchronously from here!
160 * The document won't be ready for it until OnStartRequest!
162 MOZ_ASSERT(!mExecutor->HasStarted(),
163 "Tried to start parse without initializing the parser.");
164 MOZ_ASSERT(GetStreamParser(),
165 "Can't call this Parse() variant on script-created parser");
167 GetStreamParser()->SetViewSourceTitle(aURL); // In case we're viewing source
168 mExecutor->SetStreamParser(GetStreamParser());
169 mExecutor->SetParser(this);
170 return NS_OK;
173 nsresult nsHtml5Parser::Parse(const nsAString& aSourceBuffer, void* aKey,
174 bool aLastCall) {
175 nsresult rv;
176 if (NS_FAILED(rv = mExecutor->IsBroken())) {
177 return rv;
179 if (aSourceBuffer.Length() > INT32_MAX) {
180 return mExecutor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
183 // Maintain a reference to ourselves so we don't go away
184 // till we're completely done. The old parser grips itself in this method.
185 nsCOMPtr<nsIParser> kungFuDeathGrip(this);
187 // Gripping the other objects just in case, since the other old grip
188 // required grips to these, too.
189 RefPtr<nsHtml5StreamParser> streamKungFuDeathGrip(GetStreamParser());
190 mozilla::Unused << streamKungFuDeathGrip; // Not used within function
191 RefPtr<nsHtml5TreeOpExecutor> executor(mExecutor);
193 MOZ_RELEASE_ASSERT(executor->HasStarted());
195 // Return early if the parser has processed EOF
196 if (executor->IsComplete()) {
197 return NS_OK;
200 if (aLastCall && aSourceBuffer.IsEmpty() && !aKey) {
201 // document.close()
202 NS_ASSERTION(!GetStreamParser(),
203 "Had stream parser but got document.close().");
204 if (mDocumentClosed) {
205 // already closed
206 return NS_OK;
208 mDocumentClosed = true;
209 if (!mBlocked && !mInDocumentWrite) {
210 return ParseUntilBlocked();
212 return NS_OK;
215 // If we got this far, we are dealing with a document.write or
216 // document.writeln call--not document.close().
218 MOZ_RELEASE_ASSERT(
219 IsInsertionPointDefined(),
220 "Doc.write reached parser with undefined insertion point.");
222 MOZ_RELEASE_ASSERT(!(GetStreamParser() && !aKey),
223 "Got a null key in a non-script-created parser");
225 // XXX is this optimization bogus?
226 if (aSourceBuffer.IsEmpty()) {
227 return NS_OK;
230 // This guard is here to prevent document.close from tokenizing synchronously
231 // while a document.write (that wrote the script that called document.close!)
232 // is still on the call stack.
233 mozilla::AutoRestore<bool> guard(mInDocumentWrite);
234 mInDocumentWrite = true;
236 // The script is identified by aKey. If there's nothing in the buffer
237 // chain for that key, we'll insert at the head of the queue.
238 // When the script leaves something in the queue, a zero-length
239 // key-holder "buffer" is inserted in the queue. If the same script
240 // leaves something in the chain again, it will be inserted immediately
241 // before the old key holder belonging to the same script.
243 // We don't do the actual data insertion yet in the hope that the data gets
244 // tokenized and there no data or less data to copy to the heap after
245 // tokenization. Also, this way, we avoid inserting one empty data buffer
246 // per document.write, which matters for performance when the parser isn't
247 // blocked and a badly-authored script calls document.write() once per
248 // input character. (As seen in a benchmark!)
250 // The insertion into the input stream happens conceptually before anything
251 // gets tokenized. To make sure multi-level document.write works right,
252 // it's necessary to establish the location of our parser key up front
253 // in case this is the first write with this key.
255 // In a document.open() case, the first write level has a null key, so that
256 // case is handled separately, because normal buffers containing data
257 // have null keys.
259 // These don't need to be owning references, because they always point to
260 // the buffer queue and buffers can't be removed from the buffer queue
261 // before document.write() returns. The buffer queue clean-up happens the
262 // next time ParseUntilBlocked() is called.
263 // However, they are made owning just in case the reasoning above is flawed
264 // and a flaw would lead to worse problems with plain pointers. If this
265 // turns out to be a perf problem, it's worthwhile to consider making
266 // prevSearchbuf a plain pointer again.
267 RefPtr<nsHtml5OwningUTF16Buffer> prevSearchBuf;
268 RefPtr<nsHtml5OwningUTF16Buffer> firstLevelMarker;
270 if (aKey) {
271 if (mFirstBuffer == mLastBuffer) {
272 nsHtml5OwningUTF16Buffer* keyHolder = new nsHtml5OwningUTF16Buffer(aKey);
273 keyHolder->next = mLastBuffer;
274 mFirstBuffer = keyHolder;
275 } else if (mFirstBuffer->key != aKey) {
276 prevSearchBuf = mFirstBuffer;
277 for (;;) {
278 if (prevSearchBuf->next == mLastBuffer) {
279 // key was not found
280 nsHtml5OwningUTF16Buffer* keyHolder =
281 new nsHtml5OwningUTF16Buffer(aKey);
282 keyHolder->next = mFirstBuffer;
283 mFirstBuffer = keyHolder;
284 prevSearchBuf = nullptr;
285 break;
287 if (prevSearchBuf->next->key == aKey) {
288 // found a key holder
289 break;
291 prevSearchBuf = prevSearchBuf->next;
293 } // else mFirstBuffer is the keyholder
295 // prevSearchBuf is the previous buffer before the keyholder or null if
296 // there isn't one.
297 } else {
298 // We have a first-level write in the document.open() case. We insert before
299 // mLastBuffer, effectively, by making mLastBuffer be a new sentinel object
300 // and redesignating the previous mLastBuffer as our firstLevelMarker. We
301 // need to put a marker there, because otherwise additional document.writes
302 // from nested event loops would insert in the wrong place. Sigh.
303 mLastBuffer->next = new nsHtml5OwningUTF16Buffer((void*)nullptr);
304 firstLevelMarker = mLastBuffer;
305 mLastBuffer = mLastBuffer->next;
308 nsHtml5DependentUTF16Buffer stackBuffer(aSourceBuffer);
310 while (!mBlocked && stackBuffer.hasMore()) {
311 stackBuffer.adjust(mLastWasCR);
312 mLastWasCR = false;
313 if (stackBuffer.hasMore()) {
314 int32_t lineNumberSave;
315 bool inRootContext = (!GetStreamParser() && !aKey);
316 if (inRootContext) {
317 mTokenizer->setLineNumber(mRootContextLineNumber);
318 } else {
319 // we aren't the root context, so save the line number on the
320 // *stack* so that we can restore it.
321 lineNumberSave = mTokenizer->getLineNumber();
324 if (!mTokenizer->EnsureBufferSpace(stackBuffer.getLength())) {
325 return executor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
327 mLastWasCR = mTokenizer->tokenizeBuffer(&stackBuffer);
328 if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
329 return executor->MarkAsBroken(rv);
332 if (inRootContext) {
333 mRootContextLineNumber = mTokenizer->getLineNumber();
334 } else {
335 mTokenizer->setLineNumber(lineNumberSave);
338 if (mTreeBuilder->HasScriptThatMayDocumentWriteOrBlock()) {
339 auto r = mTreeBuilder->Flush(); // Move ops to the executor
340 if (r.isErr()) {
341 return executor->MarkAsBroken(r.unwrapErr());
343 rv = executor->FlushDocumentWrite(); // run the ops
344 NS_ENSURE_SUCCESS(rv, rv);
345 // Flushing tree ops can cause all sorts of things.
346 // Return early if the parser got terminated.
347 if (executor->IsComplete()) {
348 return NS_OK;
351 // Ignore suspension requests
355 RefPtr<nsHtml5OwningUTF16Buffer> heapBuffer;
356 if (stackBuffer.hasMore()) {
357 // The buffer wasn't tokenized to completion. Create a copy of the tail
358 // on the heap.
359 heapBuffer = stackBuffer.FalliblyCopyAsOwningBuffer();
360 if (!heapBuffer) {
361 // Allocation failed. The parser is now broken.
362 return executor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
366 if (heapBuffer) {
367 // We have something to insert before the keyholder holding in the non-null
368 // aKey case and we have something to swap into firstLevelMarker in the
369 // null aKey case.
370 if (aKey) {
371 NS_ASSERTION(mFirstBuffer != mLastBuffer, "Where's the keyholder?");
372 // the key holder is still somewhere further down the list from
373 // prevSearchBuf (which may be null)
374 if (mFirstBuffer->key == aKey) {
375 NS_ASSERTION(
376 !prevSearchBuf,
377 "Non-null prevSearchBuf when mFirstBuffer is the key holder?");
378 heapBuffer->next = mFirstBuffer;
379 mFirstBuffer = heapBuffer;
380 } else {
381 if (!prevSearchBuf) {
382 prevSearchBuf = mFirstBuffer;
384 // We created a key holder earlier, so we will find it without walking
385 // past the end of the list.
386 while (prevSearchBuf->next->key != aKey) {
387 prevSearchBuf = prevSearchBuf->next;
389 heapBuffer->next = prevSearchBuf->next;
390 prevSearchBuf->next = heapBuffer;
392 } else {
393 NS_ASSERTION(firstLevelMarker, "How come we don't have a marker.");
394 firstLevelMarker->Swap(heapBuffer);
398 if (!mBlocked) { // buffer was tokenized to completion
399 NS_ASSERTION(!stackBuffer.hasMore(),
400 "Buffer wasn't tokenized to completion?");
401 // Scripting semantics require a forced tree builder flush here
402 auto r = mTreeBuilder->Flush(); // Move ops to the executor
403 if (r.isErr()) {
404 return executor->MarkAsBroken(r.unwrapErr());
406 rv = executor->FlushDocumentWrite(); // run the ops
407 NS_ENSURE_SUCCESS(rv, rv);
408 } else if (stackBuffer.hasMore()) {
409 // The buffer wasn't tokenized to completion. Tokenize the untokenized
410 // content in order to preload stuff. This content will be retokenized
411 // later for normal parsing.
412 if (!mDocWriteSpeculatorActive) {
413 mDocWriteSpeculatorActive = true;
414 if (!mDocWriteSpeculativeTreeBuilder) {
415 // Lazily initialize if uninitialized
416 mDocWriteSpeculativeTreeBuilder =
417 mozilla::MakeUnique<nsHtml5TreeBuilder>(nullptr,
418 executor->GetStage(), true);
419 mDocWriteSpeculativeTreeBuilder->setScriptingEnabled(
420 mTreeBuilder->isScriptingEnabled());
421 mDocWriteSpeculativeTokenizer = mozilla::MakeUnique<nsHtml5Tokenizer>(
422 mDocWriteSpeculativeTreeBuilder.get(), false);
423 mDocWriteSpeculativeTokenizer->setInterner(&mAtomTable);
424 mDocWriteSpeculativeTokenizer->start();
426 mDocWriteSpeculativeTokenizer->resetToDataState();
427 mDocWriteSpeculativeTreeBuilder->loadState(mTreeBuilder.get());
428 mDocWriteSpeculativeLastWasCR = false;
431 // Note that with multilevel document.write if we didn't just activate the
432 // speculator, it's possible that the speculator is now in the wrong state.
433 // That's OK for the sake of simplicity. The worst that can happen is
434 // that the speculative loads aren't exactly right. The content will be
435 // reparsed anyway for non-preload purposes.
437 // The buffer position for subsequent non-speculative parsing now lives
438 // in heapBuffer, so it's ok to let the buffer position of stackBuffer
439 // to be overwritten and not restored below.
440 while (stackBuffer.hasMore()) {
441 stackBuffer.adjust(mDocWriteSpeculativeLastWasCR);
442 if (stackBuffer.hasMore()) {
443 if (!mDocWriteSpeculativeTokenizer->EnsureBufferSpace(
444 stackBuffer.getLength())) {
445 return executor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
447 mDocWriteSpeculativeLastWasCR =
448 mDocWriteSpeculativeTokenizer->tokenizeBuffer(&stackBuffer);
449 nsresult rv;
450 if (NS_FAILED((rv = mDocWriteSpeculativeTreeBuilder->IsBroken()))) {
451 return executor->MarkAsBroken(rv);
456 auto r = mDocWriteSpeculativeTreeBuilder->Flush();
457 if (r.isErr()) {
458 return executor->MarkAsBroken(r.unwrapErr());
460 mDocWriteSpeculativeTreeBuilder->DropHandles();
461 executor->FlushSpeculativeLoads();
464 return NS_OK;
467 NS_IMETHODIMP
468 nsHtml5Parser::Terminate() {
469 // Prevent a second call to DidBuildModel via document.close()
470 mDocumentClosed = true;
471 // We should only call DidBuildModel once, so don't do anything if this is
472 // the second time that Terminate has been called.
473 if (mExecutor->IsComplete()) {
474 return NS_OK;
476 // XXX - [ until we figure out a way to break parser-sink circularity ]
477 // Hack - Hold a reference until we are completely done...
478 nsCOMPtr<nsIParser> kungFuDeathGrip(this);
479 RefPtr<nsHtml5StreamParser> streamParser(GetStreamParser());
480 RefPtr<nsHtml5TreeOpExecutor> executor(mExecutor);
481 if (streamParser) {
482 streamParser->Terminate();
484 return executor->DidBuildModel(true);
487 bool nsHtml5Parser::IsInsertionPointDefined() {
488 return !mExecutor->IsFlushing() && !mInsertionPointPermanentlyUndefined &&
489 (!GetStreamParser() || mScriptNestingLevel != 0);
492 void nsHtml5Parser::IncrementScriptNestingLevel() { ++mScriptNestingLevel; }
494 void nsHtml5Parser::DecrementScriptNestingLevel() { --mScriptNestingLevel; }
496 bool nsHtml5Parser::HasNonzeroScriptNestingLevel() const {
497 return mScriptNestingLevel != 0;
500 void nsHtml5Parser::MarkAsNotScriptCreated(const char* aCommand) {
501 MOZ_ASSERT(!mStreamListener, "Must not call this twice.");
502 eParserMode mode = NORMAL;
503 if (!nsCRT::strcmp(aCommand, "view-source")) {
504 mode = VIEW_SOURCE_HTML;
505 } else if (!nsCRT::strcmp(aCommand, "view-source-xml")) {
506 mode = VIEW_SOURCE_XML;
507 } else if (!nsCRT::strcmp(aCommand, "view-source-plain")) {
508 mode = VIEW_SOURCE_PLAIN;
509 } else if (!nsCRT::strcmp(aCommand, "plain-text")) {
510 mode = PLAIN_TEXT;
511 } else if (!nsCRT::strcmp(aCommand, kLoadAsData)) {
512 mode = LOAD_AS_DATA;
514 #ifdef DEBUG
515 else {
516 NS_ASSERTION(!nsCRT::strcmp(aCommand, "view") ||
517 !nsCRT::strcmp(aCommand, "external-resource") ||
518 !nsCRT::strcmp(aCommand, "import"),
519 "Unsupported parser command!");
521 #endif
522 mStreamListener =
523 new nsHtml5StreamListener(new nsHtml5StreamParser(mExecutor, this, mode));
526 bool nsHtml5Parser::IsScriptCreated() { return !GetStreamParser(); }
528 /* End nsIParser */
530 // not from interface
531 nsresult nsHtml5Parser::ParseUntilBlocked() {
532 nsresult rv = mExecutor->IsBroken();
533 NS_ENSURE_SUCCESS(rv, rv);
534 if (mBlocked || mInsertionPointPermanentlyUndefined ||
535 mExecutor->IsComplete()) {
536 return NS_OK;
538 NS_ASSERTION(mExecutor->HasStarted(), "Bad life cycle.");
539 NS_ASSERTION(!mInDocumentWrite,
540 "ParseUntilBlocked entered while in doc.write!");
542 mDocWriteSpeculatorActive = false;
544 for (;;) {
545 if (!mFirstBuffer->hasMore()) {
546 if (mFirstBuffer == mLastBuffer) {
547 if (mExecutor->IsComplete()) {
548 // something like cache manisfests stopped the parse in mid-flight
549 return NS_OK;
551 if (mDocumentClosed) {
552 PermanentlyUndefineInsertionPoint();
553 nsresult rv;
554 MOZ_RELEASE_ASSERT(
555 !GetStreamParser(),
556 "This should only happen with script-created parser.");
557 if (NS_SUCCEEDED((rv = mExecutor->IsBroken()))) {
558 mTokenizer->eof();
559 if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
560 mExecutor->MarkAsBroken(rv);
561 } else {
562 mTreeBuilder->StreamEnded();
565 auto r = mTreeBuilder->Flush();
566 if (r.isErr()) {
567 return mExecutor->MarkAsBroken(r.unwrapErr());
569 mExecutor->FlushDocumentWrite();
570 // The below call does memory cleanup, so call it even if the
571 // parser has been marked as broken.
572 mTokenizer->end();
573 return rv;
575 // never release the last buffer.
576 NS_ASSERTION(!mLastBuffer->getStart() && !mLastBuffer->getEnd(),
577 "Sentinel buffer had its indeces changed.");
578 if (GetStreamParser()) {
579 if (mReturnToStreamParserPermitted &&
580 !mExecutor->IsScriptExecuting()) {
581 auto r = mTreeBuilder->Flush();
582 if (r.isErr()) {
583 return mExecutor->MarkAsBroken(r.unwrapErr());
585 mReturnToStreamParserPermitted = false;
586 GetStreamParser()->ContinueAfterScriptsOrEncodingCommitment(
587 mTokenizer.get(), mTreeBuilder.get(), mLastWasCR);
589 } else {
590 // Script-created parser
591 auto r = mTreeBuilder->Flush();
592 if (r.isErr()) {
593 return mExecutor->MarkAsBroken(r.unwrapErr());
595 // No need to flush the executor, because the executor is already
596 // in a flush
597 NS_ASSERTION(mExecutor->IsInFlushLoop(),
598 "How did we come here without being in the flush loop?");
600 return NS_OK; // no more data for now but expecting more
602 mFirstBuffer = mFirstBuffer->next;
603 continue;
606 if (mBlocked || mExecutor->IsComplete()) {
607 return NS_OK;
610 // now we have a non-empty buffer
611 mFirstBuffer->adjust(mLastWasCR);
612 mLastWasCR = false;
613 if (mFirstBuffer->hasMore()) {
614 bool inRootContext = (!GetStreamParser() && !mFirstBuffer->key);
615 if (inRootContext) {
616 mTokenizer->setLineNumber(mRootContextLineNumber);
618 if (!mTokenizer->EnsureBufferSpace(mFirstBuffer->getLength())) {
619 return mExecutor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
621 mLastWasCR = mTokenizer->tokenizeBuffer(mFirstBuffer);
622 nsresult rv;
623 if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
624 return mExecutor->MarkAsBroken(rv);
626 if (inRootContext) {
627 mRootContextLineNumber = mTokenizer->getLineNumber();
629 if (mTreeBuilder->HasScriptThatMayDocumentWriteOrBlock()) {
630 auto r = mTreeBuilder->Flush();
631 if (r.isErr()) {
632 return mExecutor->MarkAsBroken(r.unwrapErr());
634 rv = mExecutor->FlushDocumentWrite();
635 NS_ENSURE_SUCCESS(rv, rv);
637 if (mBlocked) {
638 return NS_OK;
644 nsresult nsHtml5Parser::StartExecutor() {
645 MOZ_ASSERT(!GetStreamParser(),
646 "Had stream parser but document.write started life cycle.");
647 // This is part of the setup document.open() does.
648 RefPtr<nsHtml5TreeOpExecutor> executor(mExecutor);
649 executor->SetParser(this);
650 mTreeBuilder->setScriptingEnabled(executor->IsScriptEnabled());
652 mTreeBuilder->setIsSrcdocDocument(false);
654 mTokenizer->start();
655 executor->Start();
658 * We know we're in document.open(), so our document must already
659 * have a script global andthe WillBuildModel call is safe.
661 return executor->WillBuildModel();
664 nsresult nsHtml5Parser::Initialize(mozilla::dom::Document* aDoc, nsIURI* aURI,
665 nsISupports* aContainer,
666 nsIChannel* aChannel) {
667 return mExecutor->Init(aDoc, aURI, aContainer, aChannel);
670 void nsHtml5Parser::StartTokenizer(bool aScriptingEnabled) {
671 bool isSrcdoc = false;
672 nsCOMPtr<nsIChannel> channel;
673 nsresult rv = GetChannel(getter_AddRefs(channel));
674 if (NS_SUCCEEDED(rv)) {
675 isSrcdoc = NS_IsSrcdocChannel(channel);
677 mTreeBuilder->setIsSrcdocDocument(isSrcdoc);
679 mTreeBuilder->SetPreventScriptExecution(!aScriptingEnabled);
680 mTreeBuilder->setScriptingEnabled(aScriptingEnabled);
681 mTreeBuilder->setAllowDeclarativeShadowRoots(
682 mExecutor->GetDocument()->AllowsDeclarativeShadowRoots());
683 mTokenizer->start();
686 void nsHtml5Parser::InitializeDocWriteParserState(
687 nsAHtml5TreeBuilderState* aState, int32_t aLine) {
688 mTokenizer->resetToDataState();
689 mTokenizer->setLineNumber(aLine);
690 mTreeBuilder->loadState(aState);
691 mLastWasCR = false;
692 mReturnToStreamParserPermitted = true;
695 void nsHtml5Parser::ContinueAfterFailedCharsetSwitch() {
696 MOZ_ASSERT(
697 GetStreamParser(),
698 "Tried to continue after failed charset switch without a stream parser");
699 GetStreamParser()->ContinueAfterFailedCharsetSwitch();