1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef NS_HTML5_PARSER
7 #define NS_HTML5_PARSER
9 #include "mozilla/UniquePtr.h"
10 #include "nsIParser.h"
12 #include "nsIContentSink.h"
13 #include "nsIRequest.h"
14 #include "nsIChannel.h"
15 #include "nsCOMArray.h"
16 #include "nsContentSink.h"
17 #include "nsCycleCollectionParticipant.h"
18 #include "nsHtml5OwningUTF16Buffer.h"
19 #include "nsHtml5TreeOpExecutor.h"
20 #include "nsHtml5StreamParser.h"
21 #include "nsHtml5AtomTable.h"
22 #include "nsWeakReference.h"
23 #include "nsHtml5StreamListener.h"
24 #include "nsCharsetSource.h"
26 class nsHtml5Parser final
: public nsIParser
, public nsSupportsWeakReference
{
28 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
30 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsHtml5Parser
, nsIParser
)
36 * No-op for backwards compat.
38 NS_IMETHOD_(void) SetContentSink(nsIContentSink
* aSink
) override
;
41 * Returns the tree op executor for backwards compat.
43 NS_IMETHOD_(nsIContentSink
*) GetContentSink() override
;
46 * Always returns "view" for backwards compat.
48 NS_IMETHOD_(void) GetCommand(nsCString
& aCommand
) override
;
51 * No-op for backwards compat.
53 NS_IMETHOD_(void) SetCommand(const char* aCommand
) override
;
56 * No-op for backwards compat.
58 NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand
) override
;
61 * Call this method once you've created a parser, and want to instruct it
62 * about what charset to load
64 * @param aEncoding the charset of a document
65 * @param aCharsetSource the source of the charset
67 virtual void SetDocumentCharset(NotNull
<const Encoding
*> aEncoding
,
69 bool aForceAutoDetection
) override
;
72 * Get the channel associated with this parser
73 * @param aChannel out param that will contain the result
74 * @return NS_OK if successful or NS_NOT_AVAILABLE if not
76 nsresult
GetChannel(nsIChannel
** aChannel
);
79 * Get the stream parser for this parser
81 virtual nsIStreamListener
* GetStreamListener() override
;
84 * Don't call. For interface compat only.
86 NS_IMETHOD
ContinueInterruptedParsing() override
;
91 NS_IMETHOD_(void) BlockParser() override
;
94 * Unblocks the parser.
96 NS_IMETHOD_(void) UnblockParser() override
;
99 * Asynchronously continues parsing.
101 NS_IMETHOD_(void) ContinueInterruptedParsingAsync() override
;
104 * Query whether the parser is enabled (i.e. not blocked) or not.
106 NS_IMETHOD_(bool) IsParserEnabled() override
;
109 * Query whether the parser thinks it's done with parsing.
111 NS_IMETHOD_(bool) IsComplete() override
;
114 * Set up request observer.
116 * @param aURL used for View Source title
118 NS_IMETHOD
Parse(nsIURI
* aURL
) override
;
121 * document.write and document.close
123 * @param aSourceBuffer the argument of document.write (empty for .close())
124 * @param aKey a key unique to the script element that caused this call
125 * @param aLastCall true if .close() false if .write()
127 nsresult
Parse(const nsAString
& aSourceBuffer
, void* aKey
, bool aLastCall
);
130 * Stops the parser prematurely
132 NS_IMETHOD
Terminate() override
;
135 * True if the insertion point (per HTML5) is defined.
137 virtual bool IsInsertionPointDefined() override
;
140 * Call immediately before starting to evaluate a parser-inserted script or
141 * in general when the spec says to increment the script nesting level.
143 void IncrementScriptNestingLevel() final
;
146 * Call immediately after having evaluated a parser-inserted script or
147 * generally want to restore to the state before the last
148 * IncrementScriptNestingLevel call.
150 void DecrementScriptNestingLevel() final
;
153 * True if this is an HTML5 parser whose script nesting level (in
155 * <https://html.spec.whatwg.org/multipage/parsing.html#script-nesting-level>)
158 bool HasNonzeroScriptNestingLevel() const final
;
161 * Marks the HTML5 parser as not a script-created parser: Prepares the
162 * parser to be able to read a stream.
164 * @param aCommand the parser command (Yeah, this is bad API design. Let's
165 * make this better when retiring nsIParser)
167 void MarkAsNotScriptCreated(const char* aCommand
);
170 * True if this is a script-created HTML5 parser.
172 virtual bool IsScriptCreated() override
;
176 // Not from an external interface
177 // Non-inherited methods
181 * Initializes the parser to load from a channel.
183 virtual nsresult
Initialize(mozilla::dom::Document
* aDoc
, nsIURI
* aURI
,
184 nsISupports
* aContainer
, nsIChannel
* aChannel
);
186 inline nsHtml5Tokenizer
* GetTokenizer() { return mTokenizer
.get(); }
188 void InitializeDocWriteParserState(nsAHtml5TreeBuilderState
* aState
,
191 void DropStreamParser() {
192 if (GetStreamParser()) {
193 GetStreamParser()->DropTimer();
194 mStreamListener
->DropDelegate();
195 mStreamListener
= nullptr;
199 void StartTokenizer(bool aScriptingEnabled
);
201 void ContinueAfterFailedCharsetSwitch();
203 nsHtml5StreamParser
* GetStreamParser() {
204 if (!mStreamListener
) {
207 return mStreamListener
->GetDelegate();
210 void PermanentlyUndefineInsertionPoint() {
211 mInsertionPointPermanentlyUndefined
= true;
215 * Parse until pending data is exhausted or a script blocks the parser
217 nsresult
ParseUntilBlocked();
220 * Start our executor. This is meant to be used from document.open() _only_
221 * and does some work similar to what nsHtml5StreamParser::OnStartRequest does
224 nsresult
StartExecutor();
227 virtual ~nsHtml5Parser();
232 * Whether the last character tokenized was a carriage return (for CRLF)
237 * Whether the last character tokenized was a carriage return (for CRLF)
238 * when preparsing document.write.
240 bool mDocWriteSpeculativeLastWasCR
;
243 * The parser is blocking on the load of an external script from a web
244 * page, or any number of extension content scripts.
249 * Whether the document.write() speculator is already active.
251 bool mDocWriteSpeculatorActive
;
254 * The number of IncrementScriptNestingLevel calls we've seen without a
255 * matching DecrementScriptNestingLevel.
257 int32_t mScriptNestingLevel
;
260 * True if document.close() has been called.
262 bool mDocumentClosed
;
264 bool mInDocumentWrite
;
267 * This is set when the tokenizer has seen EOF. The purpose is to
268 * keep the insertion point undefined between the time the
269 * parser has reached the point where it can't accept more input
270 * and the time the document's mParser is set to nullptr.
271 * Scripts can run during this time period due to an update
272 * batch ending and due to various end-of-parse events firing.
273 * (Setting mParser on the document to nullptr at the point
274 * where this flag gets set to true would break things that for
275 * legacy reasons assume that mParser on the document stays
276 * non-null though the end-of-parse events.)
278 bool mInsertionPointPermanentlyUndefined
;
280 // Portable parser objects
282 * The first buffer in the pending UTF-16 buffer queue
284 RefPtr
<nsHtml5OwningUTF16Buffer
> mFirstBuffer
;
287 * The last buffer in the pending UTF-16 buffer queue. Always points
288 * to a sentinel object with nullptr as its parser key.
290 nsHtml5OwningUTF16Buffer
* mLastBuffer
; // weak ref;
293 * The tree operation executor
295 RefPtr
<nsHtml5TreeOpExecutor
> mExecutor
;
298 * The HTML5 tree builder
300 const mozilla::UniquePtr
<nsHtml5TreeBuilder
> mTreeBuilder
;
303 * The HTML5 tokenizer
305 const mozilla::UniquePtr
<nsHtml5Tokenizer
> mTokenizer
;
308 * Another HTML5 tree builder for preloading document.written content.
310 mozilla::UniquePtr
<nsHtml5TreeBuilder
> mDocWriteSpeculativeTreeBuilder
;
313 * Another HTML5 tokenizer for preloading document.written content.
315 mozilla::UniquePtr
<nsHtml5Tokenizer
> mDocWriteSpeculativeTokenizer
;
318 * The stream listener holding the stream parser.
320 RefPtr
<nsHtml5StreamListener
> mStreamListener
;
325 int32_t mRootContextLineNumber
;
328 * Whether it's OK to transfer parsing back to the stream parser
330 bool mReturnToStreamParserPermitted
;
333 * The scoped atom table
335 nsHtml5AtomTable mAtomTable
;