jQuery.merge speedup, bug#444
[jquery.git] / src / jquery / jquery.js
index 883bc38..d3ed8c6 100644 (file)
@@ -22,12 +22,12 @@ window.undefined = window.undefined;
  */\r
 var jQuery = function(a,c) {\r
 \r
-       // Shortcut for document ready (because $(document).each() is silly)\r
+       // Shortcut for document ready\r
        if ( a && typeof a == "function" && jQuery.fn.ready && !a.nodeType && a[0] == undefined ) // Safari reports typeof on DOM NodeLists as a function\r
                return jQuery(document).ready(a);\r
 \r
        // Make sure that a selection was provided\r
-       a = a || jQuery.context || document;\r
+       a = a || document;\r
 \r
        // Watch for when a jQuery object is passed as the selector\r
        if ( a.jquery )\r
@@ -48,7 +48,7 @@ var jQuery = function(a,c) {
        }\r
 \r
        // Watch for when an array is passed in\r
-       this.get( a.constructor == Array || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType ?\r
+       this.set( a.constructor == Array || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType ?\r
                // Assume that it is an array of DOM Elements\r
                jQuery.merge( a, [] ) :\r
 \r
@@ -73,17 +73,14 @@ if ( typeof $ != "undefined" )
 var $ = jQuery;\r
 \r
 /**\r
- * This function accepts a string containing a CSS selector,\r
- * basic XPath, or raw HTML, which is then used to match a set of elements.\r
- * The HTML string is different from the traditional selectors in that\r
- * it creates the DOM elements representing that HTML string, on the fly,\r
- * to be (assumedly) inserted into the document later.\r
+ * This function accepts a string containing a CSS or\r
+ * basic XPath selector which is then used to match a set of elements.\r
  *\r
  * The core functionality of jQuery centers around this function.\r
  * Everything in jQuery is based upon this, or uses this in some way.\r
  * The most basic use of this function is to pass in an expression\r
  * (usually consisting of CSS or XPath), which then finds all matching\r
- * elements and remembers them for later use.\r
+ * elements.\r
  *\r
  * By default, $() looks for DOM elements within the context of the\r
  * current HTML document.\r
@@ -93,29 +90,36 @@ var $ = jQuery;
  * @before <p>one</p> <div><p>two</p></div> <p>three</p>\r
  * @result [ <p>two</p> ]\r
  *\r
- * @example $("<div><p>Hello</p></div>").appendTo("#body")\r
- * @desc Creates a div element (and all of its contents) dynamically, \r
- * and appends it to the element with the ID of body. Internally, an\r
- * element is created and it's innerHTML property set to the given markup.\r
- * It is therefore both quite flexible and limited. \r
+ * @example $("input:radio", document.forms[0])\r
+ * @desc Searches for all inputs of type radio within the first form in the document\r
+ *\r
+ * @example $("div", xml.responseXML)\r
+ * @desc This finds all div elements within the specified XML document.\r
  *\r
  * @name $\r
- * @param String expr An expression to search with, or a string of HTML to create on the fly.\r
+ * @param String expr An expression to search with\r
+ * @param Element context (optional) A DOM Element, or Document, representing the base context.\r
  * @cat Core\r
  * @type jQuery\r
+ * @see $(Element)\r
+ * @see $(Element<Array>)\r
  */\r
-\r
\r
 /**\r
- * This function accepts a string containing a CSS selector, or\r
- * basic XPath, which is then used to match a set of elements with the\r
- * context of the specified DOM element, or document\r
+ * This function accepts a string of raw HTML.\r
  *\r
- * @example $("div", xml.responseXML)\r
- * @desc This finds all div elements within the specified XML document.\r
+ * The HTML string is different from the traditional selectors in that\r
+ * it creates the DOM elements representing that HTML string, on the fly,\r
+ * to be (assumedly) inserted into the document later.\r
+ *\r
+ * @example $("<div><p>Hello</p></div>").appendTo("#body")\r
+ * @desc Creates a div element (and all of its contents) dynamically, \r
+ * and appends it to the element with the ID of body. Internally, an\r
+ * element is created and it's innerHTML property set to the given markup.\r
+ * It is therefore both quite flexible and limited. \r
  *\r
  * @name $\r
- * @param String expr An expression to search with.\r
- * @param Element context A DOM Element, or Document, representing the base context.\r
+ * @param String html A string of HTML to create on the fly.\r
  * @cat Core\r
  * @type jQuery\r
  */\r
@@ -251,37 +255,34 @@ jQuery.fn = jQuery.prototype = {
         * @param Number num Access the element in the Nth position.\r
         * @cat Core\r
         */\r
+       get: function( num ) {\r
+               return num == undefined ?\r
+\r
+                       // Return a 'clean' array\r
+                       jQuery.merge( this, [] ) :\r
 \r
+                       // Return just the object\r
+                       this[num];\r
+       },\r
+       \r
        /**\r
         * Set the jQuery object to an array of elements.\r
         *\r
-        * @example $("img").get([ document.body ]);\r
-        * @result $("img").get() == [ document.body ]\r
+        * @example $("img").set([ document.body ]);\r
+        * @result $("img").set() == [ document.body ]\r
         *\r
         * @private\r
-        * @name get\r
+        * @name set\r
         * @type jQuery\r
         * @param Elements elems An array of elements\r
         * @cat Core\r
         */\r
-       get: function( num ) {\r
-               // Watch for when an array (of elements) is passed in\r
-               if ( num && num.constructor == Array ) {\r
-\r
-                       // Use a tricky hack to make the jQuery object\r
-                       // look and feel like an array\r
-                       this.length = 0;\r
-                       [].push.apply( this, num );\r
-\r
-                       return this;\r
-               } else\r
-                       return num == undefined ?\r
-\r
-                               // Return a 'clean' array\r
-                               jQuery.merge( this, [] ) :\r
-\r
-                               // Return just the object\r
-                               this[num];\r
+       set: function( array ) {\r
+               // Use a tricky hack to make the jQuery object\r
+               // look and feel like an array\r
+               this.length = 0;\r
+               [].push.apply( this, array );\r
+               return this;\r
        },\r
 \r
        /**\r
@@ -294,17 +295,12 @@ jQuery.fn = jQuery.prototype = {
         * argument representing the position of the element in the matched\r
         * set.\r
         *\r
-        * @example $("img").each(function(){\r
-        *   this.src = "test.jpg";\r
-        * });\r
-        * @before <img/> <img/>\r
-        * @result <img src="test.jpg"/> <img src="test.jpg"/>\r
-        *\r
         * @example $("img").each(function(i){\r
-        *   alert( "Image #" + i + " is " + this );\r
+        *   this.src = "test" + i + ".jpg";\r
         * });\r
         * @before <img/> <img/>\r
-        * @result <img src="test.jpg"/> <img src="test.jpg"/>\r
+        * @result <img src="test0.jpg"/> <img src="test1.jpg"/>\r
+        * @desc Iterates over two images and sets their src property\r
         *\r
         * @name each\r
         * @type jQuery\r
@@ -378,6 +374,10 @@ jQuery.fn = jQuery.prototype = {
        /**\r
         * Set a single property to a value, on all matched elements.\r
         *\r
+        * Note that you can't set the name property of input elements in IE.\r
+        * Use $(html) or $().append(html) or $().html(html) to create elements\r
+        * on the fly including the name property.\r
+        *\r
         * @example $("img").attr("src","test.jpg");\r
         * @before <img/>\r
         * @result <img src="test.jpg"/>\r
@@ -390,7 +390,7 @@ jQuery.fn = jQuery.prototype = {
         */\r
        attr: function( key, value, type ) {\r
                // Check to see if we're setting style values\r
-               return key.constructor != String || value != undefined ?\r
+               return typeof key != "string" || value != undefined ?\r
                        this.each(function(){\r
                                // See if we're setting a hash of styles\r
                                if ( value == undefined )\r
@@ -773,7 +773,7 @@ jQuery.fn = jQuery.prototype = {
        end: function() {\r
                if( !(this.stack && this.stack.length) )\r
                        return this;\r
-               return this.get( this.stack.pop() );\r
+               return this.set( this.stack.pop() );\r
        },\r
 \r
        /**\r
@@ -957,8 +957,8 @@ jQuery.fn = jQuery.prototype = {
 \r
        /**\r
         * Checks the current selection against an expression and returns true,\r
-        * if the selection fits the given expression. Does return false, if the\r
-        * selection does not fit or the expression is not valid.\r
+        * if at least one element of the selection fits the given expression.\r
+        * Does return false, if no element fits or the expression is not valid.\r
         *\r
         * @example $("input[@type='checkbox']").parent().is("form")\r
         * @before <form><input type="checkbox" /></form>\r
@@ -985,38 +985,30 @@ jQuery.fn = jQuery.prototype = {
        },\r
        \r
        /**\r
-        *\r
-        *\r
         * @private\r
         * @name domManip\r
         * @param Array args\r
-        * @param Boolean table\r
-        * @param Number int\r
+        * @param Boolean table Insert TBODY in TABLEs if one is not found.\r
+        * @param Number dir If dir<0, process args in reverse order.\r
         * @param Function fn The function doing the DOM manipulation.\r
         * @type jQuery\r
         * @cat Core\r
         */\r
        domManip: function(args, table, dir, fn){\r
-               var clone = this.size() > 1;\r
+               var clone = this.length > 1; \r
                var a = jQuery.clean(args);\r
+               if ( dir < 0 )\r
+                       a.reverse();\r
 \r
                return this.each(function(){\r
                        var obj = this;\r
 \r
-                       if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() != "THEAD" ) {\r
-                               var tbody = this.getElementsByTagName("tbody");\r
+                       if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" )\r
+                               obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));\r
 \r
-                               if ( !tbody.length ) {\r
-                                       obj = document.createElement("tbody");\r
-                                       this.appendChild( obj );\r
-                               } else\r
-                                       obj = tbody[0];\r
-                       }\r
+                       for ( var i=0; i < a.length; i++ )\r
+                               fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );\r
 \r
-                       for ( var i = ( dir < 0 ? a.length - 1 : 0 );\r
-                               i != ( dir < 0 ? dir : a.length ); i += dir ) {\r
-                                       fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );\r
-                       }\r
                });\r
        },\r
 \r
@@ -1031,8 +1023,8 @@ jQuery.fn = jQuery.prototype = {
         * @cat Core\r
         */\r
        pushStack: function(a,args) {\r
-               var fn = args && args[args.length-1];\r
-               var fn2 = args && args[args.length-2];\r
+               var fn = args && args.length > 1 && args[args.length-1];\r
+               var fn2 = args && args.length > 2 && args[args.length-2];\r
                \r
                if ( fn && fn.constructor != Function ) fn = null;\r
                if ( fn2 && fn2.constructor != Function ) fn2 = null;\r
@@ -1040,15 +1032,15 @@ jQuery.fn = jQuery.prototype = {
                if ( !fn ) {\r
                        if ( !this.stack ) this.stack = [];\r
                        this.stack.push( this.get() );\r
-                       this.get( a );\r
+                       this.set( a );\r
                } else {\r
                        var old = this.get();\r
-                       this.get( a );\r
+                       this.set( a );\r
 \r
                        if ( fn2 && a.length || !fn2 )\r
-                               this.each( fn2 || fn ).get( old );\r
+                               this.each( fn2 || fn ).set( old );\r
                        else\r
-                               this.get( old ).each( fn );\r
+                               this.set( old ).each( fn );\r
                }\r
 \r
                return this;\r
@@ -1207,6 +1199,7 @@ jQuery.extend({
         * @type Object\r
         * @cat Javascript\r
         */\r
+       // args is for internal usage only\r
        each: function( obj, fn, args ) {\r
                if ( obj.length == undefined )\r
                        for ( var i in obj )\r
@@ -1310,11 +1303,6 @@ jQuery.extend({
 \r
                        ret = elem.style[prop];\r
 \r
-               } else if (elem.currentStyle) {\r
-\r
-                       var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});\r
-                       ret = elem.currentStyle[prop] || elem.currentStyle[newProp];\r
-\r
                } else if (document.defaultView && document.defaultView.getComputedStyle) {\r
 \r
                        if (prop == "cssFloat" || prop == "styleFloat")\r
@@ -1329,34 +1317,58 @@ jQuery.extend({
                                ret = 'none';\r
                        else\r
                                jQuery.swap(elem, { display: 'block' }, function() {\r
-                                       ret = document.defaultView.getComputedStyle(this,null).getPropertyValue(prop);\r
+                                   var c = document.defaultView.getComputedStyle(this, '');\r
+                                   ret = c && c.getPropertyValue(prop) || '';\r
                                });\r
 \r
+               } else if (elem.currentStyle) {\r
+\r
+                       var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});\r
+                       ret = elem.currentStyle[prop] || elem.currentStyle[newProp];\r
+                       \r
                }\r
 \r
                return ret;\r
        },\r
        \r
-       clean: function(a) {\r
+               clean: function(a) {\r
                var r = [];\r
                for ( var i = 0; i < a.length; i++ ) {\r
                        var arg = a[i];\r
                        if ( typeof arg == "string" ) { // Convert html string into DOM nodes\r
                                // Trim whitespace, otherwise indexOf won't work as expected\r
-                               var s = jQuery.trim(arg), div = document.createElement("div"), wrap = [0,"",""];\r
+                               var s = jQuery.trim(arg), s3 = s.substring(0,3), s6 = s.substring(0,6),\r
+                                       div = document.createElement("div"), wrap = [0,"",""];\r
 \r
-                               if ( !s.indexOf("<opt") ) // option or optgroup\r
+                               if ( s.substring(0,4) == "<opt" ) // option or optgroup\r
                                        wrap = [1, "<select>", "</select>"];\r
-                               else if ( !s.indexOf("<thead") || !s.indexOf("<tbody") )\r
+                               else if ( s6 == "<thead" || s6 == "<tbody" || s6 == "<tfoot" )\r
                                        wrap = [1, "<table>", "</table>"];\r
-                               else if ( !s.indexOf("<tr") )\r
-                                       wrap = [2, "<table>", "</table>"];      // tbody auto-inserted\r
-                               else if ( !s.indexOf("<td") || !s.indexOf("<th") )\r
+                               else if ( s3 == "<tr" )\r
+                                       wrap = [2, "<table><tbody>", "</tbody></table>"];\r
+                               else if ( s3 == "<td" || s3 == "<th" ) // <thead> matched above\r
                                        wrap = [3, "<table><tbody><tr>", "</tr></tbody></table>"];\r
 \r
                                // Go to html and back, then peel off extra wrappers\r
                                div.innerHTML = wrap[1] + s + wrap[2];\r
                                while ( wrap[0]-- ) div = div.firstChild;\r
+                               \r
+                               // Remove IE's autoinserted <tbody> from table fragments\r
+                               if ( jQuery.browser.msie ) {\r
+                                       var tb = null;\r
+                                       // String was a <table>, *may* have spurious <tbody>\r
+                                       if ( s6 == "<table" && s.indexOf("<tbody") < 0 ) \r
+                                               tb = div.firstChild && div.firstChild.childNodes;\r
+                                       // String was a bare <thead> or <tfoot>\r
+                                       else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 )\r
+                                               tb = div.childNodes;\r
+                                       if ( tb ) {\r
+                                               for ( var n = tb.length-1; n >= 0 ; --n )\r
+                                                       if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length )\r
+                                                               tb[n].parentNode.removeChild(tb[n]);\r
+                                       }\r
+                               }\r
+                               \r
                                arg = div.childNodes;\r
                        } \r
                        \r
@@ -1373,7 +1385,7 @@ jQuery.extend({
 \r
        expr: {\r
                "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",\r
-               "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",\r
+               "#": "a.getAttribute('id')==m[2]",\r
                ":": {\r
                        // Position Checks\r
                        lt: "i<m[3]-0",\r
@@ -1454,9 +1466,9 @@ jQuery.extend({
                        context = null;\r
 \r
                // Set the correct context (if none is provided)\r
-               context = context || jQuery.context || document;\r
+               context = context || document;\r
 \r
-               if ( t.constructor != String ) return [t];\r
+               if ( typeof t != "string" ) return [t];\r
 \r
                if ( !t.indexOf("//") ) {\r
                        context = context.documentElement;\r
@@ -1504,13 +1516,13 @@ jQuery.extend({
                                        var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;\r
                                        var m = re2.exec(t);\r
 \r
-                                       if ( m[1] == "#" ) {\r
-                                               // Ummm, should make this work in all XML docs\r
-                                               var oid = document.getElementById(m[2]);\r
+                                       if ( m[1] == "#" && ret[ret.length-1].getElementById ) {\r
+                                               // Optimization for HTML document case\r
+                                               var oid = ret[ret.length-1].getElementById(m[2]);\r
                                                r = ret = oid ? [oid] : [];\r
                                                t = t.replace( re2, "" );\r
                                        } else {\r
-                                               if ( !m[2] || m[1] == "." ) m[2] = "*";\r
+                                               if ( !m[2] || m[1] == "." || m[1] == "#" ) m[2] = "*";\r
 \r
                                                for ( var i = 0; i < ret.length; i++ )\r
                                                        r = jQuery.merge( r,\r
@@ -1645,7 +1657,7 @@ jQuery.extend({
                        // Otherwise, find the expression to execute\r
                        else {\r
                                var f = jQuery.expr[m[1]];\r
-                               if ( f.constructor != String )\r
+                               if ( typeof f != "string" )\r
                                        f = jQuery.expr[m[1]][m[2]];\r
 \r
                                // Build a custom macro to enclose it\r
@@ -1754,19 +1766,15 @@ jQuery.extend({
                for ( var k = 0; k < first.length; k++ )\r
                        result[k] = first[k];\r
 \r
-               // Now check for duplicates between a and b and only\r
-               // add the unique items\r
+               // Now check for duplicates between a and b\r
+               // and only add the unique items\r
+          DupCheck:\r
                for ( var i = 0; i < second.length; i++ ) {\r
-                       var noCollision = true;\r
-\r
-                       // The collision-checking process\r
                        for ( var j = 0; j < first.length; j++ )\r
                                if ( second[i] == first[j] )\r
-                                       noCollision = false;\r
-\r
-                       // If the item is unique, add it\r
-                       if ( noCollision )\r
-                               result.push( second[i] );\r
+                                       continue DupCheck;\r
+                       // The item is unique, add it\r
+                       result.push( second[i] );\r
                }\r
 \r
                return result;\r
@@ -2016,19 +2024,31 @@ jQuery.extend({
  * This property is available before the DOM is ready, therefore you can\r
  * use it to add ready events only for certain browsers.\r
  *\r
- * See <a href="http://davecardwell.co.uk/geekery/javascript/jquery/jqbrowser/">\r
- * jQBrowser plugin</a> for advanced browser detection:\r
+ * There are situations where object detections is not reliable enough, in that\r
+ * cases it makes sense to use browser detection. Simply try to avoid both!\r
+ *\r
+ * A combination of browser and object detection yields quite reliable results.\r
  *\r
  * @example $.browser.msie\r
- * @desc returns true if the current useragent is some version of microsoft's internet explorer\r
+ * @desc Returns true if the current useragent is some version of microsoft's internet explorer\r
  *\r
  * @example if($.browser.safari) { $( function() { alert("this is safari!"); } ); }\r
  * @desc Alerts "this is safari!" only for safari browsers\r
  *\r
+ * @property\r
  * @name $.browser\r
  * @type Boolean\r
  * @cat Javascript\r
  */\r
\r
+/*\r
+ * Wheather the W3C compliant box model is being used.\r
+ *\r
+ * @property\r
+ * @name $.boxModel\r
+ * @type Boolean\r
+ * @cat Javascript\r
+ */\r
 new function() {\r
        var b = navigator.userAgent.toLowerCase();\r
 \r
@@ -2842,6 +2862,7 @@ jQuery.macros = {
                 * @cat DOM\r
                 */\r
                removeAttr: function( key ) {\r
+                       jQuery.attr( this, key, "" );\r
                        this.removeAttribute( key );\r
                },\r
 \r
@@ -2949,7 +2970,7 @@ jQuery.macros = {
                 * @cat DOM\r
                 */\r
                toggleClass: function( c ){\r
-                       jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this,c);\r
+                       jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);\r
                },\r
 \r
                /**\r