michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef NS_HTML5_PARSER michael@0: #define NS_HTML5_PARSER michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "nsIParser.h" michael@0: #include "nsDeque.h" michael@0: #include "nsIURL.h" michael@0: #include "nsParserCIID.h" michael@0: #include "nsITokenizer.h" michael@0: #include "nsIContentSink.h" michael@0: #include "nsIRequest.h" michael@0: #include "nsIChannel.h" michael@0: #include "nsCOMArray.h" michael@0: #include "nsContentSink.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsDetectionConfident.h" michael@0: #include "nsHtml5OwningUTF16Buffer.h" michael@0: #include "nsHtml5TreeOpExecutor.h" michael@0: #include "nsHtml5StreamParser.h" michael@0: #include "nsHtml5AtomTable.h" michael@0: #include "nsWeakReference.h" michael@0: #include "nsHtml5StreamListener.h" michael@0: michael@0: class nsHtml5Parser : public nsIParser, michael@0: public nsSupportsWeakReference michael@0: { michael@0: public: michael@0: NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: michael@0: NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsHtml5Parser, nsIParser) michael@0: michael@0: nsHtml5Parser(); michael@0: virtual ~nsHtml5Parser(); michael@0: michael@0: /* Start nsIParser */ michael@0: /** michael@0: * No-op for backwards compat. michael@0: */ michael@0: NS_IMETHOD_(void) SetContentSink(nsIContentSink* aSink); michael@0: michael@0: /** michael@0: * Returns the tree op executor for backwards compat. michael@0: */ michael@0: NS_IMETHOD_(nsIContentSink*) GetContentSink(); michael@0: michael@0: /** michael@0: * Always returns "view" for backwards compat. michael@0: */ michael@0: NS_IMETHOD_(void) GetCommand(nsCString& aCommand); michael@0: michael@0: /** michael@0: * No-op for backwards compat. michael@0: */ michael@0: NS_IMETHOD_(void) SetCommand(const char* aCommand); michael@0: michael@0: /** michael@0: * No-op for backwards compat. michael@0: */ michael@0: NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand); michael@0: michael@0: /** michael@0: * Call this method once you've created a parser, and want to instruct it michael@0: * about what charset to load michael@0: * michael@0: * @param aCharset the charset of a document michael@0: * @param aCharsetSource the source of the charset michael@0: */ michael@0: NS_IMETHOD_(void) SetDocumentCharset(const nsACString& aCharset, int32_t aSource); michael@0: michael@0: /** michael@0: * Don't call. For interface compat only. michael@0: */ michael@0: NS_IMETHOD_(void) GetDocumentCharset(nsACString& aCharset, int32_t& aSource) michael@0: { michael@0: NS_NOTREACHED("No one should call this."); michael@0: } michael@0: michael@0: /** michael@0: * Get the channel associated with this parser michael@0: * @param aChannel out param that will contain the result michael@0: * @return NS_OK if successful or NS_NOT_AVAILABLE if not michael@0: */ michael@0: NS_IMETHOD GetChannel(nsIChannel** aChannel); michael@0: michael@0: /** michael@0: * Return |this| for backwards compat. michael@0: */ michael@0: NS_IMETHOD GetDTD(nsIDTD** aDTD); michael@0: michael@0: /** michael@0: * Get the stream parser for this parser michael@0: */ michael@0: virtual nsIStreamListener* GetStreamListener(); michael@0: michael@0: /** michael@0: * Don't call. For interface compat only. michael@0: */ michael@0: NS_IMETHOD ContinueInterruptedParsing(); michael@0: michael@0: /** michael@0: * Blocks the parser. michael@0: */ michael@0: NS_IMETHOD_(void) BlockParser(); michael@0: michael@0: /** michael@0: * Unblocks the parser. michael@0: */ michael@0: NS_IMETHOD_(void) UnblockParser(); michael@0: michael@0: /** michael@0: * Asynchronously continues parsing. michael@0: */ michael@0: NS_IMETHOD_(void) ContinueInterruptedParsingAsync(); michael@0: michael@0: /** michael@0: * Query whether the parser is enabled (i.e. not blocked) or not. michael@0: */ michael@0: NS_IMETHOD_(bool) IsParserEnabled(); michael@0: michael@0: /** michael@0: * Query whether the parser thinks it's done with parsing. michael@0: */ michael@0: NS_IMETHOD_(bool) IsComplete(); michael@0: michael@0: /** michael@0: * Set up request observer. michael@0: * michael@0: * @param aURL used for View Source title michael@0: * @param aListener a listener to forward notifications to michael@0: * @param aKey the root context key (used for document.write) michael@0: * @param aMode ignored (for interface compat only) michael@0: */ michael@0: NS_IMETHOD Parse(nsIURI* aURL, michael@0: nsIRequestObserver* aListener = nullptr, michael@0: void* aKey = 0, michael@0: nsDTDMode aMode = eDTDMode_autodetect); michael@0: michael@0: /** michael@0: * document.write and document.close michael@0: * michael@0: * @param aSourceBuffer the argument of document.write (empty for .close()) michael@0: * @param aKey a key unique to the script element that caused this call michael@0: * @param aContentType "text/html" for HTML mode, else text/plain mode michael@0: * @param aLastCall true if .close() false if .write() michael@0: * @param aMode ignored (for interface compat only) michael@0: */ michael@0: NS_IMETHOD Parse(const nsAString& aSourceBuffer, michael@0: void* aKey, michael@0: const nsACString& aContentType, michael@0: bool aLastCall, michael@0: nsDTDMode aMode = eDTDMode_autodetect); michael@0: michael@0: /** michael@0: * Stops the parser prematurely michael@0: */ michael@0: NS_IMETHOD Terminate(); michael@0: michael@0: /** michael@0: * Don't call. For interface backwards compat only. michael@0: */ michael@0: NS_IMETHOD ParseFragment(const nsAString& aSourceBuffer, michael@0: nsTArray& aTagStack); michael@0: michael@0: /** michael@0: * Don't call. For interface compat only. michael@0: */ michael@0: NS_IMETHOD BuildModel(); michael@0: michael@0: /** michael@0: * Don't call. For interface compat only. michael@0: */ michael@0: NS_IMETHODIMP CancelParsingEvents(); michael@0: michael@0: /** michael@0: * Don't call. For interface compat only. michael@0: */ michael@0: virtual void Reset(); michael@0: michael@0: /** michael@0: * True in fragment mode and during synchronous document.write michael@0: */ michael@0: virtual bool CanInterrupt(); michael@0: michael@0: /** michael@0: * True if the insertion point (per HTML5) is defined. michael@0: */ michael@0: virtual bool IsInsertionPointDefined(); michael@0: michael@0: /** michael@0: * Call immediately before starting to evaluate a parser-inserted script. michael@0: */ michael@0: virtual void BeginEvaluatingParserInsertedScript(); michael@0: michael@0: /** michael@0: * Call immediately after having evaluated a parser-inserted script. michael@0: */ michael@0: virtual void EndEvaluatingParserInsertedScript(); michael@0: michael@0: /** michael@0: * Marks the HTML5 parser as not a script-created parser: Prepares the michael@0: * parser to be able to read a stream. michael@0: * michael@0: * @param aCommand the parser command (Yeah, this is bad API design. Let's michael@0: * make this better when retiring nsIParser) michael@0: */ michael@0: virtual void MarkAsNotScriptCreated(const char* aCommand); michael@0: michael@0: /** michael@0: * True if this is a script-created HTML5 parser. michael@0: */ michael@0: virtual bool IsScriptCreated(); michael@0: michael@0: /* End nsIParser */ michael@0: michael@0: // Not from an external interface michael@0: // Non-inherited methods michael@0: michael@0: public: michael@0: michael@0: /** michael@0: * Initializes the parser to load from a channel. michael@0: */ michael@0: virtual nsresult Initialize(nsIDocument* aDoc, michael@0: nsIURI* aURI, michael@0: nsISupports* aContainer, michael@0: nsIChannel* aChannel); michael@0: michael@0: inline nsHtml5Tokenizer* GetTokenizer() { michael@0: return mTokenizer; michael@0: } michael@0: michael@0: void InitializeDocWriteParserState(nsAHtml5TreeBuilderState* aState, int32_t aLine); michael@0: michael@0: void DropStreamParser() michael@0: { michael@0: if (GetStreamParser()) { michael@0: GetStreamParser()->DropTimer(); michael@0: mStreamListener->DropDelegate(); michael@0: mStreamListener = nullptr; michael@0: } michael@0: } michael@0: michael@0: void StartTokenizer(bool aScriptingEnabled); michael@0: michael@0: void ContinueAfterFailedCharsetSwitch(); michael@0: michael@0: nsHtml5StreamParser* GetStreamParser() michael@0: { michael@0: if (!mStreamListener) { michael@0: return nullptr; michael@0: } michael@0: return mStreamListener->GetDelegate(); michael@0: } michael@0: michael@0: /** michael@0: * Parse until pending data is exhausted or a script blocks the parser michael@0: */ michael@0: nsresult ParseUntilBlocked(); michael@0: michael@0: private: michael@0: michael@0: // State variables michael@0: michael@0: /** michael@0: * Whether the last character tokenized was a carriage return (for CRLF) michael@0: */ michael@0: bool mLastWasCR; michael@0: michael@0: /** michael@0: * Whether the last character tokenized was a carriage return (for CRLF) michael@0: * when preparsing document.write. michael@0: */ michael@0: bool mDocWriteSpeculativeLastWasCR; michael@0: michael@0: /** michael@0: * The parser is blocking on a script michael@0: */ michael@0: bool mBlocked; michael@0: michael@0: /** michael@0: * Whether the document.write() speculator is already active. michael@0: */ michael@0: bool mDocWriteSpeculatorActive; michael@0: michael@0: /** michael@0: * The number of parser-inserted script currently being evaluated. michael@0: */ michael@0: int32_t mParserInsertedScriptsBeingEvaluated; michael@0: michael@0: /** michael@0: * True if document.close() has been called. michael@0: */ michael@0: bool mDocumentClosed; michael@0: michael@0: bool mInDocumentWrite; michael@0: michael@0: // Portable parser objects michael@0: /** michael@0: * The first buffer in the pending UTF-16 buffer queue michael@0: */ michael@0: nsRefPtr mFirstBuffer; michael@0: michael@0: /** michael@0: * The last buffer in the pending UTF-16 buffer queue. Always points michael@0: * to a sentinel object with nullptr as its parser key. michael@0: */ michael@0: nsHtml5OwningUTF16Buffer* mLastBuffer; // weak ref; michael@0: michael@0: /** michael@0: * The tree operation executor michael@0: */ michael@0: nsRefPtr mExecutor; michael@0: michael@0: /** michael@0: * The HTML5 tree builder michael@0: */ michael@0: const nsAutoPtr mTreeBuilder; michael@0: michael@0: /** michael@0: * The HTML5 tokenizer michael@0: */ michael@0: const nsAutoPtr mTokenizer; michael@0: michael@0: /** michael@0: * Another HTML5 tree builder for preloading document.written content. michael@0: */ michael@0: nsAutoPtr mDocWriteSpeculativeTreeBuilder; michael@0: michael@0: /** michael@0: * Another HTML5 tokenizer for preloading document.written content. michael@0: */ michael@0: nsAutoPtr mDocWriteSpeculativeTokenizer; michael@0: michael@0: /** michael@0: * The stream listener holding the stream parser. michael@0: */ michael@0: nsRefPtr mStreamListener; michael@0: michael@0: /** michael@0: * michael@0: */ michael@0: int32_t mRootContextLineNumber; michael@0: michael@0: /** michael@0: * Whether it's OK to transfer parsing back to the stream parser michael@0: */ michael@0: bool mReturnToStreamParserPermitted; michael@0: michael@0: /** michael@0: * The scoped atom table michael@0: */ michael@0: nsHtml5AtomTable mAtomTable; michael@0: michael@0: }; michael@0: #endif