}  if(theModeStr) { if(0==nsCRT::strcasecmp(theModeStr,"strict")) aParseMode=eDTDMode_strict; } else { if(eDTDMode_unknown==aParseMode) {
return; }     PRInt32 theGTPos=aBuffer.FindChar(kGreaterThan);  if(kNotFound!=theGTPos) {  const PRUnichar*  theBuffer=aBuffer.GetUnicode(); CWordTokenizer theTokenizer(aBuffer,1,theGTPos); PRInt32 theOffset=theTokenizer.GetNextWord();  if((kNotFound!=theOffset) && (0==nsCRT::strncasecmp(theBuffer+theOffset,"DOCTYPE",theTokenizer.mLength))) {    PRInt32   theIter=0; PRBool    done=PR_FALSE; PRBool    readSystemID=PR_FALSE; nsDTDMode thePublicID=eDTDMode_quirks; nsDTDMode theSystemID=eDTDMode_unknown;  theOffset=theTokenizer.GetNextWord();  while((kNotFound!=theOffset) && (!done)) {  PRUnichar theChar=*(theBuffer+theTokenizer.mOffset); if(kQuote==theChar) {  if(readSystemID) {  PRInt32 thePrefix=aBuffer.Find("http:  if(kNotFound!=thePrefix) { thePrefix+=20; if(IsLoosePI(aBuffer,thePrefix,25)) { theSystemID=eDTDMode_transitional; } else if(kNotFound!=aBuffer.Find("strict.dtd",PR_TRUE,thePrefix,25)) { theSystemID=eDTDMode_strict; } }  }  else {  readSystemID=PR_TRUE;  PRInt32 theDTDPos=aBuffer.Find(" if(theDTDPos) {   PRInt32 theMLTagPos=aBuffer.Find("XHTML",PR_TRUE,theOffset,theTokenizer.mLength); if(kNotFound!=theMLTagPos) { aDocType=eXHTMLText; if(IsLoosePI(aBuffer,theMLTagPos+4,20)) thePublicID=eDTDMode_transitional; else thePublicID=eDTDMode_strict; }  else {   if(kNotFound!=aBuffer.Find("15445:1999",PR_FALSE,theOffset,theDTDPos-theTokenizer.mOffset)) { thePublicID=eDTDMode_strict; aDocType=eHTML4Text; }  else {   PRInt32 theMLTagPos=aBuffer.Find("HTML",PR_TRUE,theOffset,theTokenizer.mLength); if(kNotFound==theMLTagPos) { theMLTagPos=aBuffer.Find("HYPERTEXT MARKUP",PR_TRUE,theOffset,theTokenizer.mLength); }  if(kNotFound!=theMLTagPos) {   PRInt32 theVersionPos=aBuffer.FindCharInSet("1234567890",theMLTagPos); PRInt32 theMajorVersion=3;  if((0<=theVersionPos) && (theVersionPos<theGTPos)) { nsAutoString theNum; PRInt32 theTerminal=aBuffer.FindCharInSet(" />",theVersionPos+1); if(theTerminal) { aBuffer.Mid(theNum,theVersionPos,theTerminal-theVersionPos); } else aBuffer.Mid(theNum,theVersionPos,3); PRInt32 theErr=0; theMajorVersion=theNum.ToInteger(&theErr);  if((0==theErr) && (3<theMajorVersion) && (theMajorVersion<100)) { if(IsLoosePI(aBuffer,theVersionPos+2,20)) thePublicID=eDTDMode_transitional; else thePublicID=eDTDMode_strict; aDocType=eHTML4Text; } } } }  } }  }  }  else if(kMinus==theChar) {  }  else { if(0==nsCRT::strncasecmp(theBuffer+theOffset,"SYSTEM",theTokenizer.mLength)) readSystemID=PR_TRUE; else if(0==nsCRT::strncasecmp(theBuffer+theOffset,"HTML",theTokenizer.mLength)) readSystemID=PR_FALSE; }  theOffset=theTokenizer.GetNextWord(); if(++theIter>10) done=PR_TRUE; }   if(theSystemID==thePublicID) aParseMode=thePublicID; else if(eDTDMode_unknown==theSystemID){ aParseMode=thePublicID; if(eHTML4Text==aDocType) { if (eDTDMode_transitional==thePublicID) aParseMode=eDTDMode_quirks; } } else {   aParseMode=eDTDMode_unknown; } } }  if(eDTDMode_unknown==aParseMode) {  if(eXHTMLText==aDocType) { aParseMode=eDTDMode_transitional; } if(eHTML4Text==aDocType) {
void DetermineParseMode(nsString& aBuffer,nsDTDMode& aParseMode,eParserDocType& aDocType,const nsString& aMimeType) {  const char* theModeStr= PR_GetEnv("PARSE_MODE");  aParseMode = eDTDMode_unknown;      PRInt32 theIndex=aBuffer.Find("DOCTYPE",PR_TRUE,0,100);  if(kNotFound<theIndex) {      //good, we found "DOCTYPE" -- now go find it's end delimiter '>'    PRInt32 theGTPos=aBuffer.FindChar(kGreaterThan,theIndex+1);    PRInt32 theEnd=(kNotFound==theGTPos) ? 512 : MinInt(512,theGTPos);    PRInt32 theSubIndex=aBuffer.Find("//DTD",PR_TRUE,theIndex+8,theEnd-(theIndex+8));  //skip to the type and desc-text...    PRInt32 theErr=0;    PRInt32 theMajorVersion=3;    //note that if we don't find '>', then we just scan the first 512 bytes.    if(kNotFound!=theSubIndex) {        //if you're here then we found the //DTD identifier type...      PRInt32 theStartPos=theSubIndex+5;      PRInt32 theCount=theEnd-theStartPos;      if(kNotFound!=aBuffer.Find("ISO/IEC 15445:1999",PR_TRUE,theIndex,theEnd-theIndex)) {          //per spec, this DTD is always strict...        aParseMode=eDTDMode_strict;        aDocType=eHTML4Text;        theMajorVersion=4;        return;      }            if (kNotFound!=aBuffer.Find("XHTML",PR_TRUE,theStartPos,theCount)) {        aDocType=eXHTMLText;        aParseMode=eDTDMode_strict;        return;      }      if(kNotFound<theSubIndex) {        PRInt32 theHTMLTagPos=aBuffer.Find("HTML",PR_TRUE,theStartPos,theCount);          if(kNotFound==theHTMLTagPos) {          theHTMLTagPos=aBuffer.Find("HYPERTEXT MARKUP",PR_TRUE,theStartPos,theCount);          }        if(kNotFound!=theHTMLTagPos) {            //get the next substring from the buffer, which should be a number.            //now see what the version number is...          PRInt32 theVersionPos=aBuffer.FindCharInSet("1234567890",theHTMLTagPos);          if((0<=theVersionPos) && (theVersionPos<theEnd)) {            nsAutoString theNum;            PRInt32 theTerminal=aBuffer.FindCharInSet(" />",theVersionPos+1);            if(theTerminal) {              aBuffer.Mid(theNum,theVersionPos,theTerminal-theVersionPos);            }            else aBuffer.Mid(theNum,theVersionPos,3);            theMajorVersion=theNum.ToInteger(&theErr);            if(theMajorVersion>10) {              theMajorVersion=3; //assume that's an error for now.            }          }            //now let's see if we have descriptive text (providing strictness id)...          theStartPos=theVersionPos+2;          theCount=theEnd-theStartPos;                        if((aBuffer.Find("TRANSITIONAL",PR_TRUE,theVersionPos+2,theCount)>kNotFound)||             (aBuffer.Find("LOOSE",PR_TRUE,theStartPos,theCount)>kNotFound)       ||             (aBuffer.Find("FRAMESET",PR_TRUE,theStartPos,theCount)>kNotFound)    ||             (aBuffer.Find("LATIN1", PR_TRUE,theStartPos,theCount) >kNotFound)    ||             (aBuffer.Find("SYMBOLS",PR_TRUE,theStartPos,theCount) >kNotFound)    ||             (aBuffer.Find("SPECIAL",PR_TRUE,theStartPos,theCount) >kNotFound)) {              //for now TRANSITIONAL 4.0 documents are handled in the compatibility DTD.              //Soon, they'll be handled in the strictDTD in transitional mode.            aParseMode=eDTDMode_transitional;          }          else {            //since we didn't find descriptive text, let's check out the URI...            //to see whether that specifies the strict.dtd...            theStartPos+=6;            theCount=theEnd-theStartPos;            theSubIndex=aBuffer.Find("STRICT.DTD",PR_TRUE,theStartPos,theCount);            if(0<theSubIndex) {              //Since we found it, regardless of what's in the descr-text, kick into strict mode.              aParseMode=eDTDMode_strict;              aDocType=eHTML4Text;            }          }          switch(theMajorVersion) {            case 1:            case 2:            case 3:              aParseMode=eDTDMode_quirks;              aDocType=eHTML4Text;              break;            default:              if(eDTDMode_unknown==aParseMode) {                aParseMode=eDTDMode_strict;              }              break;          }        }      }     } //if  }  else if(kNotFound<(theIndex=aBuffer.Find("?XML",PR_TRUE,0,128))) {    aParseMode=eDTDMode_strict;    if(aMimeType.EqualsWithConversion(kHTMLTextContentType)) {      //this is here to prevent a crash if someone gives us an XML document,      //but necko tells us it's a text/html mimetype.       aDocType=eHTML4Text;      aParseMode=eDTDMode_strict;    }    else aDocType=eXMLText;  }  else if(aMimeType.EqualsWithConversion(kPlainTextContentType)) {    aDocType=ePlainText;    aParseMode=eDTDMode_quirks;  }  else if(aMimeType.EqualsWithConversion(kRTFTextContentType)) {    aDocType=ePlainText;    aParseMode=eDTDMode_quirks;  }  if(theModeStr) {    if(0==nsCRT::strcasecmp(theModeStr,"strict"))      aParseMode=eDTDMode_strict;      }  else {    if(eDTDMode_unknown==aParseMode) {      aDocType=eHTML3Text;      aParseMode=eDTDMode_quirks;    }  }}