From 0d1df09d126cc8a00dd331d17ae43f067449ae3a Mon Sep 17 00:00:00 2001 From: "bryner%netscape.com" Date: Fri, 23 Aug 2002 22:57:13 +0000 Subject: [PATCH] bug 162239 - POST document could not inherit charset from previous page if the previous charset is from autodetection. Add PrevDocCharset field and use it for POST document charset resolution. Patch by shanjian, r=ftang, darin, sr=jst. git-svn-id: svn://10.0.0.236/branches/CHIMERA_M1_0_1_BRANCH@128001 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/src/nsDocumentViewer.cpp | 26 ++++++++++++++ .../html/document/src/nsHTMLContentSink.cpp | 35 +++++++++++++++++++ .../html/document/src/nsHTMLDocument.cpp | 33 +++++++++++------ mozilla/docshell/base/nsDocShell.cpp | 6 ++++ .../docshell/base/nsIMarkupDocumentViewer.idl | 5 +++ mozilla/htmlparser/public/nsIParser.h | 15 ++++---- .../intl/chardet/src/nsDetectionAdaptor.cpp | 5 +++ mozilla/parser/htmlparser/public/nsIParser.h | 15 ++++---- 8 files changed, 116 insertions(+), 24 deletions(-) diff --git a/mozilla/content/base/src/nsDocumentViewer.cpp b/mozilla/content/base/src/nsDocumentViewer.cpp index 79f6517959b..d8703afcb31 100644 --- a/mozilla/content/base/src/nsDocumentViewer.cpp +++ b/mozilla/content/base/src/nsDocumentViewer.cpp @@ -726,6 +726,7 @@ protected: nsString mHintCharset; PRInt32 mHintCharsetSource; nsString mForceCharacterSet; + nsString mPrevDocCharacterSet; }; //--------------------------------------------------- @@ -7184,6 +7185,31 @@ NS_IMETHODIMP DocumentViewerImpl::GetHintCharacterSetSource(PRInt32 *aHintCharac return NS_OK; } + +NS_IMETHODIMP DocumentViewerImpl::GetPrevDocCharacterSet(PRUnichar * *aPrevDocCharacterSet) +{ + NS_ENSURE_ARG_POINTER(aPrevDocCharacterSet); + + *aPrevDocCharacterSet = ToNewUnicode(mPrevDocCharacterSet); + + return NS_OK; +} + +static void +SetChildPrevDocCharacterSet(nsIMarkupDocumentViewer* aChild, void* aClosure) +{ + aChild->SetPrevDocCharacterSet((PRUnichar*) aClosure); +} + + +NS_IMETHODIMP DocumentViewerImpl::SetPrevDocCharacterSet(const PRUnichar* aPrevDocCharacterSet) +{ + mPrevDocCharacterSet = aPrevDocCharacterSet; + return CallChildren(SetChildPrevDocCharacterSet, + (void*) aPrevDocCharacterSet); +} + + static void SetChildHintCharacterSetSource(nsIMarkupDocumentViewer* aChild, void* aClosure) { diff --git a/mozilla/content/html/document/src/nsHTMLContentSink.cpp b/mozilla/content/html/document/src/nsHTMLContentSink.cpp index 9f1bc276792..838e8a3466e 100644 --- a/mozilla/content/html/document/src/nsHTMLContentSink.cpp +++ b/mozilla/content/html/document/src/nsHTMLContentSink.cpp @@ -152,6 +152,7 @@ #include "nsLayoutCID.h" #include "nsIFrameManager.h" #include "nsILayoutHistoryState.h" +#include "nsIDocShellTreeItem.h" #include "nsEscape.h" @@ -5310,6 +5311,40 @@ HTMLContentSink::FlushPendingNotifications() NS_IMETHODIMP HTMLContentSink::SetDocumentCharset(nsAString& aCharset) { + if (mWebShell) { + // the following logic to get muCV is copied from + // nsHTMLDocument::StartDocumentLoad + // We need to call muCV->SetPrevDocCharacterSet here in case + // the charset is detected by parser DetectMetaTag + nsCOMPtr docShell(do_QueryInterface(mWebShell)); + nsCOMPtr muCV; + nsCOMPtr cv; + docShell->GetContentViewer(getter_AddRefs(cv)); + if (cv) { + muCV = do_QueryInterface(cv); + } else { + // in this block of code, if we get an error result, we return it + // but if we get a null pointer, that's perfectly legal for parent and parentContentViewer + nsCOMPtr docShellAsItem(do_QueryInterface(docShell)); + NS_ENSURE_TRUE(docShellAsItem, NS_ERROR_FAILURE); + + nsCOMPtr parentAsItem; + docShellAsItem->GetSameTypeParent(getter_AddRefs(parentAsItem)); + + nsCOMPtr parent(do_QueryInterface(parentAsItem)); + if (parent) { + nsCOMPtr parentContentViewer; + nsresult rv = parent->GetContentViewer(getter_AddRefs(parentContentViewer)); + if (NS_SUCCEEDED(rv) && parentContentViewer) { + muCV = do_QueryInterface(parentContentViewer); + } + } + } + if (muCV) { + muCV->SetPrevDocCharacterSet(PromiseFlatString(aCharset).get()); + } + } + if (mDocument) { return mDocument->SetDocumentCharacterSet(aCharset); } diff --git a/mozilla/content/html/document/src/nsHTMLDocument.cpp b/mozilla/content/html/document/src/nsHTMLDocument.cpp index 3ee6abf276f..4879dafb7de 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/content/html/document/src/nsHTMLDocument.cpp @@ -978,17 +978,26 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand, } } - if(kCharsetFromAutoDetection > charsetSource) { + PRBool isPostPage = PR_FALSE; + //check if current doc is from POST command + if (httpChannel) { nsCAutoString methodStr; - if (httpChannel) { - rv = httpChannel->GetRequestMethod(methodStr); - if (NS_FAILED(rv) || !methodStr.Equals(NS_LITERAL_CSTRING("POST"), - nsCaseInsensitiveCStringComparator())) { - StartAutodetection(docShell, charset, aCommand); - } + rv = httpChannel->GetRequestMethod(methodStr); + if (NS_SUCCEEDED(rv) && methodStr.Equals(NS_LITERAL_CSTRING("POST"))) + isPostPage = PR_TRUE; + } + + if (isPostPage && muCV && kCharsetFromHintPrevDoc > charsetSource) { + PRUnichar* requestCharset; + muCV->GetPrevDocCharacterSet(&requestCharset); + if (*requestCharset) { + charsetSource = kCharsetFromHintPrevDoc; + charset = requestCharset; } - else - StartAutodetection(docShell, charset, aCommand); + } + + if(kCharsetFromAutoDetection > charsetSource && !isPostPage) { + StartAutodetection(docShell, charset, aCommand); } #endif @@ -1001,7 +1010,11 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand, SetDocumentCharacterSet(charset); SetDocumentCharacterSetSource(charsetSource); - + + // set doc charset to muCV for next document. + if (muCV) + muCV->SetPrevDocCharacterSet(charset.get()); + if(cacheDescriptor) { rv = cacheDescriptor->SetMetaDataElement("charset", NS_ConvertUCS2toUTF8(charset).get()); diff --git a/mozilla/docshell/base/nsDocShell.cpp b/mozilla/docshell/base/nsDocShell.cpp index 74106e60783..899ae398c2b 100644 --- a/mozilla/docshell/base/nsDocShell.cpp +++ b/mozilla/docshell/base/nsDocShell.cpp @@ -4232,6 +4232,7 @@ nsDocShell::SetupNewViewer(nsIContentViewer * aNewViewer) nsXPIDLString forceCharset; nsXPIDLString hintCharset; PRInt32 hintCharsetSource; + nsXPIDLString prevDocCharset; nsCOMPtr newMUDV(do_QueryInterface(aNewViewer)); @@ -4249,6 +4250,9 @@ nsDocShell::SetupNewViewer(nsIContentViewer * aNewViewer) NS_ENSURE_SUCCESS(oldMUDV-> GetHintCharacterSetSource(&hintCharsetSource), NS_ERROR_FAILURE); + NS_ENSURE_SUCCESS(oldMUDV-> + GetPrevDocCharacterSet(getter_Copies(prevDocCharset)), + NS_ERROR_FAILURE); // set the old state onto the new content viewer NS_ENSURE_SUCCESS(newMUDV->SetDefaultCharacterSet(defaultCharset), @@ -4260,6 +4264,8 @@ nsDocShell::SetupNewViewer(nsIContentViewer * aNewViewer) NS_ENSURE_SUCCESS(newMUDV-> SetHintCharacterSetSource(hintCharsetSource), NS_ERROR_FAILURE); + NS_ENSURE_SUCCESS(newMUDV->SetPrevDocCharacterSet(prevDocCharset), + NS_ERROR_FAILURE); } } diff --git a/mozilla/docshell/base/nsIMarkupDocumentViewer.idl b/mozilla/docshell/base/nsIMarkupDocumentViewer.idl index c31542c0863..7c804d5ddef 100644 --- a/mozilla/docshell/base/nsIMarkupDocumentViewer.idl +++ b/mozilla/docshell/base/nsIMarkupDocumentViewer.idl @@ -70,6 +70,11 @@ interface nsIMarkupDocumentViewer : nsISupports */ attribute PRInt32 hintCharacterSetSource; + /* + character set from prev document + */ + attribute wstring prevDocCharacterSet; + //void GetCharacterSetHint(in wstring hintCharset, in PRInt32 charsetSource); /** diff --git a/mozilla/htmlparser/public/nsIParser.h b/mozilla/htmlparser/public/nsIParser.h index 8c552251631..505a9a44290 100644 --- a/mozilla/htmlparser/public/nsIParser.h +++ b/mozilla/htmlparser/public/nsIParser.h @@ -102,13 +102,14 @@ enum eParserDocType { #define kCharsetFromParentFrame 5 #define kCharsetFromBookmarks 6 #define kCharsetFromAutoDetection 7 -#define kCharsetFromMetaTag 8 -#define kCharsetFromByteOrderMark 9 -#define kCharsetFromChannel 10 -#define kCharsetFromParentForced 11 -#define kCharsetFromUserForced 12 -#define kCharsetFromOtherComponent 13 -#define kCharsetFromPreviousLoading 14 +#define kCharsetFromHintPrevDoc 8 +#define kCharsetFromMetaTag 9 +#define kCharsetFromByteOrderMark 10 +#define kCharsetFromChannel 11 +#define kCharsetFromParentForced 12 +#define kCharsetFromUserForced 13 +#define kCharsetFromOtherComponent 14 +#define kCharsetFromPreviousLoading 15 enum eStreamState {eNone,eOnStart,eOnDataAvail,eOnStop}; diff --git a/mozilla/intl/chardet/src/nsDetectionAdaptor.cpp b/mozilla/intl/chardet/src/nsDetectionAdaptor.cpp index e609f7a8bda..e926851a707 100644 --- a/mozilla/intl/chardet/src/nsDetectionAdaptor.cpp +++ b/mozilla/intl/chardet/src/nsDetectionAdaptor.cpp @@ -44,6 +44,7 @@ #include "nsIParser.h" #include "nsIDocument.h" #include "nsDetectionAdaptor.h" +#include "nsIContentSink.h" static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); @@ -65,6 +66,10 @@ NS_IMETHODIMP nsMyObserver::Notify( mWeakRefParser->GetDocumentCharset(existingCharset, existingSource); if (existingSource < kCharsetFromAutoDetection) { mWeakRefParser->SetDocumentCharset(newcharset, kCharsetFromAutoDetection); + nsCOMPtr contentSink = mWeakRefParser->GetContentSink(); + if (contentSink) + contentSink->SetDocumentCharset(newcharset); + if(mWeakRefDocument) mWeakRefDocument->SetDocumentCharacterSet(newcharset); } diff --git a/mozilla/parser/htmlparser/public/nsIParser.h b/mozilla/parser/htmlparser/public/nsIParser.h index 8c552251631..505a9a44290 100644 --- a/mozilla/parser/htmlparser/public/nsIParser.h +++ b/mozilla/parser/htmlparser/public/nsIParser.h @@ -102,13 +102,14 @@ enum eParserDocType { #define kCharsetFromParentFrame 5 #define kCharsetFromBookmarks 6 #define kCharsetFromAutoDetection 7 -#define kCharsetFromMetaTag 8 -#define kCharsetFromByteOrderMark 9 -#define kCharsetFromChannel 10 -#define kCharsetFromParentForced 11 -#define kCharsetFromUserForced 12 -#define kCharsetFromOtherComponent 13 -#define kCharsetFromPreviousLoading 14 +#define kCharsetFromHintPrevDoc 8 +#define kCharsetFromMetaTag 9 +#define kCharsetFromByteOrderMark 10 +#define kCharsetFromChannel 11 +#define kCharsetFromParentForced 12 +#define kCharsetFromUserForced 13 +#define kCharsetFromOtherComponent 14 +#define kCharsetFromPreviousLoading 15 enum eStreamState {eNone,eOnStart,eOnDataAvail,eOnStop};