Bug 1121040 - Don't send RETURN keypresses to content while a satchel autocomplete entry is selected. r=Gijs,smaug

This commit is contained in:
Matthew Noorenberghe 2015-03-02 13:59:17 -08:00
parent a56e7537c3
commit d278e9f918
3 changed files with 98 additions and 20 deletions

View File

@ -1022,6 +1022,13 @@ nsFormFillController::KeyPress(nsIDOMEvent* aEvent)
if (cancel) {
aEvent->PreventDefault();
// Don't let the page see the RETURN event when the popup is open
// (indicated by cancel=true) so sites don't manually submit forms
// (e.g. via submit.click()) without the autocompleted value being filled.
// Bug 286933 will fix this for other key events.
if (k == nsIDOMKeyEvent::DOM_VK_RETURN) {
aEvent->StopPropagation();
}
}
return NS_OK;
@ -1097,6 +1104,7 @@ nsFormFillController::AddWindowListeners(nsIDOMWindow *aWindow)
true, false);
target->AddEventListener(NS_LITERAL_STRING("input"), this,
true, false);
target->AddEventListener(NS_LITERAL_STRING("keypress"), this, true, false);
target->AddEventListener(NS_LITERAL_STRING("compositionstart"), this,
true, false);
target->AddEventListener(NS_LITERAL_STRING("compositionend"), this,
@ -1135,6 +1143,7 @@ nsFormFillController::RemoveWindowListeners(nsIDOMWindow *aWindow)
target->RemoveEventListener(NS_LITERAL_STRING("pagehide"), this, true);
target->RemoveEventListener(NS_LITERAL_STRING("mousedown"), this, true);
target->RemoveEventListener(NS_LITERAL_STRING("input"), this, true);
target->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, true);
target->RemoveEventListener(NS_LITERAL_STRING("compositionstart"), this,
true);
target->RemoveEventListener(NS_LITERAL_STRING("compositionend"), this,
@ -1142,22 +1151,6 @@ nsFormFillController::RemoveWindowListeners(nsIDOMWindow *aWindow)
target->RemoveEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
}
void
nsFormFillController::AddKeyListener(nsINode* aInput)
{
aInput->AddEventListener(NS_LITERAL_STRING("keypress"), this,
true, false);
}
void
nsFormFillController::RemoveKeyListener()
{
if (!mFocusedInputNode)
return;
mFocusedInputNode->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, true);
}
void
nsFormFillController::StartControllingInput(nsIDOMHTMLInputElement *aInput)
{
@ -1182,8 +1175,6 @@ nsFormFillController::StartControllingInput(nsIDOMHTMLInputElement *aInput)
return;
}
AddKeyListener(node);
node->AddMutationObserverUnlessExists(this);
mFocusedInputNode = node;
mFocusedInput = aInput;
@ -1203,8 +1194,6 @@ nsFormFillController::StartControllingInput(nsIDOMHTMLInputElement *aInput)
void
nsFormFillController::StopControllingInput()
{
RemoveKeyListener();
if (mListNode) {
mListNode->RemoveMutationObserver(this);
mListNode = nullptr;

View File

@ -13,3 +13,4 @@ skip-if = true # Test disabled for too many intermittent failures (bug 874429)
[test_form_submission.html]
[test_form_submission_cap.html]
[test_form_submission_cap2.html]
[test_popup_enter_event.html]

View File

@ -0,0 +1,88 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test for events while the form history popup is open</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<script type="text/javascript" src="satchel_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Form History test: Test for events while the form history popup is open
<p id="display"></p>
<div id="content">
<form id="form1">
<input type="text" name="field1">
<button type="submit">Submit</button>
</form>
</div>
<pre id="test">
<script class="testbody">
var form = document.getElementById("form1");
var input = $_(1, "field1");
var expectedValue = "value1";
function setupFormHistory(aCallback) {
updateFormHistory([
{ op : "remove" },
{ op : "add", fieldname : "field1", value : "value1" },
], aCallback);
}
var autocompleteMenu = getAutocompletePopup();
SpecialPowers.addAutoCompletePopupEventListener(window, "popupshown", popupShownListener);
function handleEnter(evt) {
if (evt.keyCode != KeyEvent.DOM_VK_RETURN) {
return;
}
info("RETURN received for phase: " + evt.eventPhase);
if (input.value == expectedValue) {
ok(true, "RETURN should be received when the popup is closed");
is(input.value, expectedValue, "Check input value when enter is pressed the 2nd time");
info("form should submit with the default handler");
} else {
ok(false, "RETURN keypress shouldn't have been received when a popup item is selected");
}
}
function popupShownListener(evt) {
doKey("down");
doKey("return"); // select the first entry in the popup
doKey("return"); // try to submit the form with the filled value
}
function runTest() {
input.addEventListener("keypress", handleEnter, true);
form.addEventListener("submit", evt => {
is(input.value, expectedValue, "Check input value in the submit handler");
evt.preventDefault();
SimpleTest.finish();
});
// Focus the input before adjusting.value so that the caret goes to the end
// (since OS X doesn't show the dropdown otherwise).
input.focus();
input.value = "value"
input.focus();
doKey("down");
}
function startTest() {
setupFormHistory(function() {
runTest();
});
}
window.onload = startTest;
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>