diff --git a/mozilla/content/html/document/src/nsHTMLContentSink.cpp b/mozilla/content/html/document/src/nsHTMLContentSink.cpp
index 8a870badda6..19be0562577 100644
--- a/mozilla/content/html/document/src/nsHTMLContentSink.cpp
+++ b/mozilla/content/html/document/src/nsHTMLContentSink.cpp
@@ -3921,7 +3921,7 @@ HTMLContentSink::StartLayout()
}
// Convert the ref from document charset to unicode.
-static nsresult CharsetConvRef(const nsString& aDocCharset, const nsCString& aRefInDocCharset, nsString& aRefInUnicode)
+nsresult CharsetConvRef(const nsString& aDocCharset, const nsCString& aRefInDocCharset, nsString& aRefInUnicode)
{
nsresult rv;
@@ -3962,6 +3962,8 @@ static nsresult CharsetConvRef(const nsString& aDocCharset, const nsCString& aRe
void
HTMLContentSink::ScrollToRef()
{
+ // XXX Duplicate code in nsXMLContentSink.
+ // XXX Be sure to change both places if you make changes here.
if (!mRef.IsEmpty()) {
char* tmpstr = mRef.ToNewCString();
if(! tmpstr)
diff --git a/mozilla/content/xml/document/src/nsXMLContentSink.cpp b/mozilla/content/xml/document/src/nsXMLContentSink.cpp
index 91cea3f2f72..8e94b875e30 100644
--- a/mozilla/content/xml/document/src/nsXMLContentSink.cpp
+++ b/mozilla/content/xml/document/src/nsXMLContentSink.cpp
@@ -79,6 +79,9 @@
#include "nsIWebNavigation.h"
#include "nsIScriptElement.h"
#include "nsStyleLinkElement.h"
+#include "nsEscape.h"
+#include "nsICharsetConverterManager.h"
+#include "nsICharsetConverterManager2.h"
// XXX misnamed header file, but oh well
#include "nsHTMLTokens.h"
@@ -246,19 +249,56 @@ nsXMLContentSink::WillBuildModel(void)
return NS_OK;
}
+// This function's implementation is in nsHTMLContentSink.cpp
+nsresult CharsetConvRef(const nsString& aDocCharset, const nsCString& aRefInDocCharset, nsString& aRefInUnicode);
+
void
nsXMLContentSink::ScrollToRef()
{
- if (mRef.Length() > 0)
- {
+ // XXX Duplicate code in nsHTMLContentSink.
+ // XXX Be sure to change both places if you make changes here.
+ if (!mRef.IsEmpty()) {
+ char* tmpstr = mRef.ToNewCString();
+ if(! tmpstr)
+ return;
+ nsUnescape(tmpstr);
+ nsCAutoString unescapedRef;
+ unescapedRef.Assign(tmpstr);
+ nsMemory::Free(tmpstr);
+
+ nsresult rv = NS_ERROR_FAILURE;
+ // We assume that the bytes are in UTF-8, as it says in the spec:
+ // http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.1
+ nsAutoString ref = NS_ConvertUTF8toUCS2(unescapedRef);
+
PRInt32 i, ns = mDocument->GetNumberOfShells();
for (i = 0; i < ns; i++) {
nsCOMPtr shell;
mDocument->GetShellAt(i, getter_AddRefs(shell));
if (shell) {
- shell->FlushPendingNotifications();
// Scroll to the anchor
- shell->GoToAnchor(mRef);
+ shell->FlushPendingNotifications();
+
+ // Check an empty string which might be caused by the UTF-8 conversion
+ if (!ref.IsEmpty())
+ rv = shell->GoToAnchor(ref);
+ else
+ rv = NS_ERROR_FAILURE;
+
+ // If UTF-8 URL failed then try to assume the string as a
+ // document's charset.
+
+ if (NS_FAILED(rv)) {
+ nsAutoString docCharset;
+ rv = mDocument->GetDocumentCharacterSet(docCharset);
+
+ if (NS_SUCCEEDED(rv)) {
+ rv = CharsetConvRef(docCharset, unescapedRef, ref);
+
+ if (NS_SUCCEEDED(rv) && !ref.IsEmpty())
+ rv = shell->GoToAnchor(ref);
+ }
+ }
}
}
}