2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set sw=2 ts=2 et tw=80: */
|
2012-05-21 15:12:37 +04: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/. */
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
1998-06-19 05:00:27 +04:00
|
|
|
#include "nsParser.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsString.h"
|
2005-12-23 01:31:36 +03:00
|
|
|
#include "nsCRT.h"
|
1998-06-18 03:13:28 +04:00
|
|
|
#include "nsScanner.h"
|
1998-04-26 07:21:14 +04:00
|
|
|
#include "plstr.h"
|
1999-05-27 10:44:44 +04:00
|
|
|
#include "nsIStringStream.h"
|
1999-06-21 04:33:51 +04:00
|
|
|
#include "nsIChannel.h"
|
2001-06-30 02:56:58 +04:00
|
|
|
#include "nsICachingChannel.h"
|
2000-08-22 11:03:33 +04:00
|
|
|
#include "nsIInputStream.h"
|
2000-04-17 10:33:31 +04:00
|
|
|
#include "CNavDTD.h"
|
2005-12-23 01:31:36 +03:00
|
|
|
#include "prenv.h"
|
2008-10-01 10:48:47 +04:00
|
|
|
#include "prlock.h"
|
|
|
|
#include "prcvar.h"
|
2000-05-04 02:09:09 +04:00
|
|
|
#include "nsParserCIID.h"
|
2001-06-30 02:56:58 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2000-05-14 23:24:59 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2002-01-09 04:37:50 +03:00
|
|
|
#include "nsExpatDriver.h"
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsIServiceManager.h"
|
2004-11-02 22:52:32 +03:00
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2005-02-18 22:18:39 +03:00
|
|
|
#include "nsIFragmentContentSink.h"
|
2005-03-30 08:18:08 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2008-10-01 10:48:47 +04:00
|
|
|
#include "nsHTMLTokenizer.h"
|
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
2009-11-21 02:12:42 +03:00
|
|
|
#include "nsMimeTypes.h"
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
#include "mozilla/CondVar.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2012-03-22 18:42:42 +04:00
|
|
|
#include "nsCharsetSource.h"
|
2013-08-12 19:28:41 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIHTMLContentSink.h"
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
|
2014-09-17 17:46:24 +04:00
|
|
|
#include "mozilla/BinarySearch.h"
|
2017-06-16 15:11:03 +03:00
|
|
|
#include "mozilla/dom/ScriptLoader.h"
|
|
|
|
#include "mozilla/Encoding.h"
|
2012-11-08 03:04:22 +04:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
2003-07-31 03:57:39 +04:00
|
|
|
|
2002-02-15 02:45:20 +03:00
|
|
|
#define NS_PARSER_FLAG_OBSERVERS_ENABLED 0x00000004
|
|
|
|
#define NS_PARSER_FLAG_PENDING_CONTINUE_EVENT 0x00000008
|
|
|
|
#define NS_PARSER_FLAG_FLUSH_TOKENS 0x00000020
|
2002-05-08 03:18:15 +04:00
|
|
|
#define NS_PARSER_FLAG_CAN_TOKENIZE 0x00000040
|
2002-02-15 02:45:20 +03:00
|
|
|
|
2001-06-21 06:06:23 +04:00
|
|
|
//-------------- Begin ParseContinue Event Definition ------------------------
|
|
|
|
/*
|
2005-12-23 01:31:36 +03:00
|
|
|
The parser can be explicitly interrupted by passing a return value of
|
|
|
|
NS_ERROR_HTMLPARSER_INTERRUPTED from BuildModel on the DTD. This will cause
|
|
|
|
the parser to stop processing and allow the application to return to the event
|
|
|
|
loop. The data which was left at the time of interruption will be processed
|
|
|
|
the next time OnDataAvailable is called. If the parser has received its final
|
|
|
|
chunk of data then OnDataAvailable will no longer be called by the networking
|
|
|
|
module, so the parser will schedule a nsParserContinueEvent which will call
|
2007-11-11 01:49:09 +03:00
|
|
|
the parser to process the remaining data after returning to the event loop.
|
2005-12-23 01:31:36 +03:00
|
|
|
If the parser is interrupted while processing the remaining data it will
|
|
|
|
schedule another ParseContinueEvent. The processing of data followed by
|
|
|
|
scheduling of the continue events will proceed until either:
|
2001-06-21 06:06:23 +04:00
|
|
|
|
|
|
|
1) All of the remaining data can be processed without interrupting
|
|
|
|
2) The parser has been cancelled.
|
|
|
|
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
This capability is currently used in CNavDTD and nsHTMLContentSink. The
|
|
|
|
nsHTMLContentSink is notified by CNavDTD when a chunk of tokens is going to be
|
|
|
|
processed and when each token is processed. The nsHTML content sink records
|
|
|
|
the time when the chunk has started processing and will return
|
|
|
|
NS_ERROR_HTMLPARSER_INTERRUPTED if the token processing time has exceeded a
|
|
|
|
threshold called max tokenizing processing time. This allows the content sink
|
|
|
|
to limit how much data is processed in a single chunk which in turn gates how
|
|
|
|
much time is spent away from the event loop. Processing smaller chunks of data
|
|
|
|
also reduces the time spent in subsequent reflows.
|
|
|
|
|
|
|
|
This capability is most apparent when loading large documents. If the maximum
|
|
|
|
token processing time is set small enough the application will remain
|
|
|
|
responsive during document load.
|
|
|
|
|
|
|
|
A side-effect of this capability is that document load is not complete when
|
|
|
|
the last chunk of data is passed to OnDataAvailable since the parser may have
|
|
|
|
been interrupted when the last chunk of data arrived. The document is complete
|
|
|
|
when all of the document has been tokenized and there aren't any pending
|
|
|
|
nsParserContinueEvents. This can cause problems if the application assumes
|
|
|
|
that it can monitor the load requests to determine when the document load has
|
|
|
|
been completed. This is what happens in Mozilla. The document is considered
|
|
|
|
completely loaded when all of the load requests have been satisfied. To delay
|
|
|
|
the document load until all of the parsing has been completed the
|
|
|
|
nsHTMLContentSink adds a dummy parser load request which is not removed until
|
|
|
|
the nsHTMLContentSink's DidBuildModel is called. The CNavDTD will not call
|
|
|
|
DidBuildModel until the final chunk of data has been passed to the parser
|
|
|
|
through the OnDataAvailable and there aren't any pending
|
2001-06-21 06:06:23 +04:00
|
|
|
nsParserContineEvents.
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
Currently the parser is ignores requests to be interrupted during the
|
|
|
|
processing of script. This is because a document.write followed by JavaScript
|
|
|
|
calls to manipulate the DOM may fail if the parser was interrupted during the
|
|
|
|
document.write.
|
2001-06-21 06:06:23 +04:00
|
|
|
|
|
|
|
For more details @see bugzilla bug 76722
|
|
|
|
*/
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsParserContinueEvent : public Runnable {
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsParser> mParser;
|
2001-06-21 06:06:23 +04:00
|
|
|
|
2014-09-02 02:04:52 +04:00
|
|
|
explicit nsParserContinueEvent(nsParser* aParser)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("nsParserContinueEvent"), mParser(aParser) {}
|
2001-06-21 06:06:23 +04:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2006-05-10 21:30:15 +04:00
|
|
|
mParser->HandleParserContinueEvent(this);
|
|
|
|
return NS_OK;
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
|
|
|
};
|
2001-06-21 06:06:23 +04:00
|
|
|
|
|
|
|
//-------------- End ParseContinue Event Definition ------------------------
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
/**
|
1998-04-14 00:24:54 +04:00
|
|
|
* default constructor
|
1998-04-25 23:45:14 +04:00
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
nsParser::nsParser()
|
2018-06-14 00:08:27 +03:00
|
|
|
: mParserContext(nullptr), mCharset(WINDOWS_1252_ENCODING) {
|
2011-10-17 18:59:28 +04:00
|
|
|
Initialize(true);
|
2008-02-09 01:07:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsParser::~nsParser() { Cleanup(); }
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsParser::Initialize(bool aConstructor) {
|
2008-02-09 01:07:51 +03:00
|
|
|
if (aConstructor) {
|
|
|
|
// Raw pointer
|
|
|
|
mParserContext = 0;
|
|
|
|
} else {
|
|
|
|
// nsCOMPtrs
|
2012-07-30 18:20:58 +04:00
|
|
|
mObserver = nullptr;
|
2008-12-08 03:15:52 +03:00
|
|
|
mUnusedInput.Truncate();
|
2008-02-09 01:07:51 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mContinueEvent = nullptr;
|
2008-02-09 01:07:51 +03:00
|
|
|
mCharsetSource = kCharsetUninitialized;
|
2017-06-18 14:37:50 +03:00
|
|
|
mCharset = WINDOWS_1252_ENCODING;
|
2008-02-09 01:07:51 +03:00
|
|
|
mInternalState = NS_OK;
|
2012-07-27 17:59:29 +04:00
|
|
|
mStreamStatus = NS_OK;
|
2008-02-09 01:07:51 +03:00
|
|
|
mCommand = eViewNormal;
|
2017-03-16 03:31:00 +03:00
|
|
|
mBlocked = 0;
|
2005-12-23 01:31:36 +03:00
|
|
|
mFlags = NS_PARSER_FLAG_OBSERVERS_ENABLED | NS_PARSER_FLAG_CAN_TOKENIZE;
|
2009-11-07 02:30:24 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mProcessingNetworkData = false;
|
2012-03-31 18:10:34 +04:00
|
|
|
mIsAboutBlank = false;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2008-02-09 01:07:51 +03:00
|
|
|
void nsParser::Cleanup() {
|
2006-02-01 21:57:09 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (mParserContext && mParserContext->mPrevContext) {
|
|
|
|
NS_WARNING("Extra parser contexts still on the parser stack");
|
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
#endif
|
2006-02-01 21:57:09 +03:00
|
|
|
|
|
|
|
while (mParserContext) {
|
|
|
|
CParserContext* pc = mParserContext->mPrevContext;
|
|
|
|
delete mParserContext;
|
|
|
|
mParserContext = pc;
|
|
|
|
}
|
2001-06-21 06:06:23 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// It should not be possible for this flag to be set when we are getting
|
|
|
|
// destroyed since this flag implies a pending nsParserContinueEvent, which
|
|
|
|
// has an owning reference to |this|.
|
|
|
|
NS_ASSERTION(!(mFlags & NS_PARSER_FLAG_PENDING_CONTINUE_EVENT), "bad");
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsParser)
|
|
|
|
|
2008-02-09 01:07:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsParser)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDTD)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSink)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mObserver)
|
2008-02-09 01:07:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsParser)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDTD)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSink)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mObserver)
|
2008-02-09 01:07:51 +03:00
|
|
|
CParserContext* pc = tmp->mParserContext;
|
|
|
|
while (pc) {
|
|
|
|
cb.NoteXPCOMChild(pc->mTokenizer);
|
|
|
|
pc = pc->mPrevContext;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2011-03-06 14:11:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsParser)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsParser)
|
2007-09-28 07:00:35 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsParser)
|
2008-02-09 01:07:51 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIParser)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
2010-12-08 15:37:19 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2008-02-09 01:07:51 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIParser)
|
2007-09-28 07:00:35 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2001-06-21 06:06:23 +04:00
|
|
|
// The parser continue event is posted only if
|
|
|
|
// all of the data to parse has been passed to ::OnDataAvailable
|
|
|
|
// and the parser has been interrupted by the content sink
|
|
|
|
// because the processing of tokens took too long.
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2001-06-21 06:06:23 +04:00
|
|
|
nsresult nsParser::PostContinueEvent() {
|
2006-05-10 21:30:15 +04:00
|
|
|
if (!(mFlags & NS_PARSER_FLAG_PENDING_CONTINUE_EVENT)) {
|
|
|
|
// If this flag isn't set, then there shouldn't be a live continue event!
|
|
|
|
NS_ASSERTION(!mContinueEvent, "bad");
|
|
|
|
|
|
|
|
// This creates a reference cycle between this and the event that is
|
|
|
|
// broken when the event fires.
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsParserContinueEvent(this);
|
|
|
|
if (NS_FAILED(NS_DispatchToCurrentThread(event))) {
|
|
|
|
NS_WARNING("failed to dispatch parser continuation event");
|
2005-12-23 01:31:36 +03:00
|
|
|
} else {
|
2003-10-06 05:46:31 +04:00
|
|
|
mFlags |= NS_PARSER_FLAG_PENDING_CONTINUE_EVENT;
|
2006-05-10 21:30:15 +04:00
|
|
|
mContinueEvent = event;
|
2005-01-27 22:28:22 +03:00
|
|
|
}
|
2001-06-21 06:06:23 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsParser::GetCommand(nsCString& aCommand) { aCommand = mCommandStr; }
|
2000-05-01 10:58:53 +04:00
|
|
|
|
1998-11-11 14:55:32 +03:00
|
|
|
/**
|
|
|
|
* Call this method once you've created a parser, and want to instruct it
|
|
|
|
* about the command which caused the parser to be constructed. For example,
|
|
|
|
* this allows us to select a DTD which can do, say, view-source.
|
2005-12-23 01:31:36 +03:00
|
|
|
*
|
2003-04-09 01:23:34 +04:00
|
|
|
* @param aCommand the command string to set
|
1998-11-11 14:55:32 +03:00
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsParser::SetCommand(const char* aCommand) {
|
2004-07-18 01:45:37 +04:00
|
|
|
mCommandStr.Assign(aCommand);
|
2014-05-22 07:48:51 +04:00
|
|
|
if (mCommandStr.EqualsLiteral("view-source")) {
|
2005-12-23 01:31:36 +03:00
|
|
|
mCommand = eViewSource;
|
2014-05-22 07:48:51 +04:00
|
|
|
} else if (mCommandStr.EqualsLiteral("view-fragment")) {
|
2005-12-23 01:31:36 +03:00
|
|
|
mCommand = eViewFragment;
|
|
|
|
} else {
|
|
|
|
mCommand = eViewNormal;
|
|
|
|
}
|
1998-11-11 14:55:32 +03:00
|
|
|
}
|
|
|
|
|
2000-03-25 06:35:50 +03:00
|
|
|
/**
|
|
|
|
* Call this method once you've created a parser, and want to instruct it
|
|
|
|
* about the command which caused the parser to be constructed. For example,
|
|
|
|
* this allows us to select a DTD which can do, say, view-source.
|
2005-12-23 01:31:36 +03:00
|
|
|
*
|
2003-04-09 01:23:34 +04:00
|
|
|
* @param aParserCommand the command to set
|
2000-03-25 06:35:50 +03:00
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsParser::SetCommand(eParserCommands aParserCommand) {
|
2003-04-09 01:23:34 +04:00
|
|
|
mCommand = aParserCommand;
|
2000-03-25 06:35:50 +03:00
|
|
|
}
|
1999-04-26 21:49:45 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this method once you've created a parser, and want to instruct it
|
|
|
|
* about what charset to load
|
2005-12-23 01:31:36 +03:00
|
|
|
*
|
2003-04-09 01:23:34 +04:00
|
|
|
* @param aCharset- the charset of a document
|
|
|
|
* @param aCharsetSource- the source of the charset
|
1999-04-26 21:49:45 +04:00
|
|
|
*/
|
2017-06-18 14:37:50 +03:00
|
|
|
void nsParser::SetDocumentCharset(NotNull<const Encoding*> aCharset,
|
|
|
|
int32_t aCharsetSource) {
|
1999-04-26 21:49:45 +04:00
|
|
|
mCharset = aCharset;
|
2005-12-23 01:31:36 +03:00
|
|
|
mCharsetSource = aCharsetSource;
|
|
|
|
if (mParserContext && mParserContext->mScanner) {
|
2000-02-15 12:13:27 +03:00
|
|
|
mParserContext->mScanner->SetDocumentCharset(aCharset, aCharsetSource);
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
1999-04-26 21:49:45 +04:00
|
|
|
}
|
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
void nsParser::SetSinkCharset(NotNull<const Encoding*> aCharset) {
|
2001-06-30 02:56:58 +04:00
|
|
|
if (mSink) {
|
|
|
|
mSink->SetDocumentCharset(aCharset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-04-25 23:45:14 +04:00
|
|
|
/**
|
1998-04-14 00:24:54 +04:00
|
|
|
* This method gets called in order to set the content
|
|
|
|
* sink for this parser to dump nodes to.
|
2005-12-23 01:31:36 +03:00
|
|
|
*
|
1998-04-14 00:24:54 +04:00
|
|
|
* @param nsIContentSink interface for node receiver
|
1998-04-25 23:45:14 +04:00
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsParser::SetContentSink(nsIContentSink* aSink) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aSink, "sink cannot be null!");
|
2003-04-09 01:23:34 +04:00
|
|
|
mSink = aSink;
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2003-04-09 01:23:34 +04:00
|
|
|
if (mSink) {
|
1998-12-11 20:02:37 +03:00
|
|
|
mSink->SetParser(this);
|
2012-03-31 18:10:34 +04:00
|
|
|
nsCOMPtr<nsIHTMLContentSink> htmlSink = do_QueryInterface(mSink);
|
|
|
|
if (htmlSink) {
|
|
|
|
mIsAboutBlank = true;
|
|
|
|
}
|
1998-04-22 22:32:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-26 05:55:59 +03:00
|
|
|
/**
|
2005-12-23 01:31:36 +03:00
|
|
|
* retrieve the sink set into the parser
|
|
|
|
* @return current sink
|
1998-11-26 05:55:59 +03:00
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP_(nsIContentSink*)
|
|
|
|
nsParser::GetContentSink() { return mSink; }
|
1998-11-26 05:55:59 +03:00
|
|
|
|
2006-01-26 22:07:41 +03:00
|
|
|
static nsIDTD* FindSuitableDTD(CParserContext& aParserContext) {
|
|
|
|
// We always find a DTD.
|
|
|
|
aParserContext.mAutoDetectStatus = ePrimaryDetect;
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2006-01-26 22:07:41 +03:00
|
|
|
// Quick check for view source.
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aParserContext.mParserCommand != eViewSource,
|
|
|
|
"The old parser is not supposed to be used for View Source "
|
|
|
|
"anymore.");
|
1998-07-10 09:35:23 +04:00
|
|
|
|
2006-01-26 22:07:41 +03:00
|
|
|
// Now see if we're parsing HTML (which, as far as we're concerned, simply
|
|
|
|
// means "not XML").
|
|
|
|
if (aParserContext.mDocType != eXML) {
|
2009-03-24 04:26:46 +03:00
|
|
|
return new CNavDTD();
|
1999-02-19 02:45:44 +03:00
|
|
|
}
|
2002-12-06 04:54:39 +03:00
|
|
|
|
2006-01-26 22:07:41 +03:00
|
|
|
// If we're here, then we'd better be parsing XML.
|
|
|
|
NS_ASSERTION(aParserContext.mDocType == eXML,
|
|
|
|
"What are you trying to send me, here?");
|
2009-03-24 04:26:46 +03:00
|
|
|
return new nsExpatDriver();
|
1998-06-23 04:53:50 +04:00
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP
|
2003-04-09 01:23:34 +04:00
|
|
|
nsParser::CancelParsingEvents() {
|
2002-02-15 02:45:20 +03:00
|
|
|
if (mFlags & NS_PARSER_FLAG_PENDING_CONTINUE_EVENT) {
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_ASSERTION(mContinueEvent, "mContinueEvent is null");
|
|
|
|
// Revoke the pending continue parsing event
|
2012-07-30 18:20:58 +04:00
|
|
|
mContinueEvent = nullptr;
|
2002-02-15 02:45:20 +03:00
|
|
|
mFlags &= ~NS_PARSER_FLAG_PENDING_CONTINUE_EVENT;
|
2001-06-21 06:06:23 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-21 06:58:12 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
1998-07-02 12:14:22 +04:00
|
|
|
|
2009-06-24 01:22:16 +04:00
|
|
|
/**
|
|
|
|
* Evalutes EXPR1 and EXPR2 exactly once each, in that order. Stores the value
|
|
|
|
* of EXPR2 in RV is EXPR2 fails, otherwise RV contains the result of EXPR1
|
|
|
|
* (which could be success or failure).
|
|
|
|
*
|
|
|
|
* To understand the motivation for this construct, consider these example
|
|
|
|
* methods:
|
|
|
|
*
|
|
|
|
* nsresult nsSomething::DoThatThing(nsIWhatever* obj) {
|
|
|
|
* nsresult rv = NS_OK;
|
|
|
|
* ...
|
|
|
|
* return obj->DoThatThing();
|
|
|
|
* NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
* ...
|
|
|
|
* return rv;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* void nsCaller::MakeThingsHappen() {
|
|
|
|
* return mSomething->DoThatThing(mWhatever);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Suppose, for whatever reason*, we want to shift responsibility for calling
|
|
|
|
* mWhatever->DoThatThing() from nsSomething::DoThatThing up to
|
|
|
|
* nsCaller::MakeThingsHappen. We might rewrite the two methods as follows:
|
|
|
|
*
|
|
|
|
* nsresult nsSomething::DoThatThing() {
|
|
|
|
* nsresult rv = NS_OK;
|
|
|
|
* ...
|
|
|
|
* ...
|
|
|
|
* return rv;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* void nsCaller::MakeThingsHappen() {
|
|
|
|
* nsresult rv;
|
|
|
|
* PREFER_LATTER_ERROR_CODE(mSomething->DoThatThing(),
|
|
|
|
* mWhatever->DoThatThing(),
|
|
|
|
* rv);
|
|
|
|
* return rv;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* *Possible reasons include: nsCaller doesn't want to give mSomething access
|
|
|
|
* to mWhatever, nsCaller wants to guarantee that mWhatever->DoThatThing() will
|
|
|
|
* be called regardless of how nsSomething::DoThatThing behaves, &c.
|
|
|
|
*/
|
|
|
|
#define PREFER_LATTER_ERROR_CODE(EXPR1, EXPR2, RV) \
|
|
|
|
{ \
|
|
|
|
nsresult RV##__temp = EXPR1; \
|
|
|
|
RV = EXPR2; \
|
|
|
|
if (NS_FAILED(RV)) { \
|
|
|
|
RV = RV##__temp; \
|
|
|
|
} \
|
|
|
|
}
|
1998-10-27 08:13:16 +03:00
|
|
|
|
1998-06-23 04:53:50 +04:00
|
|
|
/**
|
|
|
|
* This gets called just prior to the model actually
|
|
|
|
* being constructed. It's important to make this the
|
|
|
|
* last thing that happens right before parsing, so we
|
|
|
|
* can delay until the last moment the resolution of
|
|
|
|
* which DTD to use (unless of course we're assigned one).
|
1998-05-22 00:38:32 +04:00
|
|
|
*/
|
2002-12-06 04:54:39 +03:00
|
|
|
nsresult nsParser::WillBuildModel(nsString& aFilename) {
|
2016-12-23 05:51:04 +03:00
|
|
|
if (!mParserContext) return NS_ERROR_HTMLPARSER_INVALIDPARSERCONTEXT;
|
1999-01-09 04:09:02 +03:00
|
|
|
|
2002-11-28 22:43:51 +03:00
|
|
|
if (eUnknownDetect != mParserContext->mAutoDetectStatus) return NS_OK;
|
1999-01-09 04:09:02 +03:00
|
|
|
|
2002-11-28 22:43:51 +03:00
|
|
|
if (eDTDMode_unknown == mParserContext->mDTDMode ||
|
2004-05-14 03:39:06 +04:00
|
|
|
eDTDMode_autodetect == mParserContext->mDTDMode) {
|
2017-02-16 11:05:27 +03:00
|
|
|
if (mIsAboutBlank) {
|
|
|
|
mParserContext->mDTDMode = eDTDMode_quirks;
|
|
|
|
mParserContext->mDocType = eHTML_Quirks;
|
|
|
|
} else {
|
|
|
|
mParserContext->mDTDMode = eDTDMode_full_standards;
|
|
|
|
mParserContext->mDocType = eXML;
|
|
|
|
}
|
|
|
|
} // else XML fragment with nested parser context
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2009-03-24 04:26:46 +03:00
|
|
|
NS_ASSERTION(!mDTD || !mParserContext->mPrevContext,
|
|
|
|
"Clobbering DTD for non-root parser context!");
|
|
|
|
mDTD = FindSuitableDTD(*mParserContext);
|
|
|
|
NS_ENSURE_TRUE(mDTD, NS_ERROR_OUT_OF_MEMORY);
|
2000-04-17 10:33:31 +04:00
|
|
|
|
2002-11-28 22:43:51 +03:00
|
|
|
nsITokenizer* tokenizer;
|
2009-03-24 12:43:42 +03:00
|
|
|
nsresult rv = mParserContext->GetTokenizer(mDTD, mSink, tokenizer);
|
2005-11-11 22:11:36 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-06-24 01:22:16 +04:00
|
|
|
rv = mDTD->WillBuildModel(*mParserContext, tokenizer, mSink);
|
|
|
|
nsresult sinkResult = mSink->WillBuildModel(mDTD->GetMode());
|
|
|
|
// nsIDTD::WillBuildModel used to be responsible for calling
|
|
|
|
// nsIContentSink::WillBuildModel, but that obligation isn't expressible
|
|
|
|
// in the nsIDTD interface itself, so it's sounder and simpler to give that
|
|
|
|
// responsibility back to the parser. The former behavior of the DTD was to
|
|
|
|
// NS_ENSURE_SUCCESS the sink WillBuildModel call, so if the sink returns
|
|
|
|
// failure we should use sinkResult instead of rv, to preserve the old error
|
|
|
|
// handling behavior of the DTD:
|
|
|
|
return NS_FAILED(sinkResult) ? sinkResult : rv;
|
1998-05-22 00:38:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
1998-11-26 05:55:59 +03:00
|
|
|
* This gets called when the parser is done with its input.
|
|
|
|
* Note that the parser may have been called recursively, so we
|
|
|
|
* have to check for a prev. context before closing out the DTD/sink.
|
1998-05-22 00:38:32 +04:00
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
nsresult nsParser::DidBuildModel(nsresult anErrorCode) {
|
|
|
|
nsresult result = anErrorCode;
|
1998-11-26 05:55:59 +03:00
|
|
|
|
2001-06-21 06:06:23 +04:00
|
|
|
if (IsComplete()) {
|
2002-01-16 03:16:51 +03:00
|
|
|
if (mParserContext && !mParserContext->mPrevContext) {
|
2009-01-27 08:41:25 +03:00
|
|
|
// Let sink know if we're about to end load because we've been terminated.
|
|
|
|
// In that case we don't want it to run deferred scripts.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool terminated = mInternalState == NS_ERROR_HTMLPARSER_STOPPARSING;
|
2009-10-02 15:13:59 +04:00
|
|
|
if (mDTD && mSink) {
|
2009-06-24 01:22:17 +04:00
|
|
|
nsresult dtdResult = mDTD->DidBuildModel(anErrorCode),
|
2009-10-02 15:13:59 +04:00
|
|
|
sinkResult = mSink->DidBuildModel(terminated);
|
2009-06-24 01:22:16 +04:00
|
|
|
// nsIDTD::DidBuildModel used to be responsible for calling
|
|
|
|
// nsIContentSink::DidBuildModel, but that obligation isn't expressible
|
|
|
|
// in the nsIDTD interface itself, so it's sounder and simpler to give
|
|
|
|
// that responsibility back to the parser. The former behavior of the
|
|
|
|
// DTD was to NS_ENSURE_SUCCESS the sink DidBuildModel call, so if the
|
|
|
|
// sink returns failure we should use sinkResult instead of dtdResult,
|
|
|
|
// to preserve the old error handling behavior of the DTD:
|
|
|
|
result = NS_FAILED(sinkResult) ? sinkResult : dtdResult;
|
2001-06-21 06:06:23 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2001-06-21 06:06:23 +04:00
|
|
|
// Ref. to bug 61462.
|
2016-11-10 06:11:27 +03:00
|
|
|
mParserContext->mRequest = nullptr;
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
2001-06-21 06:06:23 +04:00
|
|
|
}
|
1998-08-05 06:01:44 +04:00
|
|
|
|
1998-07-04 03:14:21 +04:00
|
|
|
return result;
|
1998-05-22 00:38:32 +04:00
|
|
|
}
|
|
|
|
|
1998-07-02 12:14:22 +04:00
|
|
|
/**
|
1998-07-23 12:21:02 +04:00
|
|
|
* This method adds a new parser context to the list,
|
|
|
|
* pushing the current one to the next position.
|
2005-12-23 01:31:36 +03:00
|
|
|
*
|
1998-07-23 12:21:02 +04:00
|
|
|
* @param ptr to new context
|
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
void nsParser::PushContext(CParserContext& aContext) {
|
2009-03-24 04:26:46 +03:00
|
|
|
NS_ASSERTION(aContext.mPrevContext == mParserContext,
|
|
|
|
"Trying to push a context whose previous context differs from "
|
|
|
|
"the current parser context.");
|
2005-12-23 01:31:36 +03:00
|
|
|
mParserContext = &aContext;
|
1998-07-02 12:14:22 +04:00
|
|
|
}
|
|
|
|
|
1998-07-14 01:13:09 +04:00
|
|
|
/**
|
1998-07-23 12:21:02 +04:00
|
|
|
* This method pops the topmost context off the stack,
|
|
|
|
* returning it to the user. The next context (if any)
|
|
|
|
* becomes the current context.
|
|
|
|
* @update gess7/22/98
|
|
|
|
* @return prev. context
|
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
CParserContext* nsParser::PopContext() {
|
2003-05-29 00:56:21 +04:00
|
|
|
CParserContext* oldContext = mParserContext;
|
|
|
|
if (oldContext) {
|
|
|
|
mParserContext = oldContext->mPrevContext;
|
1999-04-14 02:22:51 +04:00
|
|
|
if (mParserContext) {
|
2003-05-29 00:56:21 +04:00
|
|
|
// If the old context was blocked, propagate the blocked state
|
|
|
|
// back to the new one. Also, propagate the stream listener state
|
|
|
|
// but don't override onStop state to guarantee the call to
|
|
|
|
// DidBuildModel().
|
|
|
|
if (mParserContext->mStreamListenerState != eOnStop) {
|
2001-02-02 00:03:48 +03:00
|
|
|
mParserContext->mStreamListenerState = oldContext->mStreamListenerState;
|
|
|
|
}
|
1999-04-14 02:22:51 +04:00
|
|
|
}
|
1998-07-23 12:21:02 +04:00
|
|
|
}
|
|
|
|
return oldContext;
|
1998-07-14 01:13:09 +04:00
|
|
|
}
|
|
|
|
|
1998-09-02 11:12:49 +04:00
|
|
|
/**
|
|
|
|
* Call this when you want control whether or not the parser will parse
|
2005-12-23 01:31:36 +03:00
|
|
|
* and tokenize input (TRUE), or whether it just caches input to be
|
1998-09-02 11:12:49 +04:00
|
|
|
* parsed later (FALSE).
|
2005-12-23 01:31:36 +03:00
|
|
|
*
|
1999-02-01 07:24:37 +03:00
|
|
|
* @param aState determines whether we parse/tokenize or just cache.
|
|
|
|
* @return current state
|
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
void nsParser::SetUnusedInput(nsString& aBuffer) { mUnusedInput = aBuffer; }
|
1999-02-01 07:24:37 +03:00
|
|
|
|
1999-07-08 08:39:38 +04:00
|
|
|
/**
|
|
|
|
* Call this when you want to *force* the parser to terminate the
|
|
|
|
* parsing process altogether. This is binary -- so once you terminate
|
|
|
|
* you can't resume without restarting altogether.
|
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParser::Terminate(void) {
|
2007-05-21 22:09:54 +04:00
|
|
|
// We should only call DidBuildModel once, so don't do anything if this is
|
|
|
|
// the second time that Terminate has been called.
|
|
|
|
if (mInternalState == NS_ERROR_HTMLPARSER_STOPPARSING) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-02-15 02:45:20 +03:00
|
|
|
nsresult result = NS_OK;
|
2005-04-28 20:57:24 +04:00
|
|
|
// XXX - [ until we figure out a way to break parser-sink circularity ]
|
|
|
|
// Hack - Hold a reference until we are completely done...
|
2005-12-23 01:31:36 +03:00
|
|
|
nsCOMPtr<nsIParser> kungFuDeathGrip(this);
|
2005-04-28 20:57:24 +04:00
|
|
|
mInternalState = result = NS_ERROR_HTMLPARSER_STOPPARSING;
|
|
|
|
|
|
|
|
// CancelParsingEvents must be called to avoid leaking the nsParser object
|
|
|
|
// @see bug 108049
|
2005-12-23 01:31:36 +03:00
|
|
|
// If NS_PARSER_FLAG_PENDING_CONTINUE_EVENT is set then CancelParsingEvents
|
|
|
|
// will reset it so DidBuildModel will call DidBuildModel on the DTD. Note:
|
2005-04-28 20:57:24 +04:00
|
|
|
// The IsComplete() call inside of DidBuildModel looks at the
|
|
|
|
// pendingContinueEvents flag.
|
|
|
|
CancelParsingEvents();
|
|
|
|
|
2005-08-23 01:51:00 +04:00
|
|
|
// If we got interrupted in the middle of a document.write, then we might
|
|
|
|
// have more than one parser context on our parsercontext stack. This has
|
|
|
|
// the effect of making DidBuildModel a no-op, meaning that we never call
|
|
|
|
// our sink's DidBuildModel and break the reference cycle, causing a leak.
|
|
|
|
// Since we're getting terminated, we manually clean up our context stack.
|
|
|
|
while (mParserContext && mParserContext->mPrevContext) {
|
|
|
|
CParserContext* prev = mParserContext->mPrevContext;
|
|
|
|
delete mParserContext;
|
|
|
|
mParserContext = prev;
|
|
|
|
}
|
|
|
|
|
2009-03-24 04:26:46 +03:00
|
|
|
if (mDTD) {
|
|
|
|
mDTD->Terminate();
|
2002-02-15 02:45:20 +03:00
|
|
|
DidBuildModel(result);
|
2005-12-23 01:31:36 +03:00
|
|
|
} else if (mSink) {
|
2005-04-28 20:57:24 +04:00
|
|
|
// We have no parser context or no DTD yet (so we got terminated before we
|
|
|
|
// got any data). Manually break the reference cycle with the sink.
|
2011-10-17 18:59:28 +04:00
|
|
|
result = mSink->DidBuildModel(true);
|
2005-06-02 02:37:04 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2005-04-28 20:57:24 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2005-04-28 20:57:24 +04:00
|
|
|
return NS_OK;
|
1999-07-08 08:39:38 +04:00
|
|
|
}
|
1999-02-01 07:24:37 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParser::ContinueInterruptedParsing() {
|
2008-12-20 02:45:55 +03:00
|
|
|
// If there are scripts executing, then the content sink is jumping the gun
|
|
|
|
// (probably due to a synchronous XMLHttpRequest) and will re-enable us
|
|
|
|
// later, see bug 460706.
|
2009-11-07 02:30:24 +03:00
|
|
|
if (!IsOkToProcessNetworkData()) {
|
2008-12-20 02:45:55 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-12-15 09:10:59 +03:00
|
|
|
// If the stream has already finished, there's a good chance
|
|
|
|
// that we might start closing things down when the parser
|
|
|
|
// is reenabled. To make sure that we're not deleted across
|
|
|
|
// the reenabling process, hold a reference to ourselves.
|
2000-03-11 11:08:24 +03:00
|
|
|
nsresult result = NS_OK;
|
2005-01-27 22:28:22 +03:00
|
|
|
nsCOMPtr<nsIParser> kungFuDeathGrip(this);
|
2013-04-05 16:36:16 +04:00
|
|
|
nsCOMPtr<nsIContentSink> sinkDeathGrip(mSink);
|
1998-12-15 09:10:59 +03:00
|
|
|
|
2005-05-17 20:27:21 +04:00
|
|
|
#ifdef DEBUG
|
2017-03-16 03:31:00 +03:00
|
|
|
if (mBlocked) {
|
2005-05-17 20:27:21 +04:00
|
|
|
NS_WARNING("Don't call ContinueInterruptedParsing on a blocked parser.");
|
|
|
|
}
|
|
|
|
#endif
|
2000-03-17 07:12:17 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isFinalChunk =
|
2005-12-23 01:31:36 +03:00
|
|
|
mParserContext && mParserContext->mStreamListenerState == eOnStop;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mProcessingNetworkData = true;
|
2016-07-18 19:42:35 +03:00
|
|
|
if (sinkDeathGrip) {
|
|
|
|
sinkDeathGrip->WillParse();
|
2008-10-31 00:31:00 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
result = ResumeParse(true, isFinalChunk); // Ref. bug 57999
|
|
|
|
mProcessingNetworkData = false;
|
2005-12-23 01:31:36 +03:00
|
|
|
|
|
|
|
if (result != NS_OK) {
|
2001-02-07 02:35:29 +03:00
|
|
|
result = mInternalState;
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
|
|
|
|
1999-07-28 03:32:30 +04:00
|
|
|
return result;
|
1998-09-02 11:12:49 +04:00
|
|
|
}
|
|
|
|
|
2001-02-07 02:35:29 +03:00
|
|
|
/**
|
2017-03-16 03:31:00 +03:00
|
|
|
* Stops parsing temporarily. That is, it will prevent the
|
|
|
|
* parser from building up content model while scripts
|
|
|
|
* are being loaded (either an external script from a web
|
|
|
|
* page, or any number of extension content scripts).
|
2001-02-07 02:35:29 +03:00
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsParser::BlockParser() { mBlocked++; }
|
2001-02-07 02:35:29 +03:00
|
|
|
|
|
|
|
/**
|
2005-12-23 01:31:36 +03:00
|
|
|
* Open up the parser for tokenization, building up content
|
|
|
|
* model..etc. However, this method does not resume parsing
|
2001-02-07 02:35:29 +03:00
|
|
|
* automatically. It's the callers' responsibility to restart
|
|
|
|
* the parsing engine.
|
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsParser::UnblockParser() {
|
2017-03-16 03:31:00 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mBlocked > 0);
|
|
|
|
if (MOZ_LIKELY(mBlocked > 0)) {
|
|
|
|
mBlocked--;
|
2005-05-17 20:27:21 +04:00
|
|
|
}
|
2001-02-07 02:35:29 +03:00
|
|
|
}
|
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsParser::ContinueInterruptedParsingAsync() {
|
2017-03-29 01:24:26 +03:00
|
|
|
MOZ_ASSERT(mSink);
|
|
|
|
if (MOZ_LIKELY(mSink)) {
|
|
|
|
mSink->ContinueInterruptedParsingAsync();
|
|
|
|
}
|
2012-01-20 15:16:27 +04:00
|
|
|
}
|
|
|
|
|
1999-04-14 02:22:51 +04:00
|
|
|
/**
|
|
|
|
* Call this to query whether the parser is enabled or not.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2005-12-23 01:31:36 +03:00
|
|
|
nsParser::IsParserEnabled() { return !mBlocked; }
|
1999-04-14 02:22:51 +04:00
|
|
|
|
2001-06-21 06:06:23 +04:00
|
|
|
/**
|
|
|
|
* Call this to query whether the parser thinks it's done with parsing.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2005-12-23 01:31:36 +03:00
|
|
|
nsParser::IsComplete() {
|
2002-02-15 02:45:20 +03:00
|
|
|
return !(mFlags & NS_PARSER_FLAG_PENDING_CONTINUE_EVENT);
|
2001-06-21 06:06:23 +04:00
|
|
|
}
|
|
|
|
|
2008-12-12 01:20:45 +03:00
|
|
|
void nsParser::HandleParserContinueEvent(nsParserContinueEvent* ev) {
|
2006-05-10 21:30:15 +04:00
|
|
|
// Ignore any revoked continue events...
|
|
|
|
if (mContinueEvent != ev) return;
|
|
|
|
|
2002-02-15 02:45:20 +03:00
|
|
|
mFlags &= ~NS_PARSER_FLAG_PENDING_CONTINUE_EVENT;
|
2012-07-30 18:20:58 +04:00
|
|
|
mContinueEvent = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2009-11-07 02:30:24 +03:00
|
|
|
NS_ASSERTION(IsOkToProcessNetworkData(),
|
|
|
|
"Interrupted in the middle of a script?");
|
2005-01-27 22:28:22 +03:00
|
|
|
ContinueInterruptedParsing();
|
2001-06-21 06:06:23 +04:00
|
|
|
}
|
|
|
|
|
2009-10-15 15:29:11 +04:00
|
|
|
bool nsParser::IsInsertionPointDefined() { return false; }
|
|
|
|
|
2019-05-08 09:10:47 +03:00
|
|
|
void nsParser::IncrementScriptNestingLevel() {}
|
2009-10-15 15:29:11 +04:00
|
|
|
|
2019-05-08 09:10:47 +03:00
|
|
|
void nsParser::DecrementScriptNestingLevel() {}
|
|
|
|
|
|
|
|
bool nsParser::HasNonzeroScriptNestingLevel() const { return false; }
|
2009-10-15 15:29:11 +04:00
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
void nsParser::MarkAsNotScriptCreated(const char* aCommand) {}
|
2009-10-15 15:29:11 +04:00
|
|
|
|
|
|
|
bool nsParser::IsScriptCreated() { return false; }
|
|
|
|
|
1998-04-25 23:45:14 +04:00
|
|
|
/**
|
2005-12-23 01:31:36 +03:00
|
|
|
* This is the main controlling routine in the parsing process.
|
|
|
|
* Note that it may get called multiple times for the same scanner,
|
|
|
|
* since this is a pushed based system, and all the tokens may
|
|
|
|
* not have been consumed by the scanner during a given invocation
|
|
|
|
* of this method.
|
1998-04-25 23:45:14 +04:00
|
|
|
*/
|
2003-04-09 01:23:34 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParser::Parse(nsIURI* aURL, nsIRequestObserver* aListener, void* aKey,
|
|
|
|
nsDTDMode aMode) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aURL, "Error: Null URL given");
|
1998-05-15 02:19:08 +04:00
|
|
|
|
2016-12-23 05:51:04 +03:00
|
|
|
nsresult result = NS_ERROR_HTMLPARSER_BADURL;
|
2000-03-07 05:35:20 +03:00
|
|
|
mObserver = aListener;
|
2005-12-23 01:31:36 +03:00
|
|
|
|
|
|
|
if (aURL) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2002-03-06 10:48:55 +03:00
|
|
|
nsresult rv = aURL->GetSpec(spec);
|
2005-12-23 01:31:36 +03:00
|
|
|
if (rv != NS_OK) {
|
1999-09-21 02:24:33 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_ConvertUTF8toUTF16 theName(spec);
|
1999-03-12 17:31:20 +03:00
|
|
|
|
2012-11-06 15:57:51 +04:00
|
|
|
nsScanner* theScanner = new nsScanner(theName, false);
|
2009-03-24 04:26:46 +03:00
|
|
|
CParserContext* pc = new CParserContext(mParserContext, theScanner, aKey,
|
|
|
|
mCommand, aListener);
|
2005-12-23 01:31:36 +03:00
|
|
|
if (pc && theScanner) {
|
2011-10-17 18:59:28 +04:00
|
|
|
pc->mMultipart = true;
|
2005-12-23 01:31:36 +03:00
|
|
|
pc->mContextType = CParserContext::eCTURL;
|
|
|
|
pc->mDTDMode = aMode;
|
1999-01-15 22:29:35 +03:00
|
|
|
PushContext(*pc);
|
2004-11-02 22:52:32 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
result = NS_OK;
|
|
|
|
} else {
|
|
|
|
result = mInternalState = NS_ERROR_HTMLPARSER_BADCONTEXT;
|
1999-05-19 09:01:46 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
1999-01-09 04:09:02 +03:00
|
|
|
return result;
|
1998-07-01 15:11:05 +04:00
|
|
|
}
|
|
|
|
|
1998-05-15 02:19:08 +04:00
|
|
|
/**
|
2012-03-27 10:27:45 +04:00
|
|
|
* Used by XML fragment parsing below.
|
1998-07-10 09:35:23 +04:00
|
|
|
*
|
1998-07-23 12:21:02 +04:00
|
|
|
* @param aSourceBuffer contains a string-full of real content
|
1998-05-15 02:19:08 +04:00
|
|
|
*/
|
2003-04-09 01:23:34 +04:00
|
|
|
nsresult nsParser::Parse(const nsAString& aSourceBuffer, void* aKey,
|
2012-03-27 10:27:45 +04:00
|
|
|
bool aLastCall) {
|
2005-12-23 01:31:36 +03:00
|
|
|
nsresult result = NS_OK;
|
2002-03-06 10:48:55 +03:00
|
|
|
|
2006-02-01 21:57:09 +03:00
|
|
|
// Don't bother if we're never going to parse this.
|
|
|
|
if (mInternalState == NS_ERROR_HTMLPARSER_STOPPARSING) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
if (!aLastCall && aSourceBuffer.IsEmpty()) {
|
2001-06-21 06:06:23 +04:00
|
|
|
// Nothing is being passed to the parser so return
|
|
|
|
// immediately. mUnusedInput will get processed when
|
|
|
|
// some data is actually passed in.
|
2005-06-01 23:13:39 +04:00
|
|
|
// But if this is the last call, make sure to finish up
|
|
|
|
// stuff correctly.
|
2001-06-21 06:06:23 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
// Maintain a reference to ourselves so we don't go away
|
|
|
|
// till we're completely done.
|
2002-04-11 02:16:46 +04:00
|
|
|
nsCOMPtr<nsIParser> kungFuDeathGrip(this);
|
2000-03-17 07:12:17 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
if (aLastCall || !aSourceBuffer.IsEmpty() || !mUnusedInput.IsEmpty()) {
|
2006-01-24 04:25:14 +03:00
|
|
|
// Note: The following code will always find the parser context associated
|
|
|
|
// with the given key, even if that context has been suspended (e.g., for
|
|
|
|
// another document.write call). This doesn't appear to be exactly what IE
|
|
|
|
// does in the case where this happens, but this makes more sense.
|
|
|
|
CParserContext* pc = mParserContext;
|
|
|
|
while (pc && pc->mKey != aKey) {
|
|
|
|
pc = pc->mPrevContext;
|
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2006-01-24 04:25:14 +03:00
|
|
|
if (!pc) {
|
2005-12-23 01:31:36 +03:00
|
|
|
// Only make a new context if we don't have one, OR if we do, but has a
|
|
|
|
// different context key.
|
2012-11-06 15:57:51 +04:00
|
|
|
nsScanner* theScanner = new nsScanner(mUnusedInput);
|
2002-04-11 02:16:46 +04:00
|
|
|
NS_ENSURE_TRUE(theScanner, NS_ERROR_OUT_OF_MEMORY);
|
2000-03-11 11:08:24 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
eAutoDetectResult theStatus = eUnknownDetect;
|
|
|
|
|
2012-03-27 10:27:45 +04:00
|
|
|
if (mParserContext &&
|
|
|
|
mParserContext->mMimeType.EqualsLiteral("application/xml")) {
|
2005-12-23 01:31:36 +03:00
|
|
|
// Ref. Bug 90379
|
2009-03-24 04:26:46 +03:00
|
|
|
NS_ASSERTION(mDTD, "How come the DTD is null?");
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2002-04-11 02:16:46 +04:00
|
|
|
if (mParserContext) {
|
2005-12-23 01:31:36 +03:00
|
|
|
theStatus = mParserContext->mAutoDetectStatus;
|
|
|
|
// Added this to fix bug 32022.
|
2001-07-18 00:46:17 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
2000-01-08 03:53:43 +03:00
|
|
|
|
2009-03-24 04:26:46 +03:00
|
|
|
pc = new CParserContext(mParserContext, theScanner, aKey, mCommand, 0,
|
|
|
|
theStatus, aLastCall);
|
2002-04-11 02:16:46 +04:00
|
|
|
NS_ENSURE_TRUE(pc, NS_ERROR_OUT_OF_MEMORY);
|
2000-03-11 11:08:24 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
PushContext(*pc);
|
2000-03-17 07:12:17 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
pc->mMultipart = !aLastCall; // By default
|
|
|
|
if (pc->mPrevContext) {
|
|
|
|
pc->mMultipart |= pc->mPrevContext->mMultipart;
|
2002-04-11 02:16:46 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
|
|
|
// Start fix bug 40143
|
|
|
|
if (pc->mMultipart) {
|
|
|
|
pc->mStreamListenerState = eOnDataAvail;
|
|
|
|
if (pc->mScanner) {
|
2011-10-17 18:59:28 +04:00
|
|
|
pc->mScanner->SetIncremental(true);
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pc->mStreamListenerState = eOnStop;
|
|
|
|
if (pc->mScanner) {
|
2011-10-17 18:59:28 +04:00
|
|
|
pc->mScanner->SetIncremental(false);
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
2002-04-11 02:16:46 +04:00
|
|
|
}
|
|
|
|
// end fix for 40143
|
2000-03-17 07:12:17 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
pc->mContextType = CParserContext::eCTString;
|
2012-03-27 10:27:45 +04:00
|
|
|
pc->SetMimeType(NS_LITERAL_CSTRING("application/xml"));
|
|
|
|
pc->mDTDMode = eDTDMode_full_standards;
|
2004-10-28 07:24:59 +04:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
mUnusedInput.Truncate();
|
2002-04-09 04:43:00 +04:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
pc->mScanner->Append(aSourceBuffer);
|
2002-04-11 02:16:46 +04:00
|
|
|
// Do not interrupt document.write() - bug 95487
|
2011-10-17 18:59:28 +04:00
|
|
|
result = ResumeParse(false, false, false);
|
2005-12-23 01:31:36 +03:00
|
|
|
} else {
|
2006-01-24 04:25:14 +03:00
|
|
|
pc->mScanner->Append(aSourceBuffer);
|
|
|
|
if (!pc->mPrevContext) {
|
2001-02-10 01:36:23 +03:00
|
|
|
// Set stream listener state to eOnStop, on the final context - Fix
|
|
|
|
// 68160, to guarantee DidBuildModel() call - Fix 36148
|
2005-12-23 01:31:36 +03:00
|
|
|
if (aLastCall) {
|
2006-01-24 04:25:14 +03:00
|
|
|
pc->mStreamListenerState = eOnStop;
|
2011-10-17 18:59:28 +04:00
|
|
|
pc->mScanner->SetIncremental(false);
|
2006-01-24 04:25:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pc == mParserContext) {
|
|
|
|
// If pc is not mParserContext, then this call to ResumeParse would
|
|
|
|
// do the wrong thing and try to continue parsing using
|
|
|
|
// mParserContext. We need to wait to actually resume parsing on pc.
|
2011-10-17 18:59:28 +04:00
|
|
|
ResumeParse(false, false, false);
|
2001-02-10 01:36:23 +03:00
|
|
|
}
|
2000-03-18 03:49:54 +03:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
|
|
|
}
|
2002-04-11 02:16:46 +04:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
return result;
|
|
|
|
}
|
2000-03-11 11:08:24 +03:00
|
|
|
|
2009-06-29 02:44:22 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsParser::ParseFragment(const nsAString& aSourceBuffer,
|
2011-08-01 11:48:24 +04:00
|
|
|
nsTArray<nsString>& aTagStack) {
|
2001-10-20 00:48:18 +04:00
|
|
|
nsresult result = NS_OK;
|
2005-02-18 22:18:39 +03:00
|
|
|
nsAutoString theContext;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t theCount = aTagStack.Length();
|
|
|
|
uint32_t theIndex = 0;
|
2005-02-18 22:18:39 +03:00
|
|
|
|
|
|
|
// Disable observers for fragments
|
|
|
|
mFlags &= ~NS_PARSER_FLAG_OBSERVERS_ENABLED;
|
|
|
|
|
2004-10-01 02:27:45 +04:00
|
|
|
for (theIndex = 0; theIndex < theCount; theIndex++) {
|
2014-05-22 07:48:51 +04:00
|
|
|
theContext.Append('<');
|
2007-11-13 04:41:40 +03:00
|
|
|
theContext.Append(aTagStack[theCount - theIndex - 1]);
|
2014-05-22 07:48:51 +04:00
|
|
|
theContext.Append('>');
|
1999-06-04 02:38:38 +04:00
|
|
|
}
|
2004-10-01 02:27:45 +04:00
|
|
|
|
2008-07-15 13:35:07 +04:00
|
|
|
if (theCount == 0) {
|
|
|
|
// Ensure that the buffer is not empty. Because none of the DTDs care
|
|
|
|
// about leading whitespace, this doesn't change the result.
|
2014-05-26 22:55:08 +04:00
|
|
|
theContext.Assign(' ');
|
2008-07-15 13:35:07 +04:00
|
|
|
}
|
|
|
|
|
2005-02-18 22:18:39 +03:00
|
|
|
// First, parse the context to build up the DTD's tag stack. Note that we
|
2011-10-17 18:59:28 +04:00
|
|
|
// pass false for the aLastCall parameter.
|
2011-08-01 11:48:24 +04:00
|
|
|
result = Parse(theContext, (void*)&theContext, false);
|
2005-02-18 22:18:39 +03:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
mFlags |= NS_PARSER_FLAG_OBSERVERS_ENABLED;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-12-29 01:42:33 +03:00
|
|
|
if (!mSink) {
|
|
|
|
// Parse must have failed in the XML case and so the sink was killed.
|
|
|
|
return NS_ERROR_HTMLPARSER_STOPPARSING;
|
|
|
|
}
|
|
|
|
|
2005-02-18 22:18:39 +03:00
|
|
|
nsCOMPtr<nsIFragmentContentSink> fragSink = do_QueryInterface(mSink);
|
|
|
|
NS_ASSERTION(fragSink, "ParseFragment requires a fragment content sink");
|
|
|
|
|
|
|
|
fragSink->WillBuildContent();
|
2006-10-11 01:28:23 +04:00
|
|
|
// Now, parse the actual content. Note that this is the last call
|
|
|
|
// for HTML content, but for XML, we will want to build and parse
|
|
|
|
// the end tags. However, if tagStack is empty, it's the last call
|
|
|
|
// for XML as well.
|
2011-08-01 11:48:24 +04:00
|
|
|
if (theCount == 0) {
|
|
|
|
result = Parse(aSourceBuffer, &theContext, true);
|
2006-10-11 01:28:23 +04:00
|
|
|
fragSink->DidBuildContent();
|
|
|
|
} else {
|
|
|
|
// Add an end tag chunk, so expat will read the whole source buffer,
|
|
|
|
// and not worry about ']]' etc.
|
|
|
|
result = Parse(aSourceBuffer + NS_LITERAL_STRING("</"), &theContext, false);
|
|
|
|
fragSink->DidBuildContent();
|
2009-05-15 00:02:13 +04:00
|
|
|
|
2006-10-11 01:28:23 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2009-05-15 00:02:13 +04:00
|
|
|
nsAutoString endContext;
|
2006-10-11 01:28:23 +04:00
|
|
|
for (theIndex = 0; theIndex < theCount; theIndex++) {
|
|
|
|
// we already added an end tag chunk above
|
|
|
|
if (theIndex > 0) {
|
|
|
|
endContext.AppendLiteral("</");
|
|
|
|
}
|
2005-02-18 22:18:39 +03:00
|
|
|
|
2008-04-11 08:38:25 +04:00
|
|
|
nsString& thisTag = aTagStack[theIndex];
|
2006-10-11 01:28:23 +04:00
|
|
|
// was there an xmlns=?
|
2014-01-04 19:02:17 +04:00
|
|
|
int32_t endOfTag = thisTag.FindChar(char16_t(' '));
|
2006-10-11 01:28:23 +04:00
|
|
|
if (endOfTag == -1) {
|
|
|
|
endContext.Append(thisTag);
|
|
|
|
} else {
|
|
|
|
endContext.Append(Substring(thisTag, 0, endOfTag));
|
|
|
|
}
|
|
|
|
|
2014-05-22 07:48:51 +04:00
|
|
|
endContext.Append('>');
|
2006-10-11 01:28:23 +04:00
|
|
|
}
|
2009-05-15 00:02:13 +04:00
|
|
|
|
2011-08-01 11:48:24 +04:00
|
|
|
result = Parse(endContext, &theContext, true);
|
2006-10-11 01:28:23 +04:00
|
|
|
}
|
2004-10-01 02:27:45 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
|
|
|
mFlags |= NS_PARSER_FLAG_OBSERVERS_ENABLED;
|
1999-06-04 02:38:38 +04:00
|
|
|
|
1999-04-05 10:55:49 +04:00
|
|
|
return result;
|
|
|
|
}
|
1999-05-04 08:22:31 +04:00
|
|
|
|
1998-04-25 23:45:14 +04:00
|
|
|
/**
|
2005-12-23 01:31:36 +03:00
|
|
|
* This routine is called to cause the parser to continue parsing its
|
|
|
|
* underlying stream. This call allows the parse process to happen in
|
|
|
|
* chunks, such as when the content is push based, and we need to parse in
|
|
|
|
* pieces.
|
|
|
|
*
|
|
|
|
* An interesting change in how the parser gets used has led us to add extra
|
|
|
|
* processing to this method. The case occurs when the parser is blocked in
|
|
|
|
* one context, and gets a parse(string) call in another context. In this
|
|
|
|
* case, the parserContexts are linked. No problem.
|
2000-03-11 11:08:24 +03:00
|
|
|
*
|
2005-12-23 01:31:36 +03:00
|
|
|
* The problem is that Parse(string) assumes that it can proceed unabated,
|
|
|
|
* but if the parser is already blocked that assumption is false. So we
|
|
|
|
* needed to add a mechanism here to allow the parser to continue to process
|
|
|
|
* (the pop and free) contexts until 1) it get's blocked again; 2) it runs
|
|
|
|
* out of contexts.
|
2000-03-11 11:08:24 +03:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param allowItertion : set to true if non-script resumption is requested
|
|
|
|
* @param aIsFinalChunk : tells us when the last chunk of data is provided.
|
1998-07-23 12:21:02 +04:00
|
|
|
* @return error code -- 0 if ok, non-zero if error.
|
1998-04-25 23:45:14 +04:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsParser::ResumeParse(bool allowIteration, bool aIsFinalChunk,
|
|
|
|
bool aCanInterrupt) {
|
2005-12-23 01:31:36 +03:00
|
|
|
nsresult result = NS_OK;
|
2000-03-11 11:08:24 +03:00
|
|
|
|
2017-03-16 03:31:00 +03:00
|
|
|
if (!mBlocked && mInternalState != NS_ERROR_HTMLPARSER_STOPPARSING) {
|
2002-12-06 04:54:39 +03:00
|
|
|
result = WillBuildModel(mParserContext->mScanner->GetFilename());
|
2002-05-08 03:18:15 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
mFlags &= ~NS_PARSER_FLAG_CAN_TOKENIZE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-03-24 04:26:46 +03:00
|
|
|
if (mDTD) {
|
2009-06-24 01:22:16 +04:00
|
|
|
mSink->WillResume();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool theIterationIsOk = true;
|
2005-12-23 01:31:36 +03:00
|
|
|
|
|
|
|
while (result == NS_OK && theIterationIsOk) {
|
|
|
|
if (!mUnusedInput.IsEmpty() && mParserContext->mScanner) {
|
|
|
|
// -- Ref: Bug# 22485 --
|
|
|
|
// Insert the unused input into the source buffer
|
|
|
|
// as if it was read from the input stream.
|
|
|
|
// Adding UngetReadable() per vidur!!
|
|
|
|
mParserContext->mScanner->UngetReadable(mUnusedInput);
|
|
|
|
mUnusedInput.Truncate(0);
|
2000-03-11 11:08:24 +03:00
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
// Only allow parsing to be interrupted in the subsequent call to
|
|
|
|
// build model.
|
|
|
|
nsresult theTokenizerResult = (mFlags & NS_PARSER_FLAG_CAN_TOKENIZE)
|
|
|
|
? Tokenize(aIsFinalChunk)
|
|
|
|
: NS_OK;
|
|
|
|
result = BuildModel();
|
1999-02-10 12:12:48 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
if (result == NS_ERROR_HTMLPARSER_INTERRUPTED && aIsFinalChunk) {
|
|
|
|
PostContinueEvent();
|
2001-06-21 06:06:23 +04:00
|
|
|
}
|
|
|
|
|
2016-12-23 05:51:04 +03:00
|
|
|
theIterationIsOk = theTokenizerResult != NS_ERROR_HTMLPARSER_EOF &&
|
2005-12-23 01:31:36 +03:00
|
|
|
result != NS_ERROR_HTMLPARSER_INTERRUPTED;
|
2002-01-09 04:37:50 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
// Make sure not to stop parsing too early. Therefore, before shutting
|
|
|
|
// down the parser, it's important to check whether the input buffer
|
|
|
|
// has been scanned to completion (theTokenizerResult should be kEOF).
|
|
|
|
// kEOF -> End of buffer.
|
1999-11-10 06:41:09 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
// If we're told to block the parser, we disable all further parsing
|
2000-03-11 11:08:24 +03:00
|
|
|
// (and cache any data coming in) until the parser is re-enabled.
|
2005-12-23 01:31:36 +03:00
|
|
|
if (NS_ERROR_HTMLPARSER_BLOCK == result) {
|
2009-06-24 01:22:16 +04:00
|
|
|
mSink->WillInterrupt();
|
2017-03-16 03:31:00 +03:00
|
|
|
if (!mBlocked) {
|
2008-12-12 01:20:44 +03:00
|
|
|
// If we were blocked by a recursive invocation, don't re-block.
|
|
|
|
BlockParser();
|
2008-10-02 04:09:21 +04:00
|
|
|
}
|
2001-07-18 00:46:17 +04:00
|
|
|
return NS_OK;
|
2000-03-11 11:08:24 +03:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
if (NS_ERROR_HTMLPARSER_STOPPARSING == result) {
|
2000-05-23 01:32:06 +04:00
|
|
|
// Note: Parser Terminate() calls DidBuildModel.
|
2005-12-23 01:31:36 +03:00
|
|
|
if (mInternalState != NS_ERROR_HTMLPARSER_STOPPARSING) {
|
2000-05-23 01:32:06 +04:00
|
|
|
DidBuildModel(mStreamStatus);
|
2001-06-19 01:18:37 +04:00
|
|
|
mInternalState = result;
|
2000-05-23 01:32:06 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2002-01-25 01:15:58 +03:00
|
|
|
return NS_OK;
|
2000-03-11 11:08:24 +03:00
|
|
|
}
|
2016-12-23 05:51:04 +03:00
|
|
|
if ((NS_OK == result &&
|
|
|
|
theTokenizerResult == NS_ERROR_HTMLPARSER_EOF) ||
|
|
|
|
result == NS_ERROR_HTMLPARSER_INTERRUPTED) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool theContextIsStringBased =
|
2005-12-23 01:31:36 +03:00
|
|
|
CParserContext::eCTString == mParserContext->mContextType;
|
1999-11-10 06:41:09 +03:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
if (mParserContext->mStreamListenerState == eOnStop ||
|
|
|
|
!mParserContext->mMultipart || theContextIsStringBased) {
|
|
|
|
if (!mParserContext->mPrevContext) {
|
|
|
|
if (mParserContext->mStreamListenerState == eOnStop) {
|
|
|
|
DidBuildModel(mStreamStatus);
|
2001-06-21 06:06:23 +04:00
|
|
|
return NS_OK;
|
2000-03-18 03:49:54 +03:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
} else {
|
|
|
|
CParserContext* theContext = PopContext();
|
|
|
|
if (theContext) {
|
|
|
|
theIterationIsOk = allowIteration && theContextIsStringBased;
|
|
|
|
if (theContext->mCopyUnused) {
|
2016-02-09 13:45:59 +03:00
|
|
|
if (!theContext->mScanner->CopyUnusedData(mUnusedInput)) {
|
|
|
|
mInternalState = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2000-03-11 11:08:24 +03:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2000-03-11 11:08:24 +03:00
|
|
|
delete theContext;
|
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
result = mInternalState;
|
|
|
|
aIsFinalChunk = mParserContext &&
|
|
|
|
mParserContext->mStreamListenerState == eOnStop;
|
2009-06-24 01:22:16 +04:00
|
|
|
// ...then intentionally fall through to mSink->WillInterrupt()...
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
|
|
|
}
|
2000-03-11 11:08:24 +03:00
|
|
|
}
|
|
|
|
|
2016-12-23 05:51:04 +03:00
|
|
|
if (theTokenizerResult == NS_ERROR_HTMLPARSER_EOF ||
|
2005-12-23 01:31:36 +03:00
|
|
|
result == NS_ERROR_HTMLPARSER_INTERRUPTED) {
|
2001-06-21 06:06:23 +04:00
|
|
|
result = (result == NS_ERROR_HTMLPARSER_INTERRUPTED) ? NS_OK : result;
|
2009-06-24 01:22:16 +04:00
|
|
|
mSink->WillInterrupt();
|
2000-03-11 11:08:24 +03:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mInternalState = result = NS_ERROR_HTMLPARSER_UNRESOLVEDDTD;
|
1999-05-19 09:01:46 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
1999-09-21 02:24:33 +04:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
return (result == NS_ERROR_HTMLPARSER_INTERRUPTED) ? NS_OK : result;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-07-02 12:14:22 +04:00
|
|
|
/**
|
2005-12-23 01:31:36 +03:00
|
|
|
* This is where we loop over the tokens created in the
|
|
|
|
* tokenization phase, and try to make sense out of them.
|
1998-07-02 12:14:22 +04:00
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
nsresult nsParser::BuildModel() {
|
2012-07-30 18:20:58 +04:00
|
|
|
nsITokenizer* theTokenizer = nullptr;
|
1998-07-02 12:14:22 +04:00
|
|
|
|
2002-04-11 02:16:46 +04:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
if (mParserContext) {
|
2009-03-24 12:43:42 +03:00
|
|
|
result = mParserContext->GetTokenizer(mDTD, mSink, theTokenizer);
|
2002-04-11 02:16:46 +04:00
|
|
|
}
|
2002-04-09 04:43:00 +04:00
|
|
|
|
2005-11-11 22:11:36 +03:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2009-03-24 04:26:46 +03:00
|
|
|
if (mDTD) {
|
2013-08-12 19:28:41 +04:00
|
|
|
result = mDTD->BuildModel(theTokenizer, mSink);
|
1999-09-30 08:04:53 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
} else {
|
2002-04-11 02:16:46 +04:00
|
|
|
mInternalState = result = NS_ERROR_HTMLPARSER_BADTOKENIZER;
|
1999-01-09 04:09:02 +03:00
|
|
|
}
|
1998-07-02 12:14:22 +04:00
|
|
|
return result;
|
|
|
|
}
|
1998-04-25 23:45:14 +04:00
|
|
|
|
1998-07-02 12:14:22 +04:00
|
|
|
/*******************************************************************
|
|
|
|
These methods are used to talk to the netlib system...
|
|
|
|
*******************************************************************/
|
|
|
|
|
2019-02-28 02:41:04 +03:00
|
|
|
nsresult nsParser::OnStartRequest(nsIRequest* request) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(eNone == mParserContext->mStreamListenerState,
|
|
|
|
"Parser's nsIStreamListener API was not setup "
|
|
|
|
"correctly in constructor.");
|
|
|
|
|
2003-04-09 01:23:34 +04:00
|
|
|
if (mObserver) {
|
2019-02-28 02:41:04 +03:00
|
|
|
mObserver->OnStartRequest(request);
|
1998-04-25 23:45:14 +04:00
|
|
|
}
|
2001-10-20 00:48:18 +04:00
|
|
|
mParserContext->mStreamListenerState = eOnStart;
|
|
|
|
mParserContext->mAutoDetectStatus = eUnknownDetect;
|
|
|
|
mParserContext->mRequest = request;
|
|
|
|
|
2009-03-24 04:26:46 +03:00
|
|
|
NS_ASSERTION(!mParserContext->mPrevContext,
|
|
|
|
"Clobbering DTD for non-root parser context!");
|
2012-07-30 18:20:58 +04:00
|
|
|
mDTD = nullptr;
|
2009-03-24 04:26:46 +03:00
|
|
|
|
1999-06-21 04:33:51 +04:00
|
|
|
nsresult rv;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString contentType;
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
2006-06-19 07:03:28 +04:00
|
|
|
if (channel) {
|
|
|
|
rv = channel->GetContentType(contentType);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mParserContext->SetMimeType(contentType);
|
|
|
|
}
|
1999-06-21 04:33:51 +04:00
|
|
|
}
|
1999-07-10 03:07:50 +04:00
|
|
|
|
2004-11-02 22:52:32 +03:00
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
return rv;
|
1998-04-25 23:45:14 +04:00
|
|
|
}
|
|
|
|
|
2012-11-06 15:57:51 +04:00
|
|
|
static bool ExtractCharsetFromXmlDeclaration(const unsigned char* aBytes,
|
|
|
|
int32_t aLen,
|
|
|
|
nsCString& oCharset) {
|
|
|
|
// This code is rather pointless to have. Might as well reuse expat as
|
|
|
|
// seen in nsHtml5StreamParser. -- hsivonen
|
|
|
|
oCharset.Truncate();
|
|
|
|
if ((aLen >= 5) && ('<' == aBytes[0]) && ('?' == aBytes[1]) &&
|
|
|
|
('x' == aBytes[2]) && ('m' == aBytes[3]) && ('l' == aBytes[4])) {
|
|
|
|
int32_t i;
|
|
|
|
bool versionFound = false, encodingFound = false;
|
|
|
|
for (i = 6; i < aLen && !encodingFound; ++i) {
|
|
|
|
// end of XML declaration?
|
|
|
|
if ((((char*)aBytes)[i] == '?') && ((i + 1) < aLen) &&
|
|
|
|
(((char*)aBytes)[i + 1] == '>')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Version is required.
|
|
|
|
if (!versionFound) {
|
|
|
|
// Want to avoid string comparisons, hence looking for 'n'
|
|
|
|
// and only if found check the string leading to it. Not
|
|
|
|
// foolproof, but fast.
|
|
|
|
// The shortest string allowed before this is (strlen==13):
|
|
|
|
// <?xml version
|
|
|
|
if ((((char*)aBytes)[i] == 'n') && (i >= 12) &&
|
|
|
|
(0 == PL_strncmp("versio", (char*)(aBytes + i - 6), 6))) {
|
|
|
|
// Fast forward through version
|
|
|
|
char q = 0;
|
|
|
|
for (++i; i < aLen; ++i) {
|
|
|
|
char qi = ((char*)aBytes)[i];
|
|
|
|
if (qi == '\'' || qi == '"') {
|
|
|
|
if (q && q == qi) {
|
|
|
|
// ending quote
|
|
|
|
versionFound = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Starting quote
|
|
|
|
q = qi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// encoding must follow version
|
|
|
|
// Want to avoid string comparisons, hence looking for 'g'
|
|
|
|
// and only if found check the string leading to it. Not
|
|
|
|
// foolproof, but fast.
|
|
|
|
// The shortest allowed string before this (strlen==26):
|
|
|
|
// <?xml version="1" encoding
|
|
|
|
if ((((char*)aBytes)[i] == 'g') && (i >= 25) &&
|
|
|
|
(0 == PL_strncmp("encodin", (char*)(aBytes + i - 7), 7))) {
|
|
|
|
int32_t encStart = 0;
|
|
|
|
char q = 0;
|
|
|
|
for (++i; i < aLen; ++i) {
|
|
|
|
char qi = ((char*)aBytes)[i];
|
|
|
|
if (qi == '\'' || qi == '"') {
|
|
|
|
if (q && q == qi) {
|
|
|
|
int32_t count = i - encStart;
|
|
|
|
// encoding value is invalid if it is UTF-16
|
2013-02-23 16:14:12 +04:00
|
|
|
if (count > 0 &&
|
|
|
|
PL_strncasecmp("UTF-16", (char*)(aBytes + encStart),
|
|
|
|
count)) {
|
2012-11-06 15:57:51 +04:00
|
|
|
oCharset.Assign((char*)(aBytes + encStart), count);
|
|
|
|
}
|
|
|
|
encodingFound = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
encStart = i + 1;
|
|
|
|
q = qi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // if (!versionFound)
|
|
|
|
} // for
|
|
|
|
}
|
|
|
|
return !oCharset.IsEmpty();
|
1999-08-31 02:25:17 +04:00
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
inline char GetNextChar(nsACString::const_iterator& aStart,
|
|
|
|
nsACString::const_iterator& aEnd) {
|
2002-04-09 04:28:49 +04:00
|
|
|
NS_ASSERTION(aStart != aEnd, "end of buffer");
|
|
|
|
return (++aStart != aEnd) ? *aStart : '\0';
|
2002-03-20 00:41:41 +03:00
|
|
|
}
|
|
|
|
|
2012-03-31 18:10:34 +04:00
|
|
|
static nsresult NoOpParserWriteFunc(nsIInputStream* in, void* closure,
|
|
|
|
const char* fromRawSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t toOffset, uint32_t count,
|
|
|
|
uint32_t* writeCount) {
|
2012-03-31 18:10:34 +04:00
|
|
|
*writeCount = count;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-12-13 00:58:14 +03:00
|
|
|
typedef struct {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mNeedCharsetCheck;
|
2000-12-13 00:58:14 +03:00
|
|
|
nsParser* mParser;
|
|
|
|
nsScanner* mScanner;
|
2001-06-30 02:56:58 +04:00
|
|
|
nsIRequest* mRequest;
|
2000-12-13 00:58:14 +03:00
|
|
|
} ParserWriteStruct;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is invoked as a result of a call to a stream's
|
2004-11-05 09:50:27 +03:00
|
|
|
* ReadSegments() method. It is called for each contiguous buffer
|
2000-12-13 00:58:14 +03:00
|
|
|
* of data in the underlying stream or pipe. Using ReadSegments
|
|
|
|
* allows us to avoid copying data to read out of the stream.
|
|
|
|
*/
|
|
|
|
static nsresult ParserWriteFunc(nsIInputStream* in, void* closure,
|
|
|
|
const char* fromRawSegment, uint32_t toOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count, uint32_t* writeCount) {
|
2000-12-13 00:58:14 +03:00
|
|
|
nsresult result;
|
2007-07-08 11:08:04 +04:00
|
|
|
ParserWriteStruct* pws = static_cast<ParserWriteStruct*>(closure);
|
2012-11-06 15:57:51 +04:00
|
|
|
const unsigned char* buf =
|
|
|
|
reinterpret_cast<const unsigned char*>(fromRawSegment);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t theNumRead = count;
|
2000-12-13 00:58:14 +03:00
|
|
|
|
|
|
|
if (!pws) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-11-05 09:50:27 +03:00
|
|
|
if (pws->mNeedCharsetCheck) {
|
2011-10-17 18:59:28 +04:00
|
|
|
pws->mNeedCharsetCheck = false;
|
2012-11-06 15:57:51 +04:00
|
|
|
int32_t source;
|
2017-06-18 14:37:50 +03:00
|
|
|
auto preferred = pws->mParser->GetDocumentCharset(source);
|
2012-11-06 15:57:51 +04:00
|
|
|
|
|
|
|
// This code was bogus when I found it. It expects the BOM or the XML
|
|
|
|
// declaration to be entirely in the first network buffer. -- hsivonen
|
2017-06-16 15:11:03 +03:00
|
|
|
const Encoding* encoding;
|
|
|
|
size_t bomLength;
|
|
|
|
Tie(encoding, bomLength) = Encoding::ForBOM(MakeSpan(buf, count));
|
|
|
|
Unused << bomLength;
|
|
|
|
if (encoding) {
|
2012-11-06 15:57:51 +04:00
|
|
|
// The decoder will swallow the BOM. The UTF-16 will re-sniff for
|
2017-04-27 13:27:03 +03:00
|
|
|
// endianness. The value of preferred is now "UTF-8", "UTF-16LE"
|
|
|
|
// or "UTF-16BE".
|
2017-06-18 14:37:50 +03:00
|
|
|
preferred = WrapNotNull(encoding);
|
2012-11-06 15:57:51 +04:00
|
|
|
source = kCharsetFromByteOrderMark;
|
|
|
|
} else if (source < kCharsetFromChannel) {
|
|
|
|
nsAutoCString declCharset;
|
|
|
|
|
|
|
|
if (ExtractCharsetFromXmlDeclaration(buf, count, declCharset)) {
|
2017-06-16 15:11:03 +03:00
|
|
|
encoding = Encoding::ForLabel(declCharset);
|
|
|
|
if (encoding) {
|
2017-06-18 14:37:50 +03:00
|
|
|
preferred = WrapNotNull(encoding);
|
2012-11-06 15:57:51 +04:00
|
|
|
source = kCharsetFromMetaTag;
|
2001-06-30 02:56:58 +04:00
|
|
|
}
|
|
|
|
}
|
2004-11-05 09:50:27 +03:00
|
|
|
}
|
2012-11-06 15:57:51 +04:00
|
|
|
|
|
|
|
pws->mParser->SetDocumentCharset(preferred, source);
|
|
|
|
pws->mParser->SetSinkCharset(preferred);
|
2004-11-05 09:50:27 +03:00
|
|
|
}
|
2000-12-13 00:58:14 +03:00
|
|
|
|
2017-02-17 09:44:49 +03:00
|
|
|
result = pws->mScanner->Append(fromRawSegment, theNumRead);
|
2000-12-13 00:58:14 +03:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
*writeCount = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
1999-08-31 02:25:17 +04:00
|
|
|
|
2019-02-28 02:42:27 +03:00
|
|
|
nsresult nsParser::OnDataAvailable(nsIRequest* request,
|
2012-09-06 06:41:02 +04:00
|
|
|
nsIInputStream* pIStream,
|
|
|
|
uint64_t sourceOffset, uint32_t aLength) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT((eOnStart == mParserContext->mStreamListenerState ||
|
2004-11-05 09:50:27 +03:00
|
|
|
eOnDataAvail == mParserContext->mStreamListenerState),
|
2001-09-08 22:37:19 +04:00
|
|
|
"Error: OnStartRequest() must be called before OnDataAvailable()");
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(NS_InputStreamIsBuffered(pIStream),
|
|
|
|
"Must have a buffered input stream");
|
2000-03-11 11:08:24 +03:00
|
|
|
|
2004-11-05 09:50:27 +03:00
|
|
|
nsresult rv = NS_OK;
|
1998-07-02 12:14:22 +04:00
|
|
|
|
2012-03-31 18:10:34 +04:00
|
|
|
if (mIsAboutBlank) {
|
2012-04-10 11:02:31 +04:00
|
|
|
MOZ_ASSERT(false, "Must not get OnDataAvailable for about:blank");
|
2012-03-31 18:10:34 +04:00
|
|
|
// ... but if an extension tries to feed us data for about:blank in a
|
|
|
|
// release build, silently ignore the data.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t totalRead;
|
2012-03-31 18:10:34 +04:00
|
|
|
rv = pIStream->ReadSegments(NoOpParserWriteFunc, nullptr, aLength,
|
|
|
|
&totalRead);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
CParserContext* theContext = mParserContext;
|
1998-05-30 07:14:55 +04:00
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
while (theContext && theContext->mRequest != request) {
|
|
|
|
theContext = theContext->mPrevContext;
|
2004-11-05 09:50:27 +03:00
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
if (theContext) {
|
2004-11-05 09:50:27 +03:00
|
|
|
theContext->mStreamListenerState = eOnDataAvail;
|
1999-12-03 03:30:29 +03:00
|
|
|
|
2004-11-05 09:50:27 +03:00
|
|
|
if (eInvalidDetect == theContext->mAutoDetectStatus) {
|
|
|
|
if (theContext->mScanner) {
|
2004-02-19 05:44:03 +03:00
|
|
|
nsScannerIterator iter;
|
2000-12-13 00:58:14 +03:00
|
|
|
theContext->mScanner->EndReading(iter);
|
2011-10-17 18:59:28 +04:00
|
|
|
theContext->mScanner->SetPosition(iter, true);
|
2004-11-05 09:50:27 +03:00
|
|
|
}
|
|
|
|
}
|
1999-01-09 04:09:02 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t totalRead;
|
2000-12-13 00:58:14 +03:00
|
|
|
ParserWriteStruct pws;
|
2012-11-06 15:57:51 +04:00
|
|
|
pws.mNeedCharsetCheck = true;
|
2000-12-13 00:58:14 +03:00
|
|
|
pws.mParser = this;
|
|
|
|
pws.mScanner = theContext->mScanner;
|
2001-06-30 02:56:58 +04:00
|
|
|
pws.mRequest = request;
|
2000-09-09 03:06:13 +04:00
|
|
|
|
2004-11-05 09:50:27 +03:00
|
|
|
rv = pIStream->ReadSegments(ParserWriteFunc, &pws, aLength, &totalRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2000-12-13 00:58:14 +03:00
|
|
|
}
|
2000-03-18 12:15:01 +03:00
|
|
|
|
2017-02-17 09:44:49 +03:00
|
|
|
if (IsOkToProcessNetworkData()) {
|
2013-04-05 16:36:16 +04:00
|
|
|
nsCOMPtr<nsIParser> kungFuDeathGrip(this);
|
|
|
|
nsCOMPtr<nsIContentSink> sinkDeathGrip(mSink);
|
2011-10-17 18:59:28 +04:00
|
|
|
mProcessingNetworkData = true;
|
2016-07-18 19:42:35 +03:00
|
|
|
if (sinkDeathGrip) {
|
|
|
|
sinkDeathGrip->WillParse();
|
2008-10-31 00:31:00 +03:00
|
|
|
}
|
2004-11-05 09:50:27 +03:00
|
|
|
rv = ResumeParse();
|
2011-10-17 18:59:28 +04:00
|
|
|
mProcessingNetworkData = false;
|
2004-08-24 22:37:33 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
} else {
|
2005-01-12 03:55:25 +03:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2000-03-18 12:15:01 +03:00
|
|
|
|
2004-11-05 09:50:27 +03:00
|
|
|
return rv;
|
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1998-04-25 23:45:14 +04:00
|
|
|
/**
|
1999-01-09 04:09:02 +03:00
|
|
|
* This is called by the networking library once the last block of data
|
|
|
|
* has been collected from the net.
|
1998-04-25 23:45:14 +04:00
|
|
|
*/
|
2019-02-28 02:41:31 +03:00
|
|
|
nsresult nsParser::OnStopRequest(nsIRequest* request, nsresult status) {
|
2004-11-02 22:52:32 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2005-02-18 21:29:02 +03:00
|
|
|
CParserContext* pc = mParserContext;
|
|
|
|
while (pc) {
|
|
|
|
if (pc->mRequest == request) {
|
|
|
|
pc->mStreamListenerState = eOnStop;
|
2011-10-17 18:59:28 +04:00
|
|
|
pc->mScanner->SetIncremental(false);
|
2005-02-18 21:29:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pc = pc->mPrevContext;
|
|
|
|
}
|
|
|
|
|
2004-11-02 22:52:32 +03:00
|
|
|
mStreamStatus = status;
|
1999-06-16 03:29:54 +04:00
|
|
|
|
2009-11-07 02:30:24 +03:00
|
|
|
if (IsOkToProcessNetworkData() && NS_SUCCEEDED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mProcessingNetworkData = true;
|
2008-10-31 00:31:00 +03:00
|
|
|
if (mSink) {
|
|
|
|
mSink->WillParse();
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = ResumeParse(true, true);
|
|
|
|
mProcessingNetworkData = false;
|
2005-08-31 21:33:54 +04:00
|
|
|
}
|
2004-11-02 22:52:32 +03:00
|
|
|
|
1998-12-11 20:02:37 +03:00
|
|
|
// If the parser isn't enabled, we don't finish parsing till
|
|
|
|
// it is reenabled.
|
1999-01-09 04:09:02 +03:00
|
|
|
|
1998-12-11 20:02:37 +03:00
|
|
|
// XXX Should we wait to notify our observers as well if the
|
|
|
|
// parser isn't yet enabled?
|
2003-04-09 01:23:34 +04:00
|
|
|
if (mObserver) {
|
2019-02-28 02:41:31 +03:00
|
|
|
mObserver->OnStopRequest(request, status);
|
1998-05-29 01:14:27 +04:00
|
|
|
}
|
1999-01-09 04:09:02 +03:00
|
|
|
|
2004-11-02 22:52:32 +03:00
|
|
|
return rv;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-06-18 03:13:28 +04:00
|
|
|
/*******************************************************************
|
2005-12-23 01:31:36 +03:00
|
|
|
Here come the tokenization methods...
|
1998-06-18 03:13:28 +04:00
|
|
|
*******************************************************************/
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1998-04-25 23:45:14 +04:00
|
|
|
/**
|
1998-06-18 03:13:28 +04:00
|
|
|
* Part of the code sandwich, this gets called right before
|
|
|
|
* the tokenization process begins. The main reason for
|
|
|
|
* this call is to allow the delegate to do initialization.
|
1998-04-25 23:45:14 +04:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsParser::WillTokenize(bool aIsFinalChunk) {
|
2005-11-11 22:11:36 +03:00
|
|
|
if (!mParserContext) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-04-11 02:16:46 +04:00
|
|
|
}
|
|
|
|
|
2005-11-11 22:11:36 +03:00
|
|
|
nsITokenizer* theTokenizer;
|
2009-03-24 12:43:42 +03:00
|
|
|
nsresult result = mParserContext->GetTokenizer(mDTD, mSink, theTokenizer);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, false);
|
2013-08-12 19:28:41 +04:00
|
|
|
return NS_SUCCEEDED(theTokenizer->WillTokenize(aIsFinalChunk));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-04-25 23:45:14 +04:00
|
|
|
/**
|
2005-12-23 01:31:36 +03:00
|
|
|
* This is the primary control routine to consume tokens.
|
|
|
|
* It iteratively consumes tokens until an error occurs or
|
2005-11-23 02:29:30 +03:00
|
|
|
* you run out of data.
|
1998-04-25 23:45:14 +04:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsParser::Tokenize(bool aIsFinalChunk) {
|
2005-11-11 22:11:36 +03:00
|
|
|
nsITokenizer* theTokenizer;
|
2005-12-23 01:31:36 +03:00
|
|
|
|
2005-11-11 22:11:36 +03:00
|
|
|
nsresult result = NS_ERROR_NOT_AVAILABLE;
|
2002-04-11 02:16:46 +04:00
|
|
|
if (mParserContext) {
|
2009-03-24 12:43:42 +03:00
|
|
|
result = mParserContext->GetTokenizer(mDTD, mSink, theTokenizer);
|
2002-04-11 02:16:46 +04:00
|
|
|
}
|
|
|
|
|
2005-11-11 22:11:36 +03:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool flushTokens = false;
|
1999-09-30 08:04:53 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool killSink = false;
|
2008-12-08 03:15:52 +03:00
|
|
|
|
2005-11-23 02:29:30 +03:00
|
|
|
WillTokenize(aIsFinalChunk);
|
|
|
|
while (NS_SUCCEEDED(result)) {
|
2013-09-20 18:26:43 +04:00
|
|
|
mParserContext->mScanner->Mark();
|
2005-12-23 01:31:36 +03:00
|
|
|
result =
|
|
|
|
theTokenizer->ConsumeToken(*mParserContext->mScanner, flushTokens);
|
2005-11-23 02:29:30 +03:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
mParserContext->mScanner->RewindToMark();
|
2016-12-23 05:51:04 +03:00
|
|
|
if (NS_ERROR_HTMLPARSER_EOF == result) {
|
2005-11-23 02:29:30 +03:00
|
|
|
break;
|
1999-05-19 09:01:46 +04:00
|
|
|
}
|
2005-11-23 02:29:30 +03:00
|
|
|
if (NS_ERROR_HTMLPARSER_STOPPARSING == result) {
|
2011-10-17 18:59:28 +04:00
|
|
|
killSink = true;
|
2005-11-23 02:29:30 +03:00
|
|
|
result = Terminate();
|
2000-05-23 01:32:06 +04:00
|
|
|
break;
|
|
|
|
}
|
2005-11-23 02:29:30 +03:00
|
|
|
} else if (flushTokens && (mFlags & NS_PARSER_FLAG_OBSERVERS_ENABLED)) {
|
|
|
|
// I added the extra test of NS_PARSER_FLAG_OBSERVERS_ENABLED to fix
|
|
|
|
// Bug# 23931. Flush tokens on seeing </SCRIPT> -- Ref: Bug# 22485 --
|
|
|
|
// Also remember to update the marked position.
|
|
|
|
mFlags |= NS_PARSER_FLAG_FLUSH_TOKENS;
|
2013-09-20 18:26:43 +04:00
|
|
|
mParserContext->mScanner->Mark();
|
2005-11-23 02:29:30 +03:00
|
|
|
break;
|
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
1999-09-30 08:04:53 +04:00
|
|
|
|
2008-12-08 03:15:52 +03:00
|
|
|
if (killSink) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mSink = nullptr;
|
2008-12-08 03:15:52 +03:00
|
|
|
}
|
2005-11-23 02:29:30 +03:00
|
|
|
} else {
|
2002-02-15 02:45:20 +03:00
|
|
|
result = mInternalState = NS_ERROR_HTMLPARSER_BADTOKENIZER;
|
1999-05-19 09:01:46 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
/**
|
2001-08-22 04:38:46 +04:00
|
|
|
* Get the channel associated with this parser
|
2005-12-23 01:31:36 +03:00
|
|
|
*
|
2001-08-22 04:38:46 +04:00
|
|
|
* @param aChannel out param that will contain the result
|
|
|
|
* @return NS_OK if successful
|
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP
|
2001-08-22 04:38:46 +04:00
|
|
|
nsParser::GetChannel(nsIChannel** aChannel) {
|
|
|
|
nsresult result = NS_ERROR_NOT_AVAILABLE;
|
2005-12-23 01:31:36 +03:00
|
|
|
if (mParserContext && mParserContext->mRequest) {
|
2001-08-22 04:38:46 +04:00
|
|
|
result = CallQueryInterface(mParserContext->mRequest, aChannel);
|
2005-12-23 01:31:36 +03:00
|
|
|
}
|
2001-08-22 04:38:46 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-12-23 01:31:36 +03:00
|
|
|
/**
|
1999-10-01 02:01:48 +04:00
|
|
|
* Get the DTD associated with this parser
|
|
|
|
*/
|
2005-12-23 01:31:36 +03:00
|
|
|
NS_IMETHODIMP
|
1999-10-01 02:01:48 +04:00
|
|
|
nsParser::GetDTD(nsIDTD** aDTD) {
|
|
|
|
if (mParserContext) {
|
2009-03-24 04:26:46 +03:00
|
|
|
NS_IF_ADDREF(*aDTD = mDTD);
|
1999-10-01 02:01:48 +04:00
|
|
|
}
|
2005-12-23 01:31:36 +03:00
|
|
|
|
1999-10-01 02:01:48 +04:00
|
|
|
return NS_OK;
|
2001-09-29 07:55:30 +04:00
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
/**
|
|
|
|
* Get this as nsIStreamListener
|
|
|
|
*/
|
2011-11-16 11:50:19 +04:00
|
|
|
nsIStreamListener* nsParser::GetStreamListener() { return this; }
|