Added in jQuery.isFunction().
[jquery.git] / src / jquery / jquery.js
index cc0af52..db65cfb 100644 (file)
@@ -33,7 +33,7 @@ var jQuery = function(a,c) {
        // HANDLE: $(function)
        // Shortcut for document ready
        // Safari reports typeof on DOM NodeLists as a function
-       if ( typeof a == "function" && !a.nodeType && a[0] == undefined )
+       if ( isFunction(a) && !a.nodeType && a[0] == undefined )
                return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a );
        
        // Handle HTML strings
@@ -154,10 +154,18 @@ var $ = jQuery;
  * });
  * @desc Executes the function when the DOM is ready to be used.
  *
+ * @example jQuery(function($) {
+ *   // Your code using failsafe $ alias here...
+ * });
+ * @desc Uses both the shortcut for $(document).ready() and the argument
+ * to write failsafe jQuery code using the $ alias, without relying on the
+ * global alias.
+ *
  * @name $
  * @param Function fn The function to execute when the DOM is ready.
  * @cat Core
  * @type jQuery
+ * @see ready(Function)
  */
 
 jQuery.fn = jQuery.prototype = {
@@ -432,7 +440,7 @@ jQuery.fn = jQuery.prototype = {
                        for ( var prop in obj )
                                jQuery.attr(
                                        type ? this.style : this,
-                                       prop, jQuery.prop(this, obj[prop])
+                                       prop, jQuery.prop(this, obj[prop], type)
                                );
                });
        },
@@ -477,16 +485,22 @@ jQuery.fn = jQuery.prototype = {
 
        /**
         * Set a single style property to a value, on all matched elements.
+        * If a number is provided, it is automatically converted into a pixel value.
         *
         * @example $("p").css("color","red");
         * @before <p>Test Paragraph.</p>
         * @result <p style="color:red;">Test Paragraph.</p>
         * @desc Changes the color of all paragraphs to red
         *
+        * @example $("p").css("left",30);
+        * @before <p>Test Paragraph.</p>
+        * @result <p style="left:30px;">Test Paragraph.</p>
+        * @desc Changes the left of all paragraphs to "30px"
+        *
         * @name css
         * @type jQuery
         * @param String key The name of the property to set.
-        * @param Object value The value to set the property to.
+        * @param String|Number value The value to set the property to.
         * @cat CSS
         */
        css: function( key, value ) {
@@ -534,7 +548,7 @@ jQuery.fn = jQuery.prototype = {
                        "textContent" : "innerText";
                        
                return e == undefined ?
-                       this.length && this[0][ type ] :
+                       jQuery.map(this, function(a){ return a[ type ]; }).join('') :
                        this.each(function(){ this[ type ] = e; });
        },
 
@@ -799,6 +813,7 @@ jQuery.fn = jQuery.prototype = {
         *
         * @name clone
         * @type jQuery
+        * @param Boolean deep (Optional) Set to false if you don't want to clone all descendant nodes, in addition to the element itself.
         * @cat DOM/Manipulation
         */
        clone: function(deep) {
@@ -849,7 +864,7 @@ jQuery.fn = jQuery.prototype = {
         */
        filter: function(t) {
                return this.pushStack(
-                       t.constructor == Function &&
+                       isFunction( t.constructor ) &&
                        jQuery.grep(this, function(el, index){
                                return t.apply(el, [index])
                        }) ||
@@ -910,7 +925,7 @@ jQuery.fn = jQuery.prototype = {
 
                        jQuery.grep(this,function(a){
                                        if ( t.constructor == Array || t.jquery )
-                                               return !jQuery.inArray( t, a );
+                                               return jQuery.inArray( t, a ) < 0;
                                        else
                                                return a != t;
                        }) );
@@ -1197,6 +1212,10 @@ jQuery.extend({
                        $ = jQuery._$;
        },
 
+       isFunction: function( fn ) {
+               return fn && typeof fn == "function";
+       },
+
        /**
         * A generic iterator function, which can be used to seemlessly
         * iterate over both objects and arrays. This function is not the same
@@ -1236,10 +1255,16 @@ jQuery.extend({
                return obj;
        },
        
-       prop: function(elem, value){
-               // Handle executable functions
-               return value.constructor == Function &&
-                       value.call( elem ) || value;
+       prop: function(elem, value, type){
+                       // Handle executable functions
+                       if ( isFunction( value ) )
+                               return value.call( elem );
+
+                       // Handle passing in a number to a CSS property
+                       if ( value.constructor == Number && type == "css" )
+                               return value + "px";
+
+                       return value;
        },
 
        className: {
@@ -1362,9 +1387,14 @@ jQuery.extend({
        
        clean: function(a) {
                var r = [];
-               
+
                for ( var i = 0, al = a.length; i < al; i++ ) {
                        var arg = a[i];
+
+                       if ( !arg ) continue;
+
+                       if ( arg.constructor == Number )
+                               arg = arg.toString();
                        
                         // Convert html string into DOM nodes
                        if ( typeof arg == "string" ) {
@@ -1415,7 +1445,7 @@ jQuery.extend({
                                arg = div.childNodes;
                        }
                        
-                       if ( arg.nodeType )
+                       if ( arg[0] == undefined )
                                r.push( arg );
                        else
                                r = jQuery.merge( r, arg );
@@ -1678,7 +1708,7 @@ jQuery.extend({
  */
  
 /*
- * Wheather the W3C compliant box model is being used.
+ * Whether the W3C compliant box model is being used.
  *
  * @property
  * @name $.boxModel