Skip to content

Commit

Permalink
bug 263083: view-source now reports tokenization-level errors in HTML…
Browse files Browse the repository at this point in the history
… and XML. r=rbs sr=dmose
  • Loading branch information
mrbkap%gmail.com committed Oct 24, 2004
1 parent baf6af1 commit 27b107f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
60 changes: 48 additions & 12 deletions parser/htmlparser/src/nsViewSourceHTML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ class CIndirectTextToken : public CTextToken {
class CSharedVSContext {
public:

CSharedVSContext() :
mErrorToken(NS_LITERAL_STRING("error")) {
CSharedVSContext() {
}

~CSharedVSContext() {
Expand All @@ -216,7 +215,8 @@ class CSharedVSContext {
nsCParserStartNode mStartNode;
nsCParserStartNode mTokenNode;
CIndirectTextToken mITextToken;
CTextToken mErrorToken;
nsCParserStartNode mErrorNode;
nsCParserNode mEndErrorNode;
};

enum {
Expand Down Expand Up @@ -937,7 +937,7 @@ PRBool CViewSourceHTML::IsContainer(PRInt32 aTag) const{
* @param
* @return result status
*/
nsresult CViewSourceHTML::WriteAttributes(PRInt32 attrCount) {
nsresult CViewSourceHTML::WriteAttributes(PRInt32 attrCount, PRBool aOwnerInError) {
nsresult result=NS_OK;

if(attrCount){ //go collect the attributes...
Expand All @@ -955,12 +955,16 @@ nsresult CViewSourceHTML::WriteAttributes(PRInt32 attrCount) {

CAttributeToken* theAttrToken = (CAttributeToken*)theToken;
const nsAString& theKey = theAttrToken->GetKey();

// The attribute is only in error if its owner is NOT in error.
const PRBool attributeInError =
!aOwnerInError && theAttrToken->IsInError();

result = WriteTag(mKey,theKey,0,PR_FALSE);
result = WriteTag(mKey,theKey,0,attributeInError);
const nsAString& theValue = theAttrToken->GetValue();

if(!theValue.IsEmpty() || theAttrToken->mHasEqualWithoutValue){
result = WriteTag(mValue,theValue,0,PR_FALSE);
result = WriteTag(mValue,theValue,0,attributeInError);
}
}
}
Expand Down Expand Up @@ -996,8 +1000,27 @@ nsresult CViewSourceHTML::WriteTag(PRInt32 aTagType,const nsAString & aText,PRIn
if(0==theAllocator)
return NS_ERROR_FAILURE;

// Highlight all parts of all erroneous tags.
if (mSyntaxHighlight && aTagInError) {
CStartToken* theTagToken=
NS_STATIC_CAST(CStartToken*,
theAllocator->CreateTokenOfType(eToken_start,
eHTMLTag_span,
NS_LITERAL_STRING("SPAN")));
theContext.mErrorNode.Init(theTagToken, theAllocator);
AddAttrToNode(theContext.mErrorNode, theAllocator,
NS_LITERAL_STRING("class"),
NS_LITERAL_STRING("error"));
mSink->OpenContainer(theContext.mErrorNode);
#ifdef DUMP_TO_FILE
if (gDumpFile) {
fprintf(gDumpFile, "<span class=\"error\">");
}
#endif
}

if (kBeforeText[aTagType][0] != 0) {
NS_ConvertASCIItoUTF16 beforeText(kBeforeText[aTagType]);
NS_ConvertASCIItoUTF16 beforeText(kBeforeText[aTagType]);
theContext.mITextToken.SetIndirectString(beforeText);
nsCParserNode theNode(&theContext.mITextToken, 0/*stack token*/);
mSink->AddLeaf(theNode);
Expand All @@ -1008,8 +1031,11 @@ nsresult CViewSourceHTML::WriteTag(PRInt32 aTagType,const nsAString & aText,PRIn
#endif // DUMP_TO_FILE

if (mSyntaxHighlight && aTagType != mText) {
CStartToken* theTagToken=NS_STATIC_CAST(CStartToken*,theAllocator->CreateTokenOfType(eToken_start,eHTMLTag_span,NS_LITERAL_STRING("SPAN")));

CStartToken* theTagToken=
NS_STATIC_CAST(CStartToken*,
theAllocator->CreateTokenOfType(eToken_start,
eHTMLTag_span,
NS_LITERAL_STRING("SPAN")));
theContext.mStartNode.Init(theTagToken, theAllocator);
AddAttrToNode(theContext.mStartNode, theAllocator,
NS_LITERAL_STRING("class"),
Expand Down Expand Up @@ -1040,21 +1066,21 @@ nsresult CViewSourceHTML::WriteTag(PRInt32 aTagType,const nsAString & aText,PRIn
theContext.mStartNode.ReleaseAll();
CEndToken theEndToken(eHTMLTag_span);
theContext.mEndNode.Init(&theEndToken, 0/*stack token*/);
mSink->CloseContainer(eHTMLTag_span); //emit </starttag>...
mSink->CloseContainer(eHTMLTag_span); //emit </endtag>...
#ifdef DUMP_TO_FILE
if (gDumpFile)
fprintf(gDumpFile, "</span>");
#endif //DUMP_TO_FILE
}

if(attrCount){
result=WriteAttributes(attrCount);
result=WriteAttributes(attrCount, aTagInError);
}

// Tokens are set in error if their ending > is not there, so don't output
// the after-text
if (!aTagInError && kAfterText[aTagType][0] != 0) {
NS_ConvertASCIItoUTF16 afterText(kAfterText[aTagType]);
NS_ConvertASCIItoUTF16 afterText(kAfterText[aTagType]);
theContext.mITextToken.SetIndirectString(afterText);
nsCParserNode theNode(&theContext.mITextToken, 0/*stack token*/);
mSink->AddLeaf(theNode);
Expand All @@ -1064,6 +1090,16 @@ nsresult CViewSourceHTML::WriteTag(PRInt32 aTagType,const nsAString & aText,PRIn
fprintf(gDumpFile, kDumpFileAfterText[aTagType]);
#endif // DUMP_TO_FILE

if (mSyntaxHighlight && aTagInError) {
theContext.mErrorNode.ReleaseAll();
CEndToken theEndToken(eHTMLTag_span);
theContext.mEndErrorNode.Init(&theEndToken, 0/*stack token*/);
mSink->CloseContainer(eHTMLTag_span); //emit </endtag>...
#ifdef DUMP_TO_FILE
if (gDumpFile)
fprintf(gDumpFile, "</span>");
#endif //DUMP_TO_FILE
}

START_TIMER();

Expand Down
2 changes: 1 addition & 1 deletion parser/htmlparser/src/nsViewSourceHTML.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CViewSourceHTML: public nsIDTD
PRInt32 attrCount,
PRBool aTagInError);

nsresult WriteAttributes(PRInt32 attrCount);
nsresult WriteAttributes(PRInt32 attrCount, PRBool aOwnerInError);
nsresult GenerateSummary();
void StartNewPreBlock(void);
// Utility method for adding attributes to the nodes we generate
Expand Down

0 comments on commit 27b107f

Please sign in to comment.