Fixed two bugs with jQuery: One with height computation, one with .find(expr,fn)...
[jquery.git] / src / jquery / jquery.js
index 63d795c..c3975e3 100644 (file)
@@ -38,11 +38,11 @@ function jQuery(a,c) {
 
        // Watch for when a jQuery object is passed as the selector
        if ( a.jquery )
-               return $( jQuery.merge( a, [] ) );
+               return jQuery( jQuery.merge( a, [] ) );
 
        // Watch for when a jQuery object is passed at the context
        if ( c && c.jquery )
-               return $( c ).find(a);
+               return jQuery( c ).find(a);
        
        // If the context is global, return a new object
        if ( window == this )
@@ -69,8 +69,110 @@ function jQuery(a,c) {
 }
 
 // Map over the $ in case of overwrite
-if ( $ )
+if ( typeof $ != "undefined" )
        jQuery._$ = $;
+       
+/**
+ * This function accepts a string containing a CSS selector, 
+ * basic XPath, or raw HTML, which is then used to match a set of elements.
+ * The HTML string is different from the traditional selectors in that
+ * it creates the DOM elements representing that HTML string, on the fly,
+ * to be (assumedly) inserted into the document later.
+ *
+ * The core functionality of jQuery centers around this function. 
+ * Everything in jQuery is based upon this, or uses this in some way. 
+ * The most basic use of this function is to pass in an expression 
+ * (usually consisting of CSS or XPath), which then finds all matching 
+ * elements and remembers them for later use.
+ *
+ * By default, $() looks for DOM elements within the context of the 
+ * current HTML document.
+ *
+ * @example $("div > p")
+ * @desc This finds all p elements that are children of a div element.
+ * @before <p>one</p> <div><p>two</p></div> <p>three</p>
+ * @result [ <p>two</p> ]
+ *
+ * @example $("<div><p>Hello</p></div>").appendTo("#body")
+ * @desc Creates a div element (and all of its contents) dynamically, and appends it to the element with the ID of body.
+ *
+ * @name $
+ * @param String expr An expression to search with, or a string of HTML to create on the fly.
+ * @cat Core
+ * @type jQuery
+ */
+/**
+ * This function accepts a string containing a CSS selector, or
+ * basic XPath, which is then used to match a set of elements with the
+ * context of the specified DOM element, or document
+ *
+ * @example $("div", xml.responseXML)
+ * @desc This finds all div elements within the specified XML document.
+ *
+ * @name $
+ * @param String expr An expression to search with.
+ * @param DOMElement context A DOM Element, or Document, representing the base context.
+ * @cat Core
+ * @type jQuery
+ */
+/**
+ * Wrap jQuery functionality around a specific DOM Element.
+ * This function also accepts XML Documents and Window objects
+ * as valid arguments (even though they are not DOM Elements).
+ *
+ * @example $(document).find("div > p")
+ * @before <p>one</p> <div><p>two</p></div> <p>three</p>
+ * @result [ <p>two</p> ]
+ *
+ * @example $(document).ready( loaded );
+ * @desc Executes the "loaded" function when the DOM is ready to
+ * be manipulated.
+ *
+ * @name $
+ * @param DOMElement elem A DOM element to be encapsulated by a jQuery object.
+ * @cat Core
+ * @type jQuery
+ */
+/**
+ * Wrap jQuery functionality around a set of DOM Elements.
+ *
+ * @example $( myForm.elements ).hide()
+ * @desc Hides all the input elements within a form
+ *
+ * @name $
+ * @param Array<DOMElement> elems An array of DOM elements to be encapsulated by a jQuery object.
+ * @cat Core
+ * @type jQuery
+ */
+/**
+ * A shorthand for $(document).ready(), allowing you to bind a function
+ * to be executed when the DOM document has finished loading.
+ *
+ * @example $( loaded )
+ * @desc Executes the function "loaded" when the DOM is ready to be used.
+ *
+ * @name $
+ * @param Function fn The function to execute when the DOM is ready.
+ * @cat Core
+ * @type jQuery
+ */
+/**
+ * A means of creating a duplicate copy of a jQuery object.
+ *
+ * @example var div = $("div");
+ * $( div ).find("p")
+ * @desc Locates all p elements with all div elements, without disrupting the original jQuery object contained in 'div'.
+ *
+ * @name $
+ * @param jQuery obj The jQuery object to be cloned.
+ * @cat Core
+ * @type jQuery
+ */
 
 // Map the jQuery namespace to the '$' one
 var $ = jQuery;
@@ -193,7 +295,15 @@ jQuery.fn = jQuery.prototype = {
         * argument representing the position of the element in the matched
         * set.
         *
-        * @example $("img").each(function(){ this.src = "test.jpg"; });
+        * @example $("img").each(function(){
+        *   this.src = "test.jpg";
+        * });
+        * @before <img/> <img/>
+        * @result <img src="test.jpg"/> <img src="test.jpg"/>
+        *
+        * @example $("img").each(function(i){
+        *   alert( "Image #" + i + " is " + this );
+        * });
         * @before <img/> <img/>
         * @result <img src="test.jpg"/> <img src="test.jpg"/>
         *
@@ -370,8 +480,9 @@ jQuery.fn = jQuery.prototype = {
                for ( var j = 0; j < e.length; j++ ) {
                        var r = e[j].childNodes;
                        for ( var i = 0; i < r.length; i++ )
-                               t += r[i].nodeType != 1 ?
-                                       r[i].nodeValue : jQuery.fn.text([ r[i] ]);
+                               if ( r[i].nodeType != 8 )
+                                       t += r[i].nodeType != 1 ?
+                                               r[i].nodeValue : jQuery.fn.text([ r[i] ]);
                }
                return t;
        },
@@ -629,6 +740,7 @@ jQuery.fn = jQuery.prototype = {
         * @example $("p").not("#selected")
         * @before <p>Hello</p><p id="selected">Hello Again</p>
         * @result [ <p>Hello</p> ]
+        * @test cmpOK($("#main > p#ap > a").not("#google").length, "==", 2, ".not")
         *
         * @name not
         * @type jQuery
@@ -666,7 +778,7 @@ jQuery.fn = jQuery.prototype = {
         * @name add
         * @type jQuery
         * @param Array<Element> els An array of Elements to add
-        * @cat jQuery
+        * @cat DOM/Traversing
         */
 
        /**
@@ -680,7 +792,7 @@ jQuery.fn = jQuery.prototype = {
         * @name add
         * @type jQuery
         * @param Element el An Element to add
-        * @cat jQuery
+        * @cat DOM/Traversing
         */
        add: function(t) {
                return this.pushStack( jQuery.merge( this, t.constructor == String ?
@@ -695,7 +807,7 @@ jQuery.fn = jQuery.prototype = {
         * @member jQuery
         * @param {String} expr The expression with which to filter
         * @type Boolean
-        * @cat jQuery
+        * @cat DOM/Traversing
         */
        is: function(expr) {
                return expr ? jQuery.filter(expr,this).r.length > 0 : this.length > 0;
@@ -756,7 +868,7 @@ jQuery.fn = jQuery.prototype = {
                        var old = this.get();
                        this.get( a );
                        if ( fn.constructor == Function )
-                               return this.each( fn );
+                               this.each( fn );
                        this.get( old );
                }
 
@@ -813,7 +925,7 @@ jQuery.extend({
                                var a = arguments;
                                return this.each(function(){
                                        for ( var j = 0; j < a.length; j++ )
-                                               $(a[j])[n]( this );
+                                               jQuery(a[j])[n]( this );
                                });
                        };
                });
@@ -914,9 +1026,9 @@ jQuery.extend({
                                        oHeight = e.offsetHeight;
                                        oWidth = e.offsetWidth;
                                } else {
-                                       e = $(e.cloneNode(true)).css({
+                                       e = jQuery(e.cloneNode(true)).css({
                                                visibility: "hidden", position: "absolute", display: "block"
-                                       }).prependTo("body")[0];
+                                       }).appendTo(e.parentNode)[0];
 
                                        oHeight = e.clientHeight;
                                        oWidth = e.clientWidth;
@@ -1016,6 +1128,7 @@ jQuery.extend({
                        odd: "i%2",
                        
                        // Child Checks
+                       "nth-child": "jQuery.sibling(a,m[3]).cur",
                        "first-child": "jQuery.sibling(a,0).cur",
                        "last-child": "jQuery.sibling(a,0).last",
                        "only-child": "jQuery.sibling(a).length==1",
@@ -1099,7 +1212,7 @@ jQuery.extend({
         * @test t( "Adjacent", "p + p", ["ap","en","sap"] );
         * @test t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] );
         * @test t( "First Child", "p:first-child", ["firstp","sndp"] );
-   * @test t( "Attribute Exists", "a[@title]", ["google"] );
+        * @test t( "Attribute Exists", "a[@title]", ["google"] );
         * @test t( "Attribute Exists", "*[@title]", ["google"] );
         * @test t( "Attribute Exists", "[@title]", ["google"] );
         * @test t( "Attribute Equals", "a[@rel='bookmark']", ["simon1"] );
@@ -1188,6 +1301,8 @@ jQuery.extend({
                        var foundToken = false;
                        
                        for ( var i = 0; i < jQuery.token.length; i += 2 ) {
+                               if ( foundToken ) continue;
+
                                var re = new RegExp("^(" + jQuery.token[i] + ")");
                                var m = re.exec(t);
                                
@@ -1224,8 +1339,9 @@ jQuery.extend({
                                                        );
                                        }
                                }
-                       }
        
+                       }
+
                        if ( t ) {
                                var val = jQuery.filter(t,r);
                                ret = r = val.r;
@@ -1256,7 +1372,9 @@ jQuery.extend({
                        "class": "className",
                        "float": "cssFloat",
                        innerHTML: "innerHTML",
-                       className: "className"
+                       className: "className",
+                       value: "value",
+                       disabled: "disabled"
                };
 
                if ( fix[name] ) {
@@ -1637,7 +1755,7 @@ new function() {
                safari: /webkit/.test(b),
                opera: /opera/.test(b),
                msie: /msie/.test(b) && !/opera/.test(b),
-               mozilla: /mozilla/.test(b) && !/compatible/.test(b)
+               mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
        };
 
        // Check to see if the W3C box model is being used
@@ -2201,7 +2319,7 @@ jQuery.macros = {
 
                /**
                 * Get a set of elements containing the unique ancestors of the matched
-                * set of elements.
+                * set of elements (except for the root element).
                 *
                 * @example $("span").ancestors()
                 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
@@ -2229,7 +2347,7 @@ jQuery.macros = {
                
                /**
                 * Get a set of elements containing the unique ancestors of the matched
-                * set of elements.
+                * set of elements (except for the root element).
                 *
                 * @example $("span").ancestors()
                 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
@@ -2374,7 +2492,7 @@ jQuery.macros = {
                 * @param String expr An expression to filter the child Elements with
                 * @cat DOM/Traversing
                 */
-               children: "a.childNodes"
+               children: "jQuery.sibling(a.firstChild)"
        },
 
        each: {
@@ -2444,7 +2562,7 @@ jQuery.macros = {
                 * @cat Effects
                 */
                toggle: function(){
-                       $(this)[ $(this).is(":hidden") ? "show" : "hide" ].apply( $(this), arguments );
+                       jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ].apply( jQuery(this), arguments );
                },
                
                /**
@@ -2540,7 +2658,7 @@ jQuery.macros = {
                 * @cat DOM/Manipulation
                 */
                remove: function(a){
-                       if ( !a || jQuery.filter( [this], a ).r )
+                       if ( !a || jQuery.filter( a, [this] ).r )
                                this.parentNode.removeChild( this );
                },
        
@@ -2588,7 +2706,7 @@ jQuery.macros = {
                 */
                bind: function( type, fn ) {
                        if ( fn.constructor == String )
-                               fn = new Function("e", ( !fn.indexOf(".") ? "$(this)" : "return " ) + fn);
+                               fn = new Function("e", ( !fn.indexOf(".") ? "jQuery(this)" : "return " ) + fn);
                        jQuery.event.add( this, type, fn );
                },
                
@@ -2655,4 +2773,4 @@ jQuery.macros = {
        }
 };
 
-jQuery.init();
\ No newline at end of file
+jQuery.init();