Added in some changes to class handling and some docs for jQuery.nth().
[jquery.git] / src / jquery / jquery.js
index d22d136..2ce32e1 100644 (file)
@@ -1235,28 +1235,21 @@ jQuery.extend({
        },
 
        className: {
-               add: function(o,c){
-                       if (jQuery.className.has(o,c)) return;
-                       o.className += ( o.className ? " " : "" ) + c;
+               add: function( elem, c ){
+                       if ( jQuery.className.has( elem, c ) )
+                return;
+                       elem.className += ( elem.className ? " " : "" ) + c;
                },
-               remove: function(o,c){
-                       if( !c ) {
-                               o.className = "";
-                       } else {
-                               var classes = o.className.split(" ");
-                               for(var i=0; i<classes.length; i++) {
-                                       if(classes[i] == c) {
-                                               classes.splice(i, 1);
-                                               break;
-                                       }
-                               }
-                               o.className = classes.join(' ');
-                       }
+               remove: function( elem, c ){
+            elem.className = c ?
+                jQuery.grep( elem.className.split(/\s+/), function(cur){ 
+                                   return jQuery.className.has( c, cur );      
+                }).join(' ') : "";
                },
-               has: function(e,a) {
-                       if ( e.className != undefined )
-                               e = e.className;
-                       return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
+               has: function( elem, c ){
+                       if ( elem.className != undefined )
+                               elem = elem.className;
+                       return new RegExp("(^|\\s)" + c + "(\\s|$)").test( elem );
                }
        },
 
@@ -1407,6 +1400,18 @@ jQuery.extend({
                return r;
        },
 
+       /**
+        * A handy, and fast, way to traverse in a particular direction and find
+        * a specific element.
+        *
+        * @private
+        * @name $.nth
+        * @type DOMElement
+        * @param DOMElement cur The element to search from.
+        * @param Number|String num The Nth result to match. Can be a number or a string (like 'even' or 'odd').
+        * @param String dir The direction to move in (pass in something like 'previousSibling' or 'nextSibling').
+        * @cat DOM/Traversing
+        */
        nth: function(cur,result,dir){
                result = result || 1;
                var num = 0;
@@ -1463,7 +1468,7 @@ jQuery.extend({
                        submit: "a.type=='submit'",
                        image: "a.type=='image'",
                        reset: "a.type=='reset'",
-                       button: "a.type=='button'",
+                       button: "a.type=='button'||a.nodeName=='BUTTON'",
                        input: "/input|select|textarea|button/i.test(a.nodeName)"
                },
                ".": "jQuery.className.has(a,m[2])",
@@ -1784,7 +1789,7 @@ jQuery.extend({
                "\\[ *(@)S *([!*$^=]*) *('?\"?)(.*?)\\4 *\\]",
 
                // Match: [div], [div p]
-               "(\\[)\s*(.*?)\s*\\]",
+               "(\\[)\\s*(.*?)\\s*\\]",
 
                // Match: :contains('foo')
                "(:)S\\(\"?'?([^\\)]*?)\"?'?\\)",
@@ -2045,12 +2050,16 @@ jQuery.extend({
 
                // Bind an event to an element
                // Original by Dean Edwards
-               add: function(element, type, handler) {
+               add: function(element, type, handler, data) {
                        // For whatever reason, IE has trouble passing the window object
                        // around, causing it to be cloned in the process
                        if ( jQuery.browser.msie && element.setInterval != undefined )
                                element = window;
 
+                       // if data is passed, bind to handler
+                       if( data ) 
+                               handler.data = data;
+
                        // Make sure that the function being executed has a unique ID
                        if ( !handler.guid )
                                handler.guid = this.guid++;
@@ -2090,7 +2099,9 @@ jQuery.extend({
                // Detach an event or set of events from an element
                remove: function(element, type, handler) {
                        if (element.events)
-                               if (type && element.events[type])
+                               if ( type && type.type )
+                                       delete element.events[ type.type ][ type.handler.guid ];
+                               else if (type && element.events[type])
                                        if ( handler )
                                                delete element.events[type][handler.guid];
                                        else
@@ -2135,6 +2146,11 @@ jQuery.extend({
                        args.unshift( event );
 
                        for ( var j in c ) {
+                               // Pass in a reference to the handler function itself
+                               // So that we can later remove it
+                               args[0].handler = c[j];
+                               args[0].data = c[j].data;
+
                                if ( c[j].apply( this, args ) === false ) {
                                        event.preventDefault();
                                        event.stopPropagation();
@@ -2143,7 +2159,7 @@ jQuery.extend({
                        }
 
                        // Clean up added properties in IE to prevent memory leak
-                       if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = null;
+                       if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = event.handler = event.data = null;
 
                        return returnValue;
                },
@@ -2843,35 +2859,7 @@ jQuery.macros = {
                 * Get a set of elements containing the unique ancestors of the matched
                 * set of elements (except for the root element).
                 *
-                * @example $("span").ancestors()
-                * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
-                * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
-                *
-                * @name ancestors
-                * @type jQuery
-                * @cat DOM/Traversing
-                */
-
-               /**
-                * Get a set of elements containing the unique ancestors of the matched
-                * set of elements, and filtered by an expression.
-                *
-                * @example $("span").ancestors("p")
-                * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
-                * @result [ <p><span>Hello</span></p> ]
-                *
-                * @name ancestors
-                * @type jQuery
-                * @param String expr An expression to filter the ancestors with
-                * @cat DOM/Traversing
-                */
-               ancestors: jQuery.parents,
-
-               /**
-                * Get a set of elements containing the unique ancestors of the matched
-                * set of elements (except for the root element).
-                *
-                * @example $("span").ancestors()
+                * @example $("span").parents()
                 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
                 * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
                 *
@@ -2884,7 +2872,7 @@ jQuery.macros = {
                 * Get a set of elements containing the unique ancestors of the matched
                 * set of elements, and filtered by an expression.
                 *
-                * @example $("span").ancestors("p")
+                * @example $("span").parents("p")
                 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
                 * @result [ <p><span>Hello</span></p> ]
                 *
@@ -3196,12 +3184,24 @@ jQuery.macros = {
                 * default behaviour. To stop both default action and event bubbling, your handler
                 * has to return false.
                 *
+                * In most cases, you can define your event handlers as anonymous functions
+                * (see first example). In cases where that is not possible, you can pass additional
+                * data as the second paramter (and the handler function as the third), see 
+                * second example.
+                *
                 * @example $("p").bind( "click", function() {
                 *   alert( $(this).text() );
                 * } )
                 * @before <p>Hello</p>
                 * @result alert("Hello")
                 *
+                * @example var handler = function(event) {
+                *   alert(event.data.foo);
+                * };
+                * $("p").bind( "click", {foo: "bar"}, handler)
+                * @result alert("bar")
+                * @desc Pass some additional data to the event handler.
+                *
                 * @example $("form").bind( "submit", function() { return false; } )
                 * @desc Cancel a default action and prevent it from bubbling by returning false
                 * from your function.
@@ -3220,11 +3220,12 @@ jQuery.macros = {
                 * @name bind
                 * @type jQuery
                 * @param String type An event type
+                * @param Object data (optional) Additional data passed to the event handler as event.data
                 * @param Function fn A function to bind to the event on each of the set of matched elements
                 * @cat Events
                 */
-               bind: function( type, fn ) {
-                       jQuery.event.add( this, type, fn );
+               bind: function( type, data, fn ) {
+                       jQuery.event.add( this, type, fn || data, data );
                },
 
                /**
@@ -3290,4 +3291,4 @@ jQuery.macros = {
        }
 };
 
-jQuery.init();
+jQuery.init();
\ No newline at end of file