/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* * The contents of this file are subject to the Netscape Public License * Version 1.0 (the "NPL"); you may not use this file except in * compliance with the NPL. You may obtain a copy of the NPL at * http://www.mozilla.org/NPL/ * * Software distributed under the NPL is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL * for the specific language governing rights and limitations under the * NPL. * * The Initial Developer of this code under the NPL is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ //#define __INCREMENTAL 1 #include "nsHTMLParser.h" #include "nsHTMLContentSink.h" #include "nsTokenizer.h" #include "nsHTMLTokens.h" #include "nsString.h" #include "nsIURL.h" #include "nsCRT.h" #include "COtherDelegate.h" #include "COtherDTD.h" #include "CNavDelegate.h" #include "CNavDTD.h" #include "prenv.h" //this is here for debug reasons... #include "plstr.h" #include #ifdef XP_PC #include //this is here for debug reasons... #endif static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); static NS_DEFINE_IID(kClassIID, NS_IHTML_PARSER_IID); static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); static const char* kNullURL = "Error: Null URL given"; static const char* kNullFilename= "Error: Null filename given"; static const char* kNullTokenizer = "Error: Unable to construct tokenizer"; static const char* kNullToken = "Error: Null token given"; static const char* kInvalidTagStackPos = "Error: invalid tag stack position"; static char* gVerificationOutputDir=0; static int rickGDebug=0; /** * This method is defined in nsIParser. It is used to * cause the COM-like construction of an nsHTMLParser. * * @update gess 3/25/98 * @param nsIParser** ptr to newly instantiated parser * @return NS_xxx error result */ NS_HTMLPARS nsresult NS_NewHTMLParser(nsIParser** aInstancePtrResult) { nsHTMLParser *it = new nsHTMLParser(); if (it == 0) { return NS_ERROR_OUT_OF_MEMORY; } return it->QueryInterface(kClassIID, (void **) aInstancePtrResult); } /** * init the set of default token handlers... * * @update gess 3/25/98 * @param * @return */ void nsHTMLParser::InitializeDefaultTokenHandlers() { AddTokenHandler(new CStartTokenHandler()); AddTokenHandler(new CEndTokenHandler()); AddTokenHandler(new CCommentTokenHandler()); AddTokenHandler(new CEntityTokenHandler()); AddTokenHandler(new CWhitespaceTokenHandler()); AddTokenHandler(new CNewlineTokenHandler()); AddTokenHandler(new CTextTokenHandler()); AddTokenHandler(new CAttributeTokenHandler()); AddTokenHandler(new CScriptTokenHandler()); AddTokenHandler(new CStyleTokenHandler()); } /** * default constructor * * @update gess 3/25/98 * @param * @return */ nsHTMLParser::nsHTMLParser() { NS_INIT_REFCNT(); mSink=0; mContextStackPos=0; mCurrentPos=0; mParseMode=eParseMode_unknown; nsCRT::zero(mContextStack,sizeof(mContextStack)); nsCRT::zero(mTokenHandlers,sizeof(mTokenHandlers)); mDTD=0; mHasOpenForm=PR_FALSE; mTokenHandlerCount=0; InitializeDefaultTokenHandlers(); gVerificationOutputDir = PR_GetEnv("VERIFY_PARSER"); } /** * Default destructor * * @update gess 3/25/98 * @param * @return */ nsHTMLParser::~nsHTMLParser() { DeleteTokenHandlers(); if(mCurrentPos) delete mCurrentPos; mCurrentPos=0; if(mTokenizer) delete mTokenizer; if(mDTD) delete mDTD; mTokenizer=0; mDTD=0; } NS_IMPL_ADDREF(nsHTMLParser) NS_IMPL_RELEASE(nsHTMLParser) //NS_IMPL_ISUPPORTS(nsHTMLParser,NS_IHTML_PARSER_IID) /** * This method gets called as part of our COM-like interfaces. * Its purpose is to create an interface to parser object * of some type. * * @update gess 3/25/98 * @param nsIID id of object to discover * @param aInstancePtr ptr to newly discovered interface * @return NS_xxx result code */ nsresult nsHTMLParser::QueryInterface(const nsIID& aIID, void** aInstancePtr) { if (NULL == aInstancePtr) { return NS_ERROR_NULL_POINTER; } if(aIID.Equals(kISupportsIID)) { //do IUnknown... *aInstancePtr = (nsIParser*)(this); } else if(aIID.Equals(kIParserIID)) { //do IParser base class... *aInstancePtr = (nsIParser*)(this); } else if(aIID.Equals(kClassIID)) { //do this class... *aInstancePtr = (nsHTMLParser*)(this); } else { *aInstancePtr=0; return NS_NOINTERFACE; } ((nsISupports*) *aInstancePtr)->AddRef(); return NS_OK; } /** * * * @update gess 4/2/98 * @param * @return */ eHTMLTags nsHTMLParser::NodeAt(PRInt32 aPos) const { NS_PRECONDITION(0 <= aPos, "bad nodeAt"); if((aPos>-1) && (aPos=0;i--){ if(mContextStack[i]==aTag) return i; } return kNotFound; } /** * Determine whether the given tag is open anywhere * in our context stack. * * @update gess 4/2/98 * @param eHTMLTags tag to be searched for in stack * @return topmost index of tag on stack */ PRBool nsHTMLParser::IsOpen(eHTMLTags aTag) const { PRInt32 pos=GetTopmostIndex(aTag); return PRBool(kNotFound!=pos); } /** * Gets the number of open containers on the stack. * * @update gess 4/2/98 * @param * @return */ PRInt32 nsHTMLParser::GetStackPos() const { return mContextStackPos; } /** * Finds a tag handler for the given tag type, given in string. * * @update gess 4/2/98 * @param aString contains name of tag to be handled * @return valid tag handler (if found) or null */ nsHTMLParser& nsHTMLParser::DeleteTokenHandlers(void) { for(int i=0;iCanHandle(aType)) { return mTokenHandlers[i]; } } return 0; } /** * Register a handler. * * @update gess 4/2/98 * @param * @return */ CTokenHandler* nsHTMLParser::AddTokenHandler(CTokenHandler* aHandler) { NS_ASSERTION(0!=aHandler,"Error: Null handler argument"); if(aHandler) { int max=sizeof(mTokenHandlers)/sizeof(mTokenHandlers[0]); if(mTokenHandlerCountVerifyContextVector(aTags,count); } if(PR_FALSE==result){ //add debugging code here to record the fact that we just encountered //a context vector we don't know how to handle. } } return result; } /** * This is where we loop over the tokens created in the * tokenization phase, and try to make sense out of them. * * @update gess 3/25/98 * @param * @return PR_TRUE if parse succeeded, PR_FALSE otherwise. */ PRBool nsHTMLParser::IterateTokens() { nsDeque& deque=mTokenizer->GetDeque(); nsDequeIterator e=deque.End(); if(mCurrentPos) delete mCurrentPos; //don't leak, now! mCurrentPos=new nsDequeIterator(deque.Begin()); CToken* theToken; PRBool done=PR_FALSE; PRBool result=PR_TRUE; PRInt32 iteration=0; while((!done) && (result)) { theToken=(CToken*)mCurrentPos->GetCurrent(); eHTMLTokenTypes type=eHTMLTokenTypes(theToken->GetTokenType()); iteration++; //debug purposes... switch(type){ case eToken_start: case eToken_text: case eToken_newline: case eToken_whitespace: result=HandleStartToken(theToken); break; case eToken_end: result=HandleEndToken(theToken); break; case eToken_entity: result=HandleEntityToken(theToken); break; case eToken_skippedcontent: //used in cases like