X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcore.js;h=0d5588c269b4eecadc2e81c31afcabc73c32dda0;hb=9997620420ec0baf7dba8ce9dee0f19e616e1bc4;hp=81779fdb20910dc5947831774cdc8a7af1b668bd;hpb=d3dc2d1234af10cb856519a01a97358eb4103008;p=jquery.git diff --git a/src/core.js b/src/core.js index 81779fd..0d5588c 100644 --- a/src/core.js +++ b/src/core.js @@ -1,9 +1,7 @@ // Define a local copy of jQuery var jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' - return arguments.length === 0 ? - rootjQuery : - new jQuery.fn.init( selector, context ); + return new jQuery.fn.init( selector, context ); }, // Map over jQuery in case of overwrite @@ -43,6 +41,9 @@ var jQuery = function( selector, context ) { // The functions to execute on DOM ready readyList = [], + // The ready event handler + DOMContentLoaded, + // Save a reference to some core methods toString = Object.prototype.toString, hasOwnProperty = Object.prototype.hasOwnProperty, @@ -52,7 +53,7 @@ var jQuery = function( selector, context ) { jQuery.fn = jQuery.prototype = { init: function( selector, context ) { - var match, elem, ret, doc; + var match, elem, ret, doc, parent; // Handle $(""), $(null), or $(undefined) if ( !selector ) { @@ -87,7 +88,12 @@ jQuery.fn = jQuery.prototype = { } else { ret = buildFragment( [ match[1] ], [ doc ] ); - selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes; + parent = ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment; + selector = []; + + while ( parent.firstChild ) { + selector.push( parent.removeChild( parent.firstChild ) ); + } } // HANDLE: $("#id") @@ -212,32 +218,13 @@ jQuery.fn = jQuery.prototype = { each: function( callback, args ) { return jQuery.each( this, callback, args ); }, - - // Determine the position of an element within - // the matched set of elements - index: function( elem ) { - if ( !elem || typeof elem === "string" ) { - return jQuery.inArray( this[0], - // If it receives a string, the selector is used - // If it receives nothing, the siblings are used - elem ? jQuery( elem ) : this.parent().children() ); - } - // Locate the position of the desired element - return jQuery.inArray( - // If it receives a jQuery object, the first element is used - elem.jquery ? elem[0] : elem, this ); - }, - - is: function( selector ) { - return !!selector && jQuery.filter( selector, this ).length > 0; - }, ready: function( fn ) { // Attach the listeners jQuery.bindReady(); // If the DOM is already ready - if ( jQuery.isReady ) { + if ( jQuery.isReady && !readyList ) { // Execute the function immediately fn.call( document, jQuery ); @@ -249,6 +236,35 @@ jQuery.fn = jQuery.prototype = { return this; }, + + eq: function( i ) { + return i === -1 ? + this.slice( i ) : + this.slice( i, +i + 1 ); + }, + + first: function() { + return this.eq( 0 ); + }, + + last: function() { + return this.eq( -1 ); + }, + + slice: function() { + return this.pushStack( slice.apply( this, arguments ), + "slice", slice.call(arguments).join(",") ); + }, + + map: function( callback ) { + return this.pushStack( jQuery.map(this, function(elem, i){ + return callback.call( elem, i, elem ); + })); + }, + + end: function() { + return this.prevObject || jQuery(null); + }, // For internal use only. // Behaves like an Array's method, not like a jQuery method. @@ -334,6 +350,7 @@ jQuery.extend({ ready: function() { // Make sure that the DOM is not already loaded if ( !jQuery.isReady ) { + // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). if ( !document.body ) { return setTimeout( jQuery.ready, 13 ); } @@ -373,10 +390,7 @@ jQuery.extend({ // Mozilla, Opera and webkit nightlies currently support this event if ( document.addEventListener ) { // Use the handy event callback - document.addEventListener( "DOMContentLoaded", function DOMContentLoaded() { - document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); - jQuery.ready(); - }, false ); + document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); // A fallback to window.onload, that will always work window.addEventListener( "load", jQuery.ready, false ); @@ -385,13 +399,7 @@ jQuery.extend({ } else if ( document.attachEvent ) { // ensure firing before onload, // maybe late but safe also for iframes - document.attachEvent("onreadystatechange", function onreadystatechange() { - // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). - if ( document.readyState === "complete" ) { - document.detachEvent( "onreadystatechange", onreadystatechange ); - jQuery.ready(); - } - }); + document.attachEvent("onreadystatechange", DOMContentLoaded); // A fallback to window.onload, that will always work window.attachEvent( "onload", jQuery.ready ); @@ -406,24 +414,6 @@ jQuery.extend({ if ( document.documentElement.doScroll && toplevel ) { doScrollCheck(); - - function doScrollCheck() { - if ( jQuery.isReady ) { - return; - } - - try { - // If IE is used, use the trick by Diego Perini - // http://javascript.nwbox.com/IEContentLoaded/ - document.documentElement.doScroll("left"); - } catch( error ) { - setTimeout( doScrollCheck, 1 ); - return; - } - - // and execute any waiting functions - jQuery.ready(); - } } } }, @@ -446,8 +436,8 @@ jQuery.extend({ // not own constructor property must be Object if ( obj.constructor - && !hasOwnProperty.call(obj, "constructor") - && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) { + && !hasOwnProperty.call(obj, "constructor") + && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) { return false; } @@ -483,7 +473,7 @@ jQuery.extend({ script.text = data; } - // Use insertBefore instead of appendChild to circumvent an IE6 bug. + // Use insertBefore instead of appendChild to circumvent an IE6 bug. // This arises when a base node is used (#2709). head.insertBefore( script, head.firstChild ); head.removeChild( script ); @@ -569,23 +559,20 @@ jQuery.extend({ }, merge: function( first, second ) { - var pos, i = second.length; + var i = first.length, j = 0; - // We have to get length this way when IE & Opera overwrite the length - // expando of getElementsByTagName - if ( i && i.nodeType ) { - for ( i = 0; second[i]; ++i ) {} - } - - pos = i + first.length; - - // Correct length for non Arrays - first.length = pos; - - while ( i ) { - first[ --pos ] = second[ --i ]; + if ( typeof second.length === "number" ) { + for ( var l = second.length; j < l; j++ ) { + first[ i++ ] = second[ j ]; + } + } else { + while ( second[j] !== undefined ) { + first[ i++ ] = second[ j++ ]; + } } + first.length = i; + return first; }, @@ -620,18 +607,20 @@ jQuery.extend({ return ret.concat.apply( [], ret ); }, - // Use of jQuery.browser is deprecated. - // It's included for backwards compatibility and plugins, - // although they should work to migrate away. + // Use of jQuery.browser is frowned upon. + // More details: http://docs.jquery.com/Utilities/jQuery.browser browser: { version: (/.*?(?:firefox|safari|opera|msie)[\/ ]([\d.]+)/.exec(userAgent) || [0,'0'])[1], safari: /safari/.test( userAgent ), opera: /opera/.test( userAgent ), msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ), - mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ) + firefox: /firefox/.test( userAgent ) } }); +// Deprecated +jQuery.browser.mozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ); + if ( indexOf ) { jQuery.inArray = function( elem, array ) { return indexOf.call( array, elem ); @@ -641,6 +630,48 @@ if ( indexOf ) { // All jQuery objects should point back to these rootjQuery = jQuery(document); +// Cleanup functions for the document ready method +if ( document.addEventListener ) { + DOMContentLoaded = function() { + document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); + jQuery.ready(); + }; + +} else if ( document.attachEvent ) { + DOMContentLoaded = function() { + // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). + if ( document.readyState === "complete" ) { + document.detachEvent( "onreadystatechange", DOMContentLoaded ); + jQuery.ready(); + } + }; +} + +// The DOM ready check for Internet Explorer +function doScrollCheck() { + if ( jQuery.isReady ) { + return; + } + + try { + // If IE is used, use the trick by Diego Perini + // http://javascript.nwbox.com/IEContentLoaded/ + document.documentElement.doScroll("left"); + } catch( error ) { + setTimeout( doScrollCheck, 1 ); + return; + } + + // and execute any waiting functions + jQuery.ready(); +} + +if ( indexOf ) { + jQuery.inArray = function( elem, array ) { + return indexOf.call( array, elem ); + }; +} + function evalScript( i, elem ) { if ( elem.src ) { jQuery.ajax({ @@ -660,31 +691,30 @@ function evalScript( i, elem ) { // Mutifunctional method to get and set values to a collection // The value/s can be optionally by executed if its a function function access( elems, key, value, exec, fn ) { - var l = elems.length; + var length = elems.length; // Setting many attributes if ( typeof key === "object" ) { - for (var k in key) { - access(elems, k, key[k], exec, fn); - } + for ( var k in key ) { + access( elems, k, key[k], exec, fn ); + } return elems; } // Setting one attribute - if (value !== undefined) { + if ( value !== undefined ) { // Optionally, function values get executed if exec is true exec = exec && jQuery.isFunction(value); - for (var i = 0; i < l; i++) { - var elem = elems[i], - val = exec ? value.call(elem, i) : value; - fn(elem, key, val); + for ( var i = 0; i < length; i++ ) { + fn( elems[i], key, exec ? value.call( elems[i], i ) : value ); } + return elems; } // Getting an attribute - return l ? fn(elems[0], key) : null; + return length ? fn( elems[0], key ) : null; } function now() {