mScriptLanguageVersion);
mScriptLanguageVersion, PR_TRUE);
HTMLContentSink::OnStreamComplete(nsIStreamLoader* aLoader,                                  nsISupports* aContext,                                  nsresult aStatus,                                  PRUint32 stringLen,                                  const char* string){  nsresult rv = NS_OK;  if(mParser) {    // make sure to unblock the parser before evaluating the script,    // we must unblock the parser even if loading the script failed or    // if the script was empty, if we don't, the parser will never be    // unblocked.    mParser->UnblockParser();  }  if (stringLen) {    nsAutoString characterSet;    nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;    nsXPIDLCString contenttypeheader;    nsCOMPtr<nsIHttpChannel> httpChannel;    nsCOMPtr<nsIChannel> channel;    nsCOMPtr<nsIRequest> request;    rv = aLoader->GetRequest(getter_AddRefs(request));        NS_ASSERTION(request, "StreamLoader's request went away prematurely");    if (NS_FAILED(rv)) return rv;    channel = do_QueryInterface(request);    if (channel) {      httpChannel = do_QueryInterface(channel);      if (httpChannel) {        rv = httpChannel->GetResponseHeader("content-type",                                            getter_Copies(contenttypeheader));      }    }    if (NS_SUCCEEDED(rv)) {      nsAutoString contentType;      contentType.AssignWithConversion(contenttypeheader.get());      PRInt32 start = contentType.RFind("charset=", PR_TRUE ) ;      if(kNotFound != start) {        start += 8; // 8 = "charset=".length        PRInt32 end = contentType.FindCharInSet(";\n\r ", start  );        if(kNotFound == end ) end = contentType.Length();        contentType.Mid(characterSet, start, end - start);        NS_WITH_SERVICE(nsICharsetAlias, calias, kCharsetAliasCID, &rv);        if(NS_SUCCEEDED(rv) && calias) {          nsAutoString preferred;          rv = calias->GetPreferred(characterSet, preferred);          if(NS_SUCCEEDED(rv)) {            characterSet = preferred;          }        }      }    }    if (NS_FAILED(rv) || characterSet.IsEmpty()) {      //charset from script charset tag      characterSet.Assign(mScriptCharset);    }    if (NS_FAILED(rv) || characterSet.IsEmpty()) {      // charset from document default      rv = mDocument->GetDocumentCharacterSet(characterSet);    }    NS_ASSERTION(NS_SUCCEEDED(rv), "Could not get document charset!");    nsCOMPtr<nsICharsetConverterManager> charsetConv =      do_GetService(kCharsetConverterManagerCID, &rv);    if (NS_SUCCEEDED(rv) && charsetConv) {      rv = charsetConv->GetUnicodeDecoder(&characterSet,                                          getter_AddRefs(unicodeDecoder));    }    // converts from the charset to unicode    if (NS_SUCCEEDED(rv)) {      PRInt32 unicodeLength = 0;      rv = unicodeDecoder->GetMaxLength(string, stringLen, &unicodeLength);      if (NS_SUCCEEDED(rv)) {        mUnicodeXferBuf.SetCapacity(unicodeLength);        // XXX: Whaaaaa! const violation!!!        PRUnichar *ustr = (PRUnichar *) mUnicodeXferBuf.GetUnicode();        rv = unicodeDecoder->Convert(string, (PRInt32 *) &stringLen, ustr,                                     &unicodeLength);        if (NS_SUCCEEDED(rv)) {          mUnicodeXferBuf.SetLength(unicodeLength);        } else {          mUnicodeXferBuf.SetLength(0);        }      }    }    NS_ASSERTION(NS_SUCCEEDED(rv),                 "Could not convert external JavaScript to Unicode!");    if ((NS_OK == aStatus) && (NS_SUCCEEDED(rv))) {      PRBool bodyPresent = PreEvaluateScript();      //-- Merge the principal of the script file with that of the document      nsCOMPtr<nsISupports> owner;      channel->GetOwner(getter_AddRefs(owner));      nsCOMPtr<nsIPrincipal> prin;      if (owner) {        prin = do_QueryInterface(owner, &rv);        if (NS_FAILED(rv)) return rv;      }      rv = mDocument->AddPrincipal(prin);      if (NS_FAILED(rv))        return rv;      rv = EvaluateScript(mUnicodeXferBuf, mScriptURI, 1,                          mScriptLanguageVersion);      if (NS_FAILED(rv))        return rv;      PostEvaluateScript(bodyPresent);    }  }  if(mParser && mParser->IsParserEnabled()){    rv = mParser->ContinueParsing();  }  //invalidate Xfer buffer content  mUnicodeXferBuf.SetLength(0);   return rv;}