CWordTokenizer theTokenizer(aBuffer,theLTPos,theGTPos);
CWordTokenizer<PRUnichar> theTokenizer(theBuffer,theLTPos,theGTPos);
void DetermineParseMode(nsString& aBuffer,nsDTDMode& aParseMode,eParserDocType& aDocType,const nsString& aMimeType) {  const char* theModeStr= PR_GetEnv("PARSE_MODE");  aParseMode=eDTDMode_quirks;  aDocType=eHTML3Text;  nsDTDMode thePublicID=eDTDMode_quirks;  nsDTDMode theSystemID=eDTDMode_unknown;  PRBool theMimeTypeIsHTML=aMimeType.EqualsWithConversion(kHTMLTextContentType);    //let's eliminate non-HTML as quickly as possible...  PRInt32 theIndex=aBuffer.Find("?XML",PR_TRUE,0,128);        if(kNotFound!=theIndex) {    aParseMode=eDTDMode_strict;    if(theMimeTypeIsHTML) {      //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 {      if(!aMimeType.EqualsWithConversion(kPlainTextContentType)) {        aDocType=eXMLText;        aParseMode=eDTDMode_strict;        theSystemID=thePublicID=eDTDMode_strict;      }      else aDocType=ePlainText;    }  }  else if(aMimeType.EqualsWithConversion(kPlainTextContentType)) {    aDocType=ePlainText;    aParseMode=eDTDMode_quirks;    return;  }  else if(aMimeType.EqualsWithConversion(kTextCSSContentType)) {    aDocType=ePlainText;    aParseMode=eDTDMode_quirks;    return;  }  else if(aMimeType.EqualsWithConversion(kApplicationJSContentType)) {    aDocType=ePlainText;    aParseMode=eDTDMode_quirks;    return;  }  else if(aMimeType.EqualsWithConversion(kTextJSContentType)) {    aDocType=ePlainText;    aParseMode=eDTDMode_quirks;    return;  }  //now let's see if we have HTML or XHTML...  PRInt32 theOffset=0;  PRInt32 theDocTypePos=aBuffer.Find("!DOCTYPE",PR_TRUE,0,500); //find doctype  if(kNotFound!=theDocTypePos){    theOffset=theDocTypePos-2;  }  PRInt32 theLTPos=aBuffer.FindChar(kLessThan,PR_FALSE,theOffset);   PRInt32 theGTPos=aBuffer.FindChar(kGreaterThan,PR_FALSE,theOffset);  PRInt32 theMajorVersion=3;  PRInt32 theMinorVersion=0;  if((kNotFound!=theGTPos) && (kNotFound!=theLTPos)) {      const PRUnichar*  theBuffer=aBuffer.GetUnicode();    CWordTokenizer theTokenizer(aBuffer,theLTPos,theGTPos);    theOffset=theTokenizer.GetNextWord();  //try to find ?xml, !doctype, etc...    if((kNotFound!=theOffset) && (kNotFound!=theDocTypePos)) {            //Ok -- so assume it's (X)HTML; now figure out the flavor...              PRInt32   theIter=0;      //prevent infinite loops...      PRBool    done=PR_FALSE;  //use this to quit if we find garbage...      PRBool    readSystemID=PR_FALSE;      theOffset=theTokenizer.GetNextWord();      while((kNotFound!=theOffset) && (!done)) {          PRUnichar theChar=*(theBuffer+theTokenizer.mOffset);        if(kQuote==theChar) {          if(readSystemID) {            PRInt32 thePrefix=aBuffer.Find("http://www.w3.org/tr/",PR_TRUE,theOffset,5);  //find the prefix            if(kNotFound!=thePrefix) {              thePrefix+=20;              if(IsLoosePI(aBuffer,thePrefix,25)) { //find loose.dtd                theSystemID=eDTDMode_transitional;              }              else if(kNotFound!=aBuffer.Find("strict.dtd",PR_TRUE,thePrefix,25)) {  //find strict.dtd                theSystemID=eDTDMode_strict;              }            }          }                    else { //the public ID...            readSystemID=PR_TRUE;            PRInt32 theDTDPos=aBuffer.Find("//DTD",PR_TRUE,theOffset,theTokenizer.mLength);            if(theDTDPos) {                //first, let's see if it's XHML...              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 {                  //now check for strict ISO/IEC OWNER...                if(kNotFound!=aBuffer.Find("15445:1999",PR_FALSE,theOffset,theDTDPos-theTokenizer.mOffset)) {                  thePublicID=eDTDMode_strict;  //this ISO/IEC DTD is always strict.                  aDocType=eHTML4Text;                }                else {                    //for W3C DTD's, let's make sure it's HTML...                  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) {                    //and now check the version number...                    PRInt32 theVersionPos=aBuffer.FindCharInSet("1234567890",theMLTagPos);                    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,4);                      PRInt32 theErr=0;                      theMajorVersion=theNum.ToInteger(&theErr);                      if('.'==theNum[1]) {                        theNum.Cut(0,2);                        theMinorVersion=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;                      }                                   } //if                  } //if                } //else                                  } //else            }          } //if publicID        } //if quote                else if(kMinus==theChar) {          //explicitly skip comments...        }                else { //handle an id          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; //prevent infinite loops...      } //while      if(theSystemID==thePublicID)         aParseMode=thePublicID;      else if(eDTDMode_unknown==theSystemID){        aParseMode=thePublicID;        if(eHTML4Text==aDocType) {          if (eDTDMode_transitional==thePublicID)            aParseMode=eDTDMode_quirks;  //degrade because the systemID is missing.        }      }      else if((eDTDMode_transitional==thePublicID) && (eDTDMode_strict==theSystemID)) {        aParseMode=(4<=theMajorVersion) ? eDTDMode_strict : eDTDMode_quirks;              }      else {        //ack! The doctype is badly formed (system and public ID's contradict).        //let's switch back to default compatibility mode...          aParseMode=eDTDMode_unknown;      }    }   }  if(eDTDMode_unknown==aParseMode) {      //nothing left to do but fail gracefully...    if(eXHTMLText==aDocType) {      aParseMode=eDTDMode_transitional;    }    if(eHTML4Text==aDocType) {      aDocType=eHTML3Text;      aParseMode=eDTDMode_quirks;    }  }  if(eXHTMLText==aDocType) {    aParseMode=eDTDMode_strict;    if(theMimeTypeIsHTML){      aDocType=eHTML4Text;    }  }#ifdef  DISABLE_TRANSITIONAL_MODE  /********************************************************************************************      The following code is here because to deal with a nasty backward compatibility problem.       The composer product emits <doctype HTML 4.0 Transitional> for the documents it creates,       but the documents aren't really compliant. To prevent lots of pages from breaking, well       disable proper handling of Transitional doctypes and use quirks mode instead. If lucky,       we'll get to add a pref to allow power users to get the right answer.   ********************************************************************************************/  if(eDTDMode_transitional==aParseMode) {    if(eHTML4Text==aDocType)      aParseMode=(0==theMinorVersion) ? eDTDMode_quirks: eDTDMode_strict;//    else if(eXHTMLText==aDocType)//      aParseMode=eDTDMode_strict;  }#endif}