Moved from the old JSMin to using YUIMin for compressing the jQuery source. Additiona...
[jquery.git] / src / core.js
index ea043c8..8c4c09b 100644 (file)
@@ -9,6 +9,9 @@
  * $Rev$
  */
 
+// Will speed up references to window, and allows munging its name.
+var window = this;
+
 // Map over jQuery in case of overwrite
 var _jQuery = window.jQuery,
 // Map over the $ in case of overwrite
@@ -24,7 +27,7 @@ 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;
@@ -81,7 +84,7 @@ jQuery.fn = jQuery.prototype = {
                // HANDLE: $(function)
                // Shortcut for document ready
                } else if ( jQuery.isFunction( selector ) )
-                       return jQuery( document )[ jQuery.fn.ready ? "ready" : "load" ]( selector );
+                       return jQuery( document ).ready( selector );
 
                return this.setArray(jQuery.makeArray(selector));
        },
@@ -151,8 +154,6 @@ jQuery.fn = jQuery.prototype = {
        // Determine the position of an element within
        // the matched set of elements
        index: function( elem ) {
-               var ret = -1;
-
                // Locate the position of the desired element
                return jQuery.inArray(
                        // If it receives a jQuery object, the first element is used
@@ -285,7 +286,7 @@ jQuery.fn = jQuery.prototype = {
        clone: function( events ) {
                // Do the clone
                var ret = this.map(function(){
-                       if ( jQuery.browser.msie && !jQuery.isXMLDoc(this) ) {
+                       if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
                                // IE copies events bound via attachEvent when
                                // using cloneNode. Calling detachEvent on the
                                // clone will also remove the events from the orignal
@@ -338,6 +339,17 @@ jQuery.fn = jQuery.prototype = {
                        }) ), "filter", selector );
        },
 
+       closest: function( selector ) {
+               return this.map(function(){
+                       var cur = this;
+                       while ( cur && cur.ownerDocument ) {
+                               if ( jQuery(cur).is(selector) )
+                                       return cur;
+                               cur = cur.parentNode;
+                       }
+               });
+       },
+
        not: function( selector ) {
                if ( typeof selector === "string" )
                        // test special case where just one selector is passed in
@@ -474,31 +486,6 @@ jQuery.fn = jQuery.prototype = {
                return this.add( this.prevObject );
        },
 
-       data: function( key, value ){
-               var parts = key.split(".");
-               parts[1] = parts[1] ? "." + parts[1] : "";
-
-               if ( value === undefined ) {
-                       var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
-
-                       if ( data === undefined && this.length )
-                               data = jQuery.data( this[0], key );
-
-                       return data == null && parts[1] ?
-                               this.data( parts[0] ) :
-                               data;
-               } else
-                       return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){
-                               jQuery.data( this, key, value );
-                       });
-       },
-
-       removeData: function( key ){
-               return this.each(function(){
-                       jQuery.removeData( this, key );
-               });
-       },
-
        domManip: function( args, table, callback ) {
                if ( this[0] ) {
                        var fragment = this[0].ownerDocument.createDocumentFragment(),
@@ -597,9 +584,8 @@ jQuery.extend = jQuery.fn.extend = function() {
        return target;
 };
 
-var expando = "jQuery" + now(), uuid = 0, windowData = {},
-       // exclude the following css properties to add px
-       exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
+// exclude the following css properties to add px
+var    exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
        // cache defaultView
        defaultView = document.defaultView || {},
        toString = Object.prototype.toString;
@@ -642,10 +628,10 @@ jQuery.extend({
                                script = document.createElement("script");
 
                        script.type = "text/javascript";
-                       if ( jQuery.browser.msie )
-                               script.text = data;
-                       else
+                       if ( jQuery.support.scriptEval )
                                script.appendChild( document.createTextNode( data ) );
+                       else
+                               script.text = data;
 
                        // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
                        // This arises when a base node is used (#2709).
@@ -658,74 +644,6 @@ jQuery.extend({
                return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
        },
 
-       cache: {},
-
-       data: function( elem, name, data ) {
-               elem = elem == window ?
-                       windowData :
-                       elem;
-
-               var id = elem[ expando ];
-
-               // Compute a unique ID for the element
-               if ( !id )
-                       id = elem[ expando ] = ++uuid;
-
-               // Only generate the data cache if we're
-               // trying to access or manipulate it
-               if ( name && !jQuery.cache[ id ] )
-                       jQuery.cache[ id ] = {};
-
-               // Prevent overriding the named cache with undefined values
-               if ( data !== undefined )
-                       jQuery.cache[ id ][ name ] = data;
-
-               // Return the named cache data, or the ID for the element
-               return name ?
-                       jQuery.cache[ id ][ name ] || null :
-                       id;
-       },
-
-       removeData: function( elem, name ) {
-               elem = elem == window ?
-                       windowData :
-                       elem;
-
-               var id = elem[ expando ];
-
-               // If we want to remove a specific section of the element's data
-               if ( name ) {
-                       if ( jQuery.cache[ id ] ) {
-                               // Remove the section of cache data
-                               delete jQuery.cache[ id ][ name ];
-
-                               // If we've removed all the data, remove the element's cache
-                               name = "";
-
-                               for ( name in jQuery.cache[ id ] )
-                                       break;
-
-                               if ( !name )
-                                       jQuery.removeData( elem );
-                       }
-
-               // Otherwise, we want to remove all of the element's data
-               } else {
-                       // Clean up the element expando
-                       try {
-                               delete elem[ expando ];
-                       } catch(e){
-                               // IE has trouble directly removing the expando
-                               // but it's ok with using removeAttribute
-                               if ( elem.removeAttribute )
-                                       elem.removeAttribute( expando );
-                       }
-
-                       // Completely remove the data cache
-                       delete jQuery.cache[ id ];
-               }
-       },
-
        // args is for internal usage only
        each: function( object, callback, args ) {
                var name, i = 0, length = object.length;
@@ -834,30 +752,14 @@ jQuery.extend({
        curCSS: function( elem, name, force ) {
                var ret, style = elem.style;
 
-               // A helper method for determining if an element's values are broken
-               function color( elem ) {
-                       if ( !jQuery.browser.safari )
-                               return false;
-
-                       // defaultView is cached
-                       var ret = defaultView.getComputedStyle( elem, null );
-                       return !ret || ret.getPropertyValue("color") == "";
-               }
-
                // We need to handle opacity special in IE
-               if ( name == "opacity" && jQuery.browser.msie ) {
+               if ( name == "opacity" && !jQuery.support.opacity ) {
                        ret = jQuery.attr( style, "opacity" );
 
                        return ret == "" ?
                                "1" :
                                ret;
                }
-               // Opera sometimes will give the wrong display answer, this fixes it, see #2037
-               if ( jQuery.browser.opera && name == "display" ) {
-                       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 ) )
@@ -876,38 +778,9 @@ jQuery.extend({
 
                        var computedStyle = defaultView.getComputedStyle( elem, null );
 
-                       if ( computedStyle && !color( elem ) )
+                       if ( computedStyle )
                                ret = computedStyle.getPropertyValue( name );
 
-                       // If the element isn't reporting its values properly in Safari
-                       // then some display: none elements are involved
-                       else {
-                               var swap = [], stack = [], a = elem, i = 0;
-
-                               // Locate all of the parent display: none elements
-                               for ( ; a && color(a); a = a.parentNode )
-                                       stack.unshift(a);
-
-                               // Go through and make them visible, but in reverse
-                               // (It would be better if we knew the exact display type that they had)
-                               for ( ; i < stack.length; i++ )
-                                       if ( color( stack[ i ] ) ) {
-                                               swap[ i ] = stack[ i ].style.display;
-                                               stack[ i ].style.display = "block";
-                                       }
-
-                               // Since we flip the display style, we have to handle that
-                               // one special, otherwise get the value
-                               ret = name == "display" && swap[ stack.length - 1 ] != null ?
-                                       "none" :
-                                       ( computedStyle && computedStyle.getPropertyValue( name ) ) || "";
-
-                               // Finally, revert the display styles back
-                               for ( i = 0; i < swap.length; i++ )
-                                       if ( swap[ i ] != null )
-                                               stack[ i ].style.display = swap[ i ];
-                       }
-
                        // We should always get a number back from opacity
                        if ( name == "opacity" && ret == "" )
                                ret = "1";
@@ -943,13 +816,22 @@ jQuery.extend({
        },
 
        clean: function( elems, context, fragment ) {
-               var ret = [], scripts = [];
                context = context || document;
 
                // !context.createElement fails in IE with an error but returns typeof 'object'
                if ( typeof context.createElement === "undefined" )
                        context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
 
+               // If a single string is passed in and it's a single tag
+               // just do a createElement and skip the rest
+               if ( !fragment && elems.length === 1 && typeof elems[0] === "string" ) {
+                       var match = /^<(\w+)\/?>$/.exec(elems[0]);
+                       if ( match )
+                               return [ context.createElement( match[1] ) ];
+               }
+
+               var ret = [], scripts = [], div = context.createElement("div");
+
                jQuery.each(elems, function(i, elem){
                        if ( typeof elem === "number" )
                                elem += '';
@@ -967,7 +849,7 @@ jQuery.extend({
                                });
 
                                // Trim whitespace, otherwise indexOf won't work as expected
-                               var tags = jQuery.trim( elem ).toLowerCase(), div = context.createElement("div");
+                               var tags = jQuery.trim( elem ).toLowerCase();
 
                                var wrap =
                                        // option or optgroup
@@ -991,7 +873,7 @@ jQuery.extend({
                                        [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ] ||
 
                                        // IE can't serialize <link> and <script> tags normally
-                                       jQuery.browser.msie &&
+                                       !jQuery.support.htmlSerialize &&
                                        [ 1, "div<div>", "</div>" ] ||
 
                                        [ 0, "", "" ];
@@ -1004,7 +886,7 @@ jQuery.extend({
                                        div = div.lastChild;
 
                                // Remove IE's autoinserted <tbody> from table fragments
-                               if ( jQuery.browser.msie ) {
+                               if ( !jQuery.support.tbody ) {
 
                                        // String was a <table>, *may* have spurious <tbody>
                                        var tbody = !tags.indexOf("<table") && tags.indexOf("<tbody") < 0 ?
@@ -1019,11 +901,11 @@ jQuery.extend({
                                                if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length )
                                                        tbody[ j ].parentNode.removeChild( tbody[ j ] );
 
-                                       // IE completely kills leading whitespace when innerHTML is used
-                                       if ( /^\s/.test( elem ) )
-                                               div.insertBefore( context.createTextNode( elem.match(/^\s*/)[0] ), div.firstChild );
+                                       }
 
-                               }
+                               // IE completely kills leading whitespace when innerHTML is used
+                               if ( !jQuery.support.leadingWhitespace && /^\s/.test( elem ) )
+                                       div.insertBefore( context.createTextNode( elem.match(/^\s*/)[0] ), div.firstChild );
                                
                                if ( fragment ) {
                                        var found = div.getElementsByTagName("script");
@@ -1043,15 +925,17 @@ jQuery.extend({
                                ret = jQuery.merge( ret, elem );
 
                });
-               
+
                if ( fragment ) {
                        for ( var i = 0; ret[i]; i++ ) {
-                               if ( jQuery.nodeName( ret[i], "script" ) ) {
-                                       ret[i].parentNode.removeChild( ret[i] );
+                               var node = ret[i];
+                               if ( jQuery.nodeName( node, "script" ) ) {
+                                       if( node.parentNode )
+                                               node.parentNode.removeChild( node );
                                } else {
-                                       if ( ret[i].nodeType === 1 )
-                                               ret = jQuery.merge( ret, ret[i].getElementsByTagName("script"));
-                                       fragment.appendChild( ret[i] );
+                                       if ( node.nodeType === 1 )
+                                               ret = jQuery.merge( ret, node.getElementsByTagName("script"));
+                                       fragment.appendChild( node );
                                }
                        }
                        
@@ -1068,8 +952,7 @@ jQuery.extend({
 
                var notxml = !jQuery.isXMLDoc( elem ),
                        // Whether we are setting (or getting)
-                       set = value !== undefined,
-                       msie = jQuery.browser.msie;
+                       set = value !== undefined;
 
                // Try to normalize/fix the name
                name = notxml && jQuery.props[ name ] || name;
@@ -1083,7 +966,7 @@ jQuery.extend({
 
                        // 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 )
+                       if ( name == "selected" )
                                elem.parentNode.selectedIndex;
 
                        // If applicable, access the attribute via the DOM 0 way
@@ -1103,14 +986,14 @@ jQuery.extend({
                                return elem[ name ];
                        }
 
-                       if ( msie && notxml &&  name == "style" )
+                       if ( !jQuery.support.style && 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 );
 
-                       var attr = msie && notxml && special
+                       var attr = !jQuery.support.hrefNormalized && notxml && special
                                        // Some attributes require a special call on IE
                                        ? elem.getAttribute( name, 2 )
                                        : elem.getAttribute( name );
@@ -1122,7 +1005,7 @@ jQuery.extend({
                // elem is actually elem.style ... set the style
 
                // IE uses filters for opacity
-               if ( msie && name == "opacity" ) {
+               if ( !jQuery.support.opacity && name == "opacity" ) {
                        if ( set ) {
                                // IE has trouble with opacity if it does not have layout
                                // Force it by setting the zoom level
@@ -1183,13 +1066,13 @@ jQuery.extend({
                var i = 0, elem, pos = first.length;
                // Also, we need to make sure that the correct elements are being returned
                // (IE returns comment nodes in a '*' query)
-               if ( jQuery.browser.msie ) {
-                       while ( (elem = second[ i++ ]) )
+               if ( !jQuery.support.getAll ) {
+                       while ( (elem = second[ i++ ]) != null )
                                if ( elem.nodeType != 8 )
                                        first[ pos++ ] = elem;
 
                } else
-                       while ( (elem = second[ i++ ]) )
+                       while ( (elem = second[ i++ ]) != null )
                                first[ pos++ ] = elem;
 
                return first;
@@ -1244,6 +1127,10 @@ jQuery.extend({
        }
 });
 
+// Use of jQuery.browser is deprecated.
+// It's included for backwards compatibility and plugins,
+// although they should work to migrate away.
+
 var userAgent = navigator.userAgent.toLowerCase();
 
 // Figure out what browser is being used
@@ -1255,26 +1142,8 @@ jQuery.browser = {
        mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
 };
 
-var styleFloat = jQuery.browser.msie ?
-       "styleFloat" :
-       "cssFloat";
-
-jQuery.extend({
-       // Check to see if the W3C box model is being used
-       boxModel: !jQuery.browser.msie || document.compatMode == "CSS1Compat",
-
-       props: {
-               "for": "htmlFor",
-               "class": "className",
-               "float": styleFloat,
-               cssFloat: styleFloat,
-               styleFloat: styleFloat,
-               readonly: "readOnly",
-               maxlength: "maxLength",
-               cellspacing: "cellSpacing",
-               rowspan: "rowSpan"
-       }
-});
+// Check to see if the W3C box model is being used
+jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
 
 jQuery.each({
        parent: function(elem){return elem.parentNode;},
@@ -1329,8 +1198,10 @@ jQuery.each({
                jQuery.className.remove( this, classNames );
        },
 
-       toggleClass: function( classNames ) {
-               jQuery.className[ jQuery.className.has( this, classNames ) ? "remove" : "add" ]( this, classNames );
+       toggleClass: function( classNames, state ) {
+               if( typeof state !== "boolean" )
+                       state = !jQuery.className.has( this, classNames );
+               jQuery.className[ state ? "add" : "remove" ]( this, classNames );
        },
 
        remove: function( selector ) {
@@ -1359,39 +1230,6 @@ jQuery.each({
        };
 });
 
-jQuery.each([ "Height", "Width" ], function(i, name){
-       var type = name.toLowerCase();
-
-       jQuery.fn[ type ] = function( size ) {
-               // Get window width or height
-               return this[0] == window ?
-                       // Opera reports document.body.client[Width/Height] properly in both quirks and standards
-                       jQuery.browser.opera && document.body.parentNode[ "client" + name ] ||
-
-                       // Safari reports inner[Width/Height] just fine (Mozilla and Opera include scroll bar widths)
-                       jQuery.browser.safari && window[ "inner" + name ] ||
-
-                       // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
-                       document.compatMode == "CSS1Compat" && document.documentElement[ "client" + name ] || document.body[ "client" + name ] :
-
-                       // Get document width or height
-                       this[0] == document ?
-                               // Either scroll[Width/Height] or offset[Width/Height], whichever is greater
-                               Math.max(
-                                       Math.max(document.body["scroll" + name], document.documentElement["scroll" + name]),
-                                       Math.max(document.body["offset" + name], document.documentElement["offset" + name])
-                               ) :
-
-                               // Get or set width or height on the element
-                               size === undefined ?
-                                       // Get width or height on the element
-                                       (this.length ? jQuery.css( this[0], type ) : null) :
-
-                                       // Set the width or height on the element (default to pixels if value is unitless)
-                                       this.css( type, typeof size === "string" ? size : size + "px" );
-       };
-});
-
 // 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;