Test Paragraph.
X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjquery%2Fjquery.js;h=af729695b92db3245847fd53de115d1e2b7447aa;hb=cc5f46454fbf34ae8a8e526a621cab55e1c79cdb;hp=9768eee002afede1e12198546b851dafc7c0c1e7;hpb=14b09024cbe39bc16200448170ccbd183e3a9ec2;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 9768eee..af72969 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -15,14 +15,6 @@ 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 @@ -30,16 +22,16 @@ window.undefined = window.undefined; */ var jQuery = function(a,c) { - // Shortcut for document ready (because $(document).each() is silly) - if ( a && typeof a == "function" && jQuery.fn.ready ) + // Shortcut for document ready + if ( a && typeof a == "function" && jQuery.fn.ready && !a.nodeType && a[0] == undefined ) // Safari reports typeof on DOM NodeLists as a function return jQuery(document).ready(a); // Make sure that a selection was provided - a = a || jQuery.context || document; + a = a || document; // 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,15 +42,15 @@ 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 ? + this.set( 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 ) ); @@ -76,19 +68,19 @@ var jQuery = function(a,c) { // 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 +90,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 +462,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,9 +481,6 @@ 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 @@ -629,11 +516,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. @@ -718,10 +590,6 @@ jQuery.fn = jQuery.prototype = { * @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. @@ -737,10 +605,6 @@ jQuery.fn = jQuery.prototype = { * @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. @@ -782,10 +641,6 @@ jQuery.fn = jQuery.prototype = { * @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. @@ -801,10 +656,6 @@ jQuery.fn = jQuery.prototype = { * @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. @@ -841,10 +688,6 @@ jQuery.fn = jQuery.prototype = { * @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. @@ -858,10 +701,6 @@ jQuery.fn = jQuery.prototype = { * @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. @@ -898,10 +733,6 @@ jQuery.fn = jQuery.prototype = { * @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. @@ -915,10 +746,6 @@ jQuery.fn = jQuery.prototype = { * @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() ); + if( !(this.stack && this.stack.length) ) + return this; + return this.set( this.stack.pop() ); }, /** @@ -961,8 +788,6 @@ 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. @@ -985,11 +810,6 @@ 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 @@ -1012,14 +832,6 @@ jQuery.fn = jQuery.prototype = { * @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" ); - * }); - * * @name filter * @type jQuery * @param String expr An expression to search with. @@ -1051,7 +863,7 @@ jQuery.fn = jQuery.prototype = { for ( var i = 0; i < t.length; i++ ) if ( jQuery.filter(t[i],[a]).r.length ) return a; - return false; + return null; }) || t.constructor == Boolean && @@ -1086,16 +898,14 @@ 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 : + return this.pushStack( typeof t == "string" ? + jQuery.filter(t,this,true).r : jQuery.grep(this,function(a){ return a != t; }), arguments ); }, @@ -1141,14 +951,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.pushStack( jQuery.merge( + this.get(), typeof t == "string" ? + jQuery.find(t) : + t.constructor == Array ? t : [t] ), arguments ); }, /** * 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 +977,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,38 +987,30 @@ 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; i < a.length; 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] ] ); - } }); }, @@ -1244,8 +1025,8 @@ jQuery.fn = jQuery.prototype = { * @cat Core */ pushStack: function(a,args) { - var fn = args && args[args.length-1]; - var fn2 = args && args[args.length-2]; + var fn = args && args.length > 1 && args[args.length-1]; + var fn2 = args && args.length > 2 && args[args.length-2]; if ( fn && fn.constructor != Function ) fn = null; if ( fn2 && fn2.constructor != Function ) fn2 = null; @@ -1253,15 +1034,15 @@ jQuery.fn = jQuery.prototype = { if ( !fn ) { if ( !this.stack ) this.stack = []; this.stack.push( this.get() ); - this.get( a ); + this.set( a ); } else { var old = this.get(); - this.get( a ); + this.set( a ); if ( fn2 && a.length || !fn2 ) - this.each( fn2 || fn ).get( old ); + this.each( fn2 || fn ).set( old ); else - this.get( old ).each( fn ); + this.set( old ).each( fn ); } return this; @@ -1269,64 +1050,73 @@ jQuery.fn = jQuery.prototype = { }; /** - * 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,7 +1132,7 @@ 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 ); }; @@ -1411,6 +1201,7 @@ 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 ) @@ -1465,7 +1256,7 @@ jQuery.extend({ if ( p == "height" || p == "width" ) { var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"]; - for ( var i in d ) { + for ( var i=0; i