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/. */
9 * This class does two primary jobs:
10 * 1) It iterates the tokens provided during the
11 * tokenization process, identifing where elements
12 * begin and end (doing validation and normalization).
13 * 2) It controls and coordinates with an instance of
14 * the IContentSink interface, to coordinate the
15 * the production of the content model.
17 * The basic operation of this class assumes that an HTML
18 * document is non-normalized. Therefore, we don't process
19 * the document in a normalized way. Don't bother to look
20 * for methods like: doHead() or doBody().
22 * Instead, in order to be backward compatible, we must
23 * scan the set of tokens and perform this basic set of
25 * 1) Determine the token type (easy, since the tokens know)
26 * 2) Determine the appropriate section of the HTML document
27 * each token belongs in (HTML,HEAD,BODY,FRAMESET).
28 * 3) Insert content into our document (via the sink) into
29 * the correct section.
30 * 4) In the case of tags that belong in the BODY, we must
31 * ensure that our underlying document state reflects
32 * the appropriate context for our tag.
34 * For example,if we see a <TR>, we must ensure our
35 * document contains a table into which the row can
36 * be placed. This may result in "implicit containers"
37 * created to ensure a well-formed document.
44 #include "nsIParser.h"
46 #include "CParserContext.h"
47 #include "nsParserCIID.h"
48 #include "nsITokenizer.h"
49 #include "nsHTMLTags.h"
50 #include "nsIContentSink.h"
51 #include "nsCOMArray.h"
52 #include "nsCycleCollectionParticipant.h"
53 #include "nsWeakReference.h"
59 # pragma warning(disable : 4275)
62 class nsParser final
: public nsIParser
,
63 public nsIStreamListener
,
64 public nsSupportsWeakReference
{
73 * Called on module init
75 static nsresult
Init();
78 * Called on module shutdown
80 static void Shutdown();
82 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
83 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsParser
, nsIParser
)
92 * Select given content sink into parser for parser output
94 * @param aSink is the new sink to be used by parser
95 * @return old sink, or nullptr
97 NS_IMETHOD_(void) SetContentSink(nsIContentSink
* aSink
) override
;
100 * retrive the sink set into the parser
101 * @update gess5/11/98
102 * @param aSink is the new sink to be used by parser
103 * @return old sink, or nullptr
105 NS_IMETHOD_(nsIContentSink
*) GetContentSink(void) override
;
108 * Call this method once you've created a parser, and want to instruct it
109 * about the command which caused the parser to be constructed. For example,
110 * this allows us to select a DTD which can do, say, view-source.
112 * @update gess 3/25/98
113 * @param aCommand -- ptrs to string that contains command
116 NS_IMETHOD_(void) GetCommand(nsCString
& aCommand
) override
;
117 NS_IMETHOD_(void) SetCommand(const char* aCommand
) override
;
118 NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand
) override
;
121 * Call this method once you've created a parser, and want to instruct it
122 * about what charset to load
124 * @update ftang 4/23/99
125 * @param aCharset- the charset of a document
126 * @param aCharsetSource- the source of the charset
127 * @param aChannelHadCharset- ignored
130 virtual void SetDocumentCharset(NotNull
<const Encoding
*> aCharset
,
132 bool aChannelHadCharset
) override
;
134 NotNull
<const Encoding
*> GetDocumentCharset(int32_t& aSource
) {
135 aSource
= mCharsetSource
;
140 * Cause parser to parse input from given URL
141 * @update gess5/11/98
142 * @param aURL is a descriptor for source document
143 * @param aListener is a listener to forward notifications to
144 * @return TRUE if all went well -- FALSE otherwise
146 NS_IMETHOD
Parse(nsIURI
* aURL
, nsIRequestObserver
* aListener
= nullptr,
148 nsDTDMode aMode
= eDTDMode_autodetect
) override
;
151 * This method needs documentation
153 NS_IMETHOD
ParseFragment(const nsAString
& aSourceBuffer
,
154 nsTArray
<nsString
>& aTagStack
) override
;
157 * This method gets called when the tokens have been consumed, and it's time
158 * to build the model via the content sink.
159 * @update gess5/11/98
160 * @return YES if model building went well -- NO otherwise.
162 NS_IMETHOD
BuildModel(void) override
;
164 NS_IMETHOD
ContinueInterruptedParsing() override
;
165 NS_IMETHOD_(void) BlockParser() override
;
166 NS_IMETHOD_(void) UnblockParser() override
;
167 NS_IMETHOD_(void) ContinueInterruptedParsingAsync() override
;
168 NS_IMETHOD
Terminate(void) override
;
171 * Call this to query whether the parser is enabled or not.
173 * @update vidur 4/12/99
174 * @return current state
176 NS_IMETHOD_(bool) IsParserEnabled() override
;
179 * Call this to query whether the parser thinks it's done with parsing.
181 * @update rickg 5/12/01
182 * @return complete state
184 NS_IMETHOD_(bool) IsComplete() override
;
187 * This rather arcane method (hack) is used as a signal between the
188 * DTD and the parser. It allows the DTD to tell the parser that content
189 * that comes through (parser::parser(string)) but not consumed should
190 * propagate into the next string based parse call.
192 * @update gess 9/1/98
193 * @param aState determines whether we propagate unused string content.
194 * @return current state
196 void SetUnusedInput(nsString
& aBuffer
);
199 * This method gets called (automatically) during incremental parsing
200 * @update gess5/11/98
201 * @return TRUE if all went well, otherwise FALSE
203 virtual nsresult
ResumeParse(bool allowIteration
= true,
204 bool aIsFinalChunk
= false,
205 bool aCanInterrupt
= true);
207 //*********************************************
208 // These methods are callback methods used by
209 // net lib to let us know about our inputstream.
210 //*********************************************
211 // nsIRequestObserver methods:
212 NS_DECL_NSIREQUESTOBSERVER
214 // nsIStreamListener methods:
215 NS_DECL_NSISTREAMLISTENER
217 void PushContext(CParserContext
& aContext
);
218 CParserContext
* PopContext();
219 CParserContext
* PeekContext() { return mParserContext
; }
222 * Get the channel associated with this parser
223 * @update harishd,gagan 07/17/01
224 * @param aChannel out param that will contain the result
225 * @return NS_OK if successful
227 NS_IMETHOD
GetChannel(nsIChannel
** aChannel
) override
;
230 * Get the DTD associated with this parser
231 * @update vidur 9/29/99
232 * @param aDTD out param that will contain the result
233 * @return NS_OK if successful, NS_ERROR_FAILURE for runtime error
235 NS_IMETHOD
GetDTD(nsIDTD
** aDTD
) override
;
238 * Get the nsIStreamListener for this parser
240 virtual nsIStreamListener
* GetStreamListener() override
;
242 void SetSinkCharset(NotNull
<const Encoding
*> aCharset
);
245 * Removes continue parsing events
246 * @update kmcclusk 5/18/98
249 NS_IMETHOD
CancelParsingEvents() override
;
254 virtual bool IsInsertionPointDefined() override
;
259 void IncrementScriptNestingLevel() final
;
264 void DecrementScriptNestingLevel() final
;
266 bool HasNonzeroScriptNestingLevel() const final
;
271 virtual void MarkAsNotScriptCreated(const char* aCommand
) override
;
276 virtual bool IsScriptCreated() override
;
279 * Set to parser state to indicate whether parsing tokens can be interrupted
280 * @param aCanInterrupt true if parser can be interrupted, false if it can
281 * not be interrupted.
282 * @update kmcclusk 5/18/98
284 void SetCanInterrupt(bool aCanInterrupt
);
287 * This is called when the final chunk has been
288 * passed to the parser and the content sink has
289 * interrupted token processing. It schedules
290 * a ParserContinue PL_Event which will ask the parser
291 * to HandleParserContinueEvent when it is handled.
292 * @update kmcclusk6/1/2001
294 nsresult
PostContinueEvent();
297 * Fired when the continue parse event is triggered.
298 * @update kmcclusk 5/18/98
300 void HandleParserContinueEvent(class nsParserContinueEvent
*);
302 virtual void Reset() override
{
307 bool IsScriptExecuting() { return mSink
&& mSink
->IsScriptExecuting(); }
309 bool IsOkToProcessNetworkData() {
310 return !IsScriptExecuting() && !mProcessingNetworkData
;
314 void Initialize(bool aConstructor
= false);
319 * @update gess5/18/98
323 nsresult
WillBuildModel(nsString
& aFilename
);
327 * @update gess5/18/98
331 nsresult
DidBuildModel(nsresult anErrorCode
);
334 /*******************************************
335 These are the tokenization methods...
336 *******************************************/
339 * Part of the code sandwich, this gets called right before
340 * the tokenization process begins. The main reason for
341 * this call is to allow the delegate to do initialization.
343 * @update gess 3/25/98
345 * @return TRUE if it's ok to proceed
347 bool WillTokenize(bool aIsFinalChunk
= false);
350 * This is the primary control routine. It iteratively
351 * consumes tokens until an error occurs or you run out
354 * @update gess 3/25/98
357 nsresult
Tokenize(bool aIsFinalChunk
= false);
360 * Pushes XML fragment parsing data to expat without an input stream.
362 nsresult
Parse(const nsAString
& aSourceBuffer
, void* aKey
, bool aLastCall
);
365 //*********************************************
366 // And now, some data members...
367 //*********************************************
369 CParserContext
* mParserContext
;
370 nsCOMPtr
<nsIDTD
> mDTD
;
371 nsCOMPtr
<nsIRequestObserver
> mObserver
;
372 nsCOMPtr
<nsIContentSink
> mSink
;
373 nsIRunnable
* mContinueEvent
; // weak ref
375 eParserCommands mCommand
;
376 nsresult mInternalState
;
377 nsresult mStreamStatus
;
378 int32_t mCharsetSource
;
383 nsString mUnusedInput
;
384 NotNull
<const Encoding
*> mCharset
;
385 nsCString mCommandStr
;
387 bool mProcessingNetworkData
;
391 nsresult
nsParserInitialize();