X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=0a21af773f98174573c33fd76d445b28c50ffacd;hb=f7efcc858dec55fa3b9babb94ebc73b0888e6d51;hp=846c8154124ce63b510eb2736af3122ff5025263;hpb=26580d2675918bb689d55293e734e68671b4dfc6;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 846c815..0a21af7 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -2,8 +2,8 @@ * jQuery - New Wave Javascript * * Copyright (c) 2006 John Resig (jquery.com) - * Licensed under the MIT License: - * http://www.opensource.org/licenses/mit-license.php + * Dual licensed under the MIT (MIT-LICENSE.txt) + * and GPL (GPL-LICENSE.txt) licenses. * * $Date$ * $Rev$ @@ -29,9 +29,6 @@ window.undefined = window.undefined; */ function jQuery(a,c) { - // Initalize the extra macro functions - if ( !jQuery.initDone ) jQuery.init(); - // Shortcut for document ready (because $(document).each() is silly) if ( a && a.constructor == Function && jQuery.fn.ready ) return jQuery(document).ready(a); @@ -39,30 +36,13 @@ function jQuery(a,c) { // Make sure that a selection was provided a = a || jQuery.context || document; - /* - * Handle support for overriding other $() functions. Way too many libraries - * provide this function to simply ignore it and overwrite it. - */ - /* - // Check to see if this is a possible collision case - if ( jQuery._$ && !c && a.constructor == String && - - // Make sure that the expression is a colliding one - !/[^a-zA-Z0-9_-]/.test(a) && - - // and that there are no elements that match it - // (this is the one truly ambiguous case) - !document.getElementsByTagName(a).length ) - - // Use the default method, in case it works some voodoo - return jQuery._$( a ); - */ - // Watch for when a jQuery object is passed as the selector - if ( a.jquery ) return a; + if ( a.jquery ) + return $( jQuery.merge( a, [] ) ); // Watch for when a jQuery object is passed at the context - if ( c && c.jquery ) return c.find(a); + if ( c && c.jquery ) + return $( c ).find(a); // If the context is global, return a new object if ( window == this ) @@ -80,13 +60,16 @@ function jQuery(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 && fn.constructor == Function ) this.each(fn); } // Map over the $ in case of overwrite -if ( $ ) +if ( typeof $ != "undefined" ) jQuery._$ = $; // Map the jQuery namespace to the '$' one @@ -210,7 +193,15 @@ jQuery.fn = jQuery.prototype = { * argument representing the position of the element in the matched * set. * - * @example $("img").each(function(){ this.src = "test.jpg"; }); + * @example $("img").each(function(){ + * this.src = "test.jpg"; + * }); + * @before + * @result + * + * @example $("img").each(function(i){ + * alert( "Image #" + i + " is " + this ); + * }); * @before * @result * @@ -230,6 +221,14 @@ jQuery.fn = jQuery.prototype = { each: function( fn, args ) { return jQuery.each( this, fn, args ); }, + + index: function( obj ) { + var pos = -1; + this.each(function(i){ + if ( this == obj ) pos = i; + }); + return pos; + }, /** * Access a property on the first matched element. @@ -255,12 +254,10 @@ jQuery.fn = jQuery.prototype = { * @before * @result Test Image * - * @test var div = $("div"); - * div.attr({foo: 'baz', zoo: 'ping'}); - * var pass = true; - * for ( var i = 0; i < div.size(); i++ ) { - * if ( div.get(i).foo != "baz" && div.get(i).zoo != "ping" ) pass = false; - * } + * @test var pass = true; + * $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){ + * if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false; + * }); * ok( pass, "Set Multiple Attributes" ); * * @name attr @@ -280,7 +277,7 @@ jQuery.fn = jQuery.prototype = { * div.attr("foo", "bar"); * var pass = true; * for ( var i = 0; i < div.size(); i++ ) { - * if ( div.get(i).foo != "bar" ) pass = false; + * if ( div.get(i).getAttribute('foo') != "bar" ) pass = false; * } * ok( pass, "Set Attribute" ); * @@ -314,7 +311,7 @@ jQuery.fn = jQuery.prototype = { // Look for the case where we're accessing a style value jQuery[ type || "attr" ]( this[0], key ); }, - + /** * Access a style property on the first matched element. * This method makes it easy to retreive a style property value @@ -557,6 +554,12 @@ jQuery.fn = jQuery.prototype = { return jQuery.find(t,a); }), arguments ); }, + + clone: function(deep) { + return this.pushStack( jQuery.map( this, function(a){ + return a.cloneNode( deep != undefined ? deep : true ); + }), arguments ); + }, /** * Removes all elements from the set of matched elements that do not @@ -634,6 +637,7 @@ jQuery.fn = jQuery.prototype = { * @example $("p").not("#selected") * @before

Hello

Hello Again

* @result [

Hello

] + * @test cmpOK($("#main > p#ap > a").not("#google").length, "==", 2, ".not") * * @name not * @type jQuery @@ -671,7 +675,7 @@ jQuery.fn = jQuery.prototype = { * @name add * @type jQuery * @param Array els An array of Elements to add - * @cat jQuery + * @cat DOM/Traversing */ /** @@ -685,7 +689,7 @@ jQuery.fn = jQuery.prototype = { * @name add * @type jQuery * @param Element el An Element to add - * @cat jQuery + * @cat DOM/Traversing */ add: function(t) { return this.pushStack( jQuery.merge( this, t.constructor == String ? @@ -700,7 +704,7 @@ jQuery.fn = jQuery.prototype = { * @member jQuery * @param {String} expr The expression with which to filter * @type Boolean - * @cat jQuery + * @cat DOM/Traversing */ is: function(expr) { return expr ? jQuery.filter(expr,this).r.length > 0 : this.length > 0; @@ -797,8 +801,6 @@ jQuery.extend = jQuery.fn.extend = function(obj,prop) { jQuery.extend({ /** - * - * * @private * @name init * @type undefined @@ -847,7 +849,7 @@ jQuery.extend({ }); jQuery.each( jQuery.macros.css, function(i,n){ - jQuery.fn[ i ] = function(h) { + jQuery.fn[ n ] = function(h) { return h == undefined ? ( this.length ? jQuery.css( this[0], n ) : null ) : this.css( n, h ); @@ -887,7 +889,7 @@ jQuery.extend({ new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), ""); }, has: function(e,a) { - if ( e.className ) + if ( e.className != undefined ) e = e.className; return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e); } @@ -920,36 +922,54 @@ jQuery.extend({ if (jQuery.css(e,"display") != "none") { oHeight = e.offsetHeight; oWidth = e.offsetWidth; - } else - jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "" }, - function(){ - oHeight = e.clientHeight; - oWidth = e.clientWidth; - }); + } else { + e = $(e.cloneNode(true)).css({ + visibility: "hidden", position: "absolute", display: "block" + }).prependTo("body")[0]; + + oHeight = e.clientHeight; + oWidth = e.clientWidth; + + e.parentNode.removeChild(e); + } }); return p == "height" ? oHeight : oWidth; } else if ( p == "opacity" && jQuery.browser.msie ) - return parseFloat( jQuery.curCSS(e,"filter").replace(/[^0-9.]/,"") ) || 1; + return parseFloat( jQuery.curCSS(e,"filter").replace(/[^0-9.]/,"") ) || 1; return jQuery.curCSS( e, p ); }, - curCSS: function(e,p,force) { - var r; + curCSS: function(elem, prop, force) { + var ret; - if (!force && e.style[p]) - r = e.style[p]; - else if (e.currentStyle) { - p = p.replace(/\-(\w)/g,function(m,c){return c.toUpperCase()}); - r = e.currentStyle[p]; + if (!force && elem.style[prop]) { + + 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) { - p = p.replace(/([A-Z])/g,"-$1").toLowerCase(); - var s = document.defaultView.getComputedStyle(e,""); - r = s ? s.getPropertyValue(p) : null; + + prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase(); + var cur = document.defaultView.getComputedStyle(elem, null); + + if ( cur ) + ret = cur.getPropertyValue(prop); + else if ( prop == 'display' ) + ret = 'none'; + else + jQuery.swap(elem, { display: 'block' }, function() { + ret = document.defaultView.getComputedStyle(this,null).getPropertyValue(prop); + }); + } - return r; + return ret; }, clean: function(a) { @@ -1023,7 +1043,8 @@ jQuery.extend({ // Form elements enabled: "!a.disabled", disabled: "a.disabled", - checked: "a.checked" + checked: "a.checked", + selected: "a.selected" }, ".": "jQuery.className.has(a,m[2])", "@": { @@ -1238,28 +1259,26 @@ jQuery.extend({ return r; }, - attr: function(o,a,v){ - if ( a && a.constructor == String ) { - var fix = { - "for": "htmlFor", - "class": "className", - "float": "cssFloat" - }; - - a = (fix[a] && fix[a].replace && fix[a] || a) - .replace(/-([a-z])/ig,function(z,b){ - return b.toUpperCase(); - }); - - if ( v != undefined ) { - o[a] = v; - if ( o.setAttribute && a != "disabled" ) - o.setAttribute(a,v); - } - - return o[a] || o.getAttribute && o.getAttribute(a) || ""; - } else - return ""; + attr: function(elem, name, value){ + var fix = { + "for": "htmlFor", + "class": "className", + "float": "cssFloat", + innerHTML: "innerHTML", + className: "className" + }; + + if ( fix[name] ) { + if ( value != undefined ) elem[fix[name]] = value; + return elem[fix[name]]; + } else if ( elem.getAttribute ) { + if ( value != undefined ) elem.setAttribute( name, value ); + return elem.getAttribute( name, 2 ); + } else { + name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();}); + if ( value != undefined ) elem[name] = value; + return elem[name]; + } }, // The regular expressions that power the parsing engine @@ -1355,14 +1374,14 @@ jQuery.extend({ * @type Array * @param Element elem The element to find the ancestors of. */ - parents: function(a){ - var b = []; - var c = a.parentNode; - while ( c && c != document ) { - b.push( c ); - c = c.parentNode; + parents: function( elem ){ + var matched = []; + var cur = elem.parentNode; + while ( cur && cur != document ) { + matched.push( cur ); + cur = cur.parentNode; } - return b; + return matched; }, /** @@ -1373,23 +1392,25 @@ jQuery.extend({ * @type Array * @param Element elem The element to find all the siblings of (including itself). */ - sibling: function(a,n) { - var type = []; - var tmp = a.parentNode.childNodes; - for ( var i = 0; i < tmp.length; i++ ) { - if ( tmp[i].nodeType == 1 ) - type.push( tmp[i] ); - if ( tmp[i] == a ) - type.n = type.length - 1; + sibling: function(elem, pos, not) { + var elems = []; + + var siblings = elem.parentNode.childNodes; + for ( var i = 0; i < siblings.length; i++ ) { + if ( not === true && siblings[i] == elem ) continue; + + if ( siblings[i].nodeType == 1 ) + elems.push( siblings[i] ); + if ( siblings[i] == elem ) + elems.n = elems.length - 1; } - type.last = type.n == type.length - 1; - type.cur = - n == "even" && type.n % 2 == 0 || - n == "odd" && type.n % 2 || - type[n] == a; - type.prev = type[type.n - 1]; - type.next = type[type.n + 1]; - return type; + + return jQuery.extend( elems, { + last: elems.n == elems.length - 1, + cur: pos == "even" && elems.n % 2 == 0 || pos == "odd" && elems.n % 2 || elems[pos] == elem, + prev: elems[elems.n - 1], + next: elems[elems.n + 1] + }); }, /** @@ -1401,30 +1422,30 @@ jQuery.extend({ * @param Array a The first array to merge. * @param Array b The second array to merge. */ - merge: function(a,b) { - var d = []; + merge: function(first, second) { + var result = []; // Move b over to the new array (this helps to avoid // StaticNodeList instances) - for ( var k = 0; k < a.length; k++ ) - d[k] = a[k]; + for ( var k = 0; k < first.length; k++ ) + result[k] = first[k]; // Now check for duplicates between a and b and only // add the unique items - for ( var i = 0; i < b.length; i++ ) { - var c = true; + for ( var i = 0; i < second.length; i++ ) { + var noCollision = true; // The collision-checking process - for ( var j = 0; j < a.length; j++ ) - if ( b[i] == a[j] ) - c = false; + for ( var j = 0; j < first.length; j++ ) + if ( second[i] == first[j] ) + noCollision = false; // If the item is unique, add it - if ( c ) - d.push( b[i] ); + if ( noCollision ) + result.push( second[i] ); } - return d; + return result; }, /** @@ -1439,21 +1460,21 @@ jQuery.extend({ * @param Function fn The function to process each item against. * @param Boolean inv Invert the selection - select the opposite of the function. */ - grep: function(a,f,s) { + grep: function(elems, fn, inv) { // If a string is passed in for the function, make a function // for it (a handy shortcut) - if ( f.constructor == String ) - f = new Function("a","i","return " + f); + if ( fn.constructor == String ) + fn = new Function("a","i","return " + fn); - var r = []; + var result = []; // Go through the array, only saving the items // that pass the validator function - for ( var i = 0; i < a.length; i++ ) - if ( !s && f(a[i],i) || s && !f(a[i],i) ) - r.push( a[i] ); + for ( var i = 0; i < elems.length; i++ ) + if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) ) + result.push( elems[i] ); - return r; + return result; }, /** @@ -1470,24 +1491,26 @@ jQuery.extend({ * @param Array array The Array to translate. * @param Function fn The function to process each item against. */ - map: function(a,f) { + map: function(elems, fn) { // If a string is passed in for the function, make a function // for it (a handy shortcut) - if ( f.constructor == String ) - f = new Function("a","return " + f); + if ( fn.constructor == String ) + fn = new Function("a","return " + fn); - var r = []; + var result = []; // Go through the array, translating each of the items to their // new value (or values). - for ( var i = 0; i < a.length; i++ ) { - var t = f(a[i],i); - if ( t !== null && t != undefined ) { - if ( t.constructor != Array ) t = [t]; - r = jQuery.merge( r, t ); + for ( var i = 0; i < elems.length; i++ ) { + var val = fn(elems[i],i); + + if ( val !== null && val != undefined ) { + if ( val.constructor != Array ) val = [val]; + result = jQuery.merge( result, val ); } } - return r; + + return result; }, /* @@ -2187,7 +2210,7 @@ jQuery.macros = { /** * Get a set of elements containing the unique ancestors of the matched - * set of elements. + * set of elements (except for the root element). * * @example $("span").ancestors() * @before

Hello

Hello Again
@@ -2215,7 +2238,7 @@ jQuery.macros = { /** * Get a set of elements containing the unique ancestors of the matched - * set of elements. + * set of elements (except for the root element). * * @example $("span").ancestors() * @before

Hello

Hello Again
@@ -2360,10 +2383,15 @@ jQuery.macros = { * @param String expr An expression to filter the child Elements with * @cat DOM/Traversing */ - children: "a.childNodes" + children: "jQuery.sibling(a.firstChild)" }, each: { + + removeAttr: function( key ) { + this.removeAttribute( key ); + }, + /** * Displays each of the set of matched elements if they are hidden. * @@ -2381,7 +2409,7 @@ jQuery.macros = { * @type jQuery * @cat Effects */ - _show: function(){ + show: function(){ this.style.display = this.oldblock ? this.oldblock : ""; if ( jQuery.css(this,"display") == "none" ) this.style.display = "block"; @@ -2404,7 +2432,7 @@ jQuery.macros = { * @type jQuery * @cat Effects */ - _hide: function(){ + hide: function(){ this.oldblock = this.oldblock || jQuery.css(this,"display"); if ( this.oldblock == "none" ) this.oldblock = "block"; @@ -2424,9 +2452,8 @@ jQuery.macros = { * @type jQuery * @cat Effects */ - _toggle: function(){ - var d = jQuery.css(this,"display"); - $(this)[ !d || d == "none" ? "show" : "hide" ](); + toggle: function(){ + $(this)[ $(this).is(":hidden") ? "show" : "hide" ].apply( $(this), arguments ); }, /** @@ -2636,3 +2663,5 @@ jQuery.macros = { } } }; + +jQuery.init();