Bug 444728 - autocomplete disregards maxlength for input fields. r=dolske, sr=mconnor

This commit is contained in:
MattN 2009-08-01 17:30:27 -07:00
parent 5b85b7cf95
commit 9e3365bba2
5 changed files with 111 additions and 9 deletions

View File

@ -38,8 +38,9 @@
#include "nsISupports.idl"
interface nsIAutoCompleteResult;
interface nsIDOMHTMLInputElement;
[scriptable, uuid(2f5bb765-428e-4e33-b2d8-441eb7ddf730)]
[scriptable, uuid(997c0c05-5d1d-47e5-9cbc-765c0b8ec699)]
interface nsIFormAutoComplete: nsISupports {
/**
@ -48,5 +49,6 @@ interface nsIFormAutoComplete: nsISupports {
nsIAutoCompleteResult autoCompleteSearch(
in AString aInputName,
in AString aSearchString,
in nsIDOMHTMLInputElement aField,
in nsIAutoCompleteResult aPreviousResult);
};

View File

@ -173,11 +173,12 @@ FormAutoComplete.prototype = {
*
* aInputName -- |name| attribute from the form input being autocompleted.
* aUntrimmedSearchString -- current value of the input
* aField -- nsIDOMHTMLInputElement being autocompleted (may be null if from chrome)
* aPreviousResult -- previous search result, if any.
*
* Returns: an nsIAutoCompleteResult
*/
autoCompleteSearch : function (aInputName, aUntrimmedSearchString, aPreviousResult) {
autoCompleteSearch : function (aInputName, aUntrimmedSearchString, aField, aPreviousResult) {
function sortBytotalScore (a, b) {
let x = a.totalScore;
let y = b.totalScore;
@ -222,6 +223,11 @@ FormAutoComplete.prototype = {
this.log("Creating new autocomplete search result.");
let entries = this.getAutoCompleteValues(aInputName, searchString);
result = new FormAutoCompleteResult(this._formHistory, entries, aInputName, aUntrimmedSearchString);
if (aField && aField.maxLength > -1) {
let original = result.wrappedJSObject.entries;
let filtered = original.filter(function (el) el.text.length <= this.maxLength, aField);
result.wrappedJSObject.entries = filtered;
}
}
return result;

View File

@ -517,6 +517,7 @@ nsFormFillController::StartSearch(const nsAString &aSearchString, const nsAStrin
rv = formAutoComplete->AutoCompleteSearch(aSearchParam,
aSearchString,
mFocusedInput,
aPreviousResult,
getter_AddRefs(result));
}

View File

@ -51,6 +51,12 @@ Form History test: form field autocomplete
<button type="submit">Submit</button>
</form>
<!-- form with maxlength attribute on input -->
<form id="form7" onsubmit="return false;">
<input type="text" name="field5" maxlength="10">
<button type="submit">Submit</button>
</form>
</div>
<pre id="test">
@ -84,6 +90,10 @@ fh.addEntry("field4", "a\xe6");
fh.addEntry("field4", "aa a\xe6");
fh.addEntry("field4", "aba\xe6");
fh.addEntry("field4", "bc d\xe6");
fh.addEntry("field5", "1");
fh.addEntry("field5", "12");
fh.addEntry("field5", "123");
fh.addEntry("field5", "1234");
// Restore the form to the default state.
function restoreForm() {
@ -521,6 +531,89 @@ function runTest(testNum) {
case 256:
checkMenuEntries([]);
// Look at form 7, try to trigger autocomplete popup
input = $_(7, "field5");
restoreForm();
doKey("down");
testNum = 299;
break;
case 300:
checkMenuEntries(["1", "12", "123", "1234"]);
input.maxLength = 4;
doKey("escape");
doKey("down");
break;
case 301:
checkMenuEntries(["1", "12", "123", "1234"]);
input.maxLength = 3;
doKey("escape");
doKey("down");
break;
case 302:
checkMenuEntries(["1", "12", "123"]);
input.maxLength = 2;
doKey("escape");
doKey("down");
break;
case 303:
checkMenuEntries(["1", "12"]);
input.maxLength = 1;
doKey("escape");
doKey("down");
break;
case 304:
checkMenuEntries(["1"]);
input.maxLength = 0;
doKey("escape");
doKey("down");
break;
case 305:
checkMenuEntries([]);
input.maxLength = 4;
// now again with a character typed
sendChar("1", input);
doKey("escape");
doKey("down");
break;
case 306:
checkMenuEntries(["1", "12", "123", "1234"]);
input.maxLength = 3;
doKey("escape");
doKey("down");
break;
case 307:
checkMenuEntries(["1", "12", "123"]);
input.maxLength = 2;
doKey("escape");
doKey("down");
break;
case 308:
checkMenuEntries(["1", "12"]);
input.maxLength = 1;
doKey("escape");
doKey("down");
break;
case 309:
checkMenuEntries(["1"]);
input.maxLength = 0;
doKey("escape");
doKey("down");
break;
case 310:
checkMenuEntries([]);
SimpleTest.finish();
return;

View File

@ -131,13 +131,13 @@ function run_test() {
// ===== 2 =====
// Check search contains all entries
testnum++;
results = fac.autoCompleteSearch("field1", "", null);
results = fac.autoCompleteSearch("field1", "", null, null);
do_check_eq(numRecords, results.matchCount);
// ===== 3 =====
// Check search result ordering with empty search term
testnum++;
results = fac.autoCompleteSearch("field1", "", null);
results = fac.autoCompleteSearch("field1", "", null, null);
let lastFound = numRecords;
for (let i = 0; i < numRecords; i+=2) {
do_check_eq(parseInt(results.getValueAt(i + 1).substr(5), 10), --lastFound);
@ -147,7 +147,7 @@ function run_test() {
// ===== 4 =====
// Check search result ordering with "v"
testnum++;
results = fac.autoCompleteSearch("field1", "v", null);
results = fac.autoCompleteSearch("field1", "v", null, null);
lastFound = numRecords;
for (let i = 0; i < numRecords; i+=2) {
do_check_eq(parseInt(results.getValueAt(i + 1).substr(5), 10), --lastFound);
@ -176,7 +176,7 @@ function run_test() {
// ===== 5 =====
// Check search result ordering with empty search term
testnum++;
results = fac.autoCompleteSearch("field2", "", null);
results = fac.autoCompleteSearch("field2", "", null, null);
lastFound = timesUsedSamples;
for (let i = 0; i < timesUsedSamples; i++) {
do_check_eq(parseInt(results.getValueAt(i).substr(5)), --lastFound);
@ -185,7 +185,7 @@ function run_test() {
// ===== 6 =====
// Check search result ordering with "v"
testnum++;
results = fac.autoCompleteSearch("field2", "v", null);
results = fac.autoCompleteSearch("field2", "v", null, null);
lastFound = timesUsedSamples;
for (let i = 0; i < timesUsedSamples; i++) {
do_check_eq(parseInt(results.getValueAt(i).substr(5)), --lastFound);
@ -217,7 +217,7 @@ function run_test() {
now +
");");
results = fac.autoCompleteSearch("field3", "", null);
results = fac.autoCompleteSearch("field3", "", null, null);
do_check_eq(results.getValueAt(0), "senior citizen");
do_check_eq(results.getValueAt(1), "old but not senior");
@ -257,7 +257,7 @@ function run_test() {
(now * 2) +
");");
results = fac.autoCompleteSearch("field4", "", null);
results = fac.autoCompleteSearch("field4", "", null, null);
do_check_eq(results.matchCount, 3);