X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcore.js;h=6b2c36a8ec2aa6d556044bc1f928b874e53d376e;hb=e9b2781d30a325f835fbf7c0043cf855cbb6edfa;hp=80115b44278daab14aa8be5e141eee4f3a450dbd;hpb=cd455eeba7ef754f35a36b10ca27a31c14bb8a1f;p=jquery.git diff --git a/src/core.js b/src/core.js index 80115b4..6b2c36a 100644 --- a/src/core.js +++ b/src/core.js @@ -24,7 +24,10 @@ var jQuery = window.jQuery = window.$ = function( selector, context ) { var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/, // Is it a simple selector - isSimple = /^.[^:#\[\.]*$/; + isSimple = /^.[^:#\[\.]*$/, + +// Will speed up references to undefined, and allows munging its name. + undefined; jQuery.fn = jQuery.prototype = { init: function( selector, context ) { @@ -36,9 +39,9 @@ jQuery.fn = jQuery.prototype = { this[0] = selector; this.length = 1; return this; - + } // Handle HTML strings - } else if ( typeof selector == "string" ) { + if ( typeof selector == "string" ) { // Are we dealing with HTML string or an ID? var match = quickExpr.exec( selector ); @@ -54,32 +57,27 @@ jQuery.fn = jQuery.prototype = { var elem = document.getElementById( match[3] ); // Make sure an element was located - if ( elem ) + if ( elem ){ // Handle the case where IE and Opera return items // by name instead of ID if ( elem.id != match[3] ) return jQuery().find( selector ); // Otherwise, we inject the element directly into the jQuery object - else { - this[0] = elem; - this.length = 1; - return this; - } - - else - selector = []; + return jQuery( elem ); + } + selector = []; } // HANDLE: $(expr, [context]) // (which is just equivalent to: $(content).find(expr) } else - return new jQuery( context ).find( selector ); + return jQuery( context ).find( selector ); // HANDLE: $(function) // Shortcut for document ready } else if ( jQuery.isFunction( selector ) ) - return new jQuery( document )[ jQuery.fn.ready ? "ready" : "load" ]( selector ); + return jQuery( document )[ jQuery.fn.ready ? "ready" : "load" ]( selector ); return this.setArray(jQuery.makeArray(selector)); }, @@ -145,12 +143,10 @@ jQuery.fn = jQuery.prototype = { var ret = -1; // Locate the position of the desired element - this.each(function(i){ - if ( this == elem ) - ret = i; - }); - - return ret; + return jQuery.inArray( + // If it receives a jQuery object, the first element is used + elem && elem.jquery ? elem[0] : elem + , this ); }, attr: function( name, value, type ) { @@ -158,8 +154,8 @@ jQuery.fn = jQuery.prototype = { // Look for the case where we're accessing a style value if ( name.constructor == String ) - if ( value == undefined ) - return this.length && jQuery[ type || "attr" ]( this[0], name ) || undefined; + if ( value === undefined ) + return this[0] && jQuery[ type || "attr" ]( this[0], name ); else { options = {}; @@ -344,12 +340,12 @@ jQuery.fn = jQuery.prototype = { }, add: function( selector ) { - return !selector ? this : this.pushStack( jQuery.merge( + return this.pushStack( jQuery.unique( jQuery.merge( this.get(), - selector.constructor == String ? - jQuery( selector ).get() : - selector.length != undefined && (!selector.nodeName || jQuery.nodeName(selector, "form")) ? - selector : [selector] ) ); + typeof selector == 'string' ? + jQuery( selector ) : + jQuery.makeArray( selector ) + ))); }, is: function( selector ) { @@ -465,13 +461,13 @@ jQuery.fn = jQuery.prototype = { var parts = key.split("."); parts[1] = parts[1] ? "." + parts[1] : ""; - if ( value == null ) { + if ( value === undefined ) { var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]); - if ( data == undefined && this.length ) + if ( data === undefined && this.length ) data = jQuery.data( this[0], key ); - return data == null && parts[1] ? + return data === undefined && parts[1] ? this.data( parts[0] ) : data; } else @@ -587,7 +583,7 @@ jQuery.extend = jQuery.fn.extend = function() { target[ name ] = jQuery.extend( deep, src, copy ); // Don't bring in undefined values - else if ( copy != undefined ) + else if ( copy !== undefined ) target[ name ] = copy; } @@ -669,7 +665,7 @@ jQuery.extend({ jQuery.cache[ id ] = {}; // Prevent overriding the named cache with undefined values - if ( data != undefined ) + if ( data !== undefined ) jQuery.cache[ id ][ name ] = data; // Return the named cache data, or the ID for the element @@ -720,24 +716,26 @@ jQuery.extend({ // args is for internal usage only each: function( object, callback, args ) { + var name, i = 0, length = object.length; + if ( args ) { - if ( object.length == undefined ) { - for ( var name in object ) + if ( length == undefined ) { + for ( name in object ) if ( callback.apply( object[ name ], args ) === false ) break; } else - for ( var i = 0, length = object.length; i < length; i++ ) - if ( callback.apply( object[ i ], args ) === false ) + for ( ; i < length; ) + if ( callback.apply( object[ i++ ], args ) === false ) break; // A special, fast, case for the most common use of each } else { - if ( object.length == undefined ) { - for ( var name in object ) + if ( length == undefined ) { + for ( name in object ) if ( callback.call( object[ name ], name, object[ name ] ) === false ) break; } else - for ( var i = 0, length = object.length, value = object[0]; + for ( var value = object[0]; i < length && callback.call( value, i, value ) !== false; value = object[++i] ){} } @@ -822,7 +820,7 @@ jQuery.extend({ }, curCSS: function( elem, name, force ) { - var ret; + var ret, style = elem.style; // A helper method for determining if an element's values are broken function color( elem ) { @@ -836,7 +834,7 @@ jQuery.extend({ // We need to handle opacity special in IE if ( name == "opacity" && jQuery.browser.msie ) { - ret = jQuery.attr( elem.style, "opacity" ); + ret = jQuery.attr( style, "opacity" ); return ret == "" ? "1" : @@ -844,17 +842,17 @@ jQuery.extend({ } // Opera sometimes will give the wrong display answer, this fixes it, see #2037 if ( jQuery.browser.opera && name == "display" ) { - var save = elem.style.outline; - elem.style.outline = "0 solid black"; - elem.style.outline = save; + var save = style.outline; + style.outline = "0 solid black"; + style.outline = save; } // Make sure we're using the right name for getting the float value if ( name.match( /float/i ) ) name = styleFloat; - if ( !force && elem.style && elem.style[ name ] ) - ret = elem.style[ name ]; + if ( !force && style && style[ name ] ) + ret = style[ name ]; else if ( getComputedStyle ) { @@ -916,16 +914,16 @@ jQuery.extend({ // but a number that has a weird ending, we need to convert it to pixels if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) { // Remember the original values - var style = elem.style.left, runtimeStyle = elem.runtimeStyle.left; + var left = style.left, rsLeft = elem.runtimeStyle.left; // Put in the new values to get a computed value out elem.runtimeStyle.left = elem.currentStyle.left; - elem.style.left = ret || 0; - ret = elem.style.pixelLeft + "px"; + style.left = ret || 0; + ret = style.pixelLeft + "px"; // Revert the changed values - elem.style.left = style; - elem.runtimeStyle.left = runtimeStyle; + style.left = left; + elem.runtimeStyle.left = rsLeft; } } @@ -1030,79 +1028,90 @@ jQuery.extend({ return ret; }, - + attr: function( elem, name, value ) { // don't set attributes on text and comment nodes if (!elem || elem.nodeType == 3 || elem.nodeType == 8) return undefined; - var fix = jQuery.isXMLDoc( elem ) ? - {} : - jQuery.props; + var notxml = !jQuery.isXMLDoc( elem ), + // Whether we are setting (or getting) + set = value !== undefined, + msie = jQuery.browser.msie; - // Safari mis-reports the default selected property of a hidden option - // Accessing the parent's selectedIndex property fixes it - if ( name == "selected" && jQuery.browser.safari ) - elem.parentNode.selectedIndex; - - // Certain attributes only work when accessed via the old DOM 0 way - if ( fix[ name ] ) { - if ( value != undefined ) - elem[ fix[ name ] ] = value; + // Try to normalize/fix the name + name = notxml && jQuery.props[ name ] || name; + + // Only do all the following if this is a node (faster for style) + // IE elem.getAttribute passes even for style + if ( elem.tagName ) { - return elem[ fix[ name ] ]; + // These attributes require special treatment + var special = /href|src|style/.test( name ); - } else if ( jQuery.browser.msie && name == "style" ) - return jQuery.attr( elem.style, "cssText", value ); + // Safari mis-reports the default selected property of a hidden option + // Accessing the parent's selectedIndex property fixes it + if ( name == "selected" && jQuery.browser.safari ) + elem.parentNode.selectedIndex; - else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName( elem, "form" ) && (name == "action" || name == "method") ) - return elem.getAttributeNode( name ).nodeValue; + // If applicable, access the attribute via the DOM 0 way + if ( notxml && !special && name in elem ) { + 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 ) + throw "type property can't be changed"; - // IE elem.getAttribute passes even for style - else if ( elem.tagName ) { + elem[ name ] = value; + } - if ( value != undefined ) { - // 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 ) - throw "type property can't be changed"; + // browsers index elements by id/name on forms, give priority to attributes. + if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) + return elem.getAttributeNode( name ).nodeValue; + return elem[ name ]; + } + + if ( msie && name == "style" ) + return jQuery.attr( elem.style, "cssText", value ); + + if ( set ) // convert the value to a string (all browsers do this but IE) see #1070 elem.setAttribute( name, "" + value ); - } - if ( jQuery.browser.msie && /href|src/.test( name ) && !jQuery.isXMLDoc( elem ) ) + if ( msie && special && notxml ) return elem.getAttribute( name, 2 ); return elem.getAttribute( name ); + } + // elem is actually elem.style ... set the style - } else { - // IE actually uses filters for opacity - if ( name == "opacity" && jQuery.browser.msie ) { - if ( value != undefined ) { - // IE has trouble with opacity if it does not have layout - // Force it by setting the zoom level - elem.zoom = 1; - - // Set the alpha filter to set the opacity - elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) + - (parseFloat( value ).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"); - } - - return elem.filter && elem.filter.indexOf("opacity=") >= 0 ? - (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() : - ""; - } - name = name.replace(/-([a-z])/ig, function(all, letter){ - return letter.toUpperCase(); - }); + // IE uses filters for opacity + if ( msie && name == "opacity" ) { + if ( set ) { + // IE has trouble with opacity if it does not have layout + // Force it by setting the zoom level + elem.zoom = 1; - if ( value != undefined ) - elem[ name ] = value; + // Set the alpha filter to set the opacity + elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) + + (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"); + } - return elem[ name ]; + return elem.filter && elem.filter.indexOf("opacity=") >= 0 ? + (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '': + ""; } + + name = name.replace(/-([a-z])/ig, function(all, letter){ + return letter.toUpperCase(); + }); + + if ( set ) + elem[ name ] = value; + + return elem[ name ]; }, trim: function( text ) { @@ -1112,14 +1121,14 @@ jQuery.extend({ makeArray: function( array ) { var ret = []; - if( array != undefined ){ + if( array != null ){ var i = array.length; //the window, strings and functions also have 'length' - if( i != null && !array.split && array != window && !array.call ) + if( i == null || array.split || array.setInterval || array.call ) + ret[0] = array; + else while( i ) ret[--i] = array[i]; - else - ret[0] = array; } return ret; @@ -1127,7 +1136,8 @@ jQuery.extend({ inArray: function( elem, array ) { for ( var i = 0, length = array.length; i < length; i++ ) - if ( array[ i ] == elem ) + // Use === because on IE, window == document + if ( array[ i ] === elem ) return i; return -1; @@ -1136,16 +1146,16 @@ jQuery.extend({ merge: function( first, second ) { // We have to loop this way because IE & Opera overwrite the length // expando of getElementsByTagName - + var i = 0; // Also, we need to make sure that the correct elements are being returned // (IE returns comment nodes in a '*' query) if ( jQuery.browser.msie ) { - for ( var i = 0; second[ i ]; i++ ) + for ( ; second[ i ]; i++ ) if ( second[ i ].nodeType != 8 ) first.push( second[ i ] ); } else - for ( var i = 0; second[ i ]; i++ ) + for ( ; second[ i ]; i++ ) first.push( second[ i ] ); return first; @@ -1178,7 +1188,7 @@ jQuery.extend({ // 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 ) || inv && !callback( elems[ i ], i ) ) + if ( !inv != !callback( elems[ i ], i ) ) ret.push( elems[ i ] ); return ret; @@ -1192,15 +1202,11 @@ jQuery.extend({ for ( var i = 0, length = elems.length; i < length; i++ ) { var value = callback( elems[ i ], i ); - if ( value !== null && value != undefined ) { - if ( value.constructor != Array ) - value = [ value ]; - - ret = ret.concat( value ); - } + if ( value != null ) + ret[ ret.length ] = value; } - return ret; + return ret.concat.apply( [], ret ); } }); @@ -1229,18 +1235,9 @@ jQuery.extend({ "float": styleFloat, cssFloat: styleFloat, styleFloat: styleFloat, - innerHTML: "innerHTML", - className: "className", - value: "value", - disabled: "disabled", - checked: "checked", readonly: "readOnly", - selected: "selected", maxlength: "maxLength", - selectedIndex: "selectedIndex", - defaultValue: "defaultValue", - tagName: "tagName", - nodeName: "nodeName" + cellspacing: "cellSpacing" } });