1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Henri Sivonen <hsivonen@iki.fi>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef NS_HTML5_PARSER__
40 #define NS_HTML5_PARSER__
42 #include "nsAutoPtr.h"
43 #include "nsIParser.h"
46 #include "nsParserCIID.h"
47 #include "nsITokenizer.h"
48 #include "nsThreadUtils.h"
49 #include "nsIContentSink.h"
50 #include "nsIParserFilter.h"
51 #include "nsIRequest.h"
52 #include "nsIChannel.h"
53 #include "nsCOMArray.h"
54 #include "nsContentSink.h"
55 #include "nsIHTMLDocument.h"
56 #include "nsIUnicharStreamListener.h"
57 #include "nsCycleCollectionParticipant.h"
58 #include "nsIInputStream.h"
59 #include "nsDetectionConfident.h"
60 #include "nsHtml5UTF16Buffer.h"
61 #include "nsHtml5TreeOpExecutor.h"
62 #include "nsHtml5StreamParser.h"
63 #include "nsHtml5AtomTable.h"
64 #include "nsWeakReference.h"
65 #include "nsAHtml5FragmentParser.h"
67 class nsHtml5Parser
: public nsAHtml5FragmentParser
, // inherits nsIParser
68 public nsSupportsWeakReference
71 NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
72 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
74 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsHtml5Parser
, nsIParser
)
77 virtual ~nsHtml5Parser();
81 * No-op for backwards compat.
83 NS_IMETHOD_(void) SetContentSink(nsIContentSink
* aSink
);
86 * Returns the tree op executor for backwards compat.
88 NS_IMETHOD_(nsIContentSink
*) GetContentSink();
91 * Always returns "view" for backwards compat.
93 NS_IMETHOD_(void) GetCommand(nsCString
& aCommand
);
96 * No-op for backwards compat.
98 NS_IMETHOD_(void) SetCommand(const char* aCommand
);
101 * No-op for backwards compat.
103 NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand
);
106 * Call this method once you've created a parser, and want to instruct it
107 * about what charset to load
109 * @param aCharset the charset of a document
110 * @param aCharsetSource the source of the charset
112 NS_IMETHOD_(void) SetDocumentCharset(const nsACString
& aCharset
, PRInt32 aSource
);
115 * Don't call. For interface compat only.
117 NS_IMETHOD_(void) GetDocumentCharset(nsACString
& aCharset
, PRInt32
& aSource
)
119 NS_NOTREACHED("No one should call this.");
123 * No-op for backwards compat.
125 NS_IMETHOD_(void) SetParserFilter(nsIParserFilter
* aFilter
);
128 * Get the channel associated with this parser
129 * @param aChannel out param that will contain the result
130 * @return NS_OK if successful or NS_NOT_AVAILABLE if not
132 NS_IMETHOD
GetChannel(nsIChannel
** aChannel
);
135 * Return |this| for backwards compat.
137 NS_IMETHOD
GetDTD(nsIDTD
** aDTD
);
140 * Get the stream parser for this parser
142 NS_IMETHOD
GetStreamListener(nsIStreamListener
** aListener
);
145 * Don't call. For interface compat only.
147 NS_IMETHOD
ContinueInterruptedParsing();
152 NS_IMETHOD_(void) BlockParser();
155 * Unblocks the parser.
157 NS_IMETHOD_(void) UnblockParser();
160 * Query whether the parser is enabled (i.e. not blocked) or not.
162 NS_IMETHOD_(PRBool
) IsParserEnabled();
165 * Query whether the parser thinks it's done with parsing.
167 NS_IMETHOD_(PRBool
) IsComplete();
170 * Set up request observer.
172 * @param aURL ignored (for interface compat only)
173 * @param aListener a listener to forward notifications to
174 * @param aKey the root context key (used for document.write)
175 * @param aMode ignored (for interface compat only)
177 NS_IMETHOD
Parse(nsIURI
* aURL
,
178 nsIRequestObserver
* aListener
= nsnull
,
180 nsDTDMode aMode
= eDTDMode_autodetect
);
183 * document.write and document.close
185 * @param aSourceBuffer the argument of document.write (empty for .close())
186 * @param aKey a key unique to the script element that caused this call
187 * @param aContentType ignored (for interface compat only)
188 * @param aLastCall true if .close() false if .write()
189 * @param aMode ignored (for interface compat only)
191 NS_IMETHOD
Parse(const nsAString
& aSourceBuffer
,
193 const nsACString
& aContentType
,
195 nsDTDMode aMode
= eDTDMode_autodetect
);
198 * Gets the key passed to initial Parse()
200 NS_IMETHOD_(void *) GetRootContextKey();
203 * Stops the parser prematurely
205 NS_IMETHOD
Terminate();
208 * Don't call. For interface backwards compat only.
210 NS_IMETHOD
ParseFragment(const nsAString
& aSourceBuffer
,
212 nsTArray
<nsString
>& aTagStack
,
214 const nsACString
& aContentType
,
215 nsDTDMode aMode
= eDTDMode_autodetect
);
218 * Don't call. For interface backwards compat only.
220 NS_IMETHOD
ParseFragment(const nsAString
& aSourceBuffer
,
221 nsIContent
* aTargetNode
,
222 nsIAtom
* aContextLocalName
,
223 PRInt32 aContextNamespace
,
227 * Don't call. For interface compat only.
229 NS_IMETHOD
BuildModel();
232 * Don't call. For interface compat only.
234 NS_IMETHODIMP
CancelParsingEvents();
237 * Sets the state to initial values
239 virtual void Reset();
242 * True in fragment mode and during synchronous document.write
244 virtual PRBool
CanInterrupt();
247 * True if the insertion point (per HTML5) is defined.
249 virtual PRBool
IsInsertionPointDefined();
252 * Call immediately before starting to evaluate a parser-inserted script.
254 virtual void BeginEvaluatingParserInsertedScript();
257 * Call immediately after having evaluated a parser-inserted script.
259 virtual void EndEvaluatingParserInsertedScript();
262 * Marks the HTML5 parser as not a script-created parser: Prepares the
263 * parser to be able to read a stream.
265 virtual void MarkAsNotScriptCreated();
268 * True if this is a script-created HTML5 parser.
270 virtual PRBool
IsScriptCreated();
274 /* Start nsAHtml5FragmentParser */
277 * Invoke the fragment parsing algorithm (innerHTML).
279 * @param aSourceBuffer the string being set as innerHTML
280 * @param aTargetNode the target container
281 * @param aContextLocalName local name of context node
282 * @param aContextNamespace namespace of context node
283 * @param aQuirks true to make <table> not close <p>
284 * @param aPreventScriptExecution true to prevent scripts from executing;
285 * don't set to false when parsing into a target node that has been bound
288 NS_IMETHOD
ParseHtml5Fragment(const nsAString
& aSourceBuffer
,
289 nsIContent
* aTargetNode
,
290 nsIAtom
* aContextLocalName
,
291 PRInt32 aContextNamespace
,
293 PRBool aPreventScriptExecution
);
296 /* End nsAHtml5FragmentParser */
298 // Not from an external interface
299 // Non-inherited methods
304 * Initializes the parser to load from a channel.
306 virtual nsresult
Initialize(nsIDocument
* aDoc
,
308 nsISupports
* aContainer
,
309 nsIChannel
* aChannel
);
311 inline nsHtml5Tokenizer
* GetTokenizer() {
315 void InitializeDocWriteParserState(nsAHtml5TreeBuilderState
* aState
, PRInt32 aLine
);
317 void DropStreamParser() {
319 mStreamParser
->DropTimer();
320 mStreamParser
= nsnull
;
324 void StartTokenizer(PRBool aScriptingEnabled
);
326 void ContinueAfterFailedCharsetSwitch();
328 nsHtml5StreamParser
* GetStreamParser() {
329 return mStreamParser
;
333 * Parse until pending data is exhausted or a script blocks the parser
335 void ParseUntilBlocked();
342 * Whether the last character tokenized was a carriage return (for CRLF)
347 * Whether the last character tokenized was a carriage return (for CRLF)
348 * when preparsing document.write.
350 PRBool mDocWriteSpeculativeLastWasCR
;
353 * The parser is in the fragment mode
355 PRBool mFragmentMode
;
358 * The parser is blocking on a script
363 * Whether the document.write() speculator is already active.
365 PRBool mDocWriteSpeculatorActive
;
368 * The number of parser-inserted script currently being evaluated.
370 PRInt32 mParserInsertedScriptsBeingEvaluated
;
373 * True if document.close() has been called.
375 PRBool mDocumentClosed
;
378 void* mRootContextKey
;
380 // Portable parser objects
382 * The first buffer in the pending UTF-16 buffer queue
384 nsRefPtr
<nsHtml5UTF16Buffer
> mFirstBuffer
;
387 * The last buffer in the pending UTF-16 buffer queue
389 nsHtml5UTF16Buffer
* mLastBuffer
; // weak ref; always points to
390 // a buffer of the size NS_HTML5_PARSER_READ_BUFFER_SIZE
393 * The tree operation executor
395 nsRefPtr
<nsHtml5TreeOpExecutor
> mExecutor
;
398 * The HTML5 tree builder
400 const nsAutoPtr
<nsHtml5TreeBuilder
> mTreeBuilder
;
403 * The HTML5 tokenizer
405 const nsAutoPtr
<nsHtml5Tokenizer
> mTokenizer
;
408 * Another HTML5 tree builder for preloading document.written content.
410 nsAutoPtr
<nsHtml5TreeBuilder
> mDocWriteSpeculativeTreeBuilder
;
413 * Another HTML5 tokenizer for preloading document.written content.
415 nsAutoPtr
<nsHtml5Tokenizer
> mDocWriteSpeculativeTokenizer
;
420 nsRefPtr
<nsHtml5StreamParser
> mStreamParser
;
425 PRInt32 mRootContextLineNumber
;
428 * Whether it's OK to transfer parsing back to the stream parser
430 PRBool mReturnToStreamParserPermitted
;
433 * The scoped atom table
435 nsHtml5AtomTable mAtomTable
;