aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Methvin <dave.methvin@gmail.com>2011-10-27 17:11:40 -0400
committerDave Methvin <dave.methvin@gmail.com>2011-10-27 17:11:40 -0400
commit5c0c86378a602fbc06535617fc5d712fa6d6e024 (patch)
tree1329f2ad0252f2f39611f5983f1cf0a0be3e55d7
parentdf4a160be7fcc234a8e17eccb1c3fd02f231bcfc (diff)
downloadjquery-5c0c86378a602fbc06535617fc5d712fa6d6e024.tar.gz
jquery-5c0c86378a602fbc06535617fc5d712fa6d6e024.zip
DRY the hover pseudo-event, get the type right, allow override.
Now with working unit test for extra correctness! If external code defines a special.hover event, we won't string-hack "hover" into "mouseenter mouseleave".
-rw-r--r--src/event.js7
-rw-r--r--test/unit/event.js22
2 files changed, 26 insertions, 3 deletions
diff --git a/src/event.js b/src/event.js
index dcbeb4b7f..509468328 100644
--- a/src/event.js
+++ b/src/event.js
@@ -26,6 +26,9 @@ var rnamespaces = /\.(.*)$/,
(!m[2] || elem.id === m[2]) &&
(!m[3] || m[3].test( elem.className ))
);
+ },
+ hoverHack = function( events ) {
+ return jQuery.event.special.hover ? events : events.replace( rhoverHack, "mouseenter$1 mouseleave$1" );
};
/*
@@ -76,7 +79,7 @@ jQuery.event = {
// Handle multiple events separated by a space
// jQuery(...).bind("mouseover mouseout", fn);
- types = types.replace( rhoverHack, "mouseover$1 mouseout$1" ).split( " " );
+ types = hoverHack(types).split( " " );
for ( t = 0; t < types.length; t++ ) {
tns = rtypenamespace.exec( types[t] ) || [];
@@ -166,7 +169,7 @@ jQuery.event = {
}
// Once for each type.namespace in types; type may be omitted
- types = ( types || "" ).replace( rhoverHack, "mouseover$1 mouseout$1" ).split(" ");
+ types = hoverHack( types || "" ).split(" ");
for ( t = 0; t < types.length; t++ ) {
tns = rtypenamespace.exec( types[t] ) || [];
type = tns[1];
diff --git a/test/unit/event.js b/test/unit/event.js
index 246bde322..10286b2b7 100644
--- a/test/unit/event.js
+++ b/test/unit/event.js
@@ -786,7 +786,9 @@ test("unbind(eventObject)", function() {
assert( 0 );
});
-test("hover()", function() {
+test("hover() and hover pseudo-event", function() {
+ expect(2);
+
var times = 0,
handler1 = function( event ) { ++times; },
handler2 = function( event ) { ++times; };
@@ -802,6 +804,24 @@ test("hover()", function() {
.mouseenter().mouseleave();
equals( times, 4, "hover handlers fired" );
+
+ var balance = 0;
+ jQuery( "#firstp" )
+ .bind("hover", function( e ) {
+ if ( e.type === "mouseenter" ) {
+ balance++;
+ } else if ( e.type === "mouseleave" ) {
+ balance--;
+ } else {
+ ok( false, "hover pseudo: unknown event type "+e.type );
+ }
+ })
+ .trigger("mouseenter")
+ .trigger("mouseleave")
+ .unbind("hover")
+ .trigger("mouseenter");
+
+ equals( balance, 0, "hover pseudo-event" );
});
test("mouseover triggers mouseenter", function() {