Fix <rdar://5483519> Pressing Enter on selected buttons should fire onclick
authoraroben@apple.com <aroben@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 9 Nov 2007 20:04:41 +0000 (20:04 +0000)
committeraroben@apple.com <aroben@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 9 Nov 2007 20:04:41 +0000 (20:04 +0000)
 LayoutTests:

         Add a test for <rdar://5483519> Pressing Enter on selected buttons should fire onclick

         Reviewed by Adele.

         * fast/forms/enter-clicks-buttons-expected.txt: Added.
         * fast/forms/enter-clicks-buttons.html: Added.

 WebCore:

         Fix <rdar://5483519> Pressing Enter on selected buttons should fire onclick

         We now match the behavior of Firefox and IE, which is to always just
         send a click event to the focused button when the Enter key is pressed
         (previously we were submitting forms directly in some cases).

         Reviewed by Adele.

         Test: fast/forms/enter-clicks-buttons.html

         * html/HTMLButtonElement.cpp:
         (WebCore::HTMLButtonElement::defaultEventHandler): Don't do anything
         fancy when Enter is pressed on a <button type=button> -- just send a
         click event like we do for other button types.
         * html/HTMLInputElement.cpp:
         (WebCore::HTMLInputElement::defaultEventHandler): Treat type=button
         the same way we treat type=submit and type=reset: just send a click
         event when Enter is pressed.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@27652 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/forms/enter-clicks-buttons-expected.txt [new file with mode: 0644]
LayoutTests/fast/forms/enter-clicks-buttons.html [new file with mode: 0644]
WebCore/ChangeLog
WebCore/html/HTMLButtonElement.cpp
WebCore/html/HTMLInputElement.cpp

index 8a93c4d..0ccc94f 100644 (file)
@@ -1,3 +1,12 @@
+2007-11-08  Adam Roben  <aroben@apple.com>
+
+        Add a test for <rdar://5483519> Pressing Enter on selected buttons should fire onclick
+
+        Reviewed by Adele.
+
+        * fast/forms/enter-clicks-buttons-expected.txt: Added.
+        * fast/forms/enter-clicks-buttons.html: Added.
+
 2007-11-09  Dan Bernstein  <mitz@apple.com>
 
         Reviewed by Antti Koivisto.
diff --git a/LayoutTests/fast/forms/enter-clicks-buttons-expected.txt b/LayoutTests/fast/forms/enter-clicks-buttons-expected.txt
new file mode 100644 (file)
index 0000000..5a69ef2
--- /dev/null
@@ -0,0 +1,61 @@
+Test for <rdar://5483519> Pressing Enter on selected buttons should fire onclick
+
+In a form:
+
+button type="button"  button type="submit"  button type="reset"       
+Outside a form:
+
+button type="button" button type="submit" button type="reset"    
+
+
+Sending Enter keypresses...
+
+
+Looping over 6 button elements...
+
+click: BUTTON type=button in form
+click: BUTTON type=submit in form
+submit: FORM
+click: BUTTON type=reset in form
+click: BUTTON type=button
+click: BUTTON type=submit
+click: BUTTON type=reset
+
+Looping over 8 input elements...
+
+click: INPUT type=submit in form
+submit: FORM
+click: INPUT type=button in form
+click: INPUT type=submit in form
+submit: FORM
+click: INPUT type=reset in form
+click: INPUT type=button
+click: INPUT type=submit
+click: INPUT type=reset
+
+
+Sending U+0020 keypresses...
+
+
+Looping over 6 button elements...
+
+click: BUTTON type=button in form
+click: BUTTON type=submit in form
+submit: FORM
+click: BUTTON type=reset in form
+click: BUTTON type=button
+click: BUTTON type=submit
+click: BUTTON type=reset
+
+Looping over 8 input elements...
+
+click: INPUT type=checkbox in form
+click: INPUT type=button in form
+click: INPUT type=submit in form
+submit: FORM
+click: INPUT type=reset in form
+click: INPUT type=checkbox
+click: INPUT type=button
+click: INPUT type=submit
+click: INPUT type=reset
+
diff --git a/LayoutTests/fast/forms/enter-clicks-buttons.html b/LayoutTests/fast/forms/enter-clicks-buttons.html
new file mode 100644 (file)
index 0000000..a2d1376
--- /dev/null
@@ -0,0 +1,66 @@
+<script>
+function log(msg)
+{
+    document.getElementById('console').appendChild(document.createTextNode(msg + '\n'));
+}
+
+function nodeIsChildOfForm(node)
+{
+    return node.parentNode && node.parentNode.tagName == 'FORM';
+}
+
+function description(node)
+{
+    return node.nodeName + (node.type ? ' type=' + node.type : '') + (nodeIsChildOfForm(node) ? ' in form' : '');
+}
+
+function logEvent(event)
+{
+    var type = event.target.type;
+    log(event.type + ': ' + description(event.target));
+}
+
+function loaded()
+{
+    if (window.layoutTestController)
+        layoutTestController.dumpAsText();
+
+    var keys = ['Enter', 'U+0020'];
+    var tagNames = ['button', 'input'];
+
+    for (var i in keys) {
+        log('\n\nSending ' + keys[i] + ' keypresses...\n');
+        for (var j in tagNames) {
+            var elements = document.getElementsByTagName(tagNames[j]);
+            log('\nLooping over ' + elements.length + ' ' + tagNames[j] + ' elements...\n');
+            for (var k = 0; k < elements.length; ++k) {
+                var event = elements[k].ownerDocument.createEvent("KeyboardEvent");
+                event.initKeyboardEvent("keypress", true, true, elements[k].ownerDocument.defaultView, keys[i], 0, false, false, false, false, false);
+                elements[k].dispatchEvent(event);
+            }
+        }
+    }
+}
+</script>
+<body onload="loaded()">
+<p>Test for &lt;<a href="rdar://5483519">rdar://5483519</a>&gt; Pressing Enter on selected buttons should fire onclick</p>
+<p>In a form:</p>
+<form onsubmit="logEvent(event); return false">
+    <button type="button" onclick="logEvent(event)">button type="button"</button>
+    <button type="submit" onclick="logEvent(event)">button type="submit"</button>
+    <button type="reset" onclick="logEvent(event)">button type="reset"</button>
+    <input type="checkbox" onclick="logEvent(event)" value="input type='checkbox'">
+    <input type="button" onclick="logEvent(event)" value="input type='button'">
+    <input type="submit" onclick="logEvent(event)" value="input type='submit'">
+    <input type="reset" onclick="logEvent(event)" value="input type='reset'">
+</form>
+<p>Outside a form:</p>
+<button type="button" onclick="logEvent(event)">button type="button"</button>
+<button type="submit" onclick="logEvent(event)">button type="submit"</button>
+<button type="reset" onclick="logEvent(event)">button type="reset"</button>
+<input type="checkbox" onclick="logEvent(event)" value="input type='checkbox'">
+<input type="button" onclick="logEvent(event)" value="input type='button'">
+<input type="submit" onclick="logEvent(event)" value="input type='submit'">
+<input type="reset" onclick="logEvent(event)" value="input type='reset'">
+
+<pre id='console'></pre>
index ed73f16..8742b83 100644 (file)
@@ -1,3 +1,24 @@
+2007-11-08  Adam Roben  <aroben@apple.com>
+
+        Fix <rdar://5483519> Pressing Enter on selected buttons should fire onclick
+
+        We now match the behavior of Firefox and IE, which is to always just
+        send a click event to the focused button when the Enter key is pressed
+        (previously we were submitting forms directly in some cases).
+
+        Reviewed by Adele.
+
+        Test: fast/forms/enter-clicks-buttons.html
+
+        * html/HTMLButtonElement.cpp:
+        (WebCore::HTMLButtonElement::defaultEventHandler): Don't do anything
+        fancy when Enter is pressed on a <button type=button> -- just send a
+        click event like we do for other button types.
+        * html/HTMLInputElement.cpp:
+        (WebCore::HTMLInputElement::defaultEventHandler): Treat type=button
+        the same way we treat type=submit and type=reset: just send a click
+        event when Enter is pressed.
+
 2007-11-09  Dan Bernstein  <mitz@apple.com>
 
         Reviewed by Antti Koivisto.
index 20c5822..3dd3439 100644 (file)
@@ -94,13 +94,6 @@ void HTMLButtonElement::defaultEventHandler(Event* evt)
     if (evt->type() == keypressEvent && evt->isKeyboardEvent()) {
         String key = static_cast<KeyboardEvent*>(evt)->keyIdentifier();
 
-        // Do the same things <input type=button/reset/submit> would do.
-        if (key == "Enter" && m_type == BUTTON) {
-            if (form())
-                form()->submitClick(evt);
-            evt->setDefaultHandled();
-            return;
-        }
         if (key == "Enter" || key == "U+0020") {
             dispatchSimulatedClick(evt);
             evt->setDefaultHandled();
index 9620b13..d906e78 100644 (file)
@@ -1202,7 +1202,6 @@ void HTMLInputElement::defaultEventHandler(Event* evt)
 
         if (key == "Enter") {
             switch (inputType()) {
-                case BUTTON:
                 case CHECKBOX:
                 case HIDDEN:
                 case ISINDEX:
@@ -1213,6 +1212,7 @@ void HTMLInputElement::defaultEventHandler(Event* evt)
                     // Simulate mouse click on the default form button for enter for these types of elements.
                     clickDefaultFormButton = true;
                     break;
+                case BUTTON:
                 case FILE:
                 case IMAGE:
                 case RESET: