Bug 945784, part 2 - Fire 'change' events for <input type=number> more frequently, per the new HTML5 rules. r=smaug

This commit is contained in:
Jonathan Watt 2013-12-05 16:20:33 +00:00
parent 9772866d88
commit 51ac041bbf
2 changed files with 40 additions and 1 deletions

View File

@ -3347,6 +3347,18 @@ HTMLInputElement::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
}
}
}
} else if (aVisitor.mEvent->message == NS_KEY_UP) {
WidgetKeyboardEvent* keyEvent = aVisitor.mEvent->AsKeyboardEvent();
if ((keyEvent->keyCode == NS_VK_UP || keyEvent->keyCode == NS_VK_DOWN) &&
!(keyEvent->IsShift() || keyEvent->IsControl() ||
keyEvent->IsAlt() || keyEvent->IsMeta() ||
keyEvent->IsAltGraph() || keyEvent->IsFn() ||
keyEvent->IsOS())) {
// The up/down arrow key events fire 'change' events when released
// so that at the end of a series of up/down arrow key repeat events
// the value is considered to be "commited" by the user.
FireChangeEventIfNeeded();
}
}
}
@ -3506,7 +3518,25 @@ HTMLInputElement::StopNumberControlSpinnerSpin()
void
HTMLInputElement::StepNumberControlForUserEvent(int32_t aDirection)
{
ApplyStep(aDirection);
// We cannot call ApplyStep here because that eventually calls SetValue, and
// that sets mFocusedValue. We are handling a user action here, not a script
// action, so we do not want to reset mFocusedValue or else we will fail to
// dispatch 'change' events correctly.
Decimal value = GetValueAsDecimal();
if (value.isNaN()) {
value = 0;
}
Decimal step = GetStep();
if (step == kStepAny) {
step = GetDefaultStep();
}
MOZ_ASSERT(value.isFinite() && step.isFinite());
Decimal newValue = value + step * aDirection;
nsAutoString newVal;
ConvertNumberToString(newValue, newVal);
SetValueInternal(newVal, true, true);
nsContentUtils::DispatchTrustedEvent(OwnerDoc(),
static_cast<nsIDOMHTMLInputElement*>(this),
NS_LITERAL_STRING("input"), true,

View File

@ -175,9 +175,18 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=722599
number.value = "";
number.focus();
synthesizeKey("1", {});
synthesizeKey("2", {});
is(numberChange, 0, "Change event shouldn't be dispatched on number input element for keyboard input until it loses focus");
number.blur();
is(numberChange, 1, "Change event should be dispatched on number input element on blur");
is(number.value, 12, "Sanity check that number keys were actually handled");
number.value = "";
number.focus();
synthesizeKey("VK_UP", {});
synthesizeKey("VK_UP", {});
synthesizeKey("VK_DOWN", {});
is(numberChange, 4, "Change event should be dispatched on number input element for up/down arrow keys (a special case)");
is(number.value, 1, "Sanity check that number and arrow keys were actually handled");
// Special case type=range
var range = document.getElementById("input_range");