X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fcore.js;h=d23d45087197d16fb24201a63ad96bca4f43e758;hb=70b5e670ca6f0c915232139db8e6766aa2c191d4;hp=9752fb16292f7fcf6f14b2aa951f76f9e529de03;hpb=a584f82aef3c20e11adeecc8513920e3d2a2ae2f;p=jquery.git diff --git a/src/core.js b/src/core.js index 9752fb1..d23d450 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 ) { @@ -140,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 ) { @@ -153,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 = {}; @@ -339,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 ) { @@ -1027,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; - return elem[ fix[ name ] ]; + // Only do all the following if this is a node (faster for style) + // IE elem.getAttribute passes even for style + if ( elem.tagName ) { - } else if ( jQuery.browser.msie && name == "style" ) - return jQuery.attr( elem.style, "cssText", value ); + // These attributes require special treatment + var special = /href|src|style/.test( name ); - else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName( elem, "form" ) && (name == "action" || name == "method") ) - return elem.getAttributeNode( name ).nodeValue; + // 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; - // IE elem.getAttribute passes even for style - else if ( elem.tagName ) { + // 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"; + + 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 && notxml && 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 ) { @@ -1111,8 +1123,8 @@ jQuery.extend({ if( array != null ){ var i = array.length; - //the window, forms, strings and functions also have 'length' - if( i == null || array.split || array.setInterval || array.call || array.elements ) + //the window, strings and functions also have 'length' + if( i == null || array.split || array.setInterval || array.call ) ret[0] = array; else while( i ) @@ -1124,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; @@ -1175,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; @@ -1222,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" } });