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