Bug 74728, take three. Properly handle stack-based tokens; specifically, require that only arena-allocated tokens may have attribute tokens. r=harishd, sr=jst

git-svn-id: svn://10.0.0.236/trunk@91933 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
waterson%netscape.com
2001-04-11 03:05:22 +00:00
parent f6fd365262
commit b3ab5ab02e
14 changed files with 274 additions and 194 deletions

View File

@@ -188,15 +188,12 @@ class CSharedVSContext {
public:
CSharedVSContext() :
mEndNode(),
mEndNode(),
mStartNode(),
mTokenNode(),
mErrorNode(),
mITextToken(),
mITextNode(&mITextToken),
mTextToken(),
mErrorToken(NS_ConvertASCIItoUCS2("error")),
mTextNode(&mTextToken){
mErrorToken(NS_ConvertASCIItoUCS2("error")) {
}
~CSharedVSContext() {
@@ -212,10 +209,7 @@ public:
nsCParserNode mTokenNode;
nsCParserNode mErrorNode;
CIndirectTextToken mITextToken;
nsCParserNode mITextNode;
CTextToken mTextToken;
CTextToken mErrorToken;
nsCParserNode mTextNode;
};
#ifdef VIEW_SOURCE_HTML
@@ -449,24 +443,6 @@ nsresult CViewSourceHTML::WillBuildModel( const CParserContext& aParserContext,
mErrorCount=0;
mTagCount=0;
#ifdef VIEW_SOURCE_HTML
nsAutoString tag;
tag.AssignWithConversion("HTML");
CStartToken htmlToken(tag, eHTMLTag_html);
nsCParserNode htmlNode(&htmlToken,0);
mSink->OpenHTML(htmlNode);
tag.AssignWithConversion("BODY");
CStartToken bodyToken(tag, eHTMLTag_body);
nsCParserNode bodyNode(&bodyToken,0);
mSink->OpenBody(bodyNode);
#else
CCommentToken ssToken(NS_LITERAL_STRING("<?xml version=\"1.0\"?>"));
nsCParserNode ssNode(&ssToken);
result= mSink->AddCharacterData(ssNode);
#endif // VIEW_SOURCE_HTML
#ifdef rickgdebug
(*gDumpFile) << theHeader << endl;
(*gDumpFile) << "<viewsource xmlns=\"viewsource\">" << endl;
@@ -503,30 +479,57 @@ NS_IMETHODIMP CViewSourceHTML::BuildModel(nsIParser* aParser,nsITokenizer* aToke
if(!mHasOpenRoot) {
#ifdef VIEW_SOURCE_HTML
// For the stack-allocated tokens below, it's safe to pass a null
// token allocator, because there are no attributes on the tokens.
nsAutoString tag;
tag.AssignWithConversion("PRE");
CStartToken theToken(tag, eHTMLTag_pre);
tag.AssignWithConversion("HTML");
CStartToken htmlToken(tag, eHTMLTag_html);
nsCParserNode htmlNode(&htmlToken,0,0/*stack token*/);
mSink->OpenHTML(htmlNode);
tag.AssignWithConversion("BODY");
CStartToken bodyToken(tag, eHTMLTag_body);
nsCParserNode bodyNode(&bodyToken,0,0/*stack token*/);
mSink->OpenBody(bodyNode);
#else
//now let's automatically open the root container...
CStartToken theToken(NS_LITERAL_STRING("viewsource"));
CCommentToken ssToken(NS_LITERAL_STRING("<?xml version=\"1.0\"?>"));
nsCParserNode ssNode(&ssToken,0,nsnull);
result= mSink->AddCharacterData(ssNode,0,mTokenizer->GetTokenAllocator());
#endif // VIEW_SOURCE_HTML
nsCParserNode theNode(&theToken,0);
CAttributeToken *theAttr=nsnull;
nsTokenAllocator* theAllocator=mTokenizer->GetTokenAllocator();
if(theAllocator) {
#ifdef VIEW_SOURCE_HTML
theAttr=(CAttributeToken*)theAllocator->CreateTokenOfType(eToken_attribute,eHTMLTag_unknown,NS_ConvertASCIItoUCS2(kPreStyle));
theAttr->SetKey(NS_LITERAL_STRING("style"));
tag.AssignWithConversion("PRE");
CStartToken* theToken=NS_STATIC_CAST(CStartToken*,theAllocator->CreateTokenOfType(eToken_start,eHTMLTag_pre,tag));
#else
theAttr=(CAttributeToken*)theAllocator->CreateTokenOfType(eToken_attribute,eHTMLTag_unknown,NS_LITERAL_STRING("http://www.mozilla.org/viewsource"));
theAttr->SetKey(NS_LITERAL_STRING("xmlns"));
//now let's automatically open the root container...
CStartToken* theToken=NS_STATIC_CAST(CStartToken*,theAllocator->CreateTokenOfType(eToken_start,eHTMLTag_unknown,NS_LITERAL_STRING("viewsource")));
#endif // VIEW_SOURCE_HTML
if(theToken) {
CAttributeToken *theAttr=nsnull;
nsCParserNode theNode(theToken,0,theAllocator);
#ifdef VIEW_SOURCE_HTML
theAttr=(CAttributeToken*)theAllocator->CreateTokenOfType(eToken_attribute,eHTMLTag_unknown,NS_ConvertASCIItoUCS2(kPreStyle));
theAttr->SetKey(NS_LITERAL_STRING("style"));
#else
theAttr=(CAttributeToken*)theAllocator->CreateTokenOfType(eToken_attribute,eHTMLTag_unknown,NS_LITERAL_STRING("http://www.mozilla.org/viewsource"));
theAttr->SetKey(NS_LITERAL_STRING("xmlns"));
#endif // VIEW_SOURCE_HTML
if(theAttr)
theNode.AddAttribute(theAttr);
result=mSink->OpenContainer(theNode);
if(NS_SUCCEEDED(result)) mHasOpenRoot=PR_TRUE;
}
IF_FREE(theToken,theAllocator);
}
if(theAttr)
theNode.AddAttribute(theAttr);
result=mSink->OpenContainer(theNode);
if(NS_SUCCEEDED(result)) mHasOpenRoot=PR_TRUE;
}
while(NS_SUCCEEDED(result)){
@@ -607,15 +610,15 @@ NS_IMETHODIMP CViewSourceHTML::DidBuildModel(nsresult anErrorCode,PRBool aNotify
if(ePlainText!=mDocType) {
#ifdef VIEW_SOURCE_HTML
CEndToken theToken(eHTMLTag_pre);
nsCParserNode preNode(&theToken,0);
nsCParserNode preNode(&theToken,0,0/*stack token*/);
mSink->CloseContainer(preNode);
CEndToken bodyToken(eHTMLTag_body);
nsCParserNode bodyNode(&bodyToken,0);
nsCParserNode bodyNode(&bodyToken,0,0/*stack token*/);
mSink->CloseBody(bodyNode);
CEndToken htmlToken(eHTMLTag_html);
nsCParserNode htmlNode(&htmlToken,0);
nsCParserNode htmlNode(&htmlToken,0,0/*stack token*/);
mSink->CloseHTML(htmlNode);
#else
//now let's automatically close the root container...
@@ -881,39 +884,43 @@ nsresult CViewSourceHTML::WriteTag(nsString &theXMLTagName,const nsAReadableStri
thePrefsService->GetBoolPref("view_source.syntax_highlight", &syntaxHighlight);
#endif // VIEW_SOURCE_COLORING
nsTokenAllocator* theAllocator=mTokenizer->GetTokenAllocator();
NS_ASSERTION(0!=theAllocator,"Error: no allocator");
if(0==theAllocator)
return NS_ERROR_FAILURE;
#ifdef VIEW_SOURCE_HTML
if (kBeforeText[aTagType][0] != 0) {
nsAutoString beforeText;
beforeText.AssignWithConversion(kBeforeText[aTagType]);
theContext.mITextToken.SetIndirectString(beforeText);
mSink->AddLeaf(theContext.mITextNode);
nsCParserNode theNode(&theContext.mITextToken,0,0/*stack token*/);
mSink->AddLeaf(theNode);
}
#ifdef VIEW_SOURCE_COLORING
nsAutoString tag (NS_LITERAL_STRING("SPAN"));
CStartToken theTagToken(tag, eHTMLTag_span);
CStartToken* theTagToken=NS_STATIC_CAST(CStartToken*,theAllocator->CreateTokenOfType(eToken_start,eHTMLTag_span,tag));
#endif // VIEW_SOURCE_COLORING
#else
CStartToken theTagToken(theXMLTagName);
CStartToken* theTagToken=NS_STATIC_CAST(CStartToken*,theAllocator->CreateTokenOfType(eToken_start,eHTMLTag_unknown,theXMLTagName));
#endif // VIEW_SOURCE_HTML
#ifdef VIEW_SOURCE_COLORING
if (syntaxHighlight)
{
theContext.mStartNode.Init(&theTagToken,mLineNumber);
theContext.mStartNode.Init(theTagToken,mLineNumber,theAllocator);
#ifdef VIEW_SOURCE_HTML
nsTokenAllocator* theAllocator=mTokenizer->GetTokenAllocator();
if(theAllocator) {
CAttributeToken* theAttr=(CAttributeToken*)theAllocator->CreateTokenOfType(eToken_attribute,eHTMLTag_unknown,NS_ConvertASCIItoUCS2(kElementStyles[aTagType]));
theAttr->SetKey(NS_LITERAL_STRING("style"));
theContext.mStartNode.AddAttribute(theAttr);
}
CAttributeToken* theAttr=(CAttributeToken*)theAllocator->CreateTokenOfType(eToken_attribute,eHTMLTag_unknown,NS_ConvertASCIItoUCS2(kElementStyles[aTagType]));
theAttr->SetKey(NS_LITERAL_STRING("style"));
theContext.mStartNode.AddAttribute(theAttr);
}
#endif // VIEW_SOURCE_HTML
STOP_TIMER();
mSink->OpenContainer(theContext.mStartNode); //emit <starttag>...
}
#endif // VIEW_SOURCE_COLORING
#ifdef rickgdebug
@@ -931,7 +938,8 @@ nsresult CViewSourceHTML::WriteTag(nsString &theXMLTagName,const nsAReadableStri
theContext.mITextToken.SetIndirectString(aText); //now emit the tag name...
mSink->AddLeaf(theContext.mITextNode);
nsCParserNode theNode(&theContext.mITextToken,0,0/*stack token*/);
mSink->AddLeaf(theNode);
if(attrCount){
result=WriteAttributes(attrCount);
@@ -943,7 +951,7 @@ nsresult CViewSourceHTML::WriteTag(nsString &theXMLTagName,const nsAReadableStri
{
theContext.mStartNode.ReleaseAll();
CEndToken theEndToken(eHTMLTag_span);
theContext.mEndNode.Init(&theEndToken,mLineNumber);
theContext.mEndNode.Init(&theEndToken,mLineNumber,0/*stack token*/);
mSink->CloseContainer(theContext.mEndNode); //emit </starttag>...
}
#endif // VIEW_SOURCE_COLORING
@@ -951,10 +959,11 @@ nsresult CViewSourceHTML::WriteTag(nsString &theXMLTagName,const nsAReadableStri
nsAutoString afterText;
afterText.AssignWithConversion(kAfterText[aTagType]);
theContext.mITextToken.SetIndirectString(afterText);
mSink->AddLeaf(theContext.mITextNode);
nsCParserNode theNode(&theContext.mITextToken,0,0/*stack token*/);
mSink->AddLeaf(theNode);
}
#else
theContext.mEndNode.Init(&theTagToken,mLineNumber);
theContext.mEndNode.Init(&theTagToken,mLineNumber,0/*stack token*/);
mSink->CloseContainer(theContext.mEndNode); //emit </starttag>...
#endif // VIEW_SOURCE_HTML
@@ -990,7 +999,7 @@ nsresult CViewSourceHTML::WriteTagWithError(nsString &theXMLTagName,const nsARea
//first write the error tag itself...
theContext.mErrorNode.Init(&theContext.mErrorToken,mLineNumber);
theContext.mErrorNode.Init(&theContext.mErrorToken,mLineNumber,0/*stack token*/);
result=mSink->OpenContainer(theContext.mErrorNode); //emit <error>...
}
@@ -1005,7 +1014,7 @@ nsresult CViewSourceHTML::WriteTagWithError(nsString &theXMLTagName,const nsARea
//now close the error tag...
STOP_TIMER();
theContext.mErrorNode.Init(&theContext.mErrorToken,mLineNumber);
theContext.mErrorNode.Init(&theContext.mErrorToken,0,0/*stack token*/);
mSink->CloseContainer(theContext.mErrorNode);
START_TIMER();
}
@@ -1057,7 +1066,7 @@ NS_IMETHODIMP CViewSourceHTML::HandleToken(CToken* aToken,nsIParser* aParser) {
#endif // VIEW_SOURCE_HTML
CSharedVSContext& theContext=CSharedVSContext::GetSharedContext();
theContext.mTokenNode.Init(theToken,mLineNumber);
theContext.mTokenNode.Init(theToken,mLineNumber,mTokenizer->GetTokenAllocator());
eHTMLTags theParent=(mTags.Length()) ? (eHTMLTags)mTags.Last() : eHTMLTag_unknown;
eHTMLTags theChild=(eHTMLTags)aToken->GetTypeID();