X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=a35823842f626f0d915704ec27439b31f47fbc68;hb=8a1adfcd2a0ea8fbb72ac53537cbfe6ebeba0cdf;hp=a0146c3721dd886c4e668c15cae98bcc1ed162e5;hpb=a42a8e5afa2e4ca7690081bce9899d5e6e0d1cf4;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index a0146c3..a358238 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -2,8 +2,8 @@ * jQuery - New Wave Javascript * * Copyright (c) 2006 John Resig (jquery.com) - * Licensed under the MIT License: - * http://www.opensource.org/licenses/mit-license.php + * Dual licensed under the MIT (MIT-LICENSE.txt) + * and GPL (GPL-LICENSE.txt) licenses. * * $Date$ * $Rev$ @@ -14,7 +14,18 @@ window.undefined = window.undefined; /** * Create a new jQuery Object + * + * @test ok( Array.prototype.push, "Array.push()" ); + * @test ok( Function.prototype.apply, "Function.apply()" ); + * @test ok( document.getElementById, "getElementById" ); + * @test ok( document.getElementsByTagName, "getElementsByTagName" ); + * @test ok( RegExp, "RegExp" ); + * @test ok( jQuery, "jQuery" ); + * @test ok( $, "$()" ); + * * @constructor + * @private + * @name jQuery */ function jQuery(a,c) { @@ -25,7 +36,7 @@ function jQuery(a,c) { if ( a && a.constructor == Function && jQuery.fn.ready ) return jQuery(document).ready(a); - // Make sure t hat a selection was provided + // Make sure that a selection was provided a = a || jQuery.context || document; /* @@ -49,11 +60,11 @@ function jQuery(a,c) { // Watch for when a jQuery object is passed as the selector if ( a.jquery ) - return a; + return $( jQuery.merge( a, [] ) ); // Watch for when a jQuery object is passed at the context if ( c && c.jquery ) - return jQuery(c.get()).find(a); + return $( c ).find(a); // If the context is global, return a new object if ( window == this ) @@ -102,6 +113,8 @@ jQuery.fn = jQuery.prototype = { * @before * @result 2 * + * @test cmpOK( $("div").length, "==", 2, "Get Number of Elements Found" ); + * * @property * @name length * @type Number @@ -115,6 +128,8 @@ jQuery.fn = jQuery.prototype = { * @before * @result 2 * + * @test cmpOK( $("div").size(), "==", 2, "Get Number of Elements Found" ); + * * @name size * @type Number * @cat Core @@ -132,6 +147,8 @@ jQuery.fn = jQuery.prototype = { * @before * @result [ ] * + * @test isSet( $("div").get(), q("main","foo"), "Get All Elements" ); + * * @name get * @type Array * @cat Core @@ -145,6 +162,8 @@ jQuery.fn = jQuery.prototype = { * @before * @result [ ] * + * @test cmpOK( $("div").get(0), "==", document.getElementById("main"), "Get A Single Element" ); + * * @name get * @type Element * @param Number num Access the element in the Nth position. @@ -197,6 +216,14 @@ jQuery.fn = jQuery.prototype = { * @before * @result * + * @test var div = $("div"); + * div.each(function(){this.foo = 'zoo';}); + * var pass = true; + * for ( var i = 0; i < div.size(); i++ ) { + * if ( div.get(i).foo != "zoo" ) pass = false; + * } + * ok( pass, "Execute a function, Relative" ); + * * @name each * @type jQuery * @param Function fn A function to execute @@ -205,6 +232,14 @@ jQuery.fn = jQuery.prototype = { each: function( fn, args ) { return jQuery.each( this, fn, args ); }, + + index: function( obj ) { + var pos = -1; + this.each(function(i){ + if ( this == obj ) pos = i; + }); + return pos; + }, /** * Access a property on the first matched element. @@ -230,6 +265,14 @@ jQuery.fn = jQuery.prototype = { * @before * @result Test Image * + * @test var div = $("div"); + * div.attr({foo: 'baz', zoo: 'ping'}); + * var pass = true; + * for ( var i = 0; i < div.size(); i++ ) { + * if ( div.get(i).foo != "baz" && div.get(i).zoo != "ping" ) pass = false; + * } + * ok( pass, "Set Multiple Attributes" ); + * * @name attr * @type jQuery * @param Hash prop A set of key/value pairs to set as object properties. @@ -243,6 +286,14 @@ jQuery.fn = jQuery.prototype = { * @before * @result * + * @test var div = $("div"); + * div.attr("foo", "bar"); + * var pass = true; + * for ( var i = 0; i < div.size(); i++ ) { + * if ( div.get(i).foo != "bar" ) pass = false; + * } + * ok( pass, "Set Attribute" ); + * * @name attr * @type jQuery * @param String key The name of the property to set. @@ -273,7 +324,7 @@ jQuery.fn = jQuery.prototype = { // Look for the case where we're accessing a style value jQuery[ type || "attr" ]( this[0], key ); }, - + /** * Access a style property on the first matched element. * This method makes it easy to retreive a style property value @@ -516,6 +567,12 @@ jQuery.fn = jQuery.prototype = { return jQuery.find(t,a); }), arguments ); }, + + clone: function(deep) { + return this.pushStack( jQuery.map( this, function(a){ + return a.cloneNode( deep != undefined ? deep : true ); + }), arguments ); + }, /** * Removes all elements from the set of matched elements that do not @@ -683,7 +740,7 @@ jQuery.fn = jQuery.prototype = { return this.each(function(){ var obj = this; - if ( table && this.nodeName == "TABLE" ) { + if ( table && this.nodeName == "TABLE" && a[0].nodeName != "THEAD" ) { var tbody = this.getElementsByTagName("tbody"); if ( !tbody.length ) { @@ -742,7 +799,7 @@ jQuery.fn = jQuery.prototype = { * Extend one object with another, returning the original, * modified, object. This is a great utility for simple inheritance. * - * @name $.extend + * @name jQuery.extend * @param Object obj The object to extend * @param Object prop The object that will be merged into the first. * @type Object @@ -806,7 +863,7 @@ jQuery.extend({ }); jQuery.each( jQuery.macros.css, function(i,n){ - jQuery.fn[ i ] = function(h) { + jQuery.fn[ n ] = function(h) { return h == undefined ? ( this.length ? jQuery.css( this[0], n ) : null ) : this.css( n, h ); @@ -819,7 +876,7 @@ jQuery.extend({ * A generic iterator function, which can be used to seemlessly * iterate over both objects and arrays. * - * @name $.each + * @name jQuery.each * @param Object obj The object, or array, to iterate over. * @param Object fn The function that will be executed on every object. * @type Object @@ -846,7 +903,7 @@ jQuery.extend({ new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), ""); }, has: function(e,a) { - if ( e.className ) + if ( e.className != undefined ) e = e.className; return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e); } @@ -880,7 +937,7 @@ jQuery.extend({ oHeight = e.offsetHeight; oWidth = e.offsetWidth; } else - jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "" }, + jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "block" }, function(){ oHeight = e.clientHeight; oWidth = e.clientWidth; @@ -915,30 +972,36 @@ jQuery.extend({ var r = []; for ( var i = 0; i < a.length; i++ ) { if ( a[i].constructor == String ) { + + var table = ""; - if ( !a[i].indexOf(""; + } else if ( !a[i].indexOf(""; } else if ( !a[i].indexOf(""; } var div = document.createElement("div"); div.innerHTML = a[i]; - if ( tr || td ) { - div = div.firstChild.firstChild; - if ( td ) div = div.firstChild; + if ( table ) { + div = div.firstChild; + if ( table != "thead" ) div = div.firstChild; + if ( table == "td" ) div = div.firstChild; } for ( var j = 0; j < div.childNodes.length; j++ ) r.push( div.childNodes[j] ); - } else if ( a[i].jquery || a[i].length && !a[i].nodeType ) - for ( var k = 0; k < a[i].length; k++ ) - r.push( a[i][k] ); - else if ( a[i] !== null ) - r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) ); + } else if ( a[i].jquery || a[i].length && !a[i].nodeType ) + for ( var k = 0; k < a[i].length; k++ ) + r.push( a[i][k] ); + else if ( a[i] !== null ) + r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) ); } return r; }, @@ -976,7 +1039,8 @@ jQuery.extend({ // Form elements enabled: "!a.disabled", disabled: "a.disabled", - checked: "a.checked" + checked: "a.checked", + selected: "a.selected" }, ".": "jQuery.className.has(a,m[2])", "@": { @@ -1004,6 +1068,97 @@ jQuery.extend({ } ], + /** + * + * @test t( "Element Selector", "div", ["main","foo"] ); + * @test t( "Element Selector", "body", ["body"] ); + * @test t( "Element Selector", "html", ["html"] ); + * @test cmpOK( $("*").size(), ">=", 30, "Element Selector" ); + * @test t( "Parent Element", "div div", ["foo"] ); + * + * @test t( "ID Selector", "#body", ["body"] ); + * @test t( "ID Selector w/ Element", "body#body", ["body"] ); + * @test t( "ID Selector w/ Element", "ul#first", [] ); + * + * @test t( "Class Selector", ".blog", ["mark","simon"] ); + * @test t( "Class Selector", ".blog.link", ["simon"] ); + * @test t( "Class Selector w/ Element", "a.blog", ["mark","simon"] ); + * @test t( "Parent Class Selector", "p .blog", ["mark","simon"] ); + * + * @test t( "Comma Support", "a.blog, div", ["mark","simon","main","foo"] ); + * @test t( "Comma Support", "a.blog , div", ["mark","simon","main","foo"] ); + * @test t( "Comma Support", "a.blog ,div", ["mark","simon","main","foo"] ); + * @test t( "Comma Support", "a.blog,div", ["mark","simon","main","foo"] ); + * + * @test t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] ); + * @test t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] ); + * @test t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] ); + * @test t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] ); + * @test t( "Child w/ Class", "p > a.blog", ["mark","simon"] ); + * @test t( "All Children", "code > *", ["anchor1","anchor2"] ); + * @test t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] ); + * @test t( "Adjacent", "a + a", ["groups"] ); + * @test t( "Adjacent", "a +a", ["groups"] ); + * @test t( "Adjacent", "a+ a", ["groups"] ); + * @test t( "Adjacent", "a+a", ["groups"] ); + * @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", "*[@title]", ["google"] ); + * @test t( "Attribute Exists", "[@title]", ["google"] ); + * @test t( "Attribute Equals", "a[@rel='bookmark']", ["simon1"] ); + * @test t( "Attribute Equals", 'a[@rel="bookmark"]', ["simon1"] ); + * @test t( "Attribute Equals", "a[@rel=bookmark]", ["simon1"] ); + * @test t( "Multiple Attribute Equals", "input[@type='hidden'],input[@type='radio']", ["hidden1","radio1","radio2"] ); + * @test t( "Multiple Attribute Equals", "input[@type=\"hidden\"],input[@type='radio']", ["hidden1","radio1","radio2"] ); + * @test t( "Multiple Attribute Equals", "input[@type=hidden],input[@type=radio]", ["hidden1","radio1","radio2"] ); + * + * @test t( "Attribute Begins With", "a[@href ^= 'http://www']", ["google","yahoo"] ); + * @test t( "Attribute Ends With", "a[@href $= 'org/']", ["mark"] ); + * @test t( "Attribute Contains", "a[@href *= 'google']", ["google","groups"] ); + * @test t( "First Child", "p:first-child", ["firstp","sndp"] ); + * @test t( "Last Child", "p:last-child", ["sap"] ); + * @test t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] ); + * @test t( "Empty", "ul:empty", ["firstUL"] ); + * @test t( "Enabled UI Element", "input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2"] ); + * @test t( "Disabled UI Element", "input:disabled", ["text2"] ); + * @test t( "Checked UI Element", "input:checked", ["radio2","check1"] ); + * @test t( "Text Contains", "a:contains('Google')", ["google","groups"] ); + * @test t( "Text Contains", "a:contains('Google Groups')", ["groups"] ); + * @test t( "Element Preceded By", "p ~ div", ["foo"] ); + * @test t( "Not", "a.blog:not(.link)", ["mark"] ); + * + * @test cmpOK( jQuery.find("//*").length, ">=", 30, "All Elements (//*)" ); + * @test t( "All Div Elements", "//div", ["main","foo"] ); + * @test t( "Absolute Path", "/html/body", ["body"] ); + * @test t( "Absolute Path w/ *", "/* /body", ["body"] ); + * @test t( "Long Absolute Path", "/html/body/dl/div/div/p", ["sndp","en","sap"] ); + * @test t( "Absolute and Relative Paths", "/html//div", ["main","foo"] ); + * @test t( "All Children, Explicit", "//code/*", ["anchor1","anchor2"] ); + * @test t( "All Children, Implicit", "//code/", ["anchor1","anchor2"] ); + * @test t( "Attribute Exists", "//a[@title]", ["google"] ); + * @test t( "Attribute Equals", "//a[@rel='bookmark']", ["simon1"] ); + * @test t( "Parent Axis", "//p/..", ["main","foo"] ); + * @test t( "Sibling Axis", "//p/../", ["firstp","ap","foo","first","firstUL","empty","form","sndp","en","sap"] ); + * @test t( "Sibling Axis", "//p/../*", ["firstp","ap","foo","first","firstUL","empty","form","sndp","en","sap"] ); + * @test t( "Has Children", "//p[a]", ["firstp","ap","en","sap"] ); + * + * @test t( "nth Element", "p:nth(1)", ["ap"] ); + * @test t( "First Element", "p:first", ["firstp"] ); + * @test t( "Last Element", "p:last", ["first"] ); + * @test t( "Even Elements", "p:even", ["firstp","sndp","sap"] ); + * @test t( "Odd Elements", "p:odd", ["ap","en","first"] ); + * @test t( "Position Equals", "p:eq(1)", ["ap"] ); + * @test t( "Position Greater Than", "p:gt(0)", ["ap","sndp","en","sap","first"] ); + * @test t( "Position Less Than", "p:lt(3)", ["firstp","ap","sndp"] ); + * @test t( "Is A Parent", "p:parent", ["firstp","ap","sndp","en","sap","first"] ); + * @test t( "Is Visible", "input:visible", ["text1","text2","radio1","radio2","check1","check2"] ); + * @test t( "Is Hidden", "input:hidden", ["hidden1","hidden2"] ); + * + * @name jQuery.find + * @private + */ find: function( t, context ) { // Make sure that the context is a DOM Element if ( context && context.nodeType == undefined ) @@ -1155,7 +1310,7 @@ jQuery.extend({ .replace( 'S', "([a-z*_-][a-z0-9_-]*)" ) // Look for something (optionally) enclosed with quotes - .replace( 'Q', " *'?\"?([^'\"]*)'?\"? *" ), "i" ); + .replace( 'Q', " *'?\"?([^'\"]*?)'?\"? *" ), "i" ); var m = re.exec( t ); @@ -1201,7 +1356,7 @@ jQuery.extend({ * Remove the whitespace from the beginning and end of a string. * * @private - * @name $.trim + * @name jQuery.trim * @type String * @param String str The string to trim. */ @@ -1213,80 +1368,82 @@ jQuery.extend({ * All ancestors of a given element. * * @private - * @name $.parents + * @name jQuery.parents * @type Array * @param Element elem The element to find the ancestors of. */ - parents: function(a){ - var b = []; - var c = a.parentNode; - while ( c && c != document ) { - b.push( c ); - c = c.parentNode; + parents: function( elem ){ + var matched = []; + var cur = elem.parentNode; + while ( cur && cur != document ) { + matched.push( cur ); + cur = cur.parentNode; } - return b; + return matched; }, /** * All elements on a specified axis. * * @private - * @name $.sibling + * @name jQuery.sibling * @type Array * @param Element elem The element to find all the siblings of (including itself). */ - sibling: function(a,n) { - var type = []; - var tmp = a.parentNode.childNodes; - for ( var i = 0; i < tmp.length; i++ ) { - if ( tmp[i].nodeType == 1 ) - type.push( tmp[i] ); - if ( tmp[i] == a ) - type.n = type.length - 1; + sibling: function(elem, pos, not) { + var elems = []; + + var siblings = elem.parentNode.childNodes; + for ( var i = 0; i < siblings.length; i++ ) { + if ( not === true && siblings[i] == elem ) continue; + + if ( siblings[i].nodeType == 1 ) + elems.push( siblings[i] ); + if ( siblings[i] == elem ) + elems.n = elems.length - 1; } - type.last = type.n == type.length - 1; - type.cur = - n == "even" && type.n % 2 == 0 || - n == "odd" && type.n % 2 || - type[n] == a; - type.prev = type[type.n - 1]; - type.next = type[type.n + 1]; - return type; + + return jQuery.extend( elems, { + last: elems.n == elems.length - 1, + cur: pos == "even" && elems.n % 2 == 0 || pos == "odd" && elems.n % 2 || elems[pos] == elem, + prev: elems[elems.n - 1], + next: elems[elems.n + 1] + }); }, /** * Merge two arrays together, removing all duplicates. * * @private - * @name $.merge + * @name jQuery.merge * @type Array * @param Array a The first array to merge. * @param Array b The second array to merge. */ - merge: function(a,b) { - var d = []; + merge: function(first, second) { + var result = []; // Move b over to the new array (this helps to avoid // StaticNodeList instances) - for ( var k = 0; k < b.length; k++ ) - d[k] = b[k]; + for ( var k = 0; k < first.length; k++ ) + result[k] = first[k]; // Now check for duplicates between a and b and only // add the unique items - for ( var i = 0; i < a.length; i++ ) { - var c = true; + for ( var i = 0; i < second.length; i++ ) { + var noCollision = true; // The collision-checking process - for ( var j = 0; j < b.length; j++ ) - if ( a[i] == b[j] ) - c = false; + for ( var j = 0; j < first.length; j++ ) + if ( second[i] == first[j] ) + noCollision = false; // If the item is unique, add it - if ( c ) - d.push( a[i] ); + if ( noCollision ) + result.push( second[i] ); } - return d; + return result; }, /** @@ -1295,27 +1452,27 @@ jQuery.extend({ * array item) and 'i' (which is the index of the item in the array). * * @private - * @name $.grep + * @name jQuery.grep * @type Array * @param Array array The Array to find items in. * @param Function fn The function to process each item against. * @param Boolean inv Invert the selection - select the opposite of the function. */ - grep: function(a,f,s) { + grep: function(elems, fn, inv) { // If a string is passed in for the function, make a function // for it (a handy shortcut) - if ( f.constructor == String ) - f = new Function("a","i","return " + f); + if ( fn.constructor == String ) + fn = new Function("a","i","return " + fn); - var r = []; + var result = []; // Go through the array, only saving the items // that pass the validator function - for ( var i = 0; i < a.length; i++ ) - if ( !s && f(a[i],i) || s && !f(a[i],i) ) - r.push( a[i] ); + for ( var i = 0; i < elems.length; i++ ) + if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) ) + result.push( elems[i] ); - return r; + return result; }, /** @@ -1327,29 +1484,31 @@ jQuery.extend({ * be the same size upon completion, as it was when it started. * * @private - * @name $.map + * @name jQuery.map * @type Array * @param Array array The Array to translate. * @param Function fn The function to process each item against. */ - map: function(a,f) { + map: function(elems, fn) { // If a string is passed in for the function, make a function // for it (a handy shortcut) - if ( f.constructor == String ) - f = new Function("a","return " + f); + if ( fn.constructor == String ) + fn = new Function("a","return " + fn); - var r = []; + var result = []; // Go through the array, translating each of the items to their // new value (or values). - for ( var i = 0; i < a.length; i++ ) { - var t = f(a[i],i); - if ( t !== null && t != undefined ) { - if ( t.constructor != Array ) t = [t]; - r = jQuery.merge( t, r ); + for ( var i = 0; i < elems.length; i++ ) { + var val = fn(elems[i],i); + + if ( val !== null && val != undefined ) { + if ( val.constructor != Array ) val = [val]; + result = jQuery.merge( result, val ); } } - return r; + + return result; }, /* @@ -1846,6 +2005,14 @@ jQuery.macros = { * @before
* @result
new stuff
* + * @test var div = $("div"); + * div.html("test"); + * var pass = true; + * for ( var i = 0; i < div.size(); i++ ) { + * if ( div.get(i).childNodes.length == 0 ) pass = false; + * } + * ok( pass, "Set HTML" ); + * * @name html * @type jQuery * @param String val Set the html contents to the specified value. @@ -2218,6 +2385,11 @@ jQuery.macros = { }, each: { + + removeAttr: function( key ) { + this.removeAttribute( key ); + }, + /** * Displays each of the set of matched elements if they are hidden. * @@ -2225,6 +2397,12 @@ jQuery.macros = { * @before

Hello

* @result [

Hello

] * + * @test var pass = true, div = $("div"); + * div.show().each(function(){ + * if ( this.style.display == "none" ) pass = false; + * }); + * ok( pass, "Show" ); + * * @name show * @type jQuery * @cat Effects @@ -2242,6 +2420,12 @@ jQuery.macros = { * @before

Hello

* @result [

Hello

] * + * var pass = true, div = $("div"); + * div.hide().each(function(){ + * if ( this.style.display != "none" ) pass = false; + * }); + * ok( pass, "Hide" ); + * * @name hide * @type jQuery * @cat Effects @@ -2277,6 +2461,14 @@ jQuery.macros = { * @example $("p").addClass("selected") * @before

Hello

* @result [

Hello

] + * + * @test var div = $("div"); + * div.addClass("test"); + * var pass = true; + * for ( var i = 0; i < div.size(); i++ ) { + * if ( div.get(i).className.indexOf("test") == -1 ) pass = false; + * } + * ok( pass, "Add Class" ); * * @name addClass * @type jQuery @@ -2294,6 +2486,14 @@ jQuery.macros = { * @before

Hello

* @result [

Hello

] * + * @test var div = $("div").addClass("test"); + * div.removeClass("test"); + * var pass = true; + * for ( var i = 0; i < div.size(); i++ ) { + * if ( div.get(i).className.indexOf("test") != -1 ) pass = false; + * } + * ok( pass, "Remove Class" ); + * * @name removeClass * @type jQuery * @param String class A CSS class to remove from the elements