Commit bb38f706 authored by jaubourg's avatar jaubourg

Do not iterate over functions. Fixes #12665. Unit tests added. Thanks to...

Do not iterate over functions. Fixes #12665. Unit tests added. Thanks to @kselden for finding the bug.
parent cfe95a86
...@@ -93,8 +93,10 @@ jQuery.Callbacks = function( options ) { ...@@ -93,8 +93,10 @@ jQuery.Callbacks = function( options ) {
(function add( args ) { (function add( args ) {
jQuery.each( args, function( _, arg ) { jQuery.each( args, function( _, arg ) {
var type = jQuery.type( arg ); var type = jQuery.type( arg );
if ( type === "function" && ( !options.unique || !self.has( arg ) ) ) { if ( type === "function" ) {
list.push( arg ); if ( !options.unique || !self.has( arg ) ) {
list.push( arg );
}
} else if ( arg && arg.length && type !== "string" ) { } else if ( arg && arg.length && type !== "string" ) {
// Inspect recursively // Inspect recursively
add( arg ); add( arg );
......
...@@ -12,17 +12,17 @@ var output, ...@@ -12,17 +12,17 @@ var output,
outputB = addToOutput( "B" ), outputB = addToOutput( "B" ),
outputC = addToOutput( "C" ), outputC = addToOutput( "C" ),
tests = { tests = {
"": "XABC X XABCABCC X XBB X XABA X", "": "XABC X XABCABCC X XBB X XABA X XX",
"once": "XABC X X X X X XABA X", "once": "XABC X X X X X XABA X XX",
"memory": "XABC XABC XABCABCCC XA XBB XB XABA XC", "memory": "XABC XABC XABCABCCC XA XBB XB XABA XC XX",
"unique": "XABC X XABCA X XBB X XAB X", "unique": "XABC X XABCA X XBB X XAB X X",
"stopOnFalse": "XABC X XABCABCC X XBB X XA X", "stopOnFalse": "XABC X XABCABCC X XBB X XA X XX",
"once memory": "XABC XABC X XA X XA XABA XC", "once memory": "XABC XABC X XA X XA XABA XC XX",
"once unique": "XABC X X X X X XAB X", "once unique": "XABC X X X X X XAB X X",
"once stopOnFalse": "XABC X X X X X XA X", "once stopOnFalse": "XABC X X X X X XA X XX",
"memory unique": "XABC XA XABCA XA XBB XB XAB XC", "memory unique": "XABC XA XABCA XA XBB XB XAB XC X",
"memory stopOnFalse": "XABC XABC XABCABCCC XA XBB XB XA X", "memory stopOnFalse": "XABC XABC XABCABCCC XA XBB XB XA X XX",
"unique stopOnFalse": "XABC X XABCA X XBB X XA X" "unique stopOnFalse": "XABC X XABCA X XBB X XA X X"
}, },
filters = { filters = {
"no filter": undefined, "no filter": undefined,
...@@ -32,7 +32,7 @@ var output, ...@@ -32,7 +32,7 @@ var output,
}; };
} }
}; };
function showFlags( flags ) { function showFlags( flags ) {
if ( typeof flags === "string" ) { if ( typeof flags === "string" ) {
return '"' + flags + '"'; return '"' + flags + '"';
...@@ -60,7 +60,7 @@ jQuery.each( tests, function( strFlags, resultString ) { ...@@ -60,7 +60,7 @@ jQuery.each( tests, function( strFlags, resultString ) {
test( "jQuery.Callbacks( " + showFlags( flags ) + " ) - " + filterLabel, function() { test( "jQuery.Callbacks( " + showFlags( flags ) + " ) - " + filterLabel, function() {
expect( 20 ); expect( 21 );
// Give qunit a little breathing room // Give qunit a little breathing room
stop(); stop();
...@@ -197,6 +197,19 @@ jQuery.each( tests, function( strFlags, resultString ) { ...@@ -197,6 +197,19 @@ jQuery.each( tests, function( strFlags, resultString ) {
cblist.add( outputC ); cblist.add( outputC );
strictEqual( output, results.shift(), "Adding a callback after one returned false" ); strictEqual( output, results.shift(), "Adding a callback after one returned false" );
// Callbacks are not iterated
output = "";
function handler( tmp ) {
output += "X";
}
handler.method = function() {
output += "!";
};
cblist = jQuery.Callbacks( flags );
cblist.add( handler );
cblist.add( handler );
cblist.fire();
strictEqual( output, results.shift(), "No callback iteration" );
}); });
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment