X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcore.js;h=271e7c434fb8693ed09a4c8e39c0e3fa982be1ae;hb=e73990a566fcb2dac71e1a25ec83382645adc5b7;hp=a9e7536955d6f2c66a7e4115f8c93fb5fad2c536;hpb=cb3f842c8889458d96511b0103ebbff5cbac217d;p=jquery.git diff --git a/src/core.js b/src/core.js index a9e7536..271e7c4 100644 --- a/src/core.js +++ b/src/core.js @@ -10,7 +10,9 @@ var jQuery = window.jQuery = window.$ = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' - return new jQuery.fn.init( selector, context ); + return selector === undefined ? + rootjQuery : + new jQuery.fn.init( selector, context ); }, // A simple way to check for HTML strings or ID strings @@ -21,8 +23,11 @@ var jQuery.fn = jQuery.prototype = { init: function( selector, context ) { - // Make sure that a selection was provided - selector = selector || document; + // Handle $("") or $(null) + if ( !selector ) { + this.length = 0; + return this; + } // Handle $(DOMElement) if ( selector.nodeType ) { @@ -31,6 +36,7 @@ jQuery.fn = jQuery.prototype = { this.context = selector; return this; } + // Handle HTML strings if ( typeof selector === "string" ) { // Are we dealing with HTML string or an ID? @@ -40,34 +46,41 @@ jQuery.fn = jQuery.prototype = { if ( match && (match[1] || !context) ) { // HANDLE: $(html) -> $(array) - if ( match[1] ) + if ( match[1] ) { selector = jQuery.clean( [ match[1] ], context ); // HANDLE: $("#id") - else { + } else { var elem = document.getElementById( match[3] ); // Handle the case where IE and Opera return items // by name instead of ID - if ( elem && elem.id != match[3] ) - return jQuery().find( selector ); + if ( elem && elem.id != match[3] ) { + return rootjQuery.find( selector ); + } // Otherwise, we inject the element directly into the jQuery object - var ret = jQuery( elem || [] ); + var ret = jQuery( elem || null ); ret.context = document; ret.selector = selector; return ret; } - // HANDLE: $(expr, [context]) - // (which is just equivalent to: $(content).find(expr) - } else + // HANDLE: $(expr, $(...)) + } else if ( !context || context.jquery ) { + return (context || rootjQuery).find( selector ); + + // HANDLE: $(expr, context) + // (which is just equivalent to: $(context).find(expr) + } else { return jQuery( context ).find( selector ); + } // HANDLE: $(function) // Shortcut for document ready - } else if ( jQuery.isFunction( selector ) ) - return jQuery( document ).ready( selector ); + } else if ( jQuery.isFunction( selector ) ) { + return rootjQuery.ready( selector ); + } // Make sure that old selector state is passed along if ( selector.selector && selector.context ) { @@ -107,7 +120,7 @@ jQuery.fn = jQuery.prototype = { // (returning the new matched element set) pushStack: function( elems, name, selector ) { // Build a new jQuery matched element set - var ret = jQuery( elems ); + var ret = jQuery( elems || null ); // Add the old object onto the stack (as a reference) ret.prevObject = this; @@ -153,29 +166,42 @@ jQuery.fn = jQuery.prototype = { }, attr: function( name, value, type ) { - var options = name; + var options = name, isFunction = jQuery.isFunction( value ); // Look for the case where we're accessing a style value - if ( typeof name === "string" ) - if ( value === undefined ) - return this[0] && jQuery[ type || "attr" ]( this[0], name ); + if ( typeof name === "string" ) { + if ( value === undefined ) { + return this.length ? + jQuery[ type || "attr" ]( this[0], name ) : + null; - else { + } else { options = {}; options[ name ] = value; } + } // Check to see if we're setting style values - return this.each(function(i){ + for ( var i = 0, l = this.length; i < l; i++ ) { + var elem = this[i]; + // Set all the styles - for ( name in options ) - jQuery.attr( - type ? - this.style : - this, - name, jQuery.prop( this, options[ name ], type, i, name ) - ); - }); + for ( var prop in options ) { + value = options[prop]; + + if ( isFunction ) { + value = value.call( elem, i ); + } + + if ( typeof value === "number" && type === "curCSS" && !exclude.test(prop) ) { + value = value + "px"; + } + + jQuery.attr( type ? elem.style : elem, prop, value ); + } + } + + return this; }, css: function( key, value ) { @@ -263,7 +289,7 @@ jQuery.fn = jQuery.prototype = { }, end: function() { - return this.prevObject || jQuery( [] ); + return this.prevObject || jQuery(null); }, // For internal use only. @@ -273,16 +299,26 @@ jQuery.fn = jQuery.prototype = { splice: [].splice, find: function( selector ) { - if ( this.length === 1 ) { - var ret = this.pushStack( [], "find", selector ); - ret.length = 0; - jQuery.find( selector, this[0], ret ); - return ret; - } else { - return this.pushStack( jQuery.unique(jQuery.map(this, function(elem){ - return jQuery.find( selector, elem ); - })), "find", selector ); + var ret = this.pushStack( "", "find", selector ), length = 0; + + for ( var i = 0, l = this.length; i < l; i++ ) { + length = ret.length; + jQuery.find( selector, this[i], ret ); + + if ( i > 0 ) { + // Make sure that the results are unique + for ( var n = length; n < ret.length; n++ ) { + for ( var r = 0; r < length; r++ ) { + if ( ret[r] === ret[n] ) { + ret.splice(n--, 1); + break; + } + } + } + } } + + return ret; }, clone: function( events ) { @@ -626,7 +662,7 @@ jQuery.extend({ // check if an element is in a (or is an) XML document isXMLDoc: function( elem ) { return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" || - !!elem.ownerDocument && jQuery.isXMLDoc( elem.ownerDocument ); + !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML"; }, // Evalulates a script in a global context @@ -682,17 +718,6 @@ jQuery.extend({ return object; }, - prop: function( elem, value, type, i, name ) { - // Handle executable functions - if ( jQuery.isFunction( value ) ) - value = value.call( elem, i ); - - // Handle passing in a number to a CSS property - return typeof value === "number" && type == "curCSS" && !exclude.test( name ) ? - value + "px" : - value; - }, - className: { // internal only, use addClass("class") add: function( elem, classNames ) { @@ -956,7 +981,7 @@ jQuery.extend({ if (!elem || elem.nodeType == 3 || elem.nodeType == 8) return undefined; - var notxml = !jQuery.isXMLDoc( elem ), + var notxml = !elem.tagName || !jQuery.isXMLDoc( elem ), // Whether we are setting (or getting) set = value !== undefined; @@ -979,7 +1004,7 @@ jQuery.extend({ if ( name in elem && notxml && !special ) { if ( set ){ // We can't allow the type property to be changed (since it causes problems in IE) - if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode ) + if ( name == "type" && elem.nodeName.match(/(button|input)/i) && elem.parentNode ) throw "type property can't be changed"; elem[ name ] = value; @@ -1146,6 +1171,9 @@ jQuery.extend({ } }); +// All jQuery objects should point back to these +var rootjQuery = jQuery(document); + // Use of jQuery.browser is deprecated. // It's included for backwards compatibility and plugins, // although they should work to migrate away. @@ -1261,8 +1289,3 @@ function cleanData( elems ) { } } } - -// Helper function used by the dimensions and offset modules -function num(elem, prop) { - return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0; -}