diff --git a/resources/static/dialog/views/test_template_with_input.ejs b/resources/static/dialog/views/test_template_with_input.ejs index 86d4ffa2a8d0dfcc8d44e6b930926dd6a4f887e1..ff302b14f730e33cf169df428bddcf4f00cef453 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 36aa78c7c29a72aae98d2d6552b08100467c54b2..54b570841d4f0b3bb250039939ec26e0f173c1f2 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 7baac2497bae62c255f945e57fc8a260e7201ee8..0544d20f0e97f9dd9e4bce4a2e6fdc2e6350ba32 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"); + }); }());