From a0201ff20146350ae661e254edb071b26aa115cc Mon Sep 17 00:00:00 2001 From: Jonathan Watt Date: Sun, 8 Dec 2013 17:58:54 +0000 Subject: [PATCH] Bug 946390 - Allow content to preventDefault() keypress events targeting . r=smaug --- content/html/content/src/HTMLInputElement.cpp | 9 ++++----- .../test/forms/test_input_number_key_events.html | 10 ++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/content/html/content/src/HTMLInputElement.cpp b/content/html/content/src/HTMLInputElement.cpp index a44d0cc45a08..00a9de823a9c 100644 --- a/content/html/content/src/HTMLInputElement.cpp +++ b/content/html/content/src/HTMLInputElement.cpp @@ -3835,11 +3835,10 @@ HTMLInputElement::PostHandleEvent(nsEventChainPostVisitor& aVisitor) // the editor's handling of up/down keypress events. For that reason we // just ignore aVisitor.mEventStatus here and go ahead and handle the // event to increase/decrease the value of the number control. - // XXX we still need to allow script to call preventDefault() on the - // event, but right now we can't tell the difference between the editor - // on script doing that (bug 930374). - StepNumberControlForUserEvent(keyEvent->keyCode == NS_VK_UP ? 1 : -1); - aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault; + if (!aVisitor.mEvent->mFlags.mDefaultPreventedByContent) { + StepNumberControlForUserEvent(keyEvent->keyCode == NS_VK_UP ? 1 : -1); + aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault; + } } else if (nsEventStatus_eIgnore == aVisitor.mEventStatus) { switch (aVisitor.mEvent->message) { diff --git a/content/html/content/test/forms/test_input_number_key_events.html b/content/html/content/test/forms/test_input_number_key_events.html index ee063d10384c..8237cd0293d2 100644 --- a/content/html/content/test/forms/test_input_number_key_events.html +++ b/content/html/content/test/forms/test_input_number_key_events.html @@ -177,6 +177,16 @@ function test() { expectedVal = expectedValAfterKeyEvent(key, elem); synthesizeKey(key, {}); is(elem.value, expectedVal, "Test repeat of " + key + " for number control"); + + // Test preventDefault(): + elem.addEventListener("keypress", function(evt) { + evt.preventDefault(); + elem.removeEventListener("keypress", arguments.callee, false); + }, false); + oldVal = elem.value = 0; + expectedVal = 0; + synthesizeKey(key, {}); + is(elem.value, expectedVal, "Test " + key + " for number control where scripted preventDefault() should prevent the value changing"); } }