Bug 1071816 - Add a pref to fallback to UTF-8 for files from file: URLs. r?hsivonen
MozReview-Commit-ID: KFjIqYmgzTl
--- a/dom/encoding/FallbackEncoding.cpp
+++ b/dom/encoding/FallbackEncoding.cpp
@@ -50,16 +50,17 @@ static const EncodingProp domainsFallbac
static constexpr nsUConvProp nonParticipatingDomains[] = {
#include "nonparticipatingdomains.properties.h"
};
NS_IMPL_ISUPPORTS(FallbackEncoding, nsIObserver)
FallbackEncoding* FallbackEncoding::sInstance = nullptr;
bool FallbackEncoding::sGuessFallbackFromTopLevelDomain = true;
+bool FallbackEncoding::sFallbackToUTF8ForFile = false;
FallbackEncoding::FallbackEncoding()
: mFallback(nullptr)
{
MOZ_ASSERT(!FallbackEncoding::sInstance,
"Singleton already exists.");
}
@@ -150,16 +151,18 @@ FallbackEncoding::Initialize()
MOZ_ASSERT(!FallbackEncoding::sInstance,
"Initializing pre-existing fallback cache.");
FallbackEncoding::sInstance = new FallbackEncoding;
Preferences::RegisterCallback(FallbackEncoding::PrefChanged,
"intl.charset.fallback.override",
nullptr);
Preferences::AddBoolVarCache(&sGuessFallbackFromTopLevelDomain,
"intl.charset.fallback.tld");
+ Preferences::AddBoolVarCache(&sFallbackToUTF8ForFile,
+ "intl.charset.fallback.utf8_for_file");
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->AddObserver(sInstance, "intl:requested-locales-changed", true);
}
}
void
--- a/dom/encoding/FallbackEncoding.h
+++ b/dom/encoding/FallbackEncoding.h
@@ -22,16 +22,21 @@ public:
NS_DECL_NSIOBSERVER
/**
* Whether FromTopLevelDomain() should be used.
*/
static bool sGuessFallbackFromTopLevelDomain;
/**
+ * Whether UTF-8 should be used for file URLs.
+ */
+ static bool sFallbackToUTF8ForFile;
+
+ /**
* Gets the locale-dependent fallback encoding for legacy HTML and plain
* text content.
*
* @param aFallback the outparam for the fallback encoding
*/
static NotNull<const Encoding*> FromLocale();
/**
--- a/dom/html/nsHTMLDocument.cpp
+++ b/dom/html/nsHTMLDocument.cpp
@@ -481,16 +481,22 @@ nsHTMLDocument::TryTLD(int32_t& aCharset
void
nsHTMLDocument::TryFallback(int32_t& aCharsetSource,
NotNull<const Encoding*>& aEncoding)
{
if (kCharsetFromFallback <= aCharsetSource)
return;
aCharsetSource = kCharsetFromFallback;
+ bool isFile = false;
+ if (FallbackEncoding::sFallbackToUTF8ForFile && mDocumentURI &&
+ NS_SUCCEEDED(mDocumentURI->SchemeIs("file", &isFile)) && isFile) {
+ aEncoding = UTF_8_ENCODING;
+ return;
+ }
aEncoding = FallbackEncoding::FromLocale();
}
void
nsHTMLDocument::SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding)
{
nsDocument::SetDocumentCharacterSet(aEncoding);
// Make sure to stash this charset on our channel as needed if it's a wyciwyg
--- a/dom/html/nsHTMLDocument.h
+++ b/dom/html/nsHTMLDocument.h
@@ -357,18 +357,18 @@ protected:
NotNull<const Encoding*>& aEncoding);
static void TryCacheCharset(nsICachingChannel* aCachingChannel,
int32_t& aCharsetSource,
NotNull<const Encoding*>& aEncoding);
void TryParentCharset(nsIDocShell* aDocShell,
int32_t& charsetSource,
NotNull<const Encoding*>& aEncoding);
void TryTLD(int32_t& aCharsetSource, NotNull<const Encoding*>& aCharset);
- static void TryFallback(int32_t& aCharsetSource,
- NotNull<const Encoding*>& aEncoding);
+ void TryFallback(int32_t& aCharsetSource,
+ NotNull<const Encoding*>& aEncoding);
// Override so we can munge the charset on our wyciwyg channel as needed.
virtual void
SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding) override;
// Tracks if we are currently processing any document.write calls (either
// implicit or explicit). Note that if a write call writes out something which
// would block the parser, then mWriteLevel will be incorrect until the parser
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -2282,16 +2282,17 @@ pref("converter.html2txt.header_strategy
pref("converter.html2txt.always_include_ruby", false);
pref("intl.accept_languages", "chrome://global/locale/intl.properties");
pref("intl.menuitems.alwaysappendaccesskeys","chrome://global/locale/intl.properties");
pref("intl.menuitems.insertseparatorbeforeaccesskeys","chrome://global/locale/intl.properties");
pref("intl.charset.detector", "chrome://global/locale/intl.properties");
pref("intl.charset.fallback.override", "");
pref("intl.charset.fallback.tld", true);
+pref("intl.charset.fallback.utf8_for_file", false);
pref("intl.ellipsis", "chrome://global-platform/locale/intl.properties");
pref("intl.locale.matchOS", false);
// this pref allows user to request that all internationalization formatters
// like date/time formatting, unit formatting, calendars etc. should use
// OS locale set instead of the app locale set.
pref("intl.regional_prefs.use_os_locales", false);
// fallback charset list for Unicode conversion (converting from Unicode)
// currently used for mail send only to handle symbol characters (e.g Euro, trademark, smartquotes)