diff --git a/mozilla/content/html/document/src/nsHTMLDocument.cpp b/mozilla/content/html/document/src/nsHTMLDocument.cpp index 0d521fa8e55..9c3ffa81847 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/content/html/document/src/nsHTMLDocument.cpp @@ -752,6 +752,19 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand, } } + if (kCharsetFromParentFrame > charsetSource) { + if (dcInfo) { + nsCOMPtr csAtom; + dcInfo->GetParentCharset(getter_AddRefs(csAtom)); + if (csAtom) { + csAtom->ToString(charset); + charsetSource = kCharsetFromParentFrame; + + // printf("### 0 >>> Having parent CS = %s\n", charset.ToNewCString()); + } + } + } + if((kCharsetFromAutoDetection > charsetSource ) && gPlugDetector) { nsCOMPtr cdet = do_CreateInstance(g_detector_progid, diff --git a/mozilla/docshell/base/nsDocShell.cpp b/mozilla/docshell/base/nsDocShell.cpp index 091042ab2bd..f1c7fa44575 100644 --- a/mozilla/docshell/base/nsDocShell.cpp +++ b/mozilla/docshell/base/nsDocShell.cpp @@ -79,6 +79,7 @@ static NS_DEFINE_IID(kDeviceContextCID, NS_DEVICE_CONTEXT_CID); static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID); static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID); +static NS_DEFINE_CID(kDocumentCharsetInfoCID, NS_DOCUMENTCHARSETINFO_CID); //***************************************************************************** //*** nsDocShell: Object Management @@ -459,6 +460,14 @@ NS_IMETHODIMP nsDocShell::GetDocumentCharsetInfo(nsIDocumentCharsetInfo** { NS_ENSURE_ARG_POINTER(aDocumentCharsetInfo); + // if the mDocumentCharsetInfo does not exist already, we create it now + if (!mDocumentCharsetInfo) { + nsresult res = nsComponentManager::CreateInstance(kDocumentCharsetInfoCID, + NULL, NS_GET_IID(nsIDocumentCharsetInfo), + getter_AddRefs(mDocumentCharsetInfo)); + if (NS_FAILED(res)) return NS_ERROR_FAILURE; + } + *aDocumentCharsetInfo = mDocumentCharsetInfo; NS_IF_ADDREF(*aDocumentCharsetInfo); return NS_OK; @@ -889,6 +898,40 @@ NS_IMETHODIMP nsDocShell::AddChild(nsIDocShellTreeItem *aChild) } } + // Now take this document's charset and set the parentCharset field of the + // child's DocumentCharsetInfo to it. We'll later use that field, in the + // loading process, for the charset choosing algorithm. + // If we fail, at any point, we just return NS_OK. + // This code has some performance impact. But this will be reduced when + // the current charset will finally be stored as an Atom, avoiding the + // alias resolution extra look-up. + + // we are NOT going to propagate the charset is this Chrome's docshell + if (mItemType == nsIDocShellTreeItem::typeChrome) return NS_OK; + + nsresult res = NS_OK; + + // get the child's docCSInfo object + nsCOMPtr dcInfo = NULL; + res = childAsDocShell->GetDocumentCharsetInfo(getter_AddRefs(dcInfo)); + if (NS_FAILED(res) || (!dcInfo)) return NS_OK; + + // get the parent's current charset + nsCOMPtr docv(do_QueryInterface(mContentViewer)); + if (!docv) return NS_OK; + nsCOMPtr doc; + res = docv->GetDocument(*getter_AddRefs(doc)); + if (NS_FAILED(res) || (!doc)) return NS_OK; + nsAutoString parentCS; + res = doc->GetDocumentCharacterSet(parentCS); + if (NS_FAILED(res)) return NS_OK; + + // set the child's parentCharset + res = dcInfo->SetParentCharset(&parentCS); + if (NS_FAILED(res)) return NS_OK; + + // printf("### 1 >>> Adding child. Parent CS = %s. ItemType = %d.\n", parentCS.ToNewCString(), mItemType); + return NS_OK; } diff --git a/mozilla/intl/chardet/public/nsIDocumentCharsetInfo.h b/mozilla/intl/chardet/public/nsIDocumentCharsetInfo.h index d8583577fd6..c1ca67f9382 100644 --- a/mozilla/intl/chardet/public/nsIDocumentCharsetInfo.h +++ b/mozilla/intl/chardet/public/nsIDocumentCharsetInfo.h @@ -38,6 +38,8 @@ "component://netscape/document-charset-info" // XXX doc me +// XXX make this interface IDL +// XXX mark the right params "const" class nsIDocumentCharsetInfo : public nsISupports { @@ -46,8 +48,19 @@ public: NS_IMETHOD SetForcedCharset(nsIAtom * aCharset) = 0; NS_IMETHOD GetForcedCharset(nsIAtom ** aResult) = 0; + NS_IMETHOD SetForcedDetector(PRBool aForced) = 0; NS_IMETHOD GetForcedDetector(PRBool * aResult) = 0; + + NS_IMETHOD SetParentCharset(nsIAtom * aCharset) = 0; + NS_IMETHOD GetParentCharset(nsIAtom ** aResult) = 0; + + /** + * You should NOT use this method!!! It will very soon be deprecated. I only + * added it here for convenience in the ongoing transition to Atoms. Use + * SetParentCharset(nsIAtom *) instead. + */ + NS_IMETHOD SetParentCharset(nsString * aCharset) = 0; }; #endif // nsIDocumentCharsetInfo_h__ diff --git a/mozilla/intl/chardet/src/nsDocumentCharsetInfo.cpp b/mozilla/intl/chardet/src/nsDocumentCharsetInfo.cpp index d30d968f4b6..64d364a0bbe 100644 --- a/mozilla/intl/chardet/src/nsDocumentCharsetInfo.cpp +++ b/mozilla/intl/chardet/src/nsDocumentCharsetInfo.cpp @@ -21,11 +21,16 @@ */ #include "nsCharDetDll.h" +#include "nsIServiceManager.h" +#include "nsICharsetConverterManager.h" +#include "nsICharsetConverterManager2.h" #include "nsDocumentCharsetInfo.h" #include "nsCOMPtr.h" // XXX doc me +static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); + class nsDocumentCharsetInfo : public nsIDocumentCharsetInfo { NS_DECL_ISUPPORTS @@ -37,11 +42,17 @@ public: NS_IMETHOD SetForcedCharset(nsIAtom * aCharset); NS_IMETHOD GetForcedCharset(nsIAtom ** aResult); + NS_IMETHOD SetForcedDetector(PRBool aForced); NS_IMETHOD GetForcedDetector(PRBool * aResult); + NS_IMETHOD SetParentCharset(nsIAtom * aCharset); + NS_IMETHOD GetParentCharset(nsIAtom ** aResult); + NS_IMETHOD SetParentCharset(nsString * aCharset); + private: nsCOMPtr mForcedCharset; + nsCOMPtr mParentCharset; }; class nsDocumentCharsetInfoFactory : public nsIFactory @@ -101,6 +112,32 @@ NS_IMETHODIMP nsDocumentCharsetInfo::GetForcedDetector(PRBool * aResult) return NS_OK; } +NS_IMETHODIMP nsDocumentCharsetInfo::SetParentCharset(nsIAtom * aCharset) +{ + mParentCharset = aCharset; + return NS_OK; +} + +NS_IMETHODIMP nsDocumentCharsetInfo::GetParentCharset(nsIAtom ** aResult) +{ + *aResult = mParentCharset; + if (mParentCharset) NS_ADDREF(*aResult); + return NS_OK; +} + +NS_IMETHODIMP nsDocumentCharsetInfo::SetParentCharset(nsString * aCharset) +{ + nsresult res = NS_OK; + NS_WITH_SERVICE(nsICharsetConverterManager2, ccMan, kCharsetConverterManagerCID, &res); + if (NS_FAILED(res)) return NS_ERROR_FAILURE; + + nsCOMPtr csAtom; + res = ccMan->GetCharsetAtom(aCharset->GetUnicode(), getter_AddRefs(csAtom)); + if (NS_FAILED(res)) return NS_ERROR_FAILURE; + + return SetParentCharset(csAtom); +} + NS_IMPL_ISUPPORTS(nsDocumentCharsetInfoFactory, NS_GET_IID(nsIFactory)); NS_IMETHODIMP nsDocumentCharsetInfoFactory::CreateInstance( diff --git a/mozilla/layout/html/document/src/nsHTMLDocument.cpp b/mozilla/layout/html/document/src/nsHTMLDocument.cpp index 0d521fa8e55..9c3ffa81847 100644 --- a/mozilla/layout/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/layout/html/document/src/nsHTMLDocument.cpp @@ -752,6 +752,19 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand, } } + if (kCharsetFromParentFrame > charsetSource) { + if (dcInfo) { + nsCOMPtr csAtom; + dcInfo->GetParentCharset(getter_AddRefs(csAtom)); + if (csAtom) { + csAtom->ToString(charset); + charsetSource = kCharsetFromParentFrame; + + // printf("### 0 >>> Having parent CS = %s\n", charset.ToNewCString()); + } + } + } + if((kCharsetFromAutoDetection > charsetSource ) && gPlugDetector) { nsCOMPtr cdet = do_CreateInstance(g_detector_progid,