X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcore.js;h=e1a3a6df2523b0d335cbfcf1ebe114513efbc186;hb=1533bf7c7794d8e078e7ce5e36de6d190651540a;hp=16d052963b90cfcbfa00f6773b4768efa64f86be;hpb=a4043cdcbfa1ca069265a7cbcaba269629789cc0;p=jquery.git diff --git a/src/core.js b/src/core.js index 16d0529..e1a3a6d 100644 --- a/src/core.js +++ b/src/core.js @@ -1,3 +1,5 @@ +(function() { + // Define a local copy of jQuery var jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' @@ -11,14 +13,14 @@ var jQuery = function( selector, context ) { _$ = window.$, // Use the correct document accordingly with window argument (sandbox) - document = window.document, + //document = window.document, // A central reference to the root jQuery(document) rootjQuery, // A simple way to check for HTML strings or ID strings // (both of which we optimize for) - quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w-]+)$/, + quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w\-]+)$/, // Is it a simple selector isSimple = /^.[^:#\[\.,]*$/, @@ -27,7 +29,8 @@ var jQuery = function( selector, context ) { rnotwhite = /\S/, // Used for trimming whitespace - rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g, + trimLeft = /^\s+/, + trimRight = /\s+$/, // Match a standalone tag rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/, @@ -52,6 +55,7 @@ var jQuery = function( selector, context ) { hasOwn = Object.prototype.hasOwnProperty, push = Array.prototype.push, slice = Array.prototype.slice, + trim = String.prototype.trim, indexOf = Array.prototype.indexOf; jQuery.fn = jQuery.prototype = { @@ -567,9 +571,20 @@ jQuery.extend({ return object; }, - trim: function( text ) { - return (text || "").replace( rtrim, "" ); - }, + // Use native String.trim function wherever possible + trim: trim ? + function( text ) { + return text == null ? + "" : + trim.call( text ); + } : + + // Otherwise use our own trimming functionality + function( text ) { + return text == null ? + "" : + text.toString().replace( trimLeft, "" ).replace( trimRight, "" ); + }, // results is for internal usage only makeArray: function( array, results ) { @@ -623,12 +638,14 @@ jQuery.extend({ }, grep: function( elems, callback, inv ) { - var ret = []; + var ret = [], retVal; + inv = !!inv; // Go through the array, only saving the items // that pass the validator function for ( var i = 0, length = elems.length; i < length; i++ ) { - if ( !inv !== !callback( elems[ i ], i ) ) { + retVal = !!callback( elems[ i ], i ); + if ( inv !== retVal ) { ret.push( elems[ i ] ); } } @@ -684,6 +701,39 @@ jQuery.extend({ return proxy; }, + // Mutifunctional method to get and set values to a collection + // The value/s can be optionally by executed if its a function + access: function( elems, key, value, exec, fn, pass ) { + var length = elems.length; + + // Setting many attributes + if ( typeof key === "object" ) { + for ( var k in key ) { + jQuery.access( elems, k, key[k], exec, fn, value ); + } + return elems; + } + + // Setting one attribute + if ( value !== undefined ) { + // Optionally, function values get executed if exec is true + exec = !pass && exec && jQuery.isFunction(value); + + for ( var i = 0; i < length; i++ ) { + fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass ); + } + + return elems; + } + + // Getting an attribute + return length ? fn( elems[0], key ) : undefined; + }, + + now: function() { + return (new Date()).getTime(); + }, + // Use of jQuery.browser is frowned upon. // More details: http://docs.jquery.com/Utilities/jQuery.browser uaMatch: function( ua ) { @@ -718,6 +768,13 @@ if ( indexOf ) { }; } +// Verify that \s matches non-breaking spaces +// (IE fails on this test) +if ( !/\s/.test( "\xA0" ) ) { + trimLeft = /^[\s\xA0]+/; + trimRight = /[\s\xA0]+$/; +} + // All jQuery objects should point back to these rootjQuery = jQuery(document); @@ -748,7 +805,7 @@ function doScrollCheck() { // If IE is used, use the trick by Diego Perini // http://javascript.nwbox.com/IEContentLoaded/ document.documentElement.doScroll("left"); - } catch( error ) { + } catch(e) { setTimeout( doScrollCheck, 1 ); return; } @@ -757,51 +814,7 @@ function doScrollCheck() { jQuery.ready(); } -function evalScript( i, elem ) { - if ( elem.src ) { - jQuery.ajax({ - url: elem.src, - async: false, - dataType: "script" - }); - } else { - jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" ); - } - - if ( elem.parentNode ) { - elem.parentNode.removeChild( elem ); - } -} +// Expose jQuery to the global object +window.jQuery = window.$ = jQuery; -// 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, pass ) { - var length = elems.length; - - // Setting many attributes - if ( typeof key === "object" ) { - for ( var k in key ) { - access( elems, k, key[k], exec, fn, value ); - } - return elems; - } - - // Setting one attribute - if ( value !== undefined ) { - // Optionally, function values get executed if exec is true - exec = !pass && exec && jQuery.isFunction(value); - - for ( var i = 0; i < length; i++ ) { - fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass ); - } - - return elems; - } - - // Getting an attribute - return length ? fn( elems[0], key ) : undefined; -} - -function now() { - return (new Date()).getTime(); -} +})();