Bug 946390 - Allow content to preventDefault() keypress events targeting <input type=number>. r=smaug
authorJonathan Watt <jwatt@jwatt.org>
Sun, 08 Dec 2013 17:58:54 +0000
changeset 175123 e9b06b13344d3ec0a9558288acd62ec601663bce
parent 175122 35d7b6de678b930ebb0ff4e1a475be7118920181
child 175124 7b670bd3c7f28bb7ffe7be0b3d8eb4c6f4190848
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs946390
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 946390 - Allow content to preventDefault() keypress events targeting <input type=number>. r=smaug
content/html/content/src/HTMLInputElement.cpp
content/html/content/test/forms/test_input_number_key_events.html
--- a/content/html/content/src/HTMLInputElement.cpp
+++ b/content/html/content/src/HTMLInputElement.cpp
@@ -3830,21 +3830,20 @@ HTMLInputElement::PostHandleEvent(nsEven
       // control and as a result aVisitor.mEventStatus will already have been
       // set to nsEventStatus_eConsumeNoDefault. However, we know that
       // whenever the up/down arrow keys cause the value of the number
       // control to change the string in the text control will change, and
       // the cursor will be moved to the end of the text control, overwriting
       // 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) {
 
         case NS_FOCUS_CONTENT:
         {
           // see if we should select the contents of the textbox. This happens
           // for text and password fields when the field was focused by the
           // keyboard or a navigation, the platform allows it, and it wasn't
--- a/content/html/content/test/forms/test_input_number_key_events.html
+++ b/content/html/content/test/forms/test_input_number_key_events.html
@@ -172,15 +172,25 @@ function test() {
     expectedVal = expectedValAfterKeyEvent(key, elem);
     synthesizeKey(key, {});
     is(elem.value, expectedVal, "Test " + key + " for number control with value set to the minimum (" + oldVal + ")");
 
     // Same again:
     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");
   }
 }
 
 </script>
 </pre>
 </body>
 </html>