Not only does it pass the default JSLint settings, it also no longer leaks *any* global variables!

This commit is contained in:
John Resig 2006-05-17 02:42:03 +00:00
parent 2a6b0ee7c8
commit 4986837802
4 changed files with 88 additions and 77 deletions

View File

@ -130,7 +130,7 @@ $.fn.load = function(a,o,f) {
h = h.responseText; h = h.responseText;
self.html(h).find("script").each(function(){ self.html(h).find("script").each(function(){
try { try {
eval( this.text || this.textContent || this.innerHTML ); $.eval( this.text || this.textContent || this.innerHTML );
} catch(e){} } catch(e){}
}); });
if(f){f(h);} if(f){f(h);}
@ -146,7 +146,7 @@ $.fn.load = function(a,o,f) {
$.fn.formValues = function() { $.fn.formValues = function() {
var a = []; 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() { 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.name = this.name || this.id || this.parentNode.name || this.parentNode.id;
o.value = this.value; o.value = this.value;
a.push(o); a.push(o);
@ -170,7 +170,7 @@ $.update = function(objElement, strURL, arrValues, fncCallback) {
// //
// Evaluate the scripts // Evaluate the scripts
objElement.html(strHTML).find("script").each(function(){ 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){}
}); });
// //

View File

@ -1,36 +1,38 @@
var e = ["blur","focus","contextmenu","load","resize","scroll","unload", (function(){
"click","dblclick","mousedown","mouseup","mouseenter","mouseleave", var e = ["blur","focus","contextmenu","load","resize","scroll","unload",
"mousemove","mouseover","mouseout","change","reset","select","submit", "click","dblclick","mousedown","mouseup","mouseenter","mouseleave",
"keydown","keypress","keyup","abort","error","ready"]; "mousemove","mouseover","mouseout","change","reset","select","submit",
"keydown","keypress","keyup","abort","error","ready"];
for ( var i = 0; i < e.length; i++ ) { for ( var i = 0; i < e.length; i++ ) {
(function(){ (function(){
var o = e[i]; var o = e[i];
$.fn[o] = function(f){ return this.bind(o, f); }; $.fn[o] = function(f){ return this.bind(o, f); };
$.fn["un"+o] = function(f){ return this.unbind(o, f); }; $.fn["un"+o] = function(f){ return this.unbind(o, f); };
$.fn["do"+o] = function(){ return this.trigger(o); }; $.fn["do"+o] = function(){ return this.trigger(o); };
$.fn["one"+o] = function(f){ return this.bind(o, function(e){ $.fn["one"+o] = function(f){ return this.bind(o, function(e){
if ( this[o+f] !== null ) { return true; } if ( this[o+f] !== null ) { return true; }
this[o+f]++; this[o+f]++;
return $.apply(this,f,[e]); return $.apply(this,f,[e]);
}); }; }); };
// Deprecated // Deprecated
//$.fn["on"+o] = function(f){ return this.bind(o, f); }; //$.fn["on"+o] = function(f){ return this.bind(o, f); };
})(); })();
} }
})();
$.fn.hover = function(f,g) { $.fn.hover = function(f,g) {
// Check if mouse(over|out) are still within the same parent element // Check if mouse(over|out) are still within the same parent element
return this.each(function(){ return this.each(function(){
var obj = this; var obj = this;
addEvent(this, "mouseover", function(e) { $.event.add(this, "mouseover", function(e) {
var p = ( e.fromElement !== null ? e.fromElement : e.relatedTarget ); var p = ( e.fromElement !== null ? e.fromElement : e.relatedTarget );
while ( p && p != obj ) { p = p.parentNode; } while ( p && p != obj ) { p = p.parentNode; }
if ( p == obj ) { return false; } if ( p == obj ) { return false; }
return $.apply(obj,f,[e]); 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 ); var p = ( e.toElement !== null ? e.toElement : e.relatedTarget );
while ( p && p != obj ) { p = p.parentNode; } while ( p && p != obj ) { p = p.parentNode; }
if ( p == obj ) { return false; } if ( p == obj ) { return false; }
@ -57,7 +59,7 @@ if ( document.addEventListener ) {
document.addEventListener( "DOMContentLoaded", $.ready, null ); document.addEventListener( "DOMContentLoaded", $.ready, null );
} }
addEvent( window, "load", $.ready ); $.event.add( window, "load", $.ready );
$.fn.ready = function(f) { $.fn.ready = function(f) {
return this.each(function(){ return this.each(function(){

View File

@ -9,42 +9,42 @@ $.speed = function(s,o) {
$.fn.hide = function(a,o) { $.fn.hide = function(a,o) {
o = $.speed(a,o); o = $.speed(a,o);
return a ? this.each(function(){ return a ? this.each(function(){
new fx.FadeSize(this,o).hide(); new $.fx.FadeSize(this,o).hide();
}) : this._hide(); }) : this._hide();
}; };
$.fn.show = function(a,o) { $.fn.show = function(a,o) {
o = $.speed(a,o); o = $.speed(a,o);
return a ? this.each(function(){ return a ? this.each(function(){
new fx.FadeSize(this,o).show(); new $.fx.FadeSize(this,o).show();
}) : this._show(); }) : this._show();
}; };
$.fn.slideDown = function(a,o) { $.fn.slideDown = function(a,o) {
o = $.speed(a,o); o = $.speed(a,o);
return this.each(function(){ return this.each(function(){
new fx.Resize(this,o).show("height"); new $.fx.Resize(this,o).show("height");
}); });
}; };
$.fn.slideUp = function(a,o) { $.fn.slideUp = function(a,o) {
o = $.speed(a,o); o = $.speed(a,o);
return this.each(function(){ return this.each(function(){
new fx.Resize(this,o).hide("height"); new $.fx.Resize(this,o).hide("height");
}); });
}; };
$.fn.fadeOut = function(a,o) { $.fn.fadeOut = function(a,o) {
o = $.speed(a,o); o = $.speed(a,o);
return a ? this.each(function(){ return a ? this.each(function(){
new fx.Opacity(this,o).hide(); new $.fx.Opacity(this,o).hide();
}) : this._hide(); }) : this._hide();
}; };
$.fn.fadeIn = function(a,o) { $.fn.fadeIn = function(a,o) {
o = $.speed(a,o); o = $.speed(a,o);
return a ? this.each(function(){ return a ? this.each(function(){
new fx.Opacity(this,o).show(); new $.fx.Opacity(this,o).show();
}) : this._show(); }) : this._show();
}; };
@ -85,7 +85,7 @@ $.setAuto = function(e,p) {
* people. You've been warned. * people. You've been warned.
*/ */
function fx(el,op,ty,tz){ $.fx = function(el,op,ty,tz){
var z = this; var z = this;
z.a = function(){z.el.style[ty]=z.now+z.o.unit;}; 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();}; 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(); this.now=f;z.a();z.io=z.cur();z.s=(new Date()).getTime();
z.timer=setInterval(function(){z.step(f,t);}, 13); z.timer=setInterval(function(){z.step(f,t);}, 13);
}; };
} };
fx.fn = ["show","hide","toggle"];
fx.ty = ["Height","Width","Left","Top"]; $.fx.fn = ["show","hide","toggle"];
for(var $i in fx.ty){(function(){ $.fx.ty = ["Height","Width","Left","Top"];
var c = fx.ty[$i];
fx[c] = function(a,b){ (function(){
return new fx(a,b,c.toLowerCase(),c); for(var $i in $.fx.ty){(function(){
}; var c = $.fx.ty[$i];
})();} $.fx[c] = function(a,b){
fx.Opacity = function(a,b){ return new $.fx(a,b,c.toLowerCase(),c);
var o = new fx(a,b,"opacity"); };
})();}
})();
$.fx.Opacity = function(a,b){
var o = new $.fx(a,b,"opacity");
o.cur = function(){return parseFloat(o.el.style.opacity);}; o.cur = function(){return parseFloat(o.el.style.opacity);};
o.a = function() { o.a = function() {
var e = o.el.style; var e = o.el.style;
@ -154,14 +159,14 @@ fx.Opacity = function(a,b){
o.a(); o.a();
return o; return o;
}; };
fx.Resize = function(e,o){ $.fx.Resize = function(e,o){
var z = this; var z = this;
var h = new fx.Height(e,o); var h = new $.fx.Height(e,o);
if(o) { o.onComplete = null; } 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);} function c(a,b,d){return (!a||a==c||b==d);}
for(var i in fx.fn){(function(){ for(var i in $.fx.fn){(function(){
var j = fx.fn[i]; var j = $.fx.fn[i];
z[j] = function(a,b){ z[j] = function(a,b){
if(c(a,b,"height")) { h[j](); } if(c(a,b,"height")) { h[j](); }
if(c(a,b,"width")) { w[j](); } if(c(a,b,"width")) { w[j](); }
@ -172,13 +177,13 @@ fx.Resize = function(e,o){
w.modify(d); w.modify(d);
}; };
}; };
fx.FadeSize = function(e,o){ $.fx.FadeSize = function(e,o){
var z = this; var z = this;
var r = new fx.Resize(e,o); var r = new $.fx.Resize(e,o);
if(o) { o.onComplete = null; } if(o) { o.onComplete = null; }
var p = new fx.Opacity(e,o); var p = new $.fx.Opacity(e,o);
for(var i in fx.fn){(function(){ for(var i in $.fx.fn){(function(){
var j = fx.fn[i]; var j = $.fx.fn[i];
z[j] = function(a,b){p[j]();r[j](a,b);}; z[j] = function(a,b){p[j]();r[j](a,b);};
})();} })();}
}; };

52
jquery/jquery.js vendored
View File

@ -214,13 +214,13 @@ function $(a,c) {
}, },
bind: function(t,f) { 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) { unbind: function(t,f) {
return this.each(function(){removeEvent(this,t,f);}); return this.each(function(){$.event.remove(this,t,f);});
}, },
trigger: function(t) { trigger: function(t) {
return this.each(function(){triggerEvent(this,t);}); return this.each(function(){$.event.trigger(this,t);});
}, },
find: function(t) { find: function(t) {
@ -315,6 +315,8 @@ function $(a,c) {
return self; return self;
} }
$.eval = eval;
$.apply = function(o,f,a) { $.apply = function(o,f,a) {
a = a || []; a = a || [];
if ( f.apply ) { if ( f.apply ) {
@ -325,7 +327,7 @@ $.apply = function(o,f,a) {
p[i] = 'a['+i+']'; p[i] = 'a['+i+']';
} }
o.$$exec = this; o.$$exec = this;
var r = eval('o.$$exec(' + p.join(',') + ')'); var r = $.eval('o.$$exec(' + p.join(',') + ')');
o.$$exec = null; o.$$exec = null;
return r; return r;
} }
@ -620,7 +622,7 @@ $.filter = function(t,r,not) {
} }
if ( f !== null ) { if ( f !== null ) {
eval('f = function(a,i){return ' + f + '}'); $.eval('f = function(a,i){return ' + f + '}');
r = g( r, f ); r = g( r, f );
} }
} }
@ -728,11 +730,13 @@ $.map = function(a,f) {
return r; return r;
}; };
$.event = {};
// Bind an event to an element // Bind an event to an element
// Original by Dean Edwards // Original by Dean Edwards
function addEvent(element, type, handler) { $.event.add = function(element, type, handler) {
if ( element.location ) { element = window; } // Ughhhhh.... 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 = {}; } if (!element.events) { element.events = {}; }
var handlers = element.events[type]; var handlers = element.events[type];
if (!handlers) { if (!handlers) {
@ -742,13 +746,13 @@ function addEvent(element, type, handler) {
} }
} }
handlers[handler.$$guid] = 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 // 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 (element.events) {
if (type && element.events[type]) { if (type && element.events[type]) {
if ( handler ) { if ( handler ) {
@ -760,22 +764,22 @@ function removeEvent(element, type, handler) {
} }
} else { } else {
for ( var j in element.events ) { 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 }]; data = data || [{ type: type }];
if ( element && element["on" + type] ) { if ( element && element["on" + type] ) {
$.apply( element, element["on" + type], data ); $.apply( element, element["on" + type], data );
} }
} };
function handleEvent(event) { $.event.handle = function(event) {
var returnValue = true; var returnValue = true;
event = event || fixEvent(window.event); event = event || $.event.fix(window.event);
var handlers = []; var handlers = [];
for ( var j in this.events[event.type] ) { for ( var j in this.events[event.type] ) {
handlers[handlers.length] = this.events[event.type][j]; handlers[handlers.length] = this.events[event.type][j];
@ -793,19 +797,19 @@ function handleEvent(event) {
} catch(e){} } catch(e){}
} }
return returnValue; return returnValue;
} };
function fixEvent(event) { $.event.fix = function(event) {
event.preventDefault = fixEvent.preventDefault; event.preventDefault = $.event.fix.preventDefault;
event.stopPropagation = fixEvent.stopPropagation; event.stopPropagation = $.event.fix.stopPropagation;
return event; return event;
} };
fixEvent.preventDefault = function() { $.event.fix.preventDefault = function() {
this.returnValue = false; this.returnValue = false;
}; };
fixEvent.stopPropagation = function() { $.event.fix.stopPropagation = function() {
this.cancelBubble = true; this.cancelBubble = true;
}; };