X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=8a0a115c40935ebcf52465b55ae92e860dd8b6b4;hb=8f1e0ef25370c1b7f959355c385e76325272b4f6;hp=1efda2126f50fcb85c8ee0a5bbe2d595cc7ed984;hpb=94e59e287a9fde9425cd96713e8130aef06bc431;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 1efda21..8a0a115 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; }; @@ -155,6 +162,8 @@ var $ = jQuery; * technically, chainable - there really isn't much use for chaining against it. * You can have as many $(document).ready events on your page as you like. * + * See ready(Function) for details about the ready event. + * * @example $(function(){ * // Document is ready * }); @@ -367,6 +376,10 @@ jQuery.fn = jQuery.prototype = { /** * Set a single property to a value, on all matched elements. * + * Note that you can't set the name property of input elements in IE. + * Use $(html) or $().append(html) or $().html(html) to create elements + * on the fly including the name property. + * * @example $("img").attr("src","test.jpg"); * @before * @result @@ -379,7 +392,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 ) @@ -785,7 +798,7 @@ jQuery.fn = jQuery.prototype = { find: function(t) { return this.pushStack( jQuery.map( this, function(a){ return jQuery.find(t,a); - })); + }), arguments ); }, /** @@ -806,7 +819,7 @@ jQuery.fn = jQuery.prototype = { clone: function(deep) { return this.pushStack( jQuery.map( this, function(a){ return a.cloneNode( deep != undefined ? deep : true ); - })); + }), arguments ); }, /** @@ -861,7 +874,7 @@ jQuery.fn = jQuery.prototype = { typeof t == "function" && jQuery.grep( this, t ) || - jQuery.filter(t,this).r ); + jQuery.filter(t,this).r, arguments ); }, /** @@ -895,7 +908,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 ); }, /** @@ -941,7 +954,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 ); }, /** @@ -977,33 +990,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] ] ); - } }); }, @@ -1017,11 +1024,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; } }; @@ -1281,11 +1305,6 @@ jQuery.extend({ ret = elem.style[prop]; - } else if (elem.currentStyle) { - - var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();}); - ret = elem.currentStyle[prop] || elem.currentStyle[newProp]; - } else if (document.defaultView && document.defaultView.getComputedStyle) { if (prop == "cssFloat" || prop == "styleFloat") @@ -1300,34 +1319,58 @@ jQuery.extend({ ret = 'none'; else jQuery.swap(elem, { display: 'block' }, function() { - ret = document.defaultView.getComputedStyle(this,null).getPropertyValue(prop); + var c = document.defaultView.getComputedStyle(this, ''); + ret = c && c.getPropertyValue(prop) || ''; }); + } else if (elem.currentStyle) { + + var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();}); + ret = elem.currentStyle[prop] || elem.currentStyle[newProp]; + } 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; } @@ -1344,7 +1387,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 * @result @@ -2430,6 +2476,9 @@ jQuery.macros = { /** * Set the html contents of every matched element. * + * A wrapper for the innerHTML property of DOM elements, therefore + * not available for XML documents. + * * @example $("div").html("new stuff"); * @before
* @result
new stuff