Fix for passing jQuery constructor a DOM NodeList in Safari and an object or embed...
[jquery.git] / src / jquery / jquery.js
index 65f8423..aa2582a 100644 (file)
@@ -31,7 +31,7 @@ window.undefined = window.undefined;
 var jQuery = function(a,c) {
 
        // Shortcut for document ready (because $(document).each() is silly)
-       if ( a && typeof a == "function" && jQuery.fn.ready )
+       if ( a && typeof a == "function" && jQuery.fn.ready && !a.nodeType && a[0] == undefined ) // Safari reports typeof on DOM NodeLists as a function
                return jQuery(document).ready(a);
 
        // Make sure that a selection was provided
@@ -1565,14 +1565,12 @@ jQuery.extend({
                                // Go to html and back, then peel off extra wrappers
                                div.innerHTML = wrap[1] + s + wrap[2];
                                while ( wrap[0]-- ) div = div.firstChild;
-                               
-                               // Have to loop through the childNodes here to 
-                               // prevent a Safari crash with text nodes and /n characters
-                               for ( var j = 0; j < div.childNodes.length; j++ )
-                                       r.push( div.childNodes[j] );
+                               arg = div.childNodes;
                        } 
-                       else if ( arg.length != undefined && !arg.nodeType ) // Handles Array, jQuery, DOM NodeList collections
-                               for ( var n = 0; n < arg.length; n++ )
+                       
+                       
+                       if ( arg.length != undefined && ( (jQuery.browser.safari && typeof arg == 'function') || !arg.nodeType ) ) // Safari reports typeof on a DOM NodeList to be a function
+                               for ( var n = 0; n < arg.length; n++ ) // Handles Array, jQuery, DOM NodeList collections
                                        r.push(arg[n]);
                        else
                                r.push( arg.nodeType ? arg : document.createTextNode(arg.toString()) );
@@ -2271,7 +2269,7 @@ jQuery.extend({
                handle: function(event) {
                        if ( typeof jQuery == "undefined" ) return false;
 
-                       event = jQuery.event.fix( event );
+                       event = jQuery.event.fix( event || window.event || {} ); // Empty object is for triggered events with no data
 
                        // If no correct event was found, fail
                        if ( !event ) return false;
@@ -2291,26 +2289,39 @@ jQuery.extend({
                                }
                        }
 
+                       // Clean up added properties in IE to prevent memory leak
+                       if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = null;
+
                        return returnValue;
                },
 
                fix: function(event) {
+                       // check IE
                        if(jQuery.browser.msie) {
-                               event = window.event;
+                               // fix target property
+                               event.target = event.srcElement;
+                               
+                       // check safari and if target is a textnode
+                       } else if(jQuery.browser.safari && event.target.nodeType == 3) {
+                               // target is readonly, clone the event object
+                               event = jQuery.extend({}, event);
+                               // get parentnode from textnode
+                               event.target = event.target.parentNode;
+                       }
+                       
+                       // fix preventDefault and stopPropagation
+                       if (!event.preventDefault)
                                event.preventDefault = function() {
                                        this.returnValue = false;
                                };
+                               
+                       if (!event.stopPropagation)
                                event.stopPropagation = function() {
                                        this.cancelBubble = true;
                                };
-                               event.target = event.srcElement;
-                       } else if(jQuery.browser.safari && event.target.nodeType == 3) {
-                               event = jQuery.extend({}, event);
-                               event.target = event.target.parentNode;
-                       }
+                       
                        return event;
                }
-
        }
 });
 
@@ -3089,7 +3100,7 @@ jQuery.macros = {
                 *
                 * It only returns the immediately previous sibling, not all previous siblings.
                 *
-                * @example $("p").previous(".selected")
+                * @example $("p").prev(".selected")
                 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
                 * @result [ <div><span>Hello</span></div> ]
                 *
@@ -3399,29 +3410,15 @@ jQuery.macros = {
                 * } )
                 * @desc Stop only an event from bubbling by using the stopPropagation method.
                 *
-                * @example $("form").bind( "submit", function(event) {
-                *   // do something after submit
-                * }, 1 )
-                * @desc Executes the function only on the first submit event and removes it afterwards
-                *
                 * @name bind
                 * @type jQuery
                 * @param String type An event type
                 * @param Function fn A function to bind to the event on each of the set of matched elements
-                * @param Number amount An optional amount of times to execute the bound function
                 * @cat Events
                 */
-               bind: function( type, fn, amount ) {
+               bind: function( type, fn ) {
                        if ( fn.constructor == String )
                                fn = new Function("e", ( !fn.indexOf(".") ? "jQuery(this)" : "return " ) + fn);
-                       if( amount > 0 ) {
-                               var element = this, handler = fn, count = 0;
-                               fn = function(e) {
-                                       if( ++count >= amount )
-                                               jQuery(element).unbind(type, fn);
-                                       handler.apply(element, [e]);
-                               };
-                       }
                        jQuery.event.add( this, type, fn );
                },