aParseMode = eDTDMode_unknown;  PRInt32 theIndex=aBuffer.Find("DOCTYPE",PR_TRUE,0,100); if(kNotFound<theIndex) {   PRInt32 theGTPos=aBuffer.FindChar(kGreaterThan,theIndex+1); PRInt32 theEnd=(kNotFound==theGTPos) ? 512 : MinInt(512,theGTPos); PRInt32 theSubIndex=aBuffer.Find(" PRInt32 theErr=0; PRInt32 theMajorVersion=3;    if(kNotFound!=theSubIndex) {    PRInt32 theStartPos=theSubIndex+5; PRInt32 theCount=theEnd-theStartPos;  if(kNotFound!=aBuffer.Find("ISO/IEC 15445:1999",PR_TRUE,theIndex,theEnd-theIndex)) {  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) {     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; } }    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)) {    aParseMode=eDTDMode_transitional;  } else {   theStartPos+=6; theCount=theEnd-theStartPos; theSubIndex=aBuffer.Find("STRICT.DTD",PR_TRUE,theStartPos,theCount); if(0<theSubIndex) {  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; }  } }  } } else if(kNotFound<(theIndex=aBuffer.Find("?XML",PR_TRUE,0,128))) {
aParseMode=eDTDMode_quirks; aDocType=eHTML3Text;     PRInt32 theIndex=aBuffer.Find("?XML",PR_TRUE,0,128); if(kNotFound!=theIndex) {
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;    }  }}