Preserve tokenizer state between document.writes. b=99467, r=heikki@netscape.com, sr=jst@netscape.com

This commit is contained in:
harishd%netscape.com 2003-05-28 20:56:21 +00:00
parent 00a02d628d
commit 9dea772152
12 changed files with 126 additions and 80 deletions

View File

@ -87,6 +87,7 @@ public:
NS_IMETHOD_(PRInt32) GetCount(void)=0;
NS_IMETHOD_(nsTokenAllocator*) GetTokenAllocator(void)=0;
NS_IMETHOD_(void) PrependTokens(nsDeque& aDeque)=0;
NS_IMETHOD CopyState(nsITokenizer* aTokenizer) = 0;
};
@ -101,7 +102,8 @@ public:
NS_IMETHOD_(CToken*) GetTokenAt(PRInt32 anIndex);\
NS_IMETHOD_(PRInt32) GetCount(void);\
NS_IMETHOD_(nsTokenAllocator*) GetTokenAllocator(void);\
NS_IMETHOD_(void) PrependTokens(nsDeque& aDeque);
NS_IMETHOD_(void) PrependTokens(nsDeque& aDeque);\
NS_IMETHOD CopyState(nsITokenizer* aTokenizer);
#endif

View File

@ -172,6 +172,10 @@ CParserContext::GetTokenizer(PRInt32 aType, nsITokenizer*& aTokenizer) {
if(!mTokenizer) {
if (aType == NS_IPARSER_FLAG_HTML || mParserCommand == eViewSource) {
result = NS_NewHTMLTokenizer(&mTokenizer,mDTDMode,mDocType,mParserCommand);
// Propagate tokenizer state so that information is preserved
// between document.write. This fixes bug 99467
if (mTokenizer && mPrevContext)
mTokenizer->CopyState(mPrevContext->mTokenizer);
}
else if (aType == NS_IPARSER_FLAG_XML)
{

View File

@ -1129,6 +1129,12 @@ nsExpatDriver::PrependTokens(nsDeque& aDeque)
}
NS_IMETHODIMP
nsExpatDriver::CopyState(nsITokenizer* aTokenizer)
{
return NS_OK;
}
NS_IMETHODIMP
nsExpatDriver::HandleToken(CToken* aToken,nsIParser* aParser)
{

View File

@ -162,9 +162,9 @@ NS_IMPL_RELEASE(nsHTMLTokenizer)
mFlags |= (aCommand==eViewSource)? NS_IPARSER_FLAG_VIEW_SOURCE:NS_IPARSER_FLAG_VIEW_NORMAL;
mRecordTrailingContent=PR_FALSE;
mTokenAllocator=nsnull;
mTokenScanPos=0;
mTokenAllocator = nsnull;
mTokenScanPos = 0;
mPreserveTarget = eHTMLTag_unknown;
}
@ -308,6 +308,15 @@ void nsHTMLTokenizer::PrependTokens(nsDeque& aDeque){
}
NS_IMETHODIMP
nsHTMLTokenizer::CopyState(nsITokenizer* aTokenizer)
{
if (aTokenizer)
mPreserveTarget =
NS_STATIC_CAST(nsHTMLTokenizer*, aTokenizer)->mPreserveTarget;
return NS_OK;
}
/**
* This is a utilty method for ScanDocStructure, which finds a given
* tag in the stack.
@ -723,17 +732,17 @@ nsresult nsHTMLTokenizer::ConsumeStartTag(PRUnichar aChar,CToken*& aToken,nsScan
CStartToken* theStartToken = NS_STATIC_CAST(CStartToken*,aToken);
//XXX - Find a better soution to record content
//Added _plaintext to fix bug 46054.
if((theTag == eHTMLTag_textarea ||
if(mPreserveTarget == eHTMLTag_unknown &&
(theTag == eHTMLTag_textarea ||
theTag == eHTMLTag_xmp ||
theTag == eHTMLTag_plaintext ||
theTag == eHTMLTag_noscript ||
theTag == eHTMLTag_noframes) &&
!mRecordTrailingContent) {
mRecordTrailingContent=PR_TRUE;
theTag == eHTMLTag_noframes)) {
mPreserveTarget = theTag;
}
if(mRecordTrailingContent)
RecordTrailingContent(theStartToken,aScanner,origin);
if (mPreserveTarget != eHTMLTag_unknown)
PreserveToken(theStartToken, aScanner, origin);
//if((eHTMLTag_style==theTag) || (eHTMLTag_script==theTag)) {
if(gHTMLElements[theTag].CanContainType(kCDATA)) {
@ -808,15 +817,11 @@ nsresult nsHTMLTokenizer::ConsumeEndTag(PRUnichar aChar,CToken*& aToken,nsScanne
aScanner.GetChar(aChar);
}
if(NS_SUCCEEDED(result)) {
eHTMLTags theTag=(eHTMLTags)aToken->GetTypeID();
if((theTag == eHTMLTag_textarea ||
theTag == eHTMLTag_xmp ||
theTag == eHTMLTag_plaintext ||
theTag == eHTMLTag_noscript ||
theTag == eHTMLTag_noframes) &&
mRecordTrailingContent) {
mRecordTrailingContent=PR_FALSE;
if (NS_SUCCEEDED(result)) {
eHTMLTags theTag = (eHTMLTags)aToken->GetTypeID();
if (mPreserveTarget == theTag) {
// Target reached. Stop preserving content.
mPreserveTarget = eHTMLTag_unknown;
}
}
} //if
@ -1043,12 +1048,14 @@ nsresult nsHTMLTokenizer::ConsumeProcessingInstruction(PRUnichar aChar,CToken*&
*
*/
void nsHTMLTokenizer::RecordTrailingContent(CStartToken* aStartToken, nsScanner& aScanner, nsReadingIterator<PRUnichar> aOrigin) {
void nsHTMLTokenizer::PreserveToken(CStartToken* aStartToken,
nsScanner& aScanner,
nsReadingIterator<PRUnichar> aOrigin) {
if(aStartToken) {
nsReadingIterator<PRUnichar> theCurrentPosition;
aScanner.CurrentPosition(theCurrentPosition);
nsString& trailingContent =aStartToken->mTrailingContent;
nsString& trailingContent = aStartToken->mTrailingContent;
PRUint32 oldLength = trailingContent.Length();
trailingContent.SetLength(oldLength + Distance(aOrigin, theCurrentPosition));

View File

@ -73,9 +73,9 @@ public:
NS_DECL_ISUPPORTS
NS_DECL_NSITOKENIZER
nsHTMLTokenizer(PRInt32 aParseMode=eDTDMode_quirks,
eParserDocType aDocType=eHTML3_Quirks,
eParserCommands aCommand=eViewNormal);
nsHTMLTokenizer(PRInt32 aParseMode = eDTDMode_quirks,
eParserDocType aDocType = eHTML3_Quirks,
eParserCommands aCommand = eViewNormal);
virtual ~nsHTMLTokenizer();
protected:
@ -92,24 +92,23 @@ protected:
virtual nsresult ConsumeSpecialMarkup(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeProcessingInstruction(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ScanDocStructure(PRBool aIsFinalChunk);
nsresult ScanDocStructure(PRBool aIsFinalChunk);
virtual void RecordTrailingContent(CStartToken* aStartToken, nsScanner& aScanner, nsReadingIterator<PRUnichar> aOrigin);
virtual void PreserveToken(CStartToken* aStartToken, nsScanner& aScanner, nsReadingIterator<PRUnichar> aOrigin);
static void AddToken(CToken*& aToken,nsresult aResult,nsDeque* aDeque,nsTokenAllocator* aTokenAllocator);
nsDeque mTokenDeque;
PRInt32 mFlags;
PRPackedBool mRecordTrailingContent;
PRPackedBool mIsFinalChunk;
nsTokenAllocator* mTokenAllocator;
PRInt32 mTokenScanPos;
PRUint32 mFlags;
eHTMLTags mPreserveTarget; // Tag whose content is preserved
};
extern nsresult NS_NewHTMLTokenizer( nsITokenizer** aInstancePtrResult,
PRInt32 aMode,
eParserDocType aDocType,
eParserCommands aCommand);
extern nsresult NS_NewHTMLTokenizer(nsITokenizer** aInstancePtrResult,
PRInt32 aMode,eParserDocType aDocType,
eParserCommands aCommand);
#endif

View File

@ -1281,17 +1281,22 @@ void nsParser::PushContext(CParserContext& aContext) {
* @update gess7/22/98
* @return prev. context
*/
CParserContext* nsParser::PopContext() {
CParserContext* oldContext=mParserContext;
if(oldContext) {
mParserContext=oldContext->mPrevContext;
// 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().
CParserContext* nsParser::PopContext()
{
CParserContext* oldContext = mParserContext;
if (oldContext) {
mParserContext = oldContext->mPrevContext;
if (mParserContext) {
if(mParserContext->mStreamListenerState!=eOnStop) {
// 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) {
mParserContext->mStreamListenerState = oldContext->mStreamListenerState;
}
// Preserve tokenizer state so that information is not lost
// between document.write. This fixes bug 99467
if (mParserContext->mTokenizer)
mParserContext->mTokenizer->CopyState(oldContext->mTokenizer);
}
}
return oldContext;

View File

@ -87,6 +87,7 @@ public:
NS_IMETHOD_(PRInt32) GetCount(void)=0;
NS_IMETHOD_(nsTokenAllocator*) GetTokenAllocator(void)=0;
NS_IMETHOD_(void) PrependTokens(nsDeque& aDeque)=0;
NS_IMETHOD CopyState(nsITokenizer* aTokenizer) = 0;
};
@ -101,7 +102,8 @@ public:
NS_IMETHOD_(CToken*) GetTokenAt(PRInt32 anIndex);\
NS_IMETHOD_(PRInt32) GetCount(void);\
NS_IMETHOD_(nsTokenAllocator*) GetTokenAllocator(void);\
NS_IMETHOD_(void) PrependTokens(nsDeque& aDeque);
NS_IMETHOD_(void) PrependTokens(nsDeque& aDeque);\
NS_IMETHOD CopyState(nsITokenizer* aTokenizer);
#endif

View File

@ -172,6 +172,10 @@ CParserContext::GetTokenizer(PRInt32 aType, nsITokenizer*& aTokenizer) {
if(!mTokenizer) {
if (aType == NS_IPARSER_FLAG_HTML || mParserCommand == eViewSource) {
result = NS_NewHTMLTokenizer(&mTokenizer,mDTDMode,mDocType,mParserCommand);
// Propagate tokenizer state so that information is preserved
// between document.write. This fixes bug 99467
if (mTokenizer && mPrevContext)
mTokenizer->CopyState(mPrevContext->mTokenizer);
}
else if (aType == NS_IPARSER_FLAG_XML)
{

View File

@ -1129,6 +1129,12 @@ nsExpatDriver::PrependTokens(nsDeque& aDeque)
}
NS_IMETHODIMP
nsExpatDriver::CopyState(nsITokenizer* aTokenizer)
{
return NS_OK;
}
NS_IMETHODIMP
nsExpatDriver::HandleToken(CToken* aToken,nsIParser* aParser)
{

View File

@ -162,9 +162,9 @@ NS_IMPL_RELEASE(nsHTMLTokenizer)
mFlags |= (aCommand==eViewSource)? NS_IPARSER_FLAG_VIEW_SOURCE:NS_IPARSER_FLAG_VIEW_NORMAL;
mRecordTrailingContent=PR_FALSE;
mTokenAllocator=nsnull;
mTokenScanPos=0;
mTokenAllocator = nsnull;
mTokenScanPos = 0;
mPreserveTarget = eHTMLTag_unknown;
}
@ -308,6 +308,15 @@ void nsHTMLTokenizer::PrependTokens(nsDeque& aDeque){
}
NS_IMETHODIMP
nsHTMLTokenizer::CopyState(nsITokenizer* aTokenizer)
{
if (aTokenizer)
mPreserveTarget =
NS_STATIC_CAST(nsHTMLTokenizer*, aTokenizer)->mPreserveTarget;
return NS_OK;
}
/**
* This is a utilty method for ScanDocStructure, which finds a given
* tag in the stack.
@ -723,17 +732,17 @@ nsresult nsHTMLTokenizer::ConsumeStartTag(PRUnichar aChar,CToken*& aToken,nsScan
CStartToken* theStartToken = NS_STATIC_CAST(CStartToken*,aToken);
//XXX - Find a better soution to record content
//Added _plaintext to fix bug 46054.
if((theTag == eHTMLTag_textarea ||
if(mPreserveTarget == eHTMLTag_unknown &&
(theTag == eHTMLTag_textarea ||
theTag == eHTMLTag_xmp ||
theTag == eHTMLTag_plaintext ||
theTag == eHTMLTag_noscript ||
theTag == eHTMLTag_noframes) &&
!mRecordTrailingContent) {
mRecordTrailingContent=PR_TRUE;
theTag == eHTMLTag_noframes)) {
mPreserveTarget = theTag;
}
if(mRecordTrailingContent)
RecordTrailingContent(theStartToken,aScanner,origin);
if (mPreserveTarget != eHTMLTag_unknown)
PreserveToken(theStartToken, aScanner, origin);
//if((eHTMLTag_style==theTag) || (eHTMLTag_script==theTag)) {
if(gHTMLElements[theTag].CanContainType(kCDATA)) {
@ -808,15 +817,11 @@ nsresult nsHTMLTokenizer::ConsumeEndTag(PRUnichar aChar,CToken*& aToken,nsScanne
aScanner.GetChar(aChar);
}
if(NS_SUCCEEDED(result)) {
eHTMLTags theTag=(eHTMLTags)aToken->GetTypeID();
if((theTag == eHTMLTag_textarea ||
theTag == eHTMLTag_xmp ||
theTag == eHTMLTag_plaintext ||
theTag == eHTMLTag_noscript ||
theTag == eHTMLTag_noframes) &&
mRecordTrailingContent) {
mRecordTrailingContent=PR_FALSE;
if (NS_SUCCEEDED(result)) {
eHTMLTags theTag = (eHTMLTags)aToken->GetTypeID();
if (mPreserveTarget == theTag) {
// Target reached. Stop preserving content.
mPreserveTarget = eHTMLTag_unknown;
}
}
} //if
@ -1043,12 +1048,14 @@ nsresult nsHTMLTokenizer::ConsumeProcessingInstruction(PRUnichar aChar,CToken*&
*
*/
void nsHTMLTokenizer::RecordTrailingContent(CStartToken* aStartToken, nsScanner& aScanner, nsReadingIterator<PRUnichar> aOrigin) {
void nsHTMLTokenizer::PreserveToken(CStartToken* aStartToken,
nsScanner& aScanner,
nsReadingIterator<PRUnichar> aOrigin) {
if(aStartToken) {
nsReadingIterator<PRUnichar> theCurrentPosition;
aScanner.CurrentPosition(theCurrentPosition);
nsString& trailingContent =aStartToken->mTrailingContent;
nsString& trailingContent = aStartToken->mTrailingContent;
PRUint32 oldLength = trailingContent.Length();
trailingContent.SetLength(oldLength + Distance(aOrigin, theCurrentPosition));

View File

@ -73,9 +73,9 @@ public:
NS_DECL_ISUPPORTS
NS_DECL_NSITOKENIZER
nsHTMLTokenizer(PRInt32 aParseMode=eDTDMode_quirks,
eParserDocType aDocType=eHTML3_Quirks,
eParserCommands aCommand=eViewNormal);
nsHTMLTokenizer(PRInt32 aParseMode = eDTDMode_quirks,
eParserDocType aDocType = eHTML3_Quirks,
eParserCommands aCommand = eViewNormal);
virtual ~nsHTMLTokenizer();
protected:
@ -92,24 +92,23 @@ protected:
virtual nsresult ConsumeSpecialMarkup(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeProcessingInstruction(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ScanDocStructure(PRBool aIsFinalChunk);
nsresult ScanDocStructure(PRBool aIsFinalChunk);
virtual void RecordTrailingContent(CStartToken* aStartToken, nsScanner& aScanner, nsReadingIterator<PRUnichar> aOrigin);
virtual void PreserveToken(CStartToken* aStartToken, nsScanner& aScanner, nsReadingIterator<PRUnichar> aOrigin);
static void AddToken(CToken*& aToken,nsresult aResult,nsDeque* aDeque,nsTokenAllocator* aTokenAllocator);
nsDeque mTokenDeque;
PRInt32 mFlags;
PRPackedBool mRecordTrailingContent;
PRPackedBool mIsFinalChunk;
nsTokenAllocator* mTokenAllocator;
PRInt32 mTokenScanPos;
PRUint32 mFlags;
eHTMLTags mPreserveTarget; // Tag whose content is preserved
};
extern nsresult NS_NewHTMLTokenizer( nsITokenizer** aInstancePtrResult,
PRInt32 aMode,
eParserDocType aDocType,
eParserCommands aCommand);
extern nsresult NS_NewHTMLTokenizer(nsITokenizer** aInstancePtrResult,
PRInt32 aMode,eParserDocType aDocType,
eParserCommands aCommand);
#endif

View File

@ -1281,17 +1281,22 @@ void nsParser::PushContext(CParserContext& aContext) {
* @update gess7/22/98
* @return prev. context
*/
CParserContext* nsParser::PopContext() {
CParserContext* oldContext=mParserContext;
if(oldContext) {
mParserContext=oldContext->mPrevContext;
// 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().
CParserContext* nsParser::PopContext()
{
CParserContext* oldContext = mParserContext;
if (oldContext) {
mParserContext = oldContext->mPrevContext;
if (mParserContext) {
if(mParserContext->mStreamListenerState!=eOnStop) {
// 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) {
mParserContext->mStreamListenerState = oldContext->mStreamListenerState;
}
// Preserve tokenizer state so that information is not lost
// between document.write. This fixes bug 99467
if (mParserContext->mTokenizer)
mParserContext->mTokenizer->CopyState(oldContext->mTokenizer);
}
}
return oldContext;