From e2fac37345dbb42553d1f1217aa6af3eb4b552a4 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Fri, 11 May 2012 12:09:05 +0100
Subject: [PATCH] Fix IE8 not submitting the password field on the enter key.

issue #1376
---
 .../dialog/views/test_template_with_input.ejs |  4 ++-
 .../static/shared/modules/page_module.js      | 11 +++++++
 .../test/cases/shared/modules/page_module.js  | 31 ++++++++++++++++++-
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/resources/static/dialog/views/test_template_with_input.ejs b/resources/static/dialog/views/test_template_with_input.ejs
index 86d4ffa2a..ff302b14f 100644
--- a/resources/static/dialog/views/test_template_with_input.ejs
+++ b/resources/static/dialog/views/test_template_with_input.ejs
@@ -2,5 +2,7 @@
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
-<input id="templateInput" type="text" value="" />
+<form>
+  <input id="templateInput" type="text" value="" />
+</form>
 
diff --git a/resources/static/shared/modules/page_module.js b/resources/static/shared/modules/page_module.js
index 36aa78c7c..54b570841 100644
--- a/resources/static/shared/modules/page_module.js
+++ b/resources/static/shared/modules/page_module.js
@@ -15,6 +15,16 @@ BrowserID.Modules.PageModule = (function() {
       cancelEvent = helpers.cancelEvent,
       mediator = bid.Mediator;
 
+   function onKeypress(event) {
+    if (event.which === 13) {
+      // IE8 does not trigger the submit event when hitting enter. Submit the
+      // form if the key press was an enter and prevent the default action so
+      // the form is not submitted twice.
+      event.preventDefault();
+      this.submit();
+    }
+   }
+
    function onSubmit() {
      if (!dom.hasClass("body", "submit_disabled") && this.validate()) {
        this.submit();
@@ -59,6 +69,7 @@ BrowserID.Modules.PageModule = (function() {
       self.options = options || {};
 
       self.bind("form", "submit", cancelEvent(onSubmit));
+      self.bind("input", "keypress", onKeypress);
     },
 
     stop: function() {
diff --git a/resources/static/test/cases/shared/modules/page_module.js b/resources/static/test/cases/shared/modules/page_module.js
index 7baac2497..0544d20f0 100644
--- a/resources/static/test/cases/shared/modules/page_module.js
+++ b/resources/static/test/cases/shared/modules/page_module.js
@@ -198,6 +198,35 @@
     $("body").removeClass("submit_disabled");
     controller.onSubmit();
     equal(submitCalled, true, "submit permitted to complete");
-  })
+  });
+
+  test("form is submitted once 'enter keypress' event", function() {
+    createController();
+    controller.renderDialog("test_template_with_input", {
+      title: "Test title",
+      message: "Test message"
+    });
+
+    controller.start();
+
+
+    var submitCalled = 0;
+    controller.submit = function() {
+      submitCalled++;
+    };
+
+    // synthesize the entire series of key* events so we replicate the behavior
+    // of keyboard interaction.
+    var e = jQuery.Event("keydown", { keyCode: 13, which: 13 });
+    $("#templateInput").trigger(e);
+
+    var e = jQuery.Event("keyup", { keyCode: 13, which: 13 });
+    $("#templateInput").trigger(e);
+
+    var e = jQuery.Event("keypress", { keyCode: 13, which: 13 });
+    $("#templateInput").trigger(e);
+
+    equal(submitCalled, 1, "submit called a single time");
+  });
 }());
 
-- 
GitLab