diff --git a/htmlparser/src/CNavDTD.cpp b/htmlparser/src/CNavDTD.cpp index a60125e3f0b5..6d957e56a7e1 100644 --- a/htmlparser/src/CNavDTD.cpp +++ b/htmlparser/src/CNavDTD.cpp @@ -417,7 +417,7 @@ PRBool CNavDTD::Verify(nsString& aURLRef,nsIParser* aParser){ else mDTDDebug->SetVerificationDirectory(kVerificationDir); } if(mDTDDebug) { - // mDTDDebug->Verify(this,aParser,mBodyContext->GetCount(),mBodyContext->mTags,aURLRef); + // mDTDDebug->Verify(this,aParser,mBodyContext->GetCount(),mBodyContext->mStack,aURLRef); } return result; } @@ -640,7 +640,7 @@ nsresult CNavDTD::HandleToken(CToken* aToken,nsIParser* aParser){ mTokenizer->PushTokenFront(theToken); else result=NS_OK; if (mDTDDebug) { - //mDTDDebug->Verify(this, mParser, mBodyContext->GetCount(), mBodyContext->mTags, mFilename); + //mDTDDebug->Verify(this, mParser, mBodyContext->GetCount(), mBodyContext->mStack, mFilename); } } } @@ -742,7 +742,7 @@ nsresult CNavDTD::DidHandleStartTag(nsCParserNode& aNode,eHTMLTags aChildTag){ static PRInt32 GetTopmostIndexOf(eHTMLTags aTag,nsTagStack& aTagStack) { int i=0; - int count = aTagStack.mTags->GetSize(); + int count = aTagStack.GetSize(); for(i=(count-1);i>=0;i--){ if(aTagStack[i]==aTag) return i; @@ -762,7 +762,7 @@ PRInt32 GetTopmostIndexOf(eHTMLTags aTag,nsTagStack& aTagStack) { */ static eHTMLTags FindAutoCloseTargetForStartTag(eHTMLTags aCurrentTag,nsTagStack& aTagStack) { - int theTopIndex = aTagStack.mTags->GetSize(); + int theTopIndex = aTagStack.GetSize(); eHTMLTags thePrevTag=aTagStack.Last(); if(nsHTMLElement::IsContainer(aCurrentTag)){ @@ -950,10 +950,10 @@ nsresult CNavDTD::HandleDefaultStartToken(CToken* aToken,eHTMLTags aChildTag,nsI eHTMLTags theParentTag=mBodyContext->Last(); PRBool theCanContainResult=CanContain(theParentTag,aChildTag); - PRBool theChildAgrees=(theCanContainResult) ? CanBeContained(theParentTag,aChildTag,mBodyContext->mTags) : PR_FALSE; + PRBool theChildAgrees=(theCanContainResult) ? CanBeContained(theParentTag,aChildTag,mBodyContext->mStack) : PR_FALSE; if(!(theCanContainResult && theChildAgrees)) { - eHTMLTags theTarget=FindAutoCloseTargetForStartTag(aChildTag,mBodyContext->mTags); + eHTMLTags theTarget=FindAutoCloseTargetForStartTag(aChildTag,mBodyContext->mStack); if(eHTMLTag_unknown!=theTarget){ result=CloseContainersTo(theTarget,PR_TRUE); theParentTag=mBodyContext->Last(); @@ -986,9 +986,7 @@ nsresult CNavDTD::HandleDefaultStartToken(CToken* aToken,eHTMLTags aChildTag,nsI //first, let's see if it's a style element... if(!nsHTMLElement::IsStyleTag(aChildTag)) { //it wasn't a style container, so open the element container... - if(mBodyContext->mOpenStyles) { - CloseTransientStyles(aChildTag); - } + CloseTransientStyles(aChildTag); } result=OpenContainer(aNode,PR_TRUE); } @@ -1159,18 +1157,19 @@ nsresult CNavDTD::HandleOmittedTag(CToken* aToken,eHTMLTags aChildTag,eHTMLTags if(!FindTagInSet(aChildTag,gWhitespaceTags,sizeof(gWhitespaceTags)/sizeof(aChildTag))) { isNotWhiteSpace = mSaveBadTokens = PR_TRUE; } - if(mSaveBadTokens &&(isNotWhiteSpace || mBodyContext->mTags.TokenCountAt(theBCIndex) > 0)) { - mBodyContext->mTags.SaveToken(aToken,theBCIndex); + if(mSaveBadTokens &&(isNotWhiteSpace || mBodyContext->TokenCountAt(theBCIndex) > 0)) { + mBodyContext->SaveToken(aToken,theBCIndex); if(attrCount > 0) { nsCParserNode* theAttrNode = (nsCParserNode*)&aNode; while(attrCount > 0){ - mBodyContext->mTags.SaveToken(theAttrNode->PopAttributeToken(),theBCIndex); + mBodyContext->SaveToken(theAttrNode->PopAttributeToken(),theBCIndex); attrCount--; } } if(!IsContainer(aChildTag) && isNotWhiteSpace) { mSaveBadTokens = PR_FALSE; } + result=NS_ERROR_HTMLPARSER_MISPLACED; } } return result; @@ -1296,7 +1295,7 @@ PRBool HasCloseablePeerAboveRoot(CTagList& aRootTagList,nsTagStack& aTagStack,eH */ static eHTMLTags FindAutoCloseTargetForEndTag(eHTMLTags aCurrentTag,nsTagStack& aTagStack) { - int theTopIndex=aTagStack.mTags->GetSize(); + int theTopIndex=aTagStack.GetSize(); eHTMLTags thePrevTag=aTagStack.Last(); if(nsHTMLElement::IsContainer(aCurrentTag)){ @@ -1401,7 +1400,7 @@ nsresult CNavDTD::HandleEndToken(CToken* aToken) { UpdateStyleStackForCloseTag(theChildTag,theChildTag); } else { - eHTMLTags theTarget=FindAutoCloseTargetForEndTag(theChildTag,mBodyContext->mTags); + eHTMLTags theTarget=FindAutoCloseTargetForEndTag(theChildTag,mBodyContext->mStack); if(gHTMLElements[theTarget].HasSpecialProperty(kBadContentWatch)){ result = HandleSavedTokensAbove(theTarget); } @@ -1443,7 +1442,7 @@ nsresult CNavDTD::HandleSavedTokensAbove(eHTMLTags aTag) return result; PRInt32 theBadContentIndex = theTopIndex - 1; - PRInt32 theBadTokenCount = mBodyContext->mTags.TokenCountAt(theBadContentIndex); + PRInt32 theBadTokenCount = mBodyContext->TokenCountAt(theBadContentIndex); if(theBadTokenCount > 0) { // Pause the main context and switch to the new context. @@ -1455,7 +1454,7 @@ nsresult CNavDTD::HandleSavedTokensAbove(eHTMLTags aTag) // Now flush out all the bad contents. while(theBadTokenCount > 0){ - theToken = mBodyContext->mTags.RestoreTokenFrom(theBadContentIndex); + theToken = mBodyContext->RestoreTokenFrom(theBadContentIndex); if(theToken) { theTag = (eHTMLTags)theToken->GetTypeID(); if(theTag != eHTMLTag_unknown) { @@ -1464,7 +1463,7 @@ nsresult CNavDTD::HandleSavedTokensAbove(eHTMLTags aTag) if(attrCount > 0) { PRInt32 theAttrCount = 0; for(PRInt32 i=0;imTags.RestoreTokenFrom(theBadContentIndex); + CToken* theAttrToken = mBodyContext->RestoreTokenFrom(theBadContentIndex); if(theAttrToken) { mTokenizer->PushTokenFront(theAttrToken); theAttrCount++; @@ -1540,7 +1539,7 @@ nsresult CNavDTD::HandleCommentToken(CToken* aToken) { // You may find this hard to beleive, but this has to be here // so that the TBODY doesnt die when it sees a comment. // This case occurs on WWW.CREAF.COM - eHTMLTags theTag=mBodyContext->mTags.Last(); + eHTMLTags theTag=mBodyContext->mStack.Last(); nsresult result=NS_OK; switch(theTag) { @@ -1919,7 +1918,7 @@ PRBool CNavDTD::CanOmitEndTag(eHTMLTags aParent,eHTMLTags aChild) const { return PR_TRUE; } - PRInt32 theChildIndex=GetIndexOfChildOrSynonym(mBodyContext->mTags,aChild); + PRInt32 theChildIndex=GetIndexOfChildOrSynonym(mBodyContext->mStack,aChild); result=PRBool(kNotFound==theChildIndex); return result; @@ -2111,7 +2110,7 @@ PRInt32 CNavDTD::GetTopmostIndexOf(eHTMLTags aTagSet[],PRInt32 aCount) const { * @return topmost index of tag on stack */ PRInt32 CNavDTD::GetTopmostIndexOf(eHTMLTags aTag) const { - return mBodyContext->mTags.GetTopmostIndexOf(aTag); + return mBodyContext->mStack.GetTopmostIndexOf(aTag); } /********************************************* @@ -2136,6 +2135,8 @@ nsresult CNavDTD::OpenTransientStyles(eHTMLTags aTag){ if(!FindTagInSet(aTag,gWhitespaceTags,sizeof(gWhitespaceTags)/sizeof(aTag))){ //the following code builds the set of style tags to be opened... + +/* eHTMLTags theStyles[50]; int theCount=0; @@ -2162,6 +2163,9 @@ nsresult CNavDTD::OpenTransientStyles(eHTMLTags aTag){ result=OpenContainer(theNode,PR_FALSE); } mBodyContext->mOpenStyles=theCount; + +*/ + } return result; } @@ -2184,6 +2188,8 @@ nsresult CNavDTD::CloseTransientStyles(eHTMLTags aTag){ int theTagPos=0; //now iterate style set, and close the containers... +/* + nsDeque* theStyleDeque=mBodyContext->GetStyles(); for(theTagPos=mBodyContext->mOpenStyles;theTagPos>0;theTagPos--){ eHTMLTags theTag=GetTopNode(); CStartToken token(theTag); @@ -2191,7 +2197,7 @@ nsresult CNavDTD::CloseTransientStyles(eHTMLTags aTag){ token.SetTypeID(theTag); result=CloseContainer(theNode,theTag,PR_FALSE); } - mBodyContext->mOpenStyles=0; +*/ return result; } @@ -2794,7 +2800,7 @@ nsresult CNavDTD::CreateContextStackFor(eHTMLTags aChildTag){ //you have that aren't in the stack... nsAutoString theEmpty; CStartToken theToken(theEmpty); - PRInt32 count = kPropagationStack.mTags->GetSize(); + PRInt32 count = kPropagationStack.GetSize(); if(PR_TRUE==bResult){ while(count>0) { eHTMLTags theTag=kPropagationStack.Pop(); @@ -2845,7 +2851,7 @@ nsresult CNavDTD::UpdateStyleStackForOpenTag(eHTMLTags aTag,eHTMLTags anActualTa nsresult result=0; if(nsHTMLElement::IsStyleTag(aTag)) { - nsTagStack* theStyleStack=mBodyContext->mStyles[mBodyContext->GetCount()-1]; + nsTagStack* theStyleStack=mBodyContext->GetStyles(); if(theStyleStack){ theStyleStack->Push(aTag); } @@ -2866,14 +2872,11 @@ nsresult CNavDTD::UpdateStyleStackForCloseTag(eHTMLTags aTag,eHTMLTags anActualTag){ nsresult result=0; - if(mBodyContext->mStyles){ - nsTagStack* theStyleStack=mBodyContext->mStyles[mBodyContext->GetCount()-1]; - if(theStyleStack){ - if(nsHTMLElement::IsStyleTag(aTag)) { - if(aTag==anActualTag) { - theStyleStack->Pop(); - mBodyContext->mOpenStyles--; - } + nsTagStack* theStyles=mBodyContext->GetStyles(); + if(theStyles){ + if(nsHTMLElement::IsStyleTag(aTag)) { + if(aTag==anActualTag) { + theStyles->Pop(); } }//if }//if diff --git a/htmlparser/src/nsDTDUtils.cpp b/htmlparser/src/nsDTDUtils.cpp index 7062a3f7e04b..343a9f5b7f2a 100644 --- a/htmlparser/src/nsDTDUtils.cpp +++ b/htmlparser/src/nsDTDUtils.cpp @@ -29,17 +29,22 @@ /** * Default constructor * @update harishd 04/04/99 + * @update gess 04/22/99 */ -nsTagStack::nsTagStack(int aDefaultSize) { - mTags =new nsDeque(nsnull); +nsTagStack::nsTagStack() { + mCapacity=0; + mCount=0; + mEntries=0; } /** * Default destructor * @update harishd 04/04/99 + * @update gess 04/22/99 */ nsTagStack::~nsTagStack() { - delete mTags; + if(mEntries) + delete [] mEntries; } /** @@ -47,30 +52,54 @@ nsTagStack::~nsTagStack() { * @update harishd 04/04/99 */ void nsTagStack::Empty(void) { - mTags->Empty(); + mCount=0; } /** * - * @update harishd 04/04/99 + * @update gess 04/22/99 */ void nsTagStack::Push(eHTMLTags aTag) { - nsTags* result = new nsTags(); - result->mTag=aTag; - mTags->Push(result); + if(mCount==mCapacity){ + nsTagEntry* temp=new nsTagEntry[mCapacity+50]; + if(mCapacity){ + PRUint32 index=0; + for(index=0;indexGetSize() > 0) { - nsTags* t = (nsTags*)mTags->Pop(); - result=t->mTag; - delete t; + if(0GetSize() > 0) { - nsTags* result = (nsTags*)mTags->Peek(); - return result->mTag; +eHTMLTags nsTagStack::TagAt(PRUint32 anIndex) const { + eHTMLTags result=eHTMLTag_unknown; + if(anIndex=0) && (anIndexGetSize())) { - nsTags* result = (nsTags*)mTags->ObjectAt(anIndex); - return result->mTag; +nsTagEntry& nsTagStack::EntryAt(PRUint32 anIndex) const { + static nsTagEntry gSentinel; + if(anIndex=0) && (anIndexGetSize())) { - nsTags* result = (nsTags*)mTags->ObjectAt(anIndex); - return result->mTag; - } - return eHTMLTag_unknown; + return gSentinel; } /** * * @update harishd 04/04/99 + * @update gess 04/21/99 + */ +eHTMLTags nsTagStack::operator[](PRUint32 anIndex) const { + eHTMLTags result=eHTMLTag_unknown; + if(anIndexGetSize(); - if(size > 0) { - nsTags* result = (nsTags*)mTags->ObjectAt(size - 1); - return result->mTag; - } - return eHTMLTag_unknown; - + return TagAt(mCount-1); } /** * * @update harishd 04/04/99 + * @update gess 04/21/99 */ PRInt32 nsTagStack::GetTopmostIndexOf(eHTMLTags aTag) const { int theIndex=0; - nsTags* result; - for(theIndex=(mTags->GetSize() - 1);theIndex>=0;theIndex--){ - result = (nsTags*)mTags->ObjectAt(theIndex); - if(result->mTag==aTag) + for(theIndex=mCount-1;theIndex>=0;theIndex--){ + if(aTag==TagAt(theIndex)) return theIndex; } return kNotFound; } -/** - * - * @update harishd 04/04/99 - */ -void nsTagStack::SaveToken(CToken* aToken, PRInt32 aID) -{ - NS_PRECONDITION(aID <= mTags->GetSize() && aID > -1,"Out of bounds"); - - if(aToken) { - nsTags* result = (nsTags*)mTags->ObjectAt(aID); - result->mTokenBank->Push(aToken); - } -} - -/** - * - * @update harishd 04/04/99 - */ -CToken* nsTagStack::RestoreTokenFrom(PRInt32 aID) -{ - NS_PRECONDITION(aID <= mTags->GetSize() && aID > -1,"Out of bounds"); - - if(mTags->GetSize() > 0) { - nsTags* result = (nsTags*)mTags->ObjectAt(aID); - return (CToken*)result->mTokenBank->PopFront(); - } - return nsnull; -} - -/** - * - * @update harishd 04/04/99 - */ -PRInt32 nsTagStack::TokenCountAt(PRInt32 aID) -{ - NS_PRECONDITION(aID <= mTags->GetSize(),"Out of bounds"); - if(aID < 0) - return kNotFound; - nsTags* result = (nsTags*)mTags->ObjectAt(aID); - return result->mTokenBank->GetSize(); -} @@ -196,12 +180,10 @@ PRInt32 nsTagStack::TokenCountAt(PRInt32 aID) * * @update gess9/10/98 */ -nsDTDContext::nsDTDContext(int aDefaultSize) { -#ifndef NS_DEBUG - mStyles =new nsTagStack*[aDefaultSize]; +nsDTDContext::nsDTDContext() : mStack(), mSkipped(0), mStyles(0) { +#ifdef NS_DEBUG + nsCRT::zero(mTags,sizeof(mTags)); #endif - mOpenStyles=0; - nsCRT::zero(mStyles,aDefaultSize*sizeof(void*)); } @@ -210,10 +192,6 @@ nsDTDContext::nsDTDContext(int aDefaultSize) { * @update gess9/10/98 */ nsDTDContext::~nsDTDContext() { -#ifndef NS_DEBUG - delete mStyles; - mStyles=0; -#endif } /** @@ -221,7 +199,7 @@ nsDTDContext::~nsDTDContext() { * @update gess7/9/98, harishd 04/04/99 */ PRInt32 nsDTDContext::GetCount(void) { - return mTags.mTags->GetSize(); + return mStack.GetSize(); } /** @@ -229,31 +207,24 @@ PRInt32 nsDTDContext::GetCount(void) { * @update gess7/9/98, harishd 04/04/99 */ void nsDTDContext::Push(eHTMLTags aTag) { -#ifndef NS_DEBUG - NS_PRECONDITION(GetCount()=nsTagStack::eStackSize) { - nsTagStack** tmp2=new nsTagStack*[2*(nsTagStack::eStackSize)]; - nsCRT::zero(tmp2,2*(nsTagStack::eStackSize)*sizeof(void*)); - nsCRT::memcpy(tmp2,mStyles,(nsTagStack::eStackSize)*sizeof(void*)); - delete mStyles; - mStyles=tmp2; - } +#ifdef NS_DEBUG + if(mStack.mCount0) { - result=mTags.Pop(); - size--; - mStyles[size]=0; - } +#ifdef NS_DEBUG + if(mStack.mCount>0) + mTags[mStack.mCount-1]=eHTMLTag_unknown; +#endif + + eHTMLTags result=mStack.Pop(); return result; } @@ -262,7 +233,7 @@ eHTMLTags nsDTDContext::Pop() { * @update gess7/9/98 */ eHTMLTags nsDTDContext::First() const { - return mTags.First(); + return mStack.First(); } /** @@ -270,7 +241,7 @@ eHTMLTags nsDTDContext::First() const { * @update gess7/9/98 */ eHTMLTags nsDTDContext::TagAt(PRInt32 anIndex) const { - return mTags.TagAt(anIndex); + return mStack.TagAt(anIndex); } @@ -279,7 +250,7 @@ eHTMLTags nsDTDContext::TagAt(PRInt32 anIndex) const { * @update gess7/9/98 */ eHTMLTags nsDTDContext::operator[](PRInt32 anIndex) const { - return mTags[anIndex]; + return mStack[anIndex]; } /** @@ -287,9 +258,83 @@ eHTMLTags nsDTDContext::operator[](PRInt32 anIndex) const { * @update gess7/9/98 */ eHTMLTags nsDTDContext::Last() const { - return mTags.Last(); + return mStack.Last(); } +/** + * + * @update gess7/9/98 + */ +nsTagStack* nsDTDContext::GetStyles(void) const { + PRInt32 theIndex=mStack.mEntries[mStack.mCount-1].mStyleIndex; + nsTagStack* result=0; + if(-1 -1,"Out of bounds"); + + if(aToken) { + nsTagEntry& theEntry=mStack.EntryAt(aID); + //ok, now go get the right tokenbank deque... + nsDeque* theDeque=0; + if(-1Push(aToken); + } +} + +/** + * + * @update harishd 04/04/99 + * @update gess 04/21/99 + */ +CToken* nsDTDContext::RestoreTokenFrom(PRInt32 aID) +{ + NS_PRECONDITION(aID <= mStack.GetSize() && aID > -1,"Out of bounds"); + CToken* result=0; + if(0PopFront(); + } + } + return result; +} + +/** + * + * @update harishd 04/04/99 + * @update gess 04/21/99 + */ +PRInt32 nsDTDContext::TokenCountAt(PRInt32 aID) +{ + NS_PRECONDITION(aID <= mStack.GetSize(),"Out of bounds"); + + nsTagEntry theEntry=mStack.EntryAt(aID); + nsDeque* theDeque=(nsDeque*)mSkipped.ObjectAt(theEntry.mBankIndex); + if(theDeque){ + return theDeque->GetSize(); + } + + return kNotFound; +} /************************************************************** Now define the tokenrecycler class... diff --git a/htmlparser/src/nsDTDUtils.h b/htmlparser/src/nsDTDUtils.h index 2d4dfa0ef3e5..8d5cacc3509e 100644 --- a/htmlparser/src/nsDTDUtils.h +++ b/htmlparser/src/nsDTDUtils.h @@ -59,36 +59,30 @@ PRUint32 AccumulateCRC(PRUint32 crc_accum, char *data_blk_ptr, int data_blk_size also the regular tags. **************************************************************/ -typedef struct _nsTagStruct{ - _nsTagStruct():mTokenBank(new nsDeque(nsnull)),mTag(eHTMLTag_unknown){} - ~_nsTagStruct(){delete mTokenBank;} +struct nsTagEntry { eHTMLTags mTag; - nsDeque* mTokenBank; -}nsTags; + PRInt8 mBankIndex; + PRInt8 mStyleIndex; +}; class nsTagStack { public: - -#ifdef NS_DEBUG - enum {eStackSize=200}; -#else - enum {eStackSize=30}; -#endif - nsTagStack(int aDefaultSize=eStackSize); + nsTagStack(); ~nsTagStack(); void Push(eHTMLTags aTag); eHTMLTags Pop(); eHTMLTags First() const; - eHTMLTags TagAt(PRInt32 anIndex) const; + eHTMLTags TagAt(PRUint32 anIndex) const; + nsTagEntry& EntryAt(PRUint32 anIndex) const; PRInt32 GetTopmostIndexOf(eHTMLTags aTag) const; - eHTMLTags operator[](PRInt32 anIndex) const; + eHTMLTags operator[](PRUint32 anIndex) const; eHTMLTags Last() const; void Empty(void); + PRInt32 GetSize(void) const {return mCount;} - void SaveToken(CToken* aToken, PRInt32 aID); - CToken* RestoreTokenFrom(PRInt32 aID); - PRInt32 TokenCountAt(PRInt32 aID); - nsDeque* mTags; + nsTagEntry* mEntries; + PRUint32 mCount; + PRUint32 mCapacity; }; @@ -101,7 +95,7 @@ public: class nsDTDContext { public: - nsDTDContext(int aDefaultSize=nsTagStack::eStackSize); + nsDTDContext(); ~nsDTDContext(); void Push(eHTMLTags aTag); eHTMLTags Pop(); @@ -111,15 +105,18 @@ public: eHTMLTags Last() const; void Empty(void); PRInt32 GetCount(void); + nsTagStack* GetStyles(void) const; - nsTagStack mTags; + void SaveToken(CToken* aToken, PRInt32 aID); + CToken* RestoreTokenFrom(PRInt32 aID); + PRInt32 TokenCountAt(PRInt32 aID); -#ifndef NS_DEBUG - nsTagStack** mStyles; -#else - nsTagStack* mStyles[nsTagStack::eStackSize]; + nsTagStack mStack; + nsDeque mSkipped; //each entry will hold a deque full of skipped tokens... + nsDeque mStyles; //each entry will hold a tagstack full of style tags... +#ifdef NS_DEBUG + eHTMLTags mTags[100]; #endif - int mOpenStyles; }; diff --git a/htmlparser/src/nsElementTable.cpp b/htmlparser/src/nsElementTable.cpp index 35f14db83ec6..c441741ddd01 100644 --- a/htmlparser/src/nsElementTable.cpp +++ b/htmlparser/src/nsElementTable.cpp @@ -48,7 +48,7 @@ PRBool CTagList::Contains(eHTMLTags aTag){ * @return */ PRInt32 CTagList::GetTopmostIndexOf(nsTagStack& aTagStack){ - int max = aTagStack.mTags->GetSize(); + int max = aTagStack.GetSize(); int index; for(index=max-1;index>=0;index--){ if(Contains(aTagStack[index])) { @@ -65,7 +65,7 @@ PRInt32 CTagList::GetTopmostIndexOf(nsTagStack& aTagStack){ * @return */ PRInt32 CTagList::GetBottommostIndexOf(nsTagStack& aTagStack,PRInt32 aStartOffset){ - int max = aTagStack.mTags->GetSize(); + int max = aTagStack.GetSize(); int index; for(index=aStartOffset;indexSetVerificationDirectory(kVerificationDir); } if(mDTDDebug) { - // mDTDDebug->Verify(this,aParser,mBodyContext->GetCount(),mBodyContext->mTags,aURLRef); + // mDTDDebug->Verify(this,aParser,mBodyContext->GetCount(),mBodyContext->mStack,aURLRef); } return result; } @@ -640,7 +640,7 @@ nsresult CNavDTD::HandleToken(CToken* aToken,nsIParser* aParser){ mTokenizer->PushTokenFront(theToken); else result=NS_OK; if (mDTDDebug) { - //mDTDDebug->Verify(this, mParser, mBodyContext->GetCount(), mBodyContext->mTags, mFilename); + //mDTDDebug->Verify(this, mParser, mBodyContext->GetCount(), mBodyContext->mStack, mFilename); } } } @@ -742,7 +742,7 @@ nsresult CNavDTD::DidHandleStartTag(nsCParserNode& aNode,eHTMLTags aChildTag){ static PRInt32 GetTopmostIndexOf(eHTMLTags aTag,nsTagStack& aTagStack) { int i=0; - int count = aTagStack.mTags->GetSize(); + int count = aTagStack.GetSize(); for(i=(count-1);i>=0;i--){ if(aTagStack[i]==aTag) return i; @@ -762,7 +762,7 @@ PRInt32 GetTopmostIndexOf(eHTMLTags aTag,nsTagStack& aTagStack) { */ static eHTMLTags FindAutoCloseTargetForStartTag(eHTMLTags aCurrentTag,nsTagStack& aTagStack) { - int theTopIndex = aTagStack.mTags->GetSize(); + int theTopIndex = aTagStack.GetSize(); eHTMLTags thePrevTag=aTagStack.Last(); if(nsHTMLElement::IsContainer(aCurrentTag)){ @@ -950,10 +950,10 @@ nsresult CNavDTD::HandleDefaultStartToken(CToken* aToken,eHTMLTags aChildTag,nsI eHTMLTags theParentTag=mBodyContext->Last(); PRBool theCanContainResult=CanContain(theParentTag,aChildTag); - PRBool theChildAgrees=(theCanContainResult) ? CanBeContained(theParentTag,aChildTag,mBodyContext->mTags) : PR_FALSE; + PRBool theChildAgrees=(theCanContainResult) ? CanBeContained(theParentTag,aChildTag,mBodyContext->mStack) : PR_FALSE; if(!(theCanContainResult && theChildAgrees)) { - eHTMLTags theTarget=FindAutoCloseTargetForStartTag(aChildTag,mBodyContext->mTags); + eHTMLTags theTarget=FindAutoCloseTargetForStartTag(aChildTag,mBodyContext->mStack); if(eHTMLTag_unknown!=theTarget){ result=CloseContainersTo(theTarget,PR_TRUE); theParentTag=mBodyContext->Last(); @@ -986,9 +986,7 @@ nsresult CNavDTD::HandleDefaultStartToken(CToken* aToken,eHTMLTags aChildTag,nsI //first, let's see if it's a style element... if(!nsHTMLElement::IsStyleTag(aChildTag)) { //it wasn't a style container, so open the element container... - if(mBodyContext->mOpenStyles) { - CloseTransientStyles(aChildTag); - } + CloseTransientStyles(aChildTag); } result=OpenContainer(aNode,PR_TRUE); } @@ -1159,18 +1157,19 @@ nsresult CNavDTD::HandleOmittedTag(CToken* aToken,eHTMLTags aChildTag,eHTMLTags if(!FindTagInSet(aChildTag,gWhitespaceTags,sizeof(gWhitespaceTags)/sizeof(aChildTag))) { isNotWhiteSpace = mSaveBadTokens = PR_TRUE; } - if(mSaveBadTokens &&(isNotWhiteSpace || mBodyContext->mTags.TokenCountAt(theBCIndex) > 0)) { - mBodyContext->mTags.SaveToken(aToken,theBCIndex); + if(mSaveBadTokens &&(isNotWhiteSpace || mBodyContext->TokenCountAt(theBCIndex) > 0)) { + mBodyContext->SaveToken(aToken,theBCIndex); if(attrCount > 0) { nsCParserNode* theAttrNode = (nsCParserNode*)&aNode; while(attrCount > 0){ - mBodyContext->mTags.SaveToken(theAttrNode->PopAttributeToken(),theBCIndex); + mBodyContext->SaveToken(theAttrNode->PopAttributeToken(),theBCIndex); attrCount--; } } if(!IsContainer(aChildTag) && isNotWhiteSpace) { mSaveBadTokens = PR_FALSE; } + result=NS_ERROR_HTMLPARSER_MISPLACED; } } return result; @@ -1296,7 +1295,7 @@ PRBool HasCloseablePeerAboveRoot(CTagList& aRootTagList,nsTagStack& aTagStack,eH */ static eHTMLTags FindAutoCloseTargetForEndTag(eHTMLTags aCurrentTag,nsTagStack& aTagStack) { - int theTopIndex=aTagStack.mTags->GetSize(); + int theTopIndex=aTagStack.GetSize(); eHTMLTags thePrevTag=aTagStack.Last(); if(nsHTMLElement::IsContainer(aCurrentTag)){ @@ -1401,7 +1400,7 @@ nsresult CNavDTD::HandleEndToken(CToken* aToken) { UpdateStyleStackForCloseTag(theChildTag,theChildTag); } else { - eHTMLTags theTarget=FindAutoCloseTargetForEndTag(theChildTag,mBodyContext->mTags); + eHTMLTags theTarget=FindAutoCloseTargetForEndTag(theChildTag,mBodyContext->mStack); if(gHTMLElements[theTarget].HasSpecialProperty(kBadContentWatch)){ result = HandleSavedTokensAbove(theTarget); } @@ -1443,7 +1442,7 @@ nsresult CNavDTD::HandleSavedTokensAbove(eHTMLTags aTag) return result; PRInt32 theBadContentIndex = theTopIndex - 1; - PRInt32 theBadTokenCount = mBodyContext->mTags.TokenCountAt(theBadContentIndex); + PRInt32 theBadTokenCount = mBodyContext->TokenCountAt(theBadContentIndex); if(theBadTokenCount > 0) { // Pause the main context and switch to the new context. @@ -1455,7 +1454,7 @@ nsresult CNavDTD::HandleSavedTokensAbove(eHTMLTags aTag) // Now flush out all the bad contents. while(theBadTokenCount > 0){ - theToken = mBodyContext->mTags.RestoreTokenFrom(theBadContentIndex); + theToken = mBodyContext->RestoreTokenFrom(theBadContentIndex); if(theToken) { theTag = (eHTMLTags)theToken->GetTypeID(); if(theTag != eHTMLTag_unknown) { @@ -1464,7 +1463,7 @@ nsresult CNavDTD::HandleSavedTokensAbove(eHTMLTags aTag) if(attrCount > 0) { PRInt32 theAttrCount = 0; for(PRInt32 i=0;imTags.RestoreTokenFrom(theBadContentIndex); + CToken* theAttrToken = mBodyContext->RestoreTokenFrom(theBadContentIndex); if(theAttrToken) { mTokenizer->PushTokenFront(theAttrToken); theAttrCount++; @@ -1540,7 +1539,7 @@ nsresult CNavDTD::HandleCommentToken(CToken* aToken) { // You may find this hard to beleive, but this has to be here // so that the TBODY doesnt die when it sees a comment. // This case occurs on WWW.CREAF.COM - eHTMLTags theTag=mBodyContext->mTags.Last(); + eHTMLTags theTag=mBodyContext->mStack.Last(); nsresult result=NS_OK; switch(theTag) { @@ -1919,7 +1918,7 @@ PRBool CNavDTD::CanOmitEndTag(eHTMLTags aParent,eHTMLTags aChild) const { return PR_TRUE; } - PRInt32 theChildIndex=GetIndexOfChildOrSynonym(mBodyContext->mTags,aChild); + PRInt32 theChildIndex=GetIndexOfChildOrSynonym(mBodyContext->mStack,aChild); result=PRBool(kNotFound==theChildIndex); return result; @@ -2111,7 +2110,7 @@ PRInt32 CNavDTD::GetTopmostIndexOf(eHTMLTags aTagSet[],PRInt32 aCount) const { * @return topmost index of tag on stack */ PRInt32 CNavDTD::GetTopmostIndexOf(eHTMLTags aTag) const { - return mBodyContext->mTags.GetTopmostIndexOf(aTag); + return mBodyContext->mStack.GetTopmostIndexOf(aTag); } /********************************************* @@ -2136,6 +2135,8 @@ nsresult CNavDTD::OpenTransientStyles(eHTMLTags aTag){ if(!FindTagInSet(aTag,gWhitespaceTags,sizeof(gWhitespaceTags)/sizeof(aTag))){ //the following code builds the set of style tags to be opened... + +/* eHTMLTags theStyles[50]; int theCount=0; @@ -2162,6 +2163,9 @@ nsresult CNavDTD::OpenTransientStyles(eHTMLTags aTag){ result=OpenContainer(theNode,PR_FALSE); } mBodyContext->mOpenStyles=theCount; + +*/ + } return result; } @@ -2184,6 +2188,8 @@ nsresult CNavDTD::CloseTransientStyles(eHTMLTags aTag){ int theTagPos=0; //now iterate style set, and close the containers... +/* + nsDeque* theStyleDeque=mBodyContext->GetStyles(); for(theTagPos=mBodyContext->mOpenStyles;theTagPos>0;theTagPos--){ eHTMLTags theTag=GetTopNode(); CStartToken token(theTag); @@ -2191,7 +2197,7 @@ nsresult CNavDTD::CloseTransientStyles(eHTMLTags aTag){ token.SetTypeID(theTag); result=CloseContainer(theNode,theTag,PR_FALSE); } - mBodyContext->mOpenStyles=0; +*/ return result; } @@ -2794,7 +2800,7 @@ nsresult CNavDTD::CreateContextStackFor(eHTMLTags aChildTag){ //you have that aren't in the stack... nsAutoString theEmpty; CStartToken theToken(theEmpty); - PRInt32 count = kPropagationStack.mTags->GetSize(); + PRInt32 count = kPropagationStack.GetSize(); if(PR_TRUE==bResult){ while(count>0) { eHTMLTags theTag=kPropagationStack.Pop(); @@ -2845,7 +2851,7 @@ nsresult CNavDTD::UpdateStyleStackForOpenTag(eHTMLTags aTag,eHTMLTags anActualTa nsresult result=0; if(nsHTMLElement::IsStyleTag(aTag)) { - nsTagStack* theStyleStack=mBodyContext->mStyles[mBodyContext->GetCount()-1]; + nsTagStack* theStyleStack=mBodyContext->GetStyles(); if(theStyleStack){ theStyleStack->Push(aTag); } @@ -2866,14 +2872,11 @@ nsresult CNavDTD::UpdateStyleStackForCloseTag(eHTMLTags aTag,eHTMLTags anActualTag){ nsresult result=0; - if(mBodyContext->mStyles){ - nsTagStack* theStyleStack=mBodyContext->mStyles[mBodyContext->GetCount()-1]; - if(theStyleStack){ - if(nsHTMLElement::IsStyleTag(aTag)) { - if(aTag==anActualTag) { - theStyleStack->Pop(); - mBodyContext->mOpenStyles--; - } + nsTagStack* theStyles=mBodyContext->GetStyles(); + if(theStyles){ + if(nsHTMLElement::IsStyleTag(aTag)) { + if(aTag==anActualTag) { + theStyles->Pop(); } }//if }//if diff --git a/parser/htmlparser/src/nsDTDUtils.cpp b/parser/htmlparser/src/nsDTDUtils.cpp index 7062a3f7e04b..343a9f5b7f2a 100644 --- a/parser/htmlparser/src/nsDTDUtils.cpp +++ b/parser/htmlparser/src/nsDTDUtils.cpp @@ -29,17 +29,22 @@ /** * Default constructor * @update harishd 04/04/99 + * @update gess 04/22/99 */ -nsTagStack::nsTagStack(int aDefaultSize) { - mTags =new nsDeque(nsnull); +nsTagStack::nsTagStack() { + mCapacity=0; + mCount=0; + mEntries=0; } /** * Default destructor * @update harishd 04/04/99 + * @update gess 04/22/99 */ nsTagStack::~nsTagStack() { - delete mTags; + if(mEntries) + delete [] mEntries; } /** @@ -47,30 +52,54 @@ nsTagStack::~nsTagStack() { * @update harishd 04/04/99 */ void nsTagStack::Empty(void) { - mTags->Empty(); + mCount=0; } /** * - * @update harishd 04/04/99 + * @update gess 04/22/99 */ void nsTagStack::Push(eHTMLTags aTag) { - nsTags* result = new nsTags(); - result->mTag=aTag; - mTags->Push(result); + if(mCount==mCapacity){ + nsTagEntry* temp=new nsTagEntry[mCapacity+50]; + if(mCapacity){ + PRUint32 index=0; + for(index=0;indexGetSize() > 0) { - nsTags* t = (nsTags*)mTags->Pop(); - result=t->mTag; - delete t; + if(0GetSize() > 0) { - nsTags* result = (nsTags*)mTags->Peek(); - return result->mTag; +eHTMLTags nsTagStack::TagAt(PRUint32 anIndex) const { + eHTMLTags result=eHTMLTag_unknown; + if(anIndex=0) && (anIndexGetSize())) { - nsTags* result = (nsTags*)mTags->ObjectAt(anIndex); - return result->mTag; +nsTagEntry& nsTagStack::EntryAt(PRUint32 anIndex) const { + static nsTagEntry gSentinel; + if(anIndex=0) && (anIndexGetSize())) { - nsTags* result = (nsTags*)mTags->ObjectAt(anIndex); - return result->mTag; - } - return eHTMLTag_unknown; + return gSentinel; } /** * * @update harishd 04/04/99 + * @update gess 04/21/99 + */ +eHTMLTags nsTagStack::operator[](PRUint32 anIndex) const { + eHTMLTags result=eHTMLTag_unknown; + if(anIndexGetSize(); - if(size > 0) { - nsTags* result = (nsTags*)mTags->ObjectAt(size - 1); - return result->mTag; - } - return eHTMLTag_unknown; - + return TagAt(mCount-1); } /** * * @update harishd 04/04/99 + * @update gess 04/21/99 */ PRInt32 nsTagStack::GetTopmostIndexOf(eHTMLTags aTag) const { int theIndex=0; - nsTags* result; - for(theIndex=(mTags->GetSize() - 1);theIndex>=0;theIndex--){ - result = (nsTags*)mTags->ObjectAt(theIndex); - if(result->mTag==aTag) + for(theIndex=mCount-1;theIndex>=0;theIndex--){ + if(aTag==TagAt(theIndex)) return theIndex; } return kNotFound; } -/** - * - * @update harishd 04/04/99 - */ -void nsTagStack::SaveToken(CToken* aToken, PRInt32 aID) -{ - NS_PRECONDITION(aID <= mTags->GetSize() && aID > -1,"Out of bounds"); - - if(aToken) { - nsTags* result = (nsTags*)mTags->ObjectAt(aID); - result->mTokenBank->Push(aToken); - } -} - -/** - * - * @update harishd 04/04/99 - */ -CToken* nsTagStack::RestoreTokenFrom(PRInt32 aID) -{ - NS_PRECONDITION(aID <= mTags->GetSize() && aID > -1,"Out of bounds"); - - if(mTags->GetSize() > 0) { - nsTags* result = (nsTags*)mTags->ObjectAt(aID); - return (CToken*)result->mTokenBank->PopFront(); - } - return nsnull; -} - -/** - * - * @update harishd 04/04/99 - */ -PRInt32 nsTagStack::TokenCountAt(PRInt32 aID) -{ - NS_PRECONDITION(aID <= mTags->GetSize(),"Out of bounds"); - if(aID < 0) - return kNotFound; - nsTags* result = (nsTags*)mTags->ObjectAt(aID); - return result->mTokenBank->GetSize(); -} @@ -196,12 +180,10 @@ PRInt32 nsTagStack::TokenCountAt(PRInt32 aID) * * @update gess9/10/98 */ -nsDTDContext::nsDTDContext(int aDefaultSize) { -#ifndef NS_DEBUG - mStyles =new nsTagStack*[aDefaultSize]; +nsDTDContext::nsDTDContext() : mStack(), mSkipped(0), mStyles(0) { +#ifdef NS_DEBUG + nsCRT::zero(mTags,sizeof(mTags)); #endif - mOpenStyles=0; - nsCRT::zero(mStyles,aDefaultSize*sizeof(void*)); } @@ -210,10 +192,6 @@ nsDTDContext::nsDTDContext(int aDefaultSize) { * @update gess9/10/98 */ nsDTDContext::~nsDTDContext() { -#ifndef NS_DEBUG - delete mStyles; - mStyles=0; -#endif } /** @@ -221,7 +199,7 @@ nsDTDContext::~nsDTDContext() { * @update gess7/9/98, harishd 04/04/99 */ PRInt32 nsDTDContext::GetCount(void) { - return mTags.mTags->GetSize(); + return mStack.GetSize(); } /** @@ -229,31 +207,24 @@ PRInt32 nsDTDContext::GetCount(void) { * @update gess7/9/98, harishd 04/04/99 */ void nsDTDContext::Push(eHTMLTags aTag) { -#ifndef NS_DEBUG - NS_PRECONDITION(GetCount()=nsTagStack::eStackSize) { - nsTagStack** tmp2=new nsTagStack*[2*(nsTagStack::eStackSize)]; - nsCRT::zero(tmp2,2*(nsTagStack::eStackSize)*sizeof(void*)); - nsCRT::memcpy(tmp2,mStyles,(nsTagStack::eStackSize)*sizeof(void*)); - delete mStyles; - mStyles=tmp2; - } +#ifdef NS_DEBUG + if(mStack.mCount0) { - result=mTags.Pop(); - size--; - mStyles[size]=0; - } +#ifdef NS_DEBUG + if(mStack.mCount>0) + mTags[mStack.mCount-1]=eHTMLTag_unknown; +#endif + + eHTMLTags result=mStack.Pop(); return result; } @@ -262,7 +233,7 @@ eHTMLTags nsDTDContext::Pop() { * @update gess7/9/98 */ eHTMLTags nsDTDContext::First() const { - return mTags.First(); + return mStack.First(); } /** @@ -270,7 +241,7 @@ eHTMLTags nsDTDContext::First() const { * @update gess7/9/98 */ eHTMLTags nsDTDContext::TagAt(PRInt32 anIndex) const { - return mTags.TagAt(anIndex); + return mStack.TagAt(anIndex); } @@ -279,7 +250,7 @@ eHTMLTags nsDTDContext::TagAt(PRInt32 anIndex) const { * @update gess7/9/98 */ eHTMLTags nsDTDContext::operator[](PRInt32 anIndex) const { - return mTags[anIndex]; + return mStack[anIndex]; } /** @@ -287,9 +258,83 @@ eHTMLTags nsDTDContext::operator[](PRInt32 anIndex) const { * @update gess7/9/98 */ eHTMLTags nsDTDContext::Last() const { - return mTags.Last(); + return mStack.Last(); } +/** + * + * @update gess7/9/98 + */ +nsTagStack* nsDTDContext::GetStyles(void) const { + PRInt32 theIndex=mStack.mEntries[mStack.mCount-1].mStyleIndex; + nsTagStack* result=0; + if(-1 -1,"Out of bounds"); + + if(aToken) { + nsTagEntry& theEntry=mStack.EntryAt(aID); + //ok, now go get the right tokenbank deque... + nsDeque* theDeque=0; + if(-1Push(aToken); + } +} + +/** + * + * @update harishd 04/04/99 + * @update gess 04/21/99 + */ +CToken* nsDTDContext::RestoreTokenFrom(PRInt32 aID) +{ + NS_PRECONDITION(aID <= mStack.GetSize() && aID > -1,"Out of bounds"); + CToken* result=0; + if(0PopFront(); + } + } + return result; +} + +/** + * + * @update harishd 04/04/99 + * @update gess 04/21/99 + */ +PRInt32 nsDTDContext::TokenCountAt(PRInt32 aID) +{ + NS_PRECONDITION(aID <= mStack.GetSize(),"Out of bounds"); + + nsTagEntry theEntry=mStack.EntryAt(aID); + nsDeque* theDeque=(nsDeque*)mSkipped.ObjectAt(theEntry.mBankIndex); + if(theDeque){ + return theDeque->GetSize(); + } + + return kNotFound; +} /************************************************************** Now define the tokenrecycler class... diff --git a/parser/htmlparser/src/nsDTDUtils.h b/parser/htmlparser/src/nsDTDUtils.h index 2d4dfa0ef3e5..8d5cacc3509e 100644 --- a/parser/htmlparser/src/nsDTDUtils.h +++ b/parser/htmlparser/src/nsDTDUtils.h @@ -59,36 +59,30 @@ PRUint32 AccumulateCRC(PRUint32 crc_accum, char *data_blk_ptr, int data_blk_size also the regular tags. **************************************************************/ -typedef struct _nsTagStruct{ - _nsTagStruct():mTokenBank(new nsDeque(nsnull)),mTag(eHTMLTag_unknown){} - ~_nsTagStruct(){delete mTokenBank;} +struct nsTagEntry { eHTMLTags mTag; - nsDeque* mTokenBank; -}nsTags; + PRInt8 mBankIndex; + PRInt8 mStyleIndex; +}; class nsTagStack { public: - -#ifdef NS_DEBUG - enum {eStackSize=200}; -#else - enum {eStackSize=30}; -#endif - nsTagStack(int aDefaultSize=eStackSize); + nsTagStack(); ~nsTagStack(); void Push(eHTMLTags aTag); eHTMLTags Pop(); eHTMLTags First() const; - eHTMLTags TagAt(PRInt32 anIndex) const; + eHTMLTags TagAt(PRUint32 anIndex) const; + nsTagEntry& EntryAt(PRUint32 anIndex) const; PRInt32 GetTopmostIndexOf(eHTMLTags aTag) const; - eHTMLTags operator[](PRInt32 anIndex) const; + eHTMLTags operator[](PRUint32 anIndex) const; eHTMLTags Last() const; void Empty(void); + PRInt32 GetSize(void) const {return mCount;} - void SaveToken(CToken* aToken, PRInt32 aID); - CToken* RestoreTokenFrom(PRInt32 aID); - PRInt32 TokenCountAt(PRInt32 aID); - nsDeque* mTags; + nsTagEntry* mEntries; + PRUint32 mCount; + PRUint32 mCapacity; }; @@ -101,7 +95,7 @@ public: class nsDTDContext { public: - nsDTDContext(int aDefaultSize=nsTagStack::eStackSize); + nsDTDContext(); ~nsDTDContext(); void Push(eHTMLTags aTag); eHTMLTags Pop(); @@ -111,15 +105,18 @@ public: eHTMLTags Last() const; void Empty(void); PRInt32 GetCount(void); + nsTagStack* GetStyles(void) const; - nsTagStack mTags; + void SaveToken(CToken* aToken, PRInt32 aID); + CToken* RestoreTokenFrom(PRInt32 aID); + PRInt32 TokenCountAt(PRInt32 aID); -#ifndef NS_DEBUG - nsTagStack** mStyles; -#else - nsTagStack* mStyles[nsTagStack::eStackSize]; + nsTagStack mStack; + nsDeque mSkipped; //each entry will hold a deque full of skipped tokens... + nsDeque mStyles; //each entry will hold a tagstack full of style tags... +#ifdef NS_DEBUG + eHTMLTags mTags[100]; #endif - int mOpenStyles; }; diff --git a/parser/htmlparser/src/nsElementTable.cpp b/parser/htmlparser/src/nsElementTable.cpp index 35f14db83ec6..c441741ddd01 100644 --- a/parser/htmlparser/src/nsElementTable.cpp +++ b/parser/htmlparser/src/nsElementTable.cpp @@ -48,7 +48,7 @@ PRBool CTagList::Contains(eHTMLTags aTag){ * @return */ PRInt32 CTagList::GetTopmostIndexOf(nsTagStack& aTagStack){ - int max = aTagStack.mTags->GetSize(); + int max = aTagStack.GetSize(); int index; for(index=max-1;index>=0;index--){ if(Contains(aTagStack[index])) { @@ -65,7 +65,7 @@ PRInt32 CTagList::GetTopmostIndexOf(nsTagStack& aTagStack){ * @return */ PRInt32 CTagList::GetBottommostIndexOf(nsTagStack& aTagStack,PRInt32 aStartOffset){ - int max = aTagStack.mTags->GetSize(); + int max = aTagStack.GetSize(); int index; for(index=aStartOffset;index