Code and documentation cleanup; fixed #317 and #464
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Sun, 3 Dec 2006 11:06:32 +0000 (11:06 +0000)
committerJörn Zaefferer <joern.zaefferer@gmail.com>
Sun, 3 Dec 2006 11:06:32 +0000 (11:06 +0000)
src/jquery/coreTest.js
src/jquery/jquery.js

index efee85b..be39a87 100644 (file)
@@ -263,20 +263,6 @@ test("filter()", function() {
        isSet( $("p").filter(["#ap", "#sndp"]).get(), q("ap", "sndp"), "filter(Array&lt;String&gt;)" );\r
 });\r
 \r
-test("filter(String) - execute callback in fitting context", function() {\r
-       expect(1);\r
-       $("input").filter(":checked",function(i){ \r
-               ok( this == q("radio2", "check1")[i], "Filter elements, context" );\r
-       });\r
-});\r
-\r
-test("filter(String) - execute callback in not-fitting context", function() {\r
-       expect(1);\r
-       $("#main > p#ap > a").filter("#foobar",function(){},function(i){\r
-               ok( this == q("google","groups", "mark")[i], "Filter elements, else context" );\r
-       });\r
-});\r
-\r
 test("not(String)", function() {\r
        ok($("#main > p#ap > a").not("#google").length == 2, "not('selector')")\r
        isSet( $("p").not("#ap, #sndp").get(), q("firstp", "en", "sap", "first", "result"), "not('selector, selector')" );\r
@@ -564,4 +550,8 @@ test("removeClass(String) - add three classes and remove again", function() {
         if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;\r
        }\r
        ok( pass, "Remove multiple classes" );\r
+});\r
+\r
+test("removeAttr(String", function() {\r
+       ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );\r
 });
\ No newline at end of file
index 883bc38..1efda21 100644 (file)
@@ -27,7 +27,7 @@ var jQuery = function(a,c) {
                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,20 +48,13 @@ 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
                // Find the matching elements and save them for later\r
                jQuery.find( a, c ) );\r
 \r
-       // See if an extra function was provided\r
-       var fn = arguments[ arguments.length - 1 ];\r
-\r
-       // If so, execute it in context\r
-       if ( fn && typeof fn == "function" )\r
-               this.each(fn);\r
-\r
        return this;\r
 };\r
 \r
@@ -73,17 +66,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 +83,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 +248,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 +288,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
@@ -773,7 +762,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
@@ -796,7 +785,7 @@ jQuery.fn = jQuery.prototype = {
        find: function(t) {\r
                return this.pushStack( jQuery.map( this, function(a){\r
                        return jQuery.find(t,a);\r
-               }), arguments );\r
+               }));\r
        },\r
 \r
        /**\r
@@ -817,7 +806,7 @@ jQuery.fn = jQuery.prototype = {
        clone: function(deep) {\r
                return this.pushStack( jQuery.map( this, function(a){\r
                        return a.cloneNode( deep != undefined ? deep : true );\r
-               }), arguments );\r
+               }));\r
        },\r
 \r
        /**\r
@@ -872,7 +861,7 @@ jQuery.fn = jQuery.prototype = {
                        typeof t == "function" &&\r
                        jQuery.grep( this, t ) ||\r
 \r
-                       jQuery.filter(t,this).r, arguments );\r
+                       jQuery.filter(t,this).r );\r
        },\r
 \r
        /**\r
@@ -906,7 +895,7 @@ jQuery.fn = jQuery.prototype = {
        not: function(t) {\r
                return this.pushStack( typeof t == "string" ?\r
                        jQuery.filter(t,this,false).r :\r
-                       jQuery.grep(this,function(a){ return a != t; }), arguments );\r
+                       jQuery.grep(this,function(a){ return a != t; }) );\r
        },\r
 \r
        /**\r
@@ -952,13 +941,13 @@ jQuery.fn = jQuery.prototype = {
         */\r
        add: function(t) {\r
                return this.pushStack( jQuery.merge( this, typeof t == "string" ?\r
-                       jQuery.find(t) : t.constructor == Array ? t : [t] ), arguments );\r
+                       jQuery.find(t) : t.constructor == Array ? t : [t] ) );\r
        },\r
 \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,13 +974,11 @@ 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 Number dir\r
         * @param Function fn The function doing the DOM manipulation.\r
         * @type jQuery\r
         * @cat Core\r
@@ -1030,28 +1017,11 @@ jQuery.fn = jQuery.prototype = {
         * @type jQuery\r
         * @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
-               \r
-               if ( fn && fn.constructor != Function ) fn = null;\r
-               if ( fn2 && fn2.constructor != Function ) fn2 = null;\r
-\r
-               if ( !fn ) {\r
-                       if ( !this.stack ) this.stack = [];\r
-                       this.stack.push( this.get() );\r
-                       this.get( a );\r
-               } else {\r
-                       var old = this.get();\r
-                       this.get( a );\r
-\r
-                       if ( fn2 && a.length || !fn2 )\r
-                               this.each( fn2 || fn ).get( old );\r
-                       else\r
-                               this.get( old ).each( fn );\r
-               }\r
-\r
-               return this;\r
+       pushStack: function(a) {\r
+               if ( !this.stack )\r
+                       this.stack = [];\r
+               this.stack.push( this.get() );\r
+               return this.set( a );\r
        }\r
 };\r
 \r
@@ -1207,6 +1177,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
@@ -1454,7 +1425,7 @@ 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
 \r
@@ -2016,19 +1987,26 @@ 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
- *\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 +2820,7 @@ jQuery.macros = {
                 * @cat DOM\r
                 */\r
                removeAttr: function( key ) {\r
+                       jQuery.attr( this, key, "" );\r
                        this.removeAttribute( key );\r
                },\r
 \r
@@ -2949,7 +2928,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