X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=8cffb8e876ddbb4e7e46f0203c011c0e977936cb;hb=42785b1c1dbdb51e798de90720a164731f495e44;hp=9e62fe3cb7f37cb845b23baee2eeb5c83a63ace7;hpb=665e1959cc03292a5109796dc1618d3848ec6f6e;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 9e62fe3..8cffb8e 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -22,7 +22,7 @@ window.undefined = window.undefined; */ var jQuery = function(a,c) { - // Shortcut for document ready (because $(document).each() is silly) + // Shortcut for document 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); @@ -55,6 +55,13 @@ var jQuery = function(a,c) { // Find the matching elements and save them for later jQuery.find( a, c ) ); + // See if an extra function was provided + var fn = arguments[ arguments.length - 1 ]; + + // If so, execute it in context + if ( fn && typeof fn == "function" ) + this.each(fn); + return this; }; @@ -383,7 +390,7 @@ jQuery.fn = jQuery.prototype = { */ attr: function( key, value, type ) { // Check to see if we're setting style values - return key.constructor != String || value != undefined ? + return typeof key != "string" || value != undefined ? this.each(function(){ // See if we're setting a hash of styles if ( value == undefined ) @@ -789,7 +796,7 @@ jQuery.fn = jQuery.prototype = { find: function(t) { return this.pushStack( jQuery.map( this, function(a){ return jQuery.find(t,a); - })); + }), arguments ); }, /** @@ -810,7 +817,7 @@ jQuery.fn = jQuery.prototype = { clone: function(deep) { return this.pushStack( jQuery.map( this, function(a){ return a.cloneNode( deep != undefined ? deep : true ); - })); + }), arguments ); }, /** @@ -865,7 +872,7 @@ jQuery.fn = jQuery.prototype = { typeof t == "function" && jQuery.grep( this, t ) || - jQuery.filter(t,this).r ); + jQuery.filter(t,this).r, arguments ); }, /** @@ -899,7 +906,7 @@ jQuery.fn = jQuery.prototype = { not: function(t) { return this.pushStack( typeof t == "string" ? jQuery.filter(t,this,false).r : - jQuery.grep(this,function(a){ return a != t; }) ); + jQuery.grep(this,function(a){ return a != t; }), arguments ); }, /** @@ -945,7 +952,7 @@ jQuery.fn = jQuery.prototype = { */ add: function(t) { return this.pushStack( jQuery.merge( this, typeof t == "string" ? - jQuery.find(t) : t.constructor == Array ? t : [t] ) ); + jQuery.find(t) : t.constructor == Array ? t : [t] ), arguments ); }, /** @@ -981,33 +988,27 @@ jQuery.fn = jQuery.prototype = { * @private * @name domManip * @param Array args - * @param Boolean table - * @param Number dir + * @param Boolean table Insert TBODY in TABLEs if one is not found. + * @param Number dir If dir<0, process args in reverse order. * @param Function fn The function doing the DOM manipulation. * @type jQuery * @cat Core */ domManip: function(args, table, dir, fn){ - var clone = this.size() > 1; + var clone = this.length > 1; var a = jQuery.clean(args); + if ( dir < 0 ) + a.reverse(); return this.each(function(){ var obj = this; - if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() != "THEAD" ) { - var tbody = this.getElementsByTagName("tbody"); + if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" ) + obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody")); - if ( !tbody.length ) { - obj = document.createElement("tbody"); - this.appendChild( obj ); - } else - obj = tbody[0]; - } + for ( var i=0; i < a.length; i++ ) + fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] ); - for ( var i = ( dir < 0 ? a.length - 1 : 0 ); - i != ( dir < 0 ? dir : a.length ); i += dir ) { - fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] ); - } }); }, @@ -1021,11 +1022,28 @@ jQuery.fn = jQuery.prototype = { * @type jQuery * @cat Core */ - pushStack: function(a) { - if ( !this.stack ) - this.stack = []; - this.stack.push( this.get() ); - return this.set( a ); + pushStack: function(a,args) { + var fn = args && args.length > 1 && args[args.length-1]; + var fn2 = args && args.length > 2 && args[args.length-2]; + + if ( fn && fn.constructor != Function ) fn = null; + if ( fn2 && fn2.constructor != Function ) fn2 = null; + + if ( !fn ) { + if ( !this.stack ) this.stack = []; + this.stack.push( this.get() ); + this.set( a ); + } else { + var old = this.get(); + this.set( a ); + + if ( fn2 && a.length || !fn2 ) + this.each( fn2 || fn ).set( old ); + else + this.set( old ).each( fn ); + } + + return this; } }; @@ -1313,26 +1331,44 @@ jQuery.extend({ return ret; }, - clean: function(a) { + clean: function(a) { var r = []; for ( var i = 0; i < a.length; i++ ) { var arg = a[i]; if ( typeof arg == "string" ) { // Convert html string into DOM nodes // Trim whitespace, otherwise indexOf won't work as expected - var s = jQuery.trim(arg), div = document.createElement("div"), wrap = [0,"",""]; + var s = jQuery.trim(arg), s3 = s.substring(0,3), s6 = s.substring(0,6), + div = document.createElement("div"), wrap = [0,"",""]; - if ( !s.indexOf("", ""]; - else if ( !s.indexOf("", ""]; - else if ( !s.indexOf("", ""]; // tbody auto-inserted - else if ( !s.indexOf("", ""]; + else if ( s3 == " matched above wrap = [3, "", "
"]; // Go to html and back, then peel off extra wrappers div.innerHTML = wrap[1] + s + wrap[2]; while ( wrap[0]-- ) div = div.firstChild; + + // Remove IE's autoinserted from table fragments + if ( jQuery.browser.msie ) { + var tb = null; + // String was a , *may* have spurious + if ( s6 == " or + else if ( wrap[1] == "
" && s.indexOf("= 0 ; --n ) + if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length ) + tb[n].parentNode.removeChild(tb[n]); + } + } + arg = div.childNodes; } @@ -1349,7 +1385,7 @@ jQuery.extend({ expr: { "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()", - "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]", + "#": "a.getAttribute('id')==m[2]", ":": { // Position Checks lt: "i