X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=jquery%2Fjquery.js;h=bd2f92faeb60cde867ab0aa9b226737a7d371fd0;hb=64179692a0eeadc7a6d93fe007bfc00990af6575;hp=f54d05ea1b92f68b072b4b6f5758b57e325a4e63;hpb=6ae392a4e57834224b2a686d774ab210da25daa5;p=jquery.git diff --git a/jquery/jquery.js b/jquery/jquery.js index f54d05e..bd2f92f 100644 --- a/jquery/jquery.js +++ b/jquery/jquery.js @@ -1,5 +1,5 @@ /* - * jQuery + * jQuery - New Wave Javascript * * Copyright (c) 2006 John Resig (jquery.com) * Licensed under the MIT License: @@ -17,18 +17,53 @@ window.undefined = window.undefined; * @constructor */ function jQuery(a,c) { + /* + * 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 ); + + // Make sure t hat a selection was provided + a = a || jQuery.context || document; + + // Watch for when a jQuery object is passed as the selector + if ( a.jquery ) + return a; + + // Watch for when a jQuery object is passed at the context + if ( c && c.jquery ) + return $(c.get()).find(a); + + // If the context is global, return a new object if ( window == this ) return new jQuery(a,c); - - this.cur = jQuery.Select( - a || jQuery.context || document, - c && c.jquery && c.get(0) || c - ); + + // Watch for when an array is passed in + if ( a.constructor == Array ) + // Assume that it's an array of DOM Elements + this.cur = a; + else + // Find the matching elements and save them for later + this.cur = jQuery.Select( a, c ); } -/** - * The jQuery query object. - */ +// Map over the $ in case of overwrite +if ( $ ) + jQuery._$ = $; + +// Map the jQuery namespace to the '$' one var $ = jQuery; jQuery.fn = jQuery.prototype = { @@ -67,7 +102,7 @@ jQuery.fn = jQuery.prototype = { }, set: function(a,b) { return this.each(function(){ - if ( b == undefined ) + if ( b === undefined ) for ( var j in a ) jQuery.attr(this,j,a[j]); else @@ -85,18 +120,19 @@ jQuery.fn = jQuery.prototype = { text: function(e) { e = e || this.get(); var t = ""; - for ( var j = 0; j < e.length; j++ ) - for ( var i = 0; i < e[j].childNodes.length; i++ ) - t += e[j].childNodes[i].nodeType != 1 ? - e[j].childNodes[i].nodeValue : - jQuery.fn.text(e[j].childNodes[i].childNodes); + 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] ]); + } return t; }, css: function(a,b) { - return a.constructor != String || b ? + return a.constructor != String || b ? this.each(function(){ - if ( !b ) + if ( b === undefined ) for ( var j in a ) jQuery.attr(this.style,j,a[j]); else @@ -129,26 +165,27 @@ jQuery.fn = jQuery.prototype = { }, addClass: function(c) { return this.each(function(){ - jQuery.class.add(this,c); + jQuery.className.add(this,c); }); }, removeClass: function(c) { return this.each(function(){ - jQuery.class.remove(this,c); + jQuery.className.remove(this,c); }); }, toggleClass: function(c) { return this.each(function(){ if (jQuery.hasWord(this,c)) - jQuery.class.remove(this,c); + jQuery.className.remove(this,c); else - jQuery.class.add(this,c); + jQuery.className.add(this,c); }); }, remove: function() { - this.each(function(){this.parentNode.removeChild( this );}); - return this.pushStack( [] ); + return this.each(function(){ + this.parentNode.removeChild( this ); + }); }, wrap: function() { @@ -245,7 +282,7 @@ jQuery.fn = jQuery.prototype = { }, parent: function(a) { - var ret = jQuery.map(this.cur,"d.parentNode"); + var ret = jQuery.map(this.cur,"a.parentNode"); if ( a ) ret = jQuery.filter(a,ret).r; return this.pushStack(ret); }, @@ -264,7 +301,15 @@ jQuery.fn = jQuery.prototype = { }, filter: function(t) { - return this.pushStack( jQuery.filter(t,this.cur).r ); + if ( /,/.test(t) ) { + var p = t.split(/\s*,\s*/); + return this.pushStack( $.map(this.cur,function(a){ + for ( var i = 0; i < p.length; i++ ) + if ( jQuery.filter(p[i],[a]).r.length ) + return a; + }) ); + } else + return this.pushStack( jQuery.filter(t,this.cur).r ); }, not: function(t) { return this.pushStack( t.constructor == String ? @@ -318,10 +363,26 @@ jQuery.fn = jQuery.prototype = { } }; -jQuery.class = { +/** + * Similar to the Prototype $A() function, only it allows you to + * forcefully pass array-like structures into $(). + */ +jQuery.A = function(a){ + // Create a temporary, clean, array + var r = []; + + // and copy the old array contents over to it + for ( var i = 0; i < a.length; i++ ) + r.push( a[i] ); + + // Return the sane jQuery object + return $(r); +}; + +jQuery.className = { add: function(o,c){ if (jQuery.hasWord(o,c)) return; - o.className += ( o.className.length > 0 ? " " : "" ) + c; + o.className += ( o.className ? " " : "" ) + c; }, remove: function(o,c){ o.className = !c ? "" : @@ -345,42 +406,39 @@ jQuery.class = { jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" ); })(); +$.swap = function(e,o,f) { + for ( var i in o ) { + e.style["old"+i] = e.style[i]; + e.style[i] = o[i]; + } + f.apply( e, [] ); + for ( var i in o ) + e.style[i] = e.style["old"+i]; +}; + jQuery.css = function(e,p) { // Adapted from Prototype 1.4.0 if ( p == "height" || p == "width" ) { + var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"]; - // Handle extra width/height provided by the W3C box model - var ph = (!jQuery.boxModel ? 0 : - jQuery.css(e,"paddingTop") + jQuery.css(e,"paddingBottom") + - jQuery.css(e,"borderTopWidth") + jQuery.css(e,"borderBottomWidth")) || 0; - - var pw = (!jQuery.boxModel ? 0 : - jQuery.css(e,"paddingLeft") + jQuery.css(e,"paddingRight") + - jQuery.css(e,"borderLeftWidth") + jQuery.css(e,"borderRightWidth")) || 0; - - var oHeight, oWidth; - - if (jQuery.css(e,"display") != 'none') { - oHeight = e.offsetHeight || parseInt(e.style.height) || 0; - oWidth = e.offsetWidth || parseInt(e.style.width) || 0; - } else { - var els = e.style; - var ov = els.visibility; - var op = els.position; - var od = els.display; - els.visibility = "hidden"; - els.position = "absolute"; - els.display = ""; - oHeight = e.clientHeight || parseInt(e.style.height); - oWidth = e.clientWidth || parseInt(e.style.width); - els.display = od; - els.position = op; - els.visibility = ov; + for ( var i in d ) { + old["padding" + d[i]] = 0; + old["border" + d[i] + "Width"] = 0; } - return p == "height" ? - (oHeight - ph < 0 ? 0 : oHeight - ph) : - (oWidth - pw < 0 ? 0 : oWidth - pw); + $.swap( e, old, function() { + if (jQuery.css(e,"display") != 'none') { + oHeight = e.offsetHeight; + oWidth = e.offsetWidth; + } else + $.swap( e, { visibility: 'hidden', position: 'absolute', display: '' }, + function(){ + oHeight = e.clientHeight; + oWidth = e.clientWidth; + }); + }); + + return p == "height" ? oHeight : oWidth; } var r; @@ -395,7 +453,7 @@ jQuery.css = function(e,p) { r = s ? s.getPropertyValue(p) : null; } - return /top|right|left|bottom/i.test(p) ? parseFloat( r ) : r; + return r; }; jQuery.clean = function(a) { @@ -457,9 +515,9 @@ jQuery.g = { empty: "!a.childNodes.length", root: "a==(a.ownerDocument||document).documentElement", contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1", - visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!= 'none'&&jQuery.css(a,'visibility')!= 'hidden')", - hidden: "(a.type&&a.type == 'hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'", - enabled: "a.disabled==false", + visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!= 'hidden')", + hidden: "(a.type&&a.type=='hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'", + enabled: "!a.disabled", disabled: "a.disabled", checked: "a.checked" }, @@ -493,6 +551,7 @@ jQuery.token = [ jQuery.Select = function( t, context ) { context = context || jQuery.context || document; + if ( t.constructor != String ) return [t]; if ( !t.indexOf("//") ) { @@ -725,7 +784,7 @@ jQuery.map = function(a,f) { var r = []; for ( var i = 0; i < a.length; i++ ) { var t = f(a[i],i); - if ( t !== null ) { + if ( t !== null && t != undefined ) { if ( t.constructor != Array ) t = [t]; r = jQuery.merge( t, r ); } @@ -753,9 +812,14 @@ jQuery.event = { } handlers[handler.guid] = handler; element["on" + type] = jQuery.event.handle; + + var g = jQuery.event.global; + if (!g[type]) g[type] = []; + g[type].push( element ); }, guid: 1, + global: {}, // Detach an event or set of events from an element remove: function(element, type, handler) { @@ -771,22 +835,42 @@ jQuery.event = { jQuery.event.remove( element, j ); }, - trigger: function(element,type,data) { - data = data || [ jQuery.event.fix({ type: type }) ]; - if ( element && element["on" + type] ) + trigger: function(type,data,element) { + // Touch up the incoming data + data = data || []; + + // Handle triggering a single element + if ( element && element["on" + type] ) { + // Pass along a fake event + data.shift( jQuery.event.fix({ type: type, target: element }) ); + + // Trigger the event element["on" + type].apply( element, data ); + + // Handle a global trigger + } else if ( !element ) { + var g = jQuery.event.global[type]; + if ( g ) + for ( var i = 0; i < g.length; i++ ) + jQuery.event.trigger( type, data, g[i] ); + } }, handle: function(event) { - if ( !event && !window.event ) return; + // Handle adding events to items in IFrames, in IE + event = event || + jQuery.event.fix( ((this.ownerDocument || this.document || + this).parentWindow || window).event ); + + // If no correct event was found, fail + if ( !event ) return; var returnValue = true, handlers = []; - event = event || jQuery.event.fix(window.event); for ( var j in this.events[event.type] ) handlers[handlers.length] = this.events[event.type][j]; - for ( var i = 0; i < handlers.length; i++ ) { + for ( var i = 0; i < handlers.length; i++ ) if ( handlers[i].constructor == Function ) { this.handleEvent = handlers[i]; if (this.handleEvent(event) === false) { @@ -795,18 +879,19 @@ jQuery.event = { returnValue = false; } } - } return returnValue; }, fix: function(event) { - event.preventDefault = function() { - this.returnValue = false; - }; + if ( event ) { + event.preventDefault = function() { + this.returnValue = false; + }; - event.stopPropagation = function() { - this.cancelBubble = true; - }; + event.stopPropagation = function() { + this.cancelBubble = true; + }; + } return event; }