LayoutTests:

        Reviewed by Beth Dakin.
        
        Tests for the change and blur events in input elements.

        * fast/events/onchange-passwordfield-expected.txt: Added.
        * fast/events/onchange-passwordfield.html: Added.
        * fast/events/onchange-searchfield-expected.txt: Added.
        * fast/events/onchange-searchfield.html: Added.
        * fast/events/onchange-textfield-expected.txt: Added.
        * fast/events/onchange-textfield.html: Added.

WebCore:

        Reviewed by Beth Dakin.

        Fixed <rdar://problem/4870551> 9A320: <input type="text"> no longer 
        dispatches onchange event in response to enter key
        
        To match our old behavior and FF, we need to dispatch onchange in response to the
        ENTER key. The strategy here is just to dispatch a blur event, since that's how
        the search field always worked, and the DOM spec says onchange only fires
        as a precursor to blur.

        * ChangeLog:
        * html/HTMLInputElement.cpp:
        (WebCore::HTMLInputElement::defaultEventHandler):



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@18073 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 8bd9730..3e96514 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,16 @@
+2006-12-07  Geoffrey Garen  <ggaren@apple.com>
+
+        Reviewed by Beth Dakin.
+        
+        Tests for the change and blur events in input elements.
+
+        * fast/events/onchange-passwordfield-expected.txt: Added.
+        * fast/events/onchange-passwordfield.html: Added.
+        * fast/events/onchange-searchfield-expected.txt: Added.
+        * fast/events/onchange-searchfield.html: Added.
+        * fast/events/onchange-textfield-expected.txt: Added.
+        * fast/events/onchange-textfield.html: Added.
+
 2006-12-07  Anders Carlsson  <acarlsson@apple.com>
 
         Reviewed by Darin.
diff --git a/LayoutTests/fast/events/onchange-passwordfield-expected.txt b/LayoutTests/fast/events/onchange-passwordfield-expected.txt
new file mode 100644
index 0000000..2ecee15
--- /dev/null
+++ b/LayoutTests/fast/events/onchange-passwordfield-expected.txt
@@ -0,0 +1,10 @@
+This test verifies that the ENTER key fires the change and then the blur event.
+
+
+PASS: change event fired.
+
+PASS: blur event fired.
+
+PASS: blur event fired last.
+
+
diff --git a/LayoutTests/fast/events/onchange-passwordfield.html b/LayoutTests/fast/events/onchange-passwordfield.html
new file mode 100644
index 0000000..aa2de1c
--- /dev/null
+++ b/LayoutTests/fast/events/onchange-passwordfield.html
@@ -0,0 +1,39 @@
+<p>This test verifies that the ENTER key fires the change and then the blur event.</p>
+<hr>
+<input id="input" type="password" onchange="changeHandler()" onblur="blurHandler()"></input>
+<pre id="console"></pre>
+
+<script>
+function log(s)
+{
+    document.getElementById('console').appendChild(document.createTextNode(s + "\n"));
+}
+
+function changeHandler()
+{
+    lastEvent = "change";
+    log ('PASS: change event fired.\n');
+}
+
+function blurHandler()
+{
+    lastEvent = "blur";
+    log ('PASS: blur event fired.\n');
+}
+
+if (window.layoutTestController)
+    layoutTestController.dumpAsText();
+
+// change the field
+document.getElementById('input').focus();
+document.execCommand("InsertText", false, "foo bar baz");
+
+// hit enter
+var enterEvent = document.createEvent("KeyboardEvents");
+enterEvent.initKeyboardEvent("keypress", true, false, window, "Enter", 0, false, false, false, false, false); // This is not at all like pulling teeth
+input.dispatchEvent(enterEvent);
+
+// test that blur happened last
+if (lastEvent == "blur")
+    log('PASS: blur event fired last.\n');
+</script>
diff --git a/LayoutTests/fast/events/onchange-searchfield-expected.txt b/LayoutTests/fast/events/onchange-searchfield-expected.txt
new file mode 100644
index 0000000..6a94c79
--- /dev/null
+++ b/LayoutTests/fast/events/onchange-searchfield-expected.txt
@@ -0,0 +1,8 @@
+This test verifies that the ENTER key fires the change and then the blur event.
+
+
+PASS: blur event fired.
+
+PASS: blur event fired last.
+
+
diff --git a/LayoutTests/fast/events/onchange-searchfield.html b/LayoutTests/fast/events/onchange-searchfield.html
new file mode 100644
index 0000000..07d4bdf
--- /dev/null
+++ b/LayoutTests/fast/events/onchange-searchfield.html
@@ -0,0 +1,39 @@
+<p>This test verifies that the ENTER key fires the change and then the blur event.</p>
+<hr>
+<input id="input" type="search" onchange="changeHandler()" onblur="blurHandler()"></input>
+<pre id="console"></pre>
+
+<script>
+function log(s)
+{
+    document.getElementById('console').appendChild(document.createTextNode(s + "\n"));
+}
+
+function changeHandler()
+{
+    window.lastEvent = "change";
+    log ('PASS: change event fired.\n');
+}
+
+function blurHandler()
+{
+    window.lastEvent = "blur";
+    log ('PASS: blur event fired.\n');
+}
+
+if (window.layoutTestController)
+    layoutTestController.dumpAsText();
+
+// change the field
+document.getElementById('input').focus();
+document.execCommand("InsertText", false, "foo bar baz"); // Won't work until searchfield becomes a non-AppKit control
+
+// hit enter
+var enterEvent = document.createEvent("KeyboardEvents");
+enterEvent.initKeyboardEvent("keypress", true, false, window, "Enter", 0, false, false, false, false, false); // This is not at all like pulling teeth
+input.dispatchEvent(enterEvent);
+
+// test that blur happened last
+if (window.lastEvent == "blur")
+    log('PASS: blur event fired last.\n');
+</script>
diff --git a/LayoutTests/fast/events/onchange-textfield-expected.txt b/LayoutTests/fast/events/onchange-textfield-expected.txt
new file mode 100644
index 0000000..2ecee15
--- /dev/null
+++ b/LayoutTests/fast/events/onchange-textfield-expected.txt
@@ -0,0 +1,10 @@
+This test verifies that the ENTER key fires the change and then the blur event.
+
+
+PASS: change event fired.
+
+PASS: blur event fired.
+
+PASS: blur event fired last.
+
+
diff --git a/LayoutTests/fast/events/onchange-textfield.html b/LayoutTests/fast/events/onchange-textfield.html
new file mode 100644
index 0000000..103433c
--- /dev/null
+++ b/LayoutTests/fast/events/onchange-textfield.html
@@ -0,0 +1,39 @@
+<p>This test verifies that the ENTER key fires the change and then the blur event.</p>
+<hr>
+<input id="input" type="text" onchange="changeHandler()" onblur="blurHandler()"></input>
+<pre id="console"></pre>
+
+<script>
+function log(s)
+{
+    document.getElementById('console').appendChild(document.createTextNode(s + "\n"));
+}
+
+function changeHandler()
+{
+    window.lastEvent = "change";
+    log ('PASS: change event fired.\n');
+}
+
+function blurHandler()
+{
+    window.lastEvent = "blur";
+    log ('PASS: blur event fired.\n');
+}
+
+if (window.layoutTestController)
+    layoutTestController.dumpAsText();
+    
+// change the field
+document.getElementById('input').focus();
+document.execCommand("InsertText", false, "foo bar baz");
+
+// hit enter
+var enterEvent = document.createEvent("KeyboardEvents");
+enterEvent.initKeyboardEvent("keypress", true, false, window, "Enter", 0, false, false, false, false, false); // This is not at all like pulling teeth
+input.dispatchEvent(enterEvent);
+
+// test that blur happened last
+if (window.lastEvent == "blur")
+    log('PASS: blur event fired last.\n');
+</script>
diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index 3840f55..46099a3 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,19 @@
+2006-12-07  Geoffrey Garen  <ggaren@apple.com>
+
+        Reviewed by Beth Dakin.
+
+        Fixed <rdar://problem/4870551> 9A320: <input type="text"> no longer 
+        dispatches onchange event in response to enter key
+        
+        To match our old behavior and FF, we need to dispatch onchange in response to the
+        ENTER key. The strategy here is just to dispatch a blur event, since that's how
+        the search field always worked, and the DOM spec says onchange only fires
+        as a precursor to blur.
+
+        * ChangeLog:
+        * html/HTMLInputElement.cpp:
+        (WebCore::HTMLInputElement::defaultEventHandler):
+
 2006-12-07  Adam Roben  <aroben@apple.com>
 
         Reviewed by Oliver.
diff --git a/WebCore/html/HTMLInputElement.cpp b/WebCore/html/HTMLInputElement.cpp
index a1e89cb..8cf3bb8 100644
--- a/WebCore/html/HTMLInputElement.cpp
+++ b/WebCore/html/HTMLInputElement.cpp
@@ -1340,14 +1340,15 @@
         if (clickElement) {
             dispatchSimulatedClick(evt);
             evt->setDefaultHandled();
-        } else if (clickDefaultFormButton && (inputType() != SEARCH || form())) {
+        } else if (clickDefaultFormButton) {
+            blur();
+            // Form may never have been present, or may have been destroyed by the blur event.
             if (form()) {
-                blur();
-                // Make sure the form hasn't been destroyed during the blur.
-                if (form())
-                    form()->submitClick(evt);
+                form()->submitClick(evt);
+                evt->setDefaultHandled();
+            } else if (inputType() != SEARCH) {
+                evt->setDefaultHandled();
             }
-            evt->setDefaultHandled();
         }
     }