X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=8c27b42b137e024beda0f7f55de054e365163f35;hb=e1e631a04440c0d9ab6580ea1593286072fa7ef5;hp=4543b7228fb3dc4755d0af8c8a377917943b605d;hpb=62303ad5efa11f0524dde236c91e8cb33ca87b3e;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 4543b72..8c27b42 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -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 ( jQuery.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

Test Paragraph.

* @result

Test Paragraph.

* @desc Changes the color of all paragraphs to red * + * @example $("p").css("left",30); + * @before

Test Paragraph.

+ * @result

Test Paragraph.

+ * @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) { @@ -812,21 +827,21 @@ jQuery.fn = jQuery.prototype = { * match the specified expression(s). This method is used to narrow down * the results of a search. * - * Provide a String array of expressions to apply multiple filters at once. + * Provide a comma-separated list of expressions to apply multiple filters at once. * * @example $("p").filter(".selected") * @before

Hello

How are you?

* @result [

Hello

] * @desc Selects all paragraphs and removes those without a class "selected". * - * @example $("p").filter([".selected", ":first"]) + * @example $("p").filter(".selected, :first") * @before

Hello

Hello Again

And Again

* @result [

Hello

,

And Again

] * @desc Selects all paragraphs and removes those without class "selected" and being the first one. * * @name filter * @type jQuery - * @param String|Array expression Expression(s) to search with. + * @param String expression Expression(s) to search with. * @cat DOM/Traversing */ @@ -849,21 +864,12 @@ jQuery.fn = jQuery.prototype = { */ filter: function(t) { return this.pushStack( - t.constructor == Array && - jQuery.map(this,function(a){ - for ( var i = 0, tl = t.length; i < tl; i++ ) - if ( jQuery.filter(t[i],[a]).r.length ) - return a; - return null; + jQuery.isFunction( t ) && + jQuery.grep(this, function(el, index){ + return t.apply(el, [index]) }) || - t.constructor == Boolean && - ( t ? this.get() : [] ) || - - typeof t == "function" && - jQuery.grep( this, function(el, index) { return t.apply(el, [index]) }) || - - jQuery.filter(t,this).r ); + jQuery.multiFilter(t,this) ); }, /** @@ -896,10 +902,33 @@ jQuery.fn = jQuery.prototype = { * @param String expr An expression with which to remove matching elements * @cat DOM/Traversing */ + + /** + * Removes any elements inside the array of elements from the set + * of matched elements. This method is used to remove one or more + * elements from a jQuery object. + * + * @example $("p").not( $("div p.selected") ) + * @before

Hello

Hello Again

+ * @result [

Hello

] + * @desc Removes all elements that match "div p.selected" from the total set of all paragraphs. + * + * @name not + * @type jQuery + * @param Array|jQuery elems A set of elements to remove from the jQuery set of matched elements. + * @cat DOM/Traversing + */ not: function(t) { - return this.pushStack( typeof t == "string" ? - jQuery.filter(t,this,true).r : - jQuery.grep(this,function(a){ return a != t; }) ); + return this.pushStack( + t.constructor == String && + jQuery.multiFilter(t,this,true) || + + jQuery.grep(this,function(a){ + if ( t.constructor == Array || t.jquery ) + return jQuery.inArray( t, a ) < 0; + else + return a != t; + }) ); }, /** @@ -1007,7 +1036,9 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Attributes */ val: function( val ) { - return val == undefined ? ( this.length ? this[0].value : null ) : this.attr( "value", val ); + return val == undefined ? + ( this.length ? this[0].value : null ) : + this.attr( "value", val ); }, /** @@ -1037,7 +1068,9 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Attributes */ html: function( val ) { - return val == undefined ? ( this.length ? this[0].innerHTML : null ) : this.empty().append( val ); + return val == undefined ? + ( this.length ? this[0].innerHTML : null ) : + this.empty().append( val ); }, /** @@ -1179,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 @@ -1218,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 ( jQuery.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: { @@ -1238,7 +1281,7 @@ jQuery.extend({ elem.className = c ? jQuery.grep( elem.className.split(/\s+/), function(cur){ return !jQuery.className.has( c, cur ); - }).join(' ') : ""; + }).join(" ") : ""; }, // internal only, use is(".class") @@ -1305,8 +1348,8 @@ jQuery.extend({ curCSS: function(elem, prop, force) { var ret; - if (prop == 'opacity' && jQuery.browser.msie) - return jQuery.attr(elem.style, 'opacity'); + if (prop == "opacity" && jQuery.browser.msie) + return jQuery.attr(elem.style, "opacity"); if (prop == "float" || prop == "cssFloat") prop = jQuery.browser.msie ? "styleFloat" : "cssFloat"; @@ -1324,12 +1367,12 @@ jQuery.extend({ if ( cur ) ret = cur.getPropertyValue(prop); - else if ( prop == 'display' ) - ret = 'none'; + else if ( prop == "display" ) + ret = "none"; else - jQuery.swap(elem, { display: 'block' }, function() { - var c = document.defaultView.getComputedStyle(this, ''); - ret = c && c.getPropertyValue(prop) || ''; + jQuery.swap(elem, { display: "block" }, function() { + var c = document.defaultView.getComputedStyle(this, ""); + ret = c && c.getPropertyValue(prop) || ""; }); } else if (elem.currentStyle) { @@ -1344,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" ) { @@ -1397,7 +1445,7 @@ jQuery.extend({ arg = div.childNodes; } - if ( arg.nodeType ) + if ( arg[0] == undefined ) r.push( arg ); else r = jQuery.merge( r, arg ); @@ -1445,7 +1493,7 @@ jQuery.extend({ if ( value != undefined ) elem[fix[name]] = value; return elem[fix[name]]; - } else if ( value == undefined && jQuery.browser.msie && elem.nodeName && elem.nodeName.toUpperCase() == 'FORM' && (name == 'action' || name == 'method') ) + } else if ( value == undefined && jQuery.browser.msie && elem.nodeName && elem.nodeName.toUpperCase() == "FORM" && (name == "action" || name == "method") ) return elem.getAttributeNode(name).nodeValue; // IE elem.getAttribute passes even for style @@ -1660,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 @@ -1828,7 +1876,7 @@ jQuery.each({ jQuery.fn[ i ] = function(a) { var ret = jQuery.map(this,n); if ( a && typeof a == "string" ) - ret = jQuery.filter(a,ret).r; + ret = jQuery.multiFilter(a,ret); return this.pushStack( ret ); }; }); @@ -2109,3 +2157,71 @@ jQuery.each( [ "eq", "lt", "gt", "contains" ], function(i,n){ return this.filter( ":" + n + "(" + num + ")", fn ); }; }); + +/** + * Get the current computed, pixel, width of the first matched element. + * + * @example $("p").width(); + * @before

This is just a test.

+ * @result 300 + * + * @name width + * @type String + * @cat CSS + */ + +/** + * Set the CSS width of every matched element. If no explicit unit + * was specified (like 'em' or '%') then "px" is added to the width. + * + * @example $("p").width(20); + * @before

This is just a test.

+ * @result

This is just a test.

+ * + * @example $("p").width("20em"); + * @before

This is just a test.

+ * @result

This is just a test.

+ * + * @name width + * @type jQuery + * @param Number|String val Set the CSS property to the specified value. + * @cat CSS + */ + +/** + * Get the current computed, pixel, height of the first matched element. + * + * @example $("p").height(); + * @before

This is just a test.

+ * @result 300 + * + * @name height + * @type String + * @cat CSS + */ + +/** + * Set the CSS width of every matched element. If no explicit unit + * was specified (like 'em' or '%') then "px" is added to the width. + * + * @example $("p").height(20); + * @before

This is just a test.

+ * @result

This is just a test.

+ * + * @example $("p").height("20em"); + * @before

This is just a test.

+ * @result

This is just a test.

+ * + * @name height + * @type jQuery + * @param Number|String val Set the CSS property to the specified value. + * @cat CSS + */ + +jQuery.each( [ "height", "width" ], function(i,n){ + jQuery.fn[ n ] = function(h) { + return h == undefined ? + ( this.length ? jQuery.css( this[0], n ) : null ) : + this.css( n, h.constructor == String ? h : h + "px" ); + }; +});