X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=f774658fa089f0c0eba74ba0eab5e15b4dcf109a;hb=18e563abe53d7a7fb4169c869f01812c119ae191;hp=08f325c34a8c1af2d1c0bb77b495587090cfca2a;hpb=23e255015fb5e4f5ce7e8dd091b1550e80cccdd9;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 08f325c..f774658 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -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,32 +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 $( jQuery.merge( a, [] ) ); + return jQuery( jQuery.merge( a, [] ) ); // Watch for when a jQuery object is passed at the context if ( c && c.jquery ) - return $( c ).find(a); + return jQuery( c ).find(a); // If the context is global, return a new object if ( window == this ) @@ -82,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 @@ -212,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 * @@ -265,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 @@ -290,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" ); * @@ -391,8 +378,9 @@ jQuery.fn = jQuery.prototype = { for ( var j = 0; j < e.length; j++ ) { var r = e[j].childNodes; for ( var i = 0; i < r.length; i++ ) - t += r[i].nodeType != 1 ? - r[i].nodeValue : jQuery.fn.text([ r[i] ]); + if ( r[i].nodeType != 8 ) + t += r[i].nodeType != 1 ? + r[i].nodeValue : jQuery.fn.text([ r[i] ]); } return t; }, @@ -650,6 +638,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 @@ -687,7 +676,7 @@ jQuery.fn = jQuery.prototype = { * @name add * @type jQuery * @param Array els An array of Elements to add - * @cat jQuery + * @cat DOM/Traversing */ /** @@ -701,7 +690,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 ? @@ -716,7 +705,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; @@ -813,8 +802,6 @@ jQuery.extend = jQuery.fn.extend = function(obj,prop) { jQuery.extend({ /** - * - * * @private * @name init * @type undefined @@ -836,7 +823,7 @@ jQuery.extend({ var a = arguments; return this.each(function(){ for ( var j = 0; j < a.length; j++ ) - $(a[j])[n]( this ); + jQuery(a[j])[n]( this ); }); }; }); @@ -936,36 +923,54 @@ jQuery.extend({ if (jQuery.css(e,"display") != "none") { oHeight = e.offsetHeight; oWidth = e.offsetWidth; - } else - jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "block" }, - function(){ - oHeight = e.clientHeight; - oWidth = e.clientWidth; - }); + } else { + e = jQuery(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) { - var np = p.replace(/\-(\w)/g,function(m,c){return c.toUpperCase()}); - r = e.currentStyle[p] || e.currentStyle[np]; + 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) { @@ -1021,6 +1026,7 @@ jQuery.extend({ odd: "i%2", // Child Checks + "nth-child": "jQuery.sibling(a,m[3]).cur", "first-child": "jQuery.sibling(a,0).cur", "last-child": "jQuery.sibling(a,0).last", "only-child": "jQuery.sibling(a).length==1", @@ -1104,7 +1110,7 @@ jQuery.extend({ * @test t( "Adjacent", "p + p", ["ap","en","sap"] ); * @test t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] ); * @test t( "First Child", "p:first-child", ["firstp","sndp"] ); - * @test t( "Attribute Exists", "a[@title]", ["google"] ); + * @test t( "Attribute Exists", "a[@title]", ["google"] ); * @test t( "Attribute Exists", "*[@title]", ["google"] ); * @test t( "Attribute Exists", "[@title]", ["google"] ); * @test t( "Attribute Equals", "a[@rel='bookmark']", ["simon1"] ); @@ -1193,6 +1199,8 @@ jQuery.extend({ var foundToken = false; for ( var i = 0; i < jQuery.token.length; i += 2 ) { + if ( foundToken ) continue; + var re = new RegExp("^(" + jQuery.token[i] + ")"); var m = re.exec(t); @@ -1229,8 +1237,9 @@ jQuery.extend({ ); } } - } + } + if ( t ) { var val = jQuery.filter(t,r); ret = r = val.r; @@ -1255,28 +1264,28 @@ 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", + value: "value", + disabled: "disabled" + }; + + 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 @@ -1644,7 +1653,7 @@ new function() { safari: /webkit/.test(b), opera: /opera/.test(b), msie: /msie/.test(b) && !/opera/.test(b), - mozilla: /mozilla/.test(b) && !/compatible/.test(b) + mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b) }; // Check to see if the W3C box model is being used @@ -2208,7 +2217,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
@@ -2236,7 +2245,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
@@ -2381,7 +2390,7 @@ 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: { @@ -2407,7 +2416,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"; @@ -2430,7 +2439,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"; @@ -2450,9 +2459,8 @@ jQuery.macros = { * @type jQuery * @cat Effects */ - _toggle: function(){ - var d = jQuery.css(this,"display"); - $(this)[ !d || d == "none" ? "show" : "hide" ](); + toggle: function(){ + jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ].apply( jQuery(this), arguments ); }, /** @@ -2548,7 +2556,7 @@ jQuery.macros = { * @cat DOM/Manipulation */ remove: function(a){ - if ( !a || jQuery.filter( [this], a ).r ) + if ( !a || jQuery.filter( a, [this] ).r ) this.parentNode.removeChild( this ); }, @@ -2596,7 +2604,7 @@ jQuery.macros = { */ bind: function( type, fn ) { if ( fn.constructor == String ) - fn = new Function("e", ( !fn.indexOf(".") ? "$(this)" : "return " ) + fn); + fn = new Function("e", ( !fn.indexOf(".") ? "jQuery(this)" : "return " ) + fn); jQuery.event.add( this, type, fn ); }, @@ -2662,3 +2670,5 @@ jQuery.macros = { } } }; + +jQuery.init();