From 748b3da785faf667164520242efed066053dae86 Mon Sep 17 00:00:00 2001 From: Wouter Groeneveld Date: Tue, 20 Mar 2012 20:28:06 +0100 Subject: [PATCH] fix for checkbox click event which now sets the checked state correctly. --- src/test/javascript/lib/env.utils.js | 13 ++++++++- src/test/javascript/specs/envUtilsSpec.js | 28 +++++++++++++++++++ .../javascript/specs/fixtures/formevents.html | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/test/javascript/lib/env.utils.js b/src/test/javascript/lib/env.utils.js index aea41d1..4d3fef1 100755 --- a/src/test/javascript/lib/env.utils.js +++ b/src/test/javascript/lib/env.utils.js @@ -57,7 +57,8 @@ * 2) Fix CSS2Properties support for parsing style attributes: get from raw node context. * 3) Fix CSS2Properties support for setting values: all properties have the same objmaps, wtf? * 4) Fix focus() which sets document.activeElement correctly for jQuery:focus - */ + * 5) Fix Input click() behavior for checkboxes. Warning: jQ's click() <-> DOM's click (checked value too late set)! + **/ (function() { var oldEnvjsUriFn = Envjs.uri; @@ -91,6 +92,16 @@ })(HTMLElement.prototype); + (function(input) { + var oldClick = input.prototype.click; + input.prototype.click = function() { + if(this.type === "checkbox") { + this.checked = !this.checked; + } + oldClick.apply(this, arguments); + } + })(HTMLInputElement); + (function(Input, Textarea, document) { var activeElement; function fixFocusForPrototype(element) { diff --git a/src/test/javascript/specs/envUtilsSpec.js b/src/test/javascript/specs/envUtilsSpec.js index 2fc2c2d..35d02bc 100755 --- a/src/test/javascript/specs/envUtilsSpec.js +++ b/src/test/javascript/specs/envUtilsSpec.js @@ -37,6 +37,34 @@ describe("envjs fixes", function() { }); }); }); + + describe("Checkbox click events", function() { + it("should set the state of the checkbox to checked if not checked when clicked", function() { + $("#checkbox").click(); + expect($("#checkbox")).toBeChecked(); + }); + + it("should set the state of the checkbox to unchecked if checked when clicked", function() { + $("#checkbox").attr('checked', true); + $("#checkbox").click(); + expect($("#checkbox")).not.toBeChecked(); + }); + + it("should still fire the click event after clicking on a checkbox", function() { + var clicked = false; + $("#checkbox").click(function() { + clicked = true; + }); + + waitsFor(function() { + return clicked; + }); + $("#checkbox").click(); + runs(function() { + expect(clicked).toBeTruthy(); + }); + }); + }); }); describe("CSS2 style property support for parsing style attributes", function() { diff --git a/src/test/javascript/specs/fixtures/formevents.html b/src/test/javascript/specs/fixtures/formevents.html index c54291f..79a9adf 100755 --- a/src/test/javascript/specs/fixtures/formevents.html +++ b/src/test/javascript/specs/fixtures/formevents.html @@ -1,6 +1,7 @@
+