Backout Bug 566489, r=sdwilsh

This commit is contained in:
Michael Ventnor
2011-05-27 08:52:47 +10:00
parent b9bf049563
commit 462cb1f9fb
10 changed files with 34 additions and 511 deletions

View File

@@ -24,7 +24,6 @@
* Dean Tessman <dean_tessman@hotmail.com>
* Johnny Stenback <jst@mozilla.jstenback.com>
* Masayuki Nakano <masayuki@d-toybox.com>
* Michael Ventnor <m.ventnor@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -55,7 +54,6 @@
#include "nsIDOMKeyEvent.h"
#include "mozilla/Services.h"
#include "mozilla/ModuleUtils.h"
#include "mozilla/Util.h"
static const char *kAutoCompleteSearchCID = "@mozilla.org/autocomplete/search;1?name=";
@@ -96,43 +94,6 @@ nsAutoCompleteController::~nsAutoCompleteController()
SetInput(nsnull);
}
////////////////////////////////////////////////////////////////////////
//// Helper methods
/**
* Cuts any URL prefixes from a given string, making it suitable for search
* comparisons.
*
* @param aOrigSpec
* The string to look for prefixes in.
* @return A substring, with any URL prefixes removed, that depends on the
* same buffer as aOrigSpec (to save allocations).
*/
static const nsDependentSubstring
RemoveURIPrefixes(const nsAString &aOrigSpec)
{
nsDependentSubstring result(aOrigSpec, 0);
if (StringBeginsWith(result, NS_LITERAL_STRING("moz-action:"))) {
PRUint32 locationOfComma = result.FindChar(',', 11);
result.Rebind(result, locationOfComma + 1);
}
if (StringBeginsWith(result, NS_LITERAL_STRING("http://"))) {
result.Rebind(result, 7);
} else if (StringBeginsWith(result, NS_LITERAL_STRING("https://"))) {
result.Rebind(result, 8);
} else if (StringBeginsWith(result, NS_LITERAL_STRING("ftp://"))) {
result.Rebind(result, 6);
}
if (StringBeginsWith(result, NS_LITERAL_STRING("www."))) {
result.Rebind(result, 4);
}
return result;
}
////////////////////////////////////////////////////////////////////////
//// nsIAutoCompleteController
@@ -1432,64 +1393,6 @@ nsAutoCompleteController::CompleteDefaultIndex(PRInt32 aSearchIndex)
return NS_OK;
}
nsresult
nsAutoCompleteController::GetDefaultCompleteURLValue(nsIAutoCompleteResult *aResult,
PRBool aPreserveCasing,
nsAString &_retval)
{
MOZ_ASSERT(aResult);
PRUint32 rowCount;
(void)aResult->GetMatchCount(&rowCount);
if (rowCount == 0) {
// Return early if we have no entries, so that we don't waste time
// fixing up mSearchString below
return NS_ERROR_FAILURE;
}
const nsDependentSubstring& fixedSearchTerm = RemoveURIPrefixes(mSearchString);
for (PRUint32 i = 0; i < rowCount; ++i) {
nsAutoString resultValue;
aResult->GetValueAt(i, resultValue);
const nsDependentSubstring& fixedResult = RemoveURIPrefixes(resultValue);
if (!StringBeginsWith(fixedResult, fixedSearchTerm,
nsCaseInsensitiveStringComparator())) {
// Not a matching URL
continue;
}
// Found a matching item! Figure out what needs to be assigned/appended.
if (aPreserveCasing) {
// Use nsDependentString here so we have access to FindCharInSet.
const nsDependentString appendValue(Substring(fixedResult, fixedSearchTerm.Length()));
// We only want to autocomplete up to the next separator. This lets a user
// go to a toplevel domain, if a longer path in that domain is higher in
// the autocomplete.
// eg. if the user types "m" and "mozilla.org/credits" is the top hit,
// autocomplete only to "mozilla.org/" in case that's where they want to go.
// They're one keystroke away from "/credits", anyway.
PRInt32 separatorIndex = appendValue.FindCharInSet("/?#");
if (separatorIndex != kNotFound && appendValue[separatorIndex] == '/') {
// Add 1 so we include the directory separator
separatorIndex++;
}
nsAutoString returnValue;
returnValue.Assign(mSearchString);
returnValue.Append(Substring(appendValue, 0, separatorIndex));
_retval = returnValue;
} else {
_retval.Assign(fixedResult);
}
return NS_OK;
}
// No match at all
return NS_ERROR_FAILURE;
}
nsresult
nsAutoCompleteController::GetDefaultCompleteValue(PRInt32 aSearchIndex,
PRBool aPreserveCasing,
@@ -1513,14 +1416,6 @@ nsAutoCompleteController::GetDefaultCompleteValue(PRInt32 aSearchIndex,
nsIAutoCompleteResult *result = mResults.SafeObjectAt(index);
NS_ENSURE_TRUE(result != nsnull, NS_ERROR_FAILURE);
PRBool isURL;
result->GetIsURLResult(&isURL);
if (isURL) {
// For URLs, we remove needless prefixes, then iterate over all values
// to find a suitable default.
return GetDefaultCompleteURLValue(result, aPreserveCasing, _retval);
}
if (defaultIndex < 0) {
// The search must explicitly provide a default index in order
// for us to be able to complete.
@@ -1571,11 +1466,37 @@ nsAutoCompleteController::CompleteValue(nsString &aValue)
// autocomplete to aValue.
mInput->SetTextValue(aValue);
} else {
// Autocompleting something from the middle.
// Use the format "searchstring >> full string" to indicate to the user
// what we are going to replace their search string with.
mInput->SetTextValue(mSearchString + NS_LITERAL_STRING(" >> ") + aValue);
endSelect = mSearchString.Length() + 4 + aValue.Length();
nsresult rv;
nsCOMPtr<nsIIOService> ios = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString scheme;
if (NS_SUCCEEDED(ios->ExtractScheme(NS_ConvertUTF16toUTF8(aValue), scheme))) {
// Trying to autocomplete a URI from somewhere other than the beginning.
// Only succeed if the missing portion is "http://"; otherwise do not
// autocomplete. This prevents us from "helpfully" autocompleting to a
// URI that isn't equivalent to what the user expected.
const PRInt32 findIndex = 7; // length of "http://"
if ((endSelect < findIndex + mSearchStringLength) ||
!scheme.LowerCaseEqualsLiteral("http") ||
!Substring(aValue, findIndex, mSearchStringLength).Equals(
mSearchString, nsCaseInsensitiveStringComparator())) {
return NS_OK;
}
mInput->SetTextValue(mSearchString +
Substring(aValue, mSearchStringLength + findIndex,
endSelect));
endSelect -= findIndex; // We're skipping this many characters of aValue.
} else {
// Autocompleting something other than a URI from the middle.
// Use the format "searchstring >> full string" to indicate to the user
// what we are going to replace their search string with.
mInput->SetTextValue(mSearchString + NS_LITERAL_STRING(" >> ") + aValue);
endSelect = mSearchString.Length() + 4 + aValue.Length();
}
}
mInput->SelectTextRange(mSearchStringLength, endSelect);