PRBool    theOutputReady=PR_FALSE;
nsresult WriteText(const nsString& aTextString,nsIContentSink& aSink,PRBool aPreserveSpace,PRBool aPlainText,CSharedVSContext& aContext) {  nsresult result=NS_OK;  nsString&  temp=aContext.mTextToken.GetStringValueXXX();  temp.Truncate();  PRInt32   theEnd=aTextString.Length();  PRInt32   theTextOffset=0;  PRInt32   theOffset=-1; //aTextString.FindCharInSet("\t\n\r ",theStartOffset);  PRInt32   theSpaces=0;  PRBool    theOutputReady=PR_FALSE;  PRUnichar theChar=0;  PRUnichar theNextChar=0;  while(++theOffset<theEnd){    theNextChar=aTextString[theOffset-1];    theChar=aTextString[theOffset];    switch(theChar){      case kSpace:        theNextChar=aTextString[theOffset+1];        if((!aPlainText) && aPreserveSpace) { //&& ((kSpace==theNextChar) || (0==theNextChar)))          if(theTextOffset<theOffset) {            if(kSpace==theNextChar) {              aTextString.Mid(temp,theTextOffset,theOffset-theTextOffset);              theTextOffset=theOffset--;            }          }          else{            while((theOffset<theEnd) && (kSpace==aTextString[theOffset])){              theOffset++;              theSpaces++;            }            theTextOffset=theOffset--;          }        }        break;      case kTab:        if((!aPlainText) && aPreserveSpace){          if(theTextOffset<theOffset) {            aTextString.Mid(temp,theTextOffset,theOffset-theTextOffset);          }          theSpaces+=8;          theOffset++;          theTextOffset=theOffset;        }        break;      case kCR:      case kLF:        if(theTextOffset<theOffset) {          aTextString.Mid(temp,theTextOffset,theOffset-theTextOffset);          theTextOffset=theOffset--;  //back up one on purpose...        }        else {          theNextChar=aTextString.CharAt(theOffset+1);          if((kCR==theChar) && (kLF==theNextChar)) {            theOffset++;          }          WriteNewline(aSink,aContext);          theTextOffset=theOffset+1;        }        break;      default:        break;    } //switch    if(0<temp.Length()){      result=aSink.AddLeaf(aContext.mTextNode);      temp.Truncate();    }    if(0<theSpaces){      WriteNBSP(theSpaces,aSink,aContext);       theSpaces=0;    }  }  if(theTextOffset<theEnd){    temp.Truncate();    aTextString.Mid(temp,theTextOffset,theEnd-theTextOffset);    result=aSink.AddLeaf(aContext.mTextNode); //just dump the whole string...  }  return result;}