Test Paragraph.
X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=inline;f=src%2Fjquery%2Fjquery.js;h=ebf98288d97501b8d9545b85daba30a7b2cb3e90;hb=e0c7e6aec4cf55854217d5c0b2c7069ce84a3d12;hp=35616deab02450d5632b43b5421cc32afeb141e6;hpb=ca91d7896325b594ce60621c4e420dc84719302a;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 35616de..ebf9828 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -15,31 +15,23 @@ window.undefined = window.undefined; /** * Create a new jQuery Object * - * @test ok( Array.prototype.push, "Array.push()" ); - * ok( Function.prototype.apply, "Function.apply()" ); - * ok( document.getElementById, "getElementById" ); - * ok( document.getElementsByTagName, "getElementsByTagName" ); - * ok( RegExp, "RegExp" ); - * ok( jQuery, "jQuery" ); - * ok( $, "$()" ); - * * @constructor * @private * @name jQuery * @cat Core */ var jQuery = function(a,c) { - - // Shortcut for document ready (because $(document).each() is silly) - if ( a && typeof a == "function" && jQuery.fn.ready ) - return jQuery(document).ready(a); - // Make sure that a selection was provided - a = a || jQuery.context || document; + a = a || document; + + // Shortcut for document ready + // Safari reports typeof on DOM NodeLists as a function + if ( typeof a == "function" && !a.nodeType && a[0] == undefined ) + return jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a ); // Watch for when a jQuery object is passed as the selector if ( a.jquery ) - return jQuery( jQuery.merge( a, [] ) ); + return jQuery( jQuery.makeArray( a ) ); // Watch for when a jQuery object is passed at the context if ( c && c.jquery ) @@ -50,45 +42,36 @@ var jQuery = function(a,c) { return new jQuery(a,c); // Handle HTML strings - if ( a.constructor == String ) { + if ( typeof a == "string" ) { var m = /^[^<]*(<.+>)[^>]*$/.exec(a); if ( m ) a = jQuery.clean( [ m[1] ] ); } // Watch for when an array is passed in - this.get( a.constructor == Array || a.length && !a.nodeType && a[0] != undefined && a[0].nodeType ? + return this.setArray( a.constructor == Array || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType ? // Assume that it is an array of DOM Elements - jQuery.merge( a, [] ) : + jQuery.makeArray( a ) : // Find the matching elements and save them for later jQuery.find( a, c ) ); - - // See if an extra function was provided - var fn = arguments[ arguments.length - 1 ]; - - // If so, execute it in context - if ( fn && typeof fn == "function" ) - this.each(fn); - - return this; }; // Map over the $ in case of overwrite if ( typeof $ != "undefined" ) jQuery._$ = $; + +// Map the jQuery namespace to the '$' one +var $ = 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. + * This function accepts a string containing a CSS or + * basic XPath selector which is then used to match a set of elements. * * 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. + * elements. * * By default, $() looks for DOM elements within the context of the * current HTML document. @@ -98,29 +81,36 @@ if ( typeof $ != "undefined" ) * @before
one
two
three
* @result [two
] * - * @example $("Hello
Hello
Test Paragraph.
* @resultTest Paragraph.
* - * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); - * $('#foo').css({display: 'none'}); - * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); - * $('#foo').css({display: 'block'}); - * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); - * $('#floatTest').css({styleFloat: 'right'}); - * ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right'); - * $('#floatTest').css({cssFloat: 'left'}); - * ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left'); - * $('#floatTest').css({'float': 'right'}); - * ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right'); - * $('#floatTest').css({'font-size': '30px'}); - * ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px'); - * * @name css * @type jQuery * @param Hash prop A set of key/value pairs to set as style properties. @@ -557,21 +476,6 @@ jQuery.fn = jQuery.prototype = { * @resultTest Paragraph.
* @desc Changes the color of all paragraphs to red * - * - * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); - * $('#foo').css('display', 'none'); - * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); - * $('#foo').css('display', 'block'); - * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); - * $('#floatTest').css('styleFloat', 'left'); - * ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left'); - * $('#floatTest').css('cssFloat', 'right'); - * ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right'); - * $('#floatTest').css('float', 'left'); - * ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left'); - * $('#floatTest').css('font-size', '20px'); - * ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px'); - * * @name css * @type jQuery * @param String key The name of the property to set. @@ -591,19 +495,36 @@ jQuery.fn = jQuery.prototype = { * @beforeTest Paragraph.
* @result Test Paragraph. * - * @test var expected = "This link has class=\"blog\": Simon Willison's Weblog"; - * ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' ); + * @name text + * @type String + * @cat DOM + */ + + /** + * Set the text contents of all matched elements. This has the same + * effect as calling .html() with your specified string. + * + * @example $("p").text("Some new text."); + * @beforeTest Paragraph.
+ * @resultSome new text.
+ * + * @param String val The text value to set the contents of the element to. * * @name text * @type String * @cat DOM */ text: function(e) { + // A surprisingly high number of people expect the + // .text() method to do this, so lets do it! + if ( typeof e == "string" ) + return this.html( e ); + e = e || this; var t = ""; - for ( var j = 0; j < e.length; j++ ) { + for ( var j = 0, el = e.length; j < el; j++ ) { var r = e[j].childNodes; - for ( var i = 0; i < r.length; i++ ) + for ( var i = 0, rl = r.length; i < rl; i++ ) if ( r[i].nodeType != 8 ) t += r[i].nodeType != 1 ? r[i].nodeValue : jQuery.fn.text([ r[i] ]); @@ -629,11 +550,6 @@ jQuery.fn = jQuery.prototype = { * @beforeTest Paragraph.
* @resultTest Paragraph.
Test Paragraph.
* @resultTest Paragraph.
I would like to say:
* @resultI would like to say: Hello
* - * @test var defaultText = 'Try them out:' - * var result = $('#first').append('buga'); - * ok( result.text() == defaultText + 'buga', 'Check if text appending works' ); - * ok( $('#select3').append('').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element'); - * - * @name append - * @type jQuery - * @param String html A string of HTML, that will be created on the fly and appended to the target. - * @cat DOM/Manipulation - */ - - /** - * Append an element to the inside of all matched elements. - * This operation is similar to doing an appendChild to all the - * specified elements, adding them into the document. - * * @example $("p").append( $("#foo")[0] ); * @beforeI would like to say:
Hello * @resultI would like to say: Hello
* - * @test var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; - * $('#sap').append(document.getElementById('first')); - * ok( expected == $('#sap').text(), "Check for appending of element" ); - * - * @name append - * @type jQuery - * @param Element elem A DOM element that will be appended. - * @cat DOM/Manipulation - */ - - /** - * Append any number of elements to the inside of all matched elements. - * This operation is similar to doing an appendChild to all the - * specified elements, adding them into the document. - * * @example $("p").append( $("b") ); * @beforeI would like to say:
Hello * @resultI would like to say: Hello
* - * @test var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; - * $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]); - * ok( expected == $('#sap').text(), "Check for appending of array of elements" ); - * * @name append * @type jQuery - * @param ArrayI would like to say:
* @resultHelloI would like to say:
* - * @test var defaultText = 'Try them out:' - * var result = $('#first').prepend('buga'); - * ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' ); - * ok( $('#select3').prepend('').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element'); - * - * @name prepend - * @type jQuery - * @param String html A string of HTML, that will be created on the fly and appended to the target. - * @cat DOM/Manipulation - */ - - /** - * Prepend an element to the inside of all matched elements. - * This operation is the best way to insert an element inside, at the - * beginning, of all the matched element. - * * @example $("p").prepend( $("#foo")[0] ); * @beforeI would like to say:
Hello * @resultHelloI would like to say:
- * - * @test var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; - * $('#sap').prepend(document.getElementById('first')); - * ok( expected == $('#sap').text(), "Check for prepending of element" ); - * - * @name prepend - * @type jQuery - * @param Element elem A DOM element that will be appended. - * @cat DOM/Manipulation - */ - - /** - * Prepend any number of elements to the inside of all matched elements. - * This operation is the best way to insert a set of elements inside, at the - * beginning, of all the matched element. - * + * * @example $("p").prepend( $("b") ); * @beforeI would like to say:
Hello * @resultHelloI would like to say:
* - * @test var expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; - * $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]); - * ok( expected == $('#sap').text(), "Check for prepending of array of elements" ); - * * @name prepend * @type jQuery - * @param ArrayI would like to say:
* @result HelloI would like to say:
* - * @test var expected = 'This is a normal link: bugaYahoo'; - * $('#yahoo').before('buga'); - * ok( expected == $('#en').text(), 'Insert String before' ); - * - * @name before - * @type jQuery - * @param String html A string of HTML, that will be created on the fly and appended to the target. - * @cat DOM/Manipulation - */ - - /** - * Insert an element before each of the matched elements. - * * @example $("p").before( $("#foo")[0] ); * @beforeI would like to say:
Hello * @result HelloI would like to say:
* - * @test var expected = "This is a normal link: Try them out:Yahoo"; - * $('#yahoo').before(document.getElementById('first')); - * ok( expected == $('#en').text(), "Insert element before" ); - * - * @name before - * @type jQuery - * @param Element elem A DOM element that will be appended. - * @cat DOM/Manipulation - */ - - /** - * Insert any number of elements before each of the matched elements. - * * @example $("p").before( $("b") ); * @beforeI would like to say:
Hello * @result HelloI would like to say:
* - * @test var expected = "This is a normal link: Try them out:diveintomarkYahoo"; - * $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]); - * ok( expected == $('#en').text(), "Insert array of elements before" ); - * * @name before * @type jQuery - * @param ArrayI would like to say:
* @resultI would like to say:
Hello * - * @test var expected = 'This is a normal link: Yahoobuga'; - * $('#yahoo').after('buga'); - * ok( expected == $('#en').text(), 'Insert String after' ); - * - * @name after - * @type jQuery - * @param String html A string of HTML, that will be created on the fly and appended to the target. - * @cat DOM/Manipulation - */ - - /** - * Insert an element after each of the matched elements. - * * @example $("p").after( $("#foo")[0] ); * @before HelloI would like to say:
* @resultI would like to say:
Hello * - * @test var expected = "This is a normal link: YahooTry them out:"; - * $('#yahoo').after(document.getElementById('first')); - * ok( expected == $('#en').text(), "Insert element after" ); - * - * @name after - * @type jQuery - * @param Element elem A DOM element that will be appended. - * @cat DOM/Manipulation - */ - - /** - * Insert any number of elements after each of the matched elements. - * * @example $("p").after( $("b") ); * @before HelloI would like to say:
* @resultI would like to say:
Hello * - * @test var expected = "This is a normal link: YahooTry them out:diveintomark"; - * $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]); - * ok( expected == $('#en').text(), "Insert array of elements after" ); - * * @name after * @type jQuery - * @param ArrayHello, how are you?
* @result $("p").find("span").end() == [...
] * - * @test ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' ); - * * @name end * @type jQuery * @cat DOM/Traversing */ end: function() { - return this.get( this.stack.pop() ); + return this.prevObject || jQuery([]); }, /** @@ -961,17 +752,15 @@ jQuery.fn = jQuery.prototype = { * @beforeHello, how are you?
* @result $("p").find("span") == [ Hello ] * - * @test ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' ); - * * @name find * @type jQuery * @param String expr An expression to search with. * @cat DOM/Traversing */ find: function(t) { - return this.pushStack( jQuery.map( this, function(a){ + return this.set( jQuery.map( this, function(a){ return jQuery.find(t,a); - }), arguments ); + }) ); }, /** @@ -985,19 +774,14 @@ jQuery.fn = jQuery.prototype = { * @before Hello, how are you?
* @result HelloHello, how are you?
* - * @test ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' ); - * var clone = $('#yahoo').clone(); - * ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' ); - * ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' ); - * * @name clone * @type jQuery * @cat DOM/Manipulation */ clone: function(deep) { - return this.pushStack( jQuery.map( this, function(a){ + return this.set( jQuery.map( this, function(a){ return a.cloneNode( deep != undefined ? deep : true ); - }), arguments ); + }) ); }, /** @@ -1010,21 +794,34 @@ jQuery.fn = jQuery.prototype = { * * @example $("p").filter(".selected") * @beforeHello
How are you?
- * @result $("p").filter(".selected") == [Hello
] - * - * @test isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "Filter elements" ); - * @test $("input").filter(":checked",function(i){ - * ok( this == q("radio2", "check1")[i], "Filter elements, context" ); - * }); - * @test $("#main > p#ap > a").filter("#foobar",function(){},function(i){ - * ok( this == q("google","groups", "mark")[i], "Filter elements, else context" ); - * }); + * @result [Hello
] * * @name filter * @type jQuery * @param String expr An expression to search with. * @cat DOM/Traversing */ + + /** + * Removes all elements from the set of matched elements that do not + * pass the specified filter. This method is used to narrow down + * the results of a search. + * + * The elements to filter are passed as the first argument, their + * index inside the set as the second. + * + * @example $("p").filter(function(element, index) { + * return $("ol", element).length == 0; + * }) + * @beforeHow are you?
+ * @result [How are you?
] + * @desc Remove all elements that have a child ol element + * + * @name filter + * @type jQuery + * @param Function filter A function to use for filtering + * @cat DOM/Traversing + */ /** * Removes all elements from the set of matched elements that do not @@ -1037,7 +834,7 @@ jQuery.fn = jQuery.prototype = { * * @example $("p").filter([".selected", ":first"]) * @beforeHello
Hello Again
And Again
- * @result $("p").filter([".selected", ":first"]) == [Hello
,And Again
] + * @result [Hello
,And Again
] * * @name filter * @type jQuery @@ -1045,13 +842,13 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ filter: function(t) { - return this.pushStack( + return this.set( t.constructor == Array && jQuery.map(this,function(a){ - for ( var i = 0; i < t.length; i++ ) + for ( var i = 0, tl = t.length; i < tl; i++ ) if ( jQuery.filter(t[i],[a]).r.length ) return a; - return false; + return null; }) || t.constructor == Boolean && @@ -1060,7 +857,7 @@ jQuery.fn = jQuery.prototype = { typeof t == "function" && jQuery.grep( this, t ) || - jQuery.filter(t,this).r, arguments ); + jQuery.filter(t,this).r ); }, /** @@ -1086,17 +883,15 @@ jQuery.fn = jQuery.prototype = { * @beforeHello
Hello Again
* @result [Hello
] * - * @test ok($("#main > p#ap > a").not("#google").length == 2, ".not") - * * @name not * @type jQuery * @param String expr An expression with which to remove matching elements * @cat DOM/Traversing */ not: function(t) { - return this.pushStack( t.constructor == String ? - jQuery.filter(t,this,false).r : - jQuery.grep(this,function(a){ return a != t; }), arguments ); + return this.set( typeof t == "string" ? + jQuery.filter(t,this,true).r : + jQuery.grep(this,function(a){ return a != t; }) ); }, /** @@ -1141,14 +936,16 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ add: function(t) { - return this.pushStack( jQuery.merge( this, t.constructor == String ? - jQuery.find(t) : t.constructor == Array ? t : [t] ), arguments ); + return this.set( jQuery.merge( + this.get(), typeof t == "string" ? + jQuery.find(t) : + t.constructor == Array ? t : [t] ) ); }, /** * Checks the current selection against an expression and returns true, - * if the selection fits the given expression. Does return false, if the - * selection does not fit or the expression is not valid. + * if at least one element of the selection fits the given expression. + * Does return false, if no element fits or the expression is not valid. * * @example $("input[@type='checkbox']").parent().is("form") * @before @@ -1165,29 +962,6 @@ jQuery.fn = jQuery.prototype = { * @result false * @desc An invalid expression always returns false. * - * @test ok( $('#form').is('form'), 'Check for element: A form must be a form' ); - * ok( !$('#form').is('div'), 'Check for element: A form is not a div' ); - * ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' ); - * ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' ); - * ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' ); - * ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' ); - * ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' ); - * ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' ); - * ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' ); - * ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' ); - * ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' ); - * ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' ); - * ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' ); - * ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' ); - * ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' ); - * ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' ); - * ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' ); - * ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' ); - * ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' ); - * ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' ); - * ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' ); - * ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' ); - * * @name is * @type Boolean * @param String expr The expression with which to filter @@ -1198,135 +972,102 @@ jQuery.fn = jQuery.prototype = { }, /** - * - * * @private * @name domManip * @param Array args - * @param Boolean table - * @param Number int + * @param Boolean table Insert TBODY in TABLEs if one is not found. + * @param Number dir If dir<0, process args in reverse order. * @param Function fn The function doing the DOM manipulation. * @type jQuery * @cat Core */ domManip: function(args, table, dir, fn){ - var clone = this.size() > 1; + var clone = this.length > 1; var a = jQuery.clean(args); + if ( dir < 0 ) + a.reverse(); return this.each(function(){ var obj = this; - if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() != "THEAD" ) { - var tbody = this.getElementsByTagName("tbody"); + if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" ) + obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody")); - if ( !tbody.length ) { - obj = document.createElement("tbody"); - this.appendChild( obj ); - } else - obj = tbody[0]; - } + for ( var i = 0, al = a.length; i < al; i++ ) + fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] ); - for ( var i = ( dir < 0 ? a.length - 1 : 0 ); - i != ( dir < 0 ? dir : a.length ); i += dir ) { - fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] ); - } }); - }, - - /** - * - * - * @private - * @name pushStack - * @param Array a - * @param Array args - * @type jQuery - * @cat Core - */ - pushStack: function(a,args) { - var fn = args && args[args.length-1]; - var fn2 = args && args[args.length-2]; - - if ( fn && fn.constructor != Function ) fn = null; - if ( fn2 && fn2.constructor != Function ) fn2 = null; - - if ( !fn ) { - if ( !this.stack ) this.stack = []; - this.stack.push( this.get() ); - this.get( a ); - } else { - var old = this.get(); - this.get( a ); - - if ( fn2 && a.length || !fn2 ) - this.each( fn2 || fn ).get( old ); - else - this.get( old ).each( fn ); - } - - return this; } }; /** - * Extends the jQuery object itself. Can be used to add both static - * functions and plugin methods. + * Extends the jQuery object itself. Can be used to add functions into + * the jQuery namespace and to add plugin methods (plugins). * - * @example $.fn.extend({ + * @example jQuery.fn.extend({ * check: function() { - * this.each(function() { this.checked = true; }); + * return this.each(function() { this.checked = true; }); * ), * uncheck: function() { - * this.each(function() { this.checked = false; }); + * return this.each(function() { this.checked = false; }); * } * }); * $("input[@type=checkbox]").check(); * $("input[@type=radio]").uncheck(); * @desc Adds two plugin methods. * - * @private - * @name extend - * @param Object obj + * @example jQuery.extend({ + * min: function(a, b) { return a < b ? a : b; }, + * max: function(a, b) { return a > b ? a : b; } + * }); + * @desc Adds two functions into the jQuery namespace + * + * @name $.extend + * @param Object prop The object that will be merged into the jQuery object * @type Object * @cat Core */ /** - * Extend one object with another, returning the original, + * Extend one object with one or more others, returning the original, * modified, object. This is a great utility for simple inheritance. * * @example var settings = { validate: false, limit: 5, name: "foo" }; * var options = { validate: true, name: "bar" }; * jQuery.extend(settings, options); * @result settings == { validate: true, limit: 5, name: "bar" } + * @desc Merge settings and options, modifying settings * - * @test var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }; - * var options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }; - * var optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" }; - * var merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" }; - * jQuery.extend(settings, options); - * isSet( settings, merged, "Check if extended: settings must be extended" ); - * isSet ( options, optionsCopy, "Check if not modified: options must not be modified" ); + * @example var defaults = { validate: false, limit: 5, name: "foo" }; + * var options = { validate: true, name: "bar" }; + * var settings = jQuery.extend({}, defaults, options); + * @result settings == { validate: true, limit: 5, name: "bar" } + * @desc Merge defaults and options, without modifying the defaults * * @name $.extend - * @param Object obj The object to extend - * @param Object prop The object that will be merged into the first. + * @param Object target The object to extend + * @param Object prop1 The object that will be merged into the first. + * @param Object propN (optional) More objects to merge into the first * @type Object * @cat Javascript */ -jQuery.extend = jQuery.fn.extend = function(obj,prop) { - // Watch for the case where null or undefined gets passed in by accident - if ( arguments.length > 1 && (prop === null || prop == undefined) ) - return obj; - - // If no property object was provided, then we're extending jQuery - if ( !prop ) { prop = obj; obj = this; } - - // Extend the base object - for ( var i in prop ) obj[i] = prop[i]; +jQuery.extend = jQuery.fn.extend = function() { + // copy reference to target object + var target = arguments[0], + a = 1; + + // extend jQuery itself if only one argument is passed + if ( arguments.length == 1 ) { + target = this; + a = 0; + } + var prop; + while (prop = arguments[a++]) + // Extend the base object + for ( var i in prop ) target[i] = prop[i]; // Return the modified object - return obj; + return target; }; jQuery.extend({ @@ -1342,9 +1083,9 @@ jQuery.extend({ jQuery.each( jQuery.macros.axis, function(i,n){ jQuery.fn[ i ] = function(a) { var ret = jQuery.map(this,n); - if ( a && a.constructor == String ) + if ( a && typeof a == "string" ) ret = jQuery.filter(a,ret).r; - return this.pushStack( ret, arguments ); + return this.set( ret ); }; }); @@ -1352,7 +1093,7 @@ jQuery.extend({ jQuery.fn[ i ] = function(){ var a = arguments; return this.each(function(){ - for ( var j = 0; j < a.length; j++ ) + for ( var j = 0, al = a.length; j < al; j++ ) jQuery(a[j])[n]( this ); }); }; @@ -1411,39 +1152,32 @@ jQuery.extend({ * @type Object * @cat Javascript */ + // args is for internal usage only each: function( obj, fn, args ) { if ( obj.length == undefined ) for ( var i in obj ) fn.apply( obj[i], args || [i, obj[i]] ); else - for ( var i = 0; i < obj.length; i++ ) + for ( var i = 0, ol = obj.length; i < ol; i++ ) if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break; return obj; }, className: { - add: function(o,c){ - if (jQuery.className.has(o,c)) return; - o.className += ( o.className ? " " : "" ) + c; + add: function( elem, c ){ + jQuery.each( c.split(/\s+/), function(i, cur){ + if ( !jQuery.className.has( elem.className, cur ) ) + elem.className += ( elem.className ? " " : "" ) + cur; + }); }, - remove: function(o,c){ - if( !c ) { - o.className = ""; - } else { - var classes = o.className.split(" "); - for(var i=0; i