Added jQuery.browser.version (see ticket #1101). Works in all browsers that we support.
[jquery.git] / src / jquery / jquery.js
index 489522f..edcaee2 100644 (file)
@@ -817,9 +817,9 @@ jQuery.fn = jQuery.prototype = {
         * @cat DOM/Traversing
         */
        find: function(t) {
-               return this.pushStack( jQuery.map( this, function(a){
+               return this.pushStack( jQuery.unique( jQuery.map( this, function(a){
                        return jQuery.find(t,a);
-               }), t );
+               }) ), t );
        },
 
        /**
@@ -1177,6 +1177,10 @@ jQuery.fn = jQuery.prototype = {
 /**
  * Extend one object with one or more others, returning the original,
  * modified, object. This is a great utility for simple inheritance.
+ *
+ * There is also an optional collision resolution function. Any time the target and
+ * merged object both contain a key this function is called. This may be used to create
+ * a recursive merge. (See example) Unless you know what this is, you probably don't care.
  * 
  * @example var settings = { validate: false, limit: 5, name: "foo" };
  * var options = { validate: true, name: "bar" };
@@ -1190,8 +1194,19 @@ jQuery.fn = jQuery.prototype = {
  * @result settings == { validate: true, limit: 5, name: "bar" }
  * @desc Merge defaults and options, without modifying the defaults
  *
+ * @example var defaults = { validate: false, limit: 5, name: "foo", nested: {depth: false} };
+ * var options = { validate: true, name: "bar", nested: {depth: true} };
+ * var collision_resolver_fn = function(target, mergee) {
+ *   // combine nested Objects, in this case the object being merged takes priority
+ *   return jQuery.extend({}, target, mergee);
+ * }
+ * var settings = jQuery.extend({}, collision_resolver_fn, defaults, options);
+ * @result settings == { validate: true, limit: 5, name: "bar" }
+ * @desc Recursively merge defaults and options, without modifying the defaults
+ *
  * @name $.extend
  * @param Object target The object to extend
+ * @param Function (optional) collision resolution function. Hook to extend the merging behavior of $.extend(). See example.
  * @param Object prop1 The object that will be merged into the first.
  * @param Object propN (optional) More objects to merge into the first
  * @type Object
@@ -1199,18 +1214,26 @@ jQuery.fn = jQuery.prototype = {
  */
 jQuery.extend = jQuery.fn.extend = function() {
        // copy reference to target object
-       var target = arguments[0],
+       var resolver, prop, target = arguments[0],
                a = 1;
-
+       
        // extend jQuery itself if only one argument is passed
        if ( arguments.length == 1 ) {
                target = this;
                a = 0;
+       } else if (jQuery.isFunction(arguments[a])) {
+         resolver = arguments[a++];
        }
-       var prop;
+
        while (prop = arguments[a++])
                // Extend the base object
-               for ( var i in prop ) target[i] = prop[i];
+               for ( var i in prop ) {
+                 if (resolver && target[i] && prop[i]) {
+                   target[i] = resolver(target[i], prop[i]);
+           } else {
+                   target[i] = prop[i];
+                 }
+         }
 
        // Return the modified object
        return target;
@@ -1345,10 +1368,7 @@ jQuery.extend({
 
                // internal only, use is(".class")
                has: function( t, c ) {
-                       t = t.className || t;
-                       // escape regex characters
-                       c = c.replace(/([\.\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
-                       return t && new RegExp("(^|\\s)" + c + "(\\s|$)").test( t );
+                       return jQuery.inArray( c, (t.className || t).toString().split(/\s+/) ) > -1;
                }
        },
 
@@ -1376,7 +1396,7 @@ jQuery.extend({
                        });
 
                        jQuery.swap( e, old, function() {
-                               if (jQuery.css(e,"display") != "none") {
+                               if ( jQuery(e).is(':visible') ) {
                                        oHeight = e.offsetHeight;
                                        oWidth = e.offsetWidth;
                                } else {
@@ -1386,14 +1406,14 @@ jQuery.extend({
                                                        visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
                                                }).appendTo(e.parentNode)[0];
 
-                                       var parPos = jQuery.css(e.parentNode,"position");
-                                       if ( parPos == "" || parPos == "static" )
+                                       var parPos = jQuery.css(e.parentNode,"position") || "static";
+                                       if ( parPos == "static" )
                                                e.parentNode.style.position = "relative";
 
                                        oHeight = e.clientHeight;
                                        oWidth = e.clientWidth;
 
-                                       if ( parPos == "" || parPos == "static" )
+                                       if ( parPos == "static" )
                                                e.parentNode.style.position = "static";
 
                                        e.parentNode.removeChild(e);
@@ -1459,13 +1479,16 @@ jQuery.extend({
                         // Convert html string into DOM nodes
                        if ( typeof arg == "string" ) {
                                // Trim whitespace, otherwise indexOf won't work as expected
-                               var s = jQuery.trim(arg), div = doc.createElement("div"), tb = [];
+                               var s = jQuery.trim(arg).toLowerCase(), div = doc.createElement("div"), tb = [];
 
                                var wrap =
                                         // option or optgroup
                                        !s.indexOf("<opt") &&
                                        [1, "<select>", "</select>"] ||
                                        
+                                       !s.indexOf("<leg") &&
+                                       [1, "<fieldset>", "</fieldset>"] ||
+                                       
                                        (!s.indexOf("<thead") || !s.indexOf("<tbody") || !s.indexOf("<tfoot")) &&
                                        [1, "<table>", "</table>"] ||
                                        
@@ -1505,10 +1528,10 @@ jQuery.extend({
                                arg = jQuery.makeArray( div.childNodes );
                        }
 
-                       if ( arg.length === 0 && !jQuery.nodeName(arg, "form") )
+                       if ( arg.length === 0 && !jQuery(arg).is("form, select") )
                                return;
-                       
-                       if ( arg[0] == undefined || jQuery.nodeName(arg, "form") )
+
+                       if ( arg[0] == undefined || jQuery(arg).is("form, select") )
                                r.push( arg );
                        else
                                r = jQuery.merge( r, arg );
@@ -1629,19 +1652,27 @@ jQuery.extend({
         * @cat JavaScript
         */
        merge: function(first, second) {
-               var r = [].slice.call( first, 0 );
+               // We have to loop this way because IE & Opera overwrite the length
+               // expando of getElementsByTagName
+               for ( var i = 0; second[i]; i++ )
+                       first.push(second[i]);
+               return first;
+       },
 
-               // Now check for duplicates between the two arrays
-               // and only add the unique items
-               for ( var i = 0, sl = second.length; i < sl; i++ )
-                       // Check for duplicates
-                       if ( jQuery.inArray( second[i], r ) == -1 )
-                               // The item is unique, add it
-                               first.push( second[i] );
+       unique: function(first) {
+               var r = [], num = jQuery.mergeNum++;
 
-               return first;
+               for ( var i = 0, fl = first.length; i < fl; i++ )
+                       if ( first[i].mergeNum != num ) {
+                               first[i].mergeNum = num;
+                               r.push(first[i]);
+                       }
+
+               return r;
        },
 
+       mergeNum: 0,
+
        /**
         * Filter items out of an array, by using a filter function.
         *
@@ -1735,17 +1766,7 @@ jQuery.extend({
                        }
                }
 
-               var r = result.length ? [ result[0] ] : [];
-
-               check: for ( var i = 1, rl = result.length; i < rl; i++ ) {
-                       for ( var j = 0; j < i; j++ )
-                               if ( result[i] == r[j] )
-                                       continue check;
-
-                       r.push( result[i] );
-               }
-
-               return r;
+               return result;
        }
 });
 
@@ -1786,6 +1807,7 @@ new function() {
 
        // Figure out what browser is being used
        jQuery.browser = {
+               version: b.match(/.+[xiae][\/ ]([\d.]+)/)[1],
                safari: /webkit/.test(b),
                opera: /opera/.test(b),
                msie: /msie/.test(b) && !/opera/.test(b),