if(aMimeType.EqualsWithConversion(kHTMLTextContentType)) {   aDocType=eHTML4Text; aParseMode=eDTDMode_strict; } else aDocType=eXMLText; } else if(aMimeType.EqualsWithConversion(kPlainTextContentType)) { aDocType=ePlainText; aParseMode=eDTDMode_quirks;
void DetermineParseMode(nsString& aBuffer,nsDTDMode& aParseMode,eParserDocType& aDocType) {  const char* theModeStr= PR_GetEnv("PARSE_MODE");  aParseMode = eDTDMode_unknown;      PRInt32 theIndex=aBuffer.Find("DOCTYPE",PR_TRUE,0,10);  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(0<=theSubIndex) {      PRInt32 theStartPos=theSubIndex+5;      PRInt32 theCount=theEnd-theStartPos;      if(kNotFound<theSubIndex) {        theSubIndex=aBuffer.Find("XHTML",PR_TRUE,theStartPos,theCount);        if(0<=theSubIndex) {          aDocType=eXHTMLText;          aParseMode=eDTDMode_strict;          return;        }        else {          theSubIndex=aBuffer.Find("ISO/IEC 15445:",PR_TRUE,theIndex+8,theEnd-(theIndex+8));          if(0<=theSubIndex) {            aDocType=eHTML4Text;            aParseMode=eDTDMode_strict;            theMajorVersion=4;            theSubIndex+=15;            return;          }          else {            theSubIndex=aBuffer.Find("HTML",PR_TRUE,theStartPos,theCount);            if(0<=theSubIndex) {              aDocType=eHTML4Text;              aParseMode=eDTDMode_strict;              theMajorVersion=3;            }            else {              theSubIndex=aBuffer.Find("HYPERTEXT MARKUP",PR_TRUE,theStartPos,theCount);              if(0<=theSubIndex) {                aDocType=eHTML3Text;                aParseMode=eDTDMode_quirks;                theSubIndex+=20;              }            }           }        }      }      theStartPos=theSubIndex+5;      theCount=theEnd-theStartPos;      nsAutoString theNum;        //get the next substring from the buffer, which should be a number.        //now see what the version number is...      theStartPos=aBuffer.FindCharInSet("123456789",theStartPos);      if(0<=theStartPos) {        PRInt32 theTerminal=aBuffer.FindCharInSet(" />",theStartPos+1);        if(theTerminal) {          aBuffer.Mid(theNum,theStartPos,theTerminal-theStartPos);        }        else aBuffer.Mid(theNum,theStartPos,3);        theMajorVersion=theNum.ToInteger(&theErr);      }      //now see what the      theStartPos+=theNum.Length();      theCount=theEnd-theStartPos;      if((aBuffer.Find("TRANSITIONAL",PR_TRUE,theStartPos,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_quirks;      }      //one last thing: look for a URI 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;      }      if (0==theErr){        switch(theMajorVersion) {          case 0: case 1: case 2: case 3:            if(aDocType!=eXHTMLText){              aParseMode=eDTDMode_quirks; //be as backward compatible as possible              aDocType=eHTML3Text;            }            break;            default:              //XXX hack -- someday, the next line of code will be criticized               //for it's lack of vision...            if(theMajorVersion>20) {              aParseMode=eDTDMode_strict;            }            break;        } //switch      }     } //if    else {      PRInt32 thePos=aBuffer.Find("HTML",PR_TRUE,1,50);      if(kNotFound!=thePos) {        aDocType=eHTML4Text;        PRInt32 theIDPos=aBuffer.Find("PublicID",thePos);        if(kNotFound==theIDPos)          theIDPos=aBuffer.Find("SystemID",thePos);        aParseMode=(kNotFound==theIDPos) ? eDTDMode_quirks : eDTDMode_strict;      }    }  }  else if(kNotFound<(theIndex=aBuffer.Find("?XML",PR_TRUE,0,128))) {    aDocType=eXMLText;    aParseMode=eDTDMode_strict;  }  if(theModeStr) {    if(0==nsCRT::strcasecmp(theModeStr,"strict"))      aParseMode=eDTDMode_strict;      }  else {    if(eDTDMode_unknown==aParseMode) {      aBuffer.InsertWithConversion("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n",0);      aDocType=eHTML3Text;      aParseMode=eDTDMode_quirks;    }  }}