Bug 1093611 - AnchorElement.hash should be the encoded version of the href attribute's fragment r=smaug

This commit is contained in:
Valentin Gosu 2014-11-12 03:14:04 +02:00
parent 9909fd462c
commit 6cfe195762
6 changed files with 46 additions and 21 deletions

View File

@ -441,8 +441,10 @@ Link::GetHash(nsAString &_hash, ErrorResult& aError)
nsAutoCString ref;
nsresult rv = uri->GetRef(ref);
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
_hash.Assign(char16_t('#'));
if (!nsContentUtils::ShouldEncodeURLHash()) {
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
}
AppendUTF8toUTF16(ref, _hash);
}
}

View File

@ -514,8 +514,10 @@ URL::GetHash(nsString& aHash, ErrorResult& aRv) const
nsAutoCString ref;
nsresult rv = mURI->GetRef(ref);
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
aHash.Assign(char16_t('#'));
if (!nsContentUtils::ShouldEncodeURLHash()) {
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
}
AppendUTF8toUTF16(ref, aHash);
}
}

View File

@ -236,6 +236,7 @@ bool nsContentUtils::sFullscreenApiIsContentOnly = false;
bool nsContentUtils::sIsPerformanceTimingEnabled = false;
bool nsContentUtils::sIsResourceTimingEnabled = false;
bool nsContentUtils::sIsExperimentalAutocompleteEnabled = false;
bool nsContentUtils::sEncodeURLHash = true;
uint32_t nsContentUtils::sHandlingInputTimeout = 1000;
@ -513,6 +514,9 @@ nsContentUtils::Init()
Preferences::AddBoolVarCache(&sIsExperimentalAutocompleteEnabled,
"dom.forms.autocomplete.experimental", false);
Preferences::AddBoolVarCache(&sEncodeURLHash,
"dom.url.encode_hash", true);
Preferences::AddUintVarCache(&sHandlingInputTimeout,
"dom.event.handling-user-input-time-limit",
1000);

View File

@ -1853,6 +1853,11 @@ public:
return sIsResourceTimingEnabled;
}
static bool ShouldEncodeURLHash()
{
return sEncodeURLHash;
}
/**
* Returns true if the doc tree branch which contains aDoc contains any
* plugins which we don't control event dispatch for, i.e. do any plugins
@ -2304,6 +2309,7 @@ private:
static bool sIsPerformanceTimingEnabled;
static bool sIsResourceTimingEnabled;
static bool sIsExperimentalAutocompleteEnabled;
static bool sEncodeURLHash;
static nsHtml5StringParser* sHTMLFragmentParser;
static nsIParser* sXMLFragmentParser;

View File

@ -298,28 +298,36 @@ nsLocation::GetHash(nsAString& aHash)
nsAutoString unicodeRef;
rv = uri->GetRef(ref);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsITextToSubURI> textToSubURI(
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv));
if (!nsContentUtils::ShouldEncodeURLHash()) {
if (NS_SUCCEEDED(rv)) {
nsAutoCString charset;
uri->GetOriginCharset(charset);
rv = textToSubURI->UnEscapeURIForUI(charset, ref, unicodeRef);
}
if (NS_FAILED(rv)) {
// Oh, well. No intl here!
NS_UnescapeURL(ref);
CopyASCIItoUTF16(ref, unicodeRef);
rv = NS_OK;
}
}
nsCOMPtr<nsITextToSubURI> textToSubURI(
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv) && !unicodeRef.IsEmpty()) {
aHash.Assign(char16_t('#'));
aHash.Append(unicodeRef);
if (NS_SUCCEEDED(rv)) {
nsAutoCString charset;
uri->GetOriginCharset(charset);
rv = textToSubURI->UnEscapeURIForUI(charset, ref, unicodeRef);
}
if (NS_FAILED(rv)) {
// Oh, well. No intl here!
NS_UnescapeURL(ref);
CopyASCIItoUTF16(ref, unicodeRef);
rv = NS_OK;
}
}
if (NS_SUCCEEDED(rv) && !unicodeRef.IsEmpty()) {
aHash.Assign(char16_t('#'));
aHash.Append(unicodeRef);
}
} else { // URL Hash should be encoded
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
aHash.Assign(char16_t('#'));
AppendASCIItoUTF16(ref, aHash);
}
}
if (aHash == mCachedHash) {

View File

@ -161,6 +161,9 @@ pref("dom.webcrypto.enabled", true);
// Whether the UndoManager API is enabled
pref("dom.undo_manager.enabled", false);
// Whether URL,nsLocation,Link::GetHash should be percent encoded.
pref("dom.url.encode_hash", true);
// Whether to run add-on code in different compartments from browser code. This
// causes a separate compartment for each (addon, global) combination, which may
// significantly increase the number of compartments in the system.