From 17b1e407d101c5c7f91db633df3a80cd9b4466ae Mon Sep 17 00:00:00 2001 From: Ariel Flesler Date: Tue, 29 Apr 2008 23:34:50 +0000 Subject: [PATCH] mainly made the code shorter: - removed some needless if's - replace multiple "var x" for one, comma separated declaration. - added a local fn called now() for the (new Date)s - fixed the indentation of a block, and a typo in a comment. - used fn instead of prototype where possible - jquery fx: exposed the speeds hash as jQuery.fx.speeds. Also fixed (again) line endings --- src/ajax.js | 10 ++++---- src/core.js | 46 ++++++++++++++++------------------ src/event.js | 74 +++++++++++++++++++++++++++---------------------------- src/fx.js | 66 +++++++++++++++++++++++++++---------------------- src/selector.js | 12 +++++---- 5 files changed, 105 insertions(+), 103 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index ca17f4f..b089f22 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -91,7 +91,7 @@ jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".sp }; }); -var jsc = (new Date).getTime(); +var jsc = now(); jQuery.extend({ get: function( url, data, callback, type ) { @@ -211,7 +211,7 @@ jQuery.extend({ s.cache = false; if ( s.cache === false && s.type.toLowerCase() == "get" ) { - var ts = (new Date()).getTime(); + var ts = now(); // try replacing _= if it is there var ret = s.url.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2"); // if nothing was replaced, add timestamp to the end @@ -448,9 +448,9 @@ jQuery.extend({ }, httpData: function( r, type ) { - var ct = r.getResponseHeader("content-type"); - var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0; - var data = xml ? r.responseXML : r.responseText; + var ct = r.getResponseHeader("content-type"), + xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0, + data = xml ? r.responseXML : r.responseText; if ( xml && data.documentElement.tagName == "parsererror" ) throw "parsererror"; diff --git a/src/core.js b/src/core.js index c685b4e..1353eba 100644 --- a/src/core.js +++ b/src/core.js @@ -10,27 +10,21 @@ */ // Map over jQuery in case of overwrite -if ( window.jQuery ) - var _jQuery = window.jQuery; +var _jQuery = window.jQuery, +// Map over the $ in case of overwrite + _$ = window.$; -var jQuery = window.jQuery = function( selector, context ) { +var jQuery = window.jQuery = window.$ = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' - return new jQuery.prototype.init( selector, context ); + return new jQuery.fn.init( selector, context ); }; -// Map over the $ in case of overwrite -if ( window.$ ) - var _$ = window.$; - -// Map the jQuery namespace to the '$' one -window.$ = jQuery; - // A simple way to check for HTML strings or ID strings // (both of which we optimize for) -var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/; +var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/, // Is it a simple selector -var isSimple = /^.[^:#\[\.]*$/; + isSimple = /^.[^:#\[\.]*$/; jQuery.fn = jQuery.prototype = { init: function( selector, context ) { @@ -359,9 +353,7 @@ jQuery.fn = jQuery.prototype = { }, is: function( selector ) { - return selector ? - jQuery.multiFilter( selector, this ).length > 0 : - false; + return !!selector && jQuery.multiFilter( selector, this ).length > 0; }, hasClass: function( selector ) { @@ -536,7 +528,7 @@ jQuery.fn = jQuery.prototype = { }; // Give the init function the jQuery prototype for later instantiation -jQuery.prototype.init.prototype = jQuery.prototype; +jQuery.fn.init.prototype = jQuery.fn; function evalScript( i, elem ) { if ( elem.src ) @@ -553,6 +545,10 @@ function evalScript( i, elem ) { elem.parentNode.removeChild( elem ); } +function now(){ + return +new Date; +} + jQuery.extend = jQuery.fn.extend = function() { // copy reference to target object var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options; @@ -598,12 +594,12 @@ jQuery.extend = jQuery.fn.extend = function() { return target; }; -var expando = "jQuery" + (new Date()).getTime(), uuid = 0, windowData = {}; +var expando = "jQuery" + now(), uuid = 0, windowData = {}, // exclude the following css properties to add px -var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i; + exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, // cache getComputedStyle -var getComputedStyle = document.defaultView && document.defaultView.getComputedStyle; + getComputedStyle = document.defaultView && document.defaultView.getComputedStyle; jQuery.extend({ noConflict: function( deep ) { @@ -874,15 +870,15 @@ jQuery.extend({ // If the element isn't reporting its values properly in Safari // then some display: none elements are involved else { - var swap = [], stack = []; + var swap = [], stack = [], a = elem, i = 0; // Locate all of the parent display: none elements - for ( var a = elem; a && color(a); a = a.parentNode ) + for ( ; a && color(a); a = a.parentNode ) stack.unshift(a); // Go through and make them visible, but in reverse // (It would be better if we knew the exact display type that they had) - for ( var i = 0; i < stack.length; i++ ) + for ( ; i < stack.length; i++ ) if ( color( stack[ i ] ) ) { swap[ i ] = stack[ i ].style.display; stack[ i ].style.display = "block"; @@ -895,7 +891,7 @@ jQuery.extend({ ( computedStyle && computedStyle.getPropertyValue( name ) ) || ""; // Finally, revert the display styles back - for ( var i = 0; i < swap.length; i++ ) + for ( i = 0; i < swap.length; i++ ) if ( swap[ i ] != null ) stack[ i ].style.display = swap[ i ]; } @@ -946,7 +942,7 @@ jQuery.extend({ return; if ( elem.constructor == Number ) - elem = elem.toString(); + elem += ''; // Convert html string into DOM nodes if ( typeof elem == "string" ) { diff --git a/src/event.js b/src/event.js index 5f0adff..8e52081 100644 --- a/src/event.js +++ b/src/event.js @@ -13,7 +13,7 @@ jQuery.event = { // For whatever reason, IE has trouble passing the window object // around, causing it to be cloned in the process - if ( jQuery.browser.msie && elem.setInterval != undefined ) + if ( jQuery.browser.msie && elem.setInterval ) elem = window; // Make sure that the function being executed has a unique ID @@ -51,39 +51,39 @@ jQuery.event = { // event in IE. handle.elem = elem; - // Handle multiple events seperated by a space - // jQuery(...).bind("mouseover mouseout", fn); - jQuery.each(types.split(/\s+/), function(index, type) { - // Namespaced event handlers - var parts = type.split("."); - type = parts[0]; - handler.type = parts[1]; - - // Get the current list of functions bound to this event - var handlers = events[type]; - - // Init the event handler queue - if (!handlers) { - handlers = events[type] = {}; - - // Check for a special event handler - // Only use addEventListener/attachEvent if the special - // events handler returns false - if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem) === false ) { - // Bind the global event handler to the element - if (elem.addEventListener) - elem.addEventListener(type, handle, false); - else if (elem.attachEvent) - elem.attachEvent("on" + type, handle); - } + // Handle multiple events separated by a space + // jQuery(...).bind("mouseover mouseout", fn); + jQuery.each(types.split(/\s+/), function(index, type) { + // Namespaced event handlers + var parts = type.split("."); + type = parts[0]; + handler.type = parts[1]; + + // Get the current list of functions bound to this event + var handlers = events[type]; + + // Init the event handler queue + if (!handlers) { + handlers = events[type] = {}; + + // Check for a special event handler + // Only use addEventListener/attachEvent if the special + // events handler returns false + if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem) === false ) { + // Bind the global event handler to the element + if (elem.addEventListener) + elem.addEventListener(type, handle, false); + else if (elem.attachEvent) + elem.attachEvent("on" + type, handle); } + } - // Add the function to the element's handler list - handlers[handler.guid] = handler; + // Add the function to the element's handler list + handlers[handler.guid] = handler; - // Keep track of which events have been used, for global triggering - jQuery.event.global[type] = true; - }); + // Keep track of which events have been used, for global triggering + jQuery.event.global[type] = true; + }); // Nullify elem to prevent memory leaks in IE elem = null; @@ -190,7 +190,7 @@ jQuery.event = { target: elem, preventDefault: function(){}, stopPropagation: function(){}, - timeStamp: +new Date + timeStamp: now() }); data[0][expando] = true; // no need to fix fake event } @@ -309,7 +309,7 @@ jQuery.event = { }; // Fix timeStamp - event.timeStamp = event.timeStamp || +new Date; + event.timeStamp = event.timeStamp || now(); // Fix target property, if necessary if ( !event.target ) @@ -380,7 +380,7 @@ jQuery.event = { // If we actually just moused on to a sub-element, ignore it if ( withinElement(event, this) ) return true; // Execute the right handlers by setting the event type to mouseenter - arguments[0].type = "mouseenter"; + event.type = "mouseenter"; return jQuery.event.handle.apply(this, arguments); } }, @@ -402,7 +402,7 @@ jQuery.event = { // If we actually just moused on to a sub-element, ignore it if ( withinElement(event, this) ) return true; // Execute the right handlers by setting the event type to mouseleave - arguments[0].type = "mouseleave"; + event.type = "mouseleave"; return jQuery.event.handle.apply(this, arguments); } } @@ -439,9 +439,7 @@ jQuery.fn.extend({ }, triggerHandler: function( type, data, fn ) { - if ( this[0] ) - return jQuery.event.trigger( type, data, this[0], false, fn ); - return undefined; + return this[0] && jQuery.event.trigger( type, data, this[0], false, fn ); }, toggle: function( fn ) { diff --git a/src/fx.js b/src/fx.js index d5185c5..d76dbdb 100644 --- a/src/fx.js +++ b/src/fx.js @@ -76,10 +76,10 @@ jQuery.fn.extend({ if ( this.nodeType != 1) return false; - var opt = jQuery.extend({}, optall); - var hidden = jQuery(this).is(":hidden"), self = this; + var opt = jQuery.extend({}, optall), p, + hidden = jQuery(this).is(":hidden"), self = this; - for ( var p in prop ) { + for ( p in prop ) { if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden ) return jQuery.isFunction(opt.complete) && opt.complete.apply(this); @@ -180,16 +180,16 @@ jQuery.fn.extend({ }); var queue = function( elem, type, array ) { - if ( !elem ) - return undefined; - - type = type || "fx"; - - var q = jQuery.data( elem, type + "queue" ); - - if ( !q || array ) - q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) ); + if ( elem ){ + + type = type || "fx"; + + var q = jQuery.data( elem, type + "queue" ); + + if ( !q || array ) + q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) ); + } return q; }; @@ -218,7 +218,7 @@ jQuery.extend({ opt.duration = (opt.duration && opt.duration.constructor == Number ? opt.duration : - { slow: 600, fast: 200 }[opt.duration]) || 400; + jQuery.fx.speeds[opt.duration]) || 400; // Queueing opt.old = opt.complete; @@ -280,7 +280,7 @@ jQuery.fx.prototype = { // Start an animation from one number to another custom: function(from, to, unit){ - this.startTime = (new Date()).getTime(); + this.startTime = now(); this.start = from; this.end = to; this.unit = unit || this.unit || "px"; @@ -343,7 +343,7 @@ jQuery.fx.prototype = { // Each step of an animation step: function(gotoEnd){ - var t = (new Date()).getTime(); + var t = now(); if ( gotoEnd || t > this.options.duration + this.startTime ) { this.now = this.end; @@ -401,20 +401,26 @@ jQuery.fx.prototype = { }; -jQuery.fx.step = { - scrollLeft: function(fx){ - fx.elem.scrollLeft = fx.now; - }, - - scrollTop: function(fx){ - fx.elem.scrollTop = fx.now; - }, - - opacity: function(fx){ - jQuery.attr(fx.elem.style, "opacity", fx.now); +jQuery.extend( jQuery.fx, { + speeds:{ + slow: 600, + fast: 200 }, - - _default: function(fx){ - fx.elem.style[ fx.prop ] = fx.now + fx.unit; + step: { + scrollLeft: function(fx){ + fx.elem.scrollLeft = fx.now; + }, + + scrollTop: function(fx){ + fx.elem.scrollTop = fx.now; + }, + + opacity: function(fx){ + jQuery.attr(fx.elem.style, "opacity", fx.now); + }, + + _default: function(fx){ + fx.elem.style[ fx.prop ] = fx.now + fx.unit; + } } -}; +}); diff --git a/src/selector.js b/src/selector.js index 72fe07e..f88f7e9 100644 --- a/src/selector.js +++ b/src/selector.js @@ -114,12 +114,13 @@ jQuery.extend({ t = jQuery.trim(t); - var foundToken = false; + var foundToken = false, // An attempt at speeding up child selectors that // point to a specific element tag - var re = quickChild; - var m = re.exec(t); + re = quickChild, + + m = re.exec(t); if ( m ) { nodeName = m[1].toUpperCase(); @@ -416,8 +417,8 @@ jQuery.extend({ }, dir: function( elem, dir ){ - var matched = []; - var cur = elem[dir]; + var matched = [], + cur = elem[dir]; while ( cur && cur != document ) { if ( cur.nodeType == 1 ) matched.push( cur ); @@ -449,3 +450,4 @@ jQuery.extend({ } }); + -- 1.7.10.4