From: John Resig Date: Wed, 17 May 2006 02:42:03 +0000 (+0000) Subject: Not only does it pass the default JSLint settings, it also no longer leaks *any*... X-Git-Tag: 1.0a~74 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4986837802a65ced713c2dac2ff7d80e42fd7807;p=jquery.git Not only does it pass the default JSLint settings, it also no longer leaks *any* global variables! --- diff --git a/ajax/ajax.js b/ajax/ajax.js index a972871bf..257ae02b2 100644 --- a/ajax/ajax.js +++ b/ajax/ajax.js @@ -130,7 +130,7 @@ $.fn.load = function(a,o,f) { h = h.responseText; self.html(h).find("script").each(function(){ try { - eval( this.text || this.textContent || this.innerHTML ); + $.eval( this.text || this.textContent || this.innerHTML ); } catch(e){} }); if(f){f(h);} @@ -146,7 +146,7 @@ $.fn.load = function(a,o,f) { $.fn.formValues = function() { var a = []; this.find("input[@type='submit'],input[@type='hidden'],textarea,input[@checked],input[@type='password'],input[@type='text'],option[@selected]").filter(":enabled").each(function() { - o = {}; + var o = {}; o.name = this.name || this.id || this.parentNode.name || this.parentNode.id; o.value = this.value; a.push(o); @@ -170,7 +170,7 @@ $.update = function(objElement, strURL, arrValues, fncCallback) { // // Evaluate the scripts objElement.html(strHTML).find("script").each(function(){ - try { eval( this.text || this.textContent || this.innerHTML ); } catch(e){} + try { $.eval( this.text || this.textContent || this.innerHTML ); } catch(e){} }); // diff --git a/event/event.js b/event/event.js index dff2e2eb1..feec9ee8c 100644 --- a/event/event.js +++ b/event/event.js @@ -1,36 +1,38 @@ -var e = ["blur","focus","contextmenu","load","resize","scroll","unload", - "click","dblclick","mousedown","mouseup","mouseenter","mouseleave", - "mousemove","mouseover","mouseout","change","reset","select","submit", - "keydown","keypress","keyup","abort","error","ready"]; +(function(){ + var e = ["blur","focus","contextmenu","load","resize","scroll","unload", + "click","dblclick","mousedown","mouseup","mouseenter","mouseleave", + "mousemove","mouseover","mouseout","change","reset","select","submit", + "keydown","keypress","keyup","abort","error","ready"]; -for ( var i = 0; i < e.length; i++ ) { - (function(){ - var o = e[i]; - $.fn[o] = function(f){ return this.bind(o, f); }; - $.fn["un"+o] = function(f){ return this.unbind(o, f); }; - $.fn["do"+o] = function(){ return this.trigger(o); }; - $.fn["one"+o] = function(f){ return this.bind(o, function(e){ - if ( this[o+f] !== null ) { return true; } - this[o+f]++; - return $.apply(this,f,[e]); - }); }; + for ( var i = 0; i < e.length; i++ ) { + (function(){ + var o = e[i]; + $.fn[o] = function(f){ return this.bind(o, f); }; + $.fn["un"+o] = function(f){ return this.unbind(o, f); }; + $.fn["do"+o] = function(){ return this.trigger(o); }; + $.fn["one"+o] = function(f){ return this.bind(o, function(e){ + if ( this[o+f] !== null ) { return true; } + this[o+f]++; + return $.apply(this,f,[e]); + }); }; - // Deprecated - //$.fn["on"+o] = function(f){ return this.bind(o, f); }; - })(); -} + // Deprecated + //$.fn["on"+o] = function(f){ return this.bind(o, f); }; + })(); + } +})(); $.fn.hover = function(f,g) { // Check if mouse(over|out) are still within the same parent element return this.each(function(){ var obj = this; - addEvent(this, "mouseover", function(e) { + $.event.add(this, "mouseover", function(e) { var p = ( e.fromElement !== null ? e.fromElement : e.relatedTarget ); while ( p && p != obj ) { p = p.parentNode; } if ( p == obj ) { return false; } return $.apply(obj,f,[e]); }); - addEvent(this, "mouseout", function(e) { + $.event.add(this, "mouseout", function(e) { var p = ( e.toElement !== null ? e.toElement : e.relatedTarget ); while ( p && p != obj ) { p = p.parentNode; } if ( p == obj ) { return false; } @@ -57,7 +59,7 @@ if ( document.addEventListener ) { document.addEventListener( "DOMContentLoaded", $.ready, null ); } -addEvent( window, "load", $.ready ); +$.event.add( window, "load", $.ready ); $.fn.ready = function(f) { return this.each(function(){ diff --git a/fx/fx.js b/fx/fx.js index ca7ebbb0b..3eb894583 100644 --- a/fx/fx.js +++ b/fx/fx.js @@ -9,42 +9,42 @@ $.speed = function(s,o) { $.fn.hide = function(a,o) { o = $.speed(a,o); return a ? this.each(function(){ - new fx.FadeSize(this,o).hide(); + new $.fx.FadeSize(this,o).hide(); }) : this._hide(); }; $.fn.show = function(a,o) { o = $.speed(a,o); return a ? this.each(function(){ - new fx.FadeSize(this,o).show(); + new $.fx.FadeSize(this,o).show(); }) : this._show(); }; $.fn.slideDown = function(a,o) { o = $.speed(a,o); return this.each(function(){ - new fx.Resize(this,o).show("height"); + new $.fx.Resize(this,o).show("height"); }); }; $.fn.slideUp = function(a,o) { o = $.speed(a,o); return this.each(function(){ - new fx.Resize(this,o).hide("height"); + new $.fx.Resize(this,o).hide("height"); }); }; $.fn.fadeOut = function(a,o) { o = $.speed(a,o); return a ? this.each(function(){ - new fx.Opacity(this,o).hide(); + new $.fx.Opacity(this,o).hide(); }) : this._hide(); }; $.fn.fadeIn = function(a,o) { o = $.speed(a,o); return a ? this.each(function(){ - new fx.Opacity(this,o).show(); + new $.fx.Opacity(this,o).show(); }) : this._show(); }; @@ -85,7 +85,7 @@ $.setAuto = function(e,p) { * people. You've been warned. */ -function fx(el,op,ty,tz){ +$.fx = function(el,op,ty,tz){ var z = this; z.a = function(){z.el.style[ty]=z.now+z.o.unit;}; z.max = function(){return z.el["io"+ty]||z.el["natural"+tz]||z.el["scroll"+tz]||z.cur();}; @@ -130,17 +130,22 @@ function fx(el,op,ty,tz){ this.now=f;z.a();z.io=z.cur();z.s=(new Date()).getTime(); z.timer=setInterval(function(){z.step(f,t);}, 13); }; -} -fx.fn = ["show","hide","toggle"]; -fx.ty = ["Height","Width","Left","Top"]; -for(var $i in fx.ty){(function(){ - var c = fx.ty[$i]; - fx[c] = function(a,b){ - return new fx(a,b,c.toLowerCase(),c); - }; -})();} -fx.Opacity = function(a,b){ - var o = new fx(a,b,"opacity"); +}; + +$.fx.fn = ["show","hide","toggle"]; +$.fx.ty = ["Height","Width","Left","Top"]; + +(function(){ + for(var $i in $.fx.ty){(function(){ + var c = $.fx.ty[$i]; + $.fx[c] = function(a,b){ + return new $.fx(a,b,c.toLowerCase(),c); + }; + })();} +})(); + +$.fx.Opacity = function(a,b){ + var o = new $.fx(a,b,"opacity"); o.cur = function(){return parseFloat(o.el.style.opacity);}; o.a = function() { var e = o.el.style; @@ -154,14 +159,14 @@ fx.Opacity = function(a,b){ o.a(); return o; }; -fx.Resize = function(e,o){ +$.fx.Resize = function(e,o){ var z = this; - var h = new fx.Height(e,o); + var h = new $.fx.Height(e,o); if(o) { o.onComplete = null; } - var w = new fx.Width(e,o); + var w = new $.fx.Width(e,o); function c(a,b,d){return (!a||a==c||b==d);} - for(var i in fx.fn){(function(){ - var j = fx.fn[i]; + for(var i in $.fx.fn){(function(){ + var j = $.fx.fn[i]; z[j] = function(a,b){ if(c(a,b,"height")) { h[j](); } if(c(a,b,"width")) { w[j](); } @@ -172,13 +177,13 @@ fx.Resize = function(e,o){ w.modify(d); }; }; -fx.FadeSize = function(e,o){ +$.fx.FadeSize = function(e,o){ var z = this; - var r = new fx.Resize(e,o); + var r = new $.fx.Resize(e,o); if(o) { o.onComplete = null; } - var p = new fx.Opacity(e,o); - for(var i in fx.fn){(function(){ - var j = fx.fn[i]; + var p = new $.fx.Opacity(e,o); + for(var i in $.fx.fn){(function(){ + var j = $.fx.fn[i]; z[j] = function(a,b){p[j]();r[j](a,b);}; })();} }; diff --git a/jquery/jquery.js b/jquery/jquery.js index e9db33d1f..2e29fa59e 100644 --- a/jquery/jquery.js +++ b/jquery/jquery.js @@ -214,13 +214,13 @@ function $(a,c) { }, bind: function(t,f) { - return this.each(function(){addEvent(this,t,f);}); + return this.each(function(){$.event.add(this,t,f);}); }, unbind: function(t,f) { - return this.each(function(){removeEvent(this,t,f);}); + return this.each(function(){$.event.remove(this,t,f);}); }, trigger: function(t) { - return this.each(function(){triggerEvent(this,t);}); + return this.each(function(){$.event.trigger(this,t);}); }, find: function(t) { @@ -315,6 +315,8 @@ function $(a,c) { return self; } +$.eval = eval; + $.apply = function(o,f,a) { a = a || []; if ( f.apply ) { @@ -325,7 +327,7 @@ $.apply = function(o,f,a) { p[i] = 'a['+i+']'; } o.$$exec = this; - var r = eval('o.$$exec(' + p.join(',') + ')'); + var r = $.eval('o.$$exec(' + p.join(',') + ')'); o.$$exec = null; return r; } @@ -620,7 +622,7 @@ $.filter = function(t,r,not) { } if ( f !== null ) { - eval('f = function(a,i){return ' + f + '}'); + $.eval('f = function(a,i){return ' + f + '}'); r = g( r, f ); } } @@ -728,11 +730,13 @@ $.map = function(a,f) { return r; }; +$.event = {}; + // Bind an event to an element // Original by Dean Edwards -function addEvent(element, type, handler) { +$.event.add = function(element, type, handler) { if ( element.location ) { element = window; } // Ughhhhh.... - if (!handler.$$guid) { handler.$$guid = addEvent.guid++; } + if (!handler.$$guid) { handler.$$guid = $.event.add.guid++; } if (!element.events) { element.events = {}; } var handlers = element.events[type]; if (!handlers) { @@ -742,13 +746,13 @@ function addEvent(element, type, handler) { } } handlers[handler.$$guid] = handler; - element["on" + type] = handleEvent; -} + element["on" + type] = $.event.handle; +}; -addEvent.guid = 1; +$.event.add.guid = 1; // Detach an event or set of events from an element -function removeEvent(element, type, handler) { +$.event.remove = function(element, type, handler) { if (element.events) { if (type && element.events[type]) { if ( handler ) { @@ -760,22 +764,22 @@ function removeEvent(element, type, handler) { } } else { for ( var j in element.events ) { - removeEvent( element, j ); + $.event.remove( element, j ); } } } -} +}; -function triggerEvent(element,type,data) { +$.event.trigger = function(element,type,data) { data = data || [{ type: type }]; if ( element && element["on" + type] ) { $.apply( element, element["on" + type], data ); } -} +}; -function handleEvent(event) { +$.event.handle = function(event) { var returnValue = true; - event = event || fixEvent(window.event); + event = event || $.event.fix(window.event); var handlers = []; for ( var j in this.events[event.type] ) { handlers[handlers.length] = this.events[event.type][j]; @@ -793,19 +797,19 @@ function handleEvent(event) { } catch(e){} } return returnValue; -} +}; -function fixEvent(event) { - event.preventDefault = fixEvent.preventDefault; - event.stopPropagation = fixEvent.stopPropagation; +$.event.fix = function(event) { + event.preventDefault = $.event.fix.preventDefault; + event.stopPropagation = $.event.fix.stopPropagation; return event; -} +}; -fixEvent.preventDefault = function() { +$.event.fix.preventDefault = function() { this.returnValue = false; }; -fixEvent.stopPropagation = function() { +$.event.fix.stopPropagation = function() { this.cancelBubble = true; };