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"
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
)
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()
42 mDocWriteSpeculativeLastWasCR(false),
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() {
61 if (mDocWriteSpeculativeTokenizer
) {
62 mDocWriteSpeculativeTokenizer
->end();
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
);
78 nsHtml5Parser::GetCommand(nsCString
& aCommand
) {
79 aCommand
.AssignLiteral("view");
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");
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
);
112 return NS_ERROR_NOT_AVAILABLE
;
116 nsIStreamListener
* nsHtml5Parser::GetStreamListener() {
117 return mStreamListener
;
121 nsHtml5Parser::ContinueInterruptedParsing() {
122 MOZ_ASSERT_UNREACHABLE("Don't call. For interface compat only.");
123 return NS_ERROR_NOT_IMPLEMENTED
;
127 nsHtml5Parser::BlockParser() { mBlocked
++; }
130 nsHtml5Parser::UnblockParser() {
131 MOZ_DIAGNOSTIC_ASSERT(mBlocked
> 0);
132 if (MOZ_LIKELY(mBlocked
> 0)) {
135 if (MOZ_LIKELY(mBlocked
== 0) && mExecutor
) {
136 mExecutor
->ContinueInterruptedParsingAsync();
141 nsHtml5Parser::ContinueInterruptedParsingAsync() {
143 mExecutor
->ContinueInterruptedParsingAsync();
148 nsHtml5Parser::IsParserEnabled() { return !mBlocked
; }
151 nsHtml5Parser::IsComplete() { return mExecutor
->IsComplete(); }
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);
170 nsresult
nsHtml5Parser::Parse(const nsAString
& aSourceBuffer
, void* aKey
,
173 if (NS_FAILED(rv
= mExecutor
->IsBroken())) {
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()) {
197 if (aLastCall
&& aSourceBuffer
.IsEmpty() && !aKey
) {
199 NS_ASSERTION(!GetStreamParser(),
200 "Had stream parser but got document.close().");
201 if (mDocumentClosed
) {
205 mDocumentClosed
= true;
206 if (!mBlocked
&& !mInDocumentWrite
) {
207 return ParseUntilBlocked();
212 // If we got this far, we are dealing with a document.write or
213 // document.writeln call--not document.close().
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()) {
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
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
;
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
;
275 if (prevSearchBuf
->next
== mLastBuffer
) {
277 nsHtml5OwningUTF16Buffer
* keyHolder
=
278 new nsHtml5OwningUTF16Buffer(aKey
);
279 keyHolder
->next
= mFirstBuffer
;
280 mFirstBuffer
= keyHolder
;
281 prevSearchBuf
= nullptr;
284 if (prevSearchBuf
->next
->key
== aKey
) {
285 // found a key holder
288 prevSearchBuf
= prevSearchBuf
->next
;
290 } // else mFirstBuffer is the keyholder
292 // prevSearchBuf is the previous buffer before the keyholder or null if
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
);
310 if (stackBuffer
.hasMore()) {
311 int32_t lineNumberSave
;
312 bool inRootContext
= (!GetStreamParser() && !aKey
);
314 mTokenizer
->setLineNumber(mRootContextLineNumber
);
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
);
330 mRootContextLineNumber
= mTokenizer
->getLineNumber();
332 mTokenizer
->setLineNumber(lineNumberSave
);
335 if (mTreeBuilder
->HasScript()) {
336 auto r
= mTreeBuilder
->Flush(); // Move ops to the executor
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()) {
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
356 heapBuffer
= stackBuffer
.FalliblyCopyAsOwningBuffer();
358 // Allocation failed. The parser is now broken.
359 return executor
->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY
);
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
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
) {
374 "Non-null prevSearchBuf when mFirstBuffer is the key holder?");
375 heapBuffer
->next
= mFirstBuffer
;
376 mFirstBuffer
= heapBuffer
;
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
;
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
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
);
447 if (NS_FAILED((rv
= mDocWriteSpeculativeTreeBuilder
->IsBroken()))) {
448 return executor
->MarkAsBroken(rv
);
453 auto r
= mDocWriteSpeculativeTreeBuilder
->Flush();
455 return executor
->MarkAsBroken(r
.unwrapErr());
457 mDocWriteSpeculativeTreeBuilder
->DropHandles();
458 executor
->FlushSpeculativeLoads();
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()) {
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
);
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")) {
508 } else if (!nsCRT::strcmp(aCommand
, kLoadAsData
)) {
513 NS_ASSERTION(!nsCRT::strcmp(aCommand
, "view") ||
514 !nsCRT::strcmp(aCommand
, "external-resource") ||
515 !nsCRT::strcmp(aCommand
, "import"),
516 "Unsupported parser command!");
520 new nsHtml5StreamListener(new nsHtml5StreamParser(mExecutor
, this, mode
));
523 bool nsHtml5Parser::IsScriptCreated() { return !GetStreamParser(); }
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()) {
535 NS_ASSERTION(mExecutor
->HasStarted(), "Bad life cycle.");
536 NS_ASSERTION(!mInDocumentWrite
,
537 "ParseUntilBlocked entered while in doc.write!");
539 mDocWriteSpeculatorActive
= false;
542 if (!mFirstBuffer
->hasMore()) {
543 if (mFirstBuffer
== mLastBuffer
) {
544 if (mExecutor
->IsComplete()) {
545 // something like cache manisfests stopped the parse in mid-flight
548 if (mDocumentClosed
) {
549 PermanentlyUndefineInsertionPoint();
553 "This should only happen with script-created parser.");
554 if (NS_SUCCEEDED((rv
= mExecutor
->IsBroken()))) {
556 if (NS_FAILED((rv
= mTreeBuilder
->IsBroken()))) {
557 mExecutor
->MarkAsBroken(rv
);
559 mTreeBuilder
->StreamEnded();
562 auto r
= mTreeBuilder
->Flush();
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.
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();
580 return mExecutor
->MarkAsBroken(r
.unwrapErr());
582 mReturnToStreamParserPermitted
= false;
583 GetStreamParser()->ContinueAfterScriptsOrEncodingCommitment(
584 mTokenizer
.get(), mTreeBuilder
.get(), mLastWasCR
);
587 // Script-created parser
588 auto r
= mTreeBuilder
->Flush();
590 return mExecutor
->MarkAsBroken(r
.unwrapErr());
592 // No need to flush the executor, because the executor is already
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
;
603 if (mBlocked
|| mExecutor
->IsComplete()) {
607 // now we have a non-empty buffer
608 mFirstBuffer
->adjust(mLastWasCR
);
610 if (mFirstBuffer
->hasMore()) {
611 bool inRootContext
= (!GetStreamParser() && !mFirstBuffer
->key
);
613 mTokenizer
->setLineNumber(mRootContextLineNumber
);
615 if (!mTokenizer
->EnsureBufferSpace(mFirstBuffer
->getLength())) {
616 return mExecutor
->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY
);
618 mLastWasCR
= mTokenizer
->tokenizeBuffer(mFirstBuffer
);
620 if (NS_FAILED((rv
= mTreeBuilder
->IsBroken()))) {
621 return mExecutor
->MarkAsBroken(rv
);
624 mRootContextLineNumber
= mTokenizer
->getLineNumber();
626 if (mTreeBuilder
->HasScript()) {
627 auto r
= mTreeBuilder
->Flush();
629 return mExecutor
->MarkAsBroken(r
.unwrapErr());
631 rv
= mExecutor
->FlushDocumentWrite();
632 NS_ENSURE_SUCCESS(rv
, rv
);
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);
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
);
681 void nsHtml5Parser::InitializeDocWriteParserState(
682 nsAHtml5TreeBuilderState
* aState
, int32_t aLine
) {
683 mTokenizer
->resetToDataState();
684 mTokenizer
->setLineNumber(aLine
);
685 mTreeBuilder
->loadState(aState
);
687 mReturnToStreamParserPermitted
= true;
690 void nsHtml5Parser::ContinueAfterFailedCharsetSwitch() {
693 "Tried to continue after failed charset switch without a stream parser");
694 GetStreamParser()->ContinueAfterFailedCharsetSwitch();