X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=467c0748168d18f5daa439c587007e1404b83d8e;hb=c8009abcce562198cbc3930ed11f74dd62eba531;hp=a0146c3721dd886c4e668c15cae98bcc1ed162e5;hpb=a42a8e5afa2e4ca7690081bce9899d5e6e0d1cf4;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index a0146c3..467c074 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,46 +14,36 @@ 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 + * @cat Core */ function jQuery(a,c) { - // Initalize the extra macro functions - if ( !jQuery.initDone ) jQuery.init(); - // Shortcut for document ready (because $(document).each() is silly) 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; - /* - * Handle support for overriding other $() functions. Way too many libraries - * provide this function to simply ignore it and overwrite it. - */ - /* - // Check to see if this is a possible collision case - if ( jQuery._$ && !c && a.constructor == String && - - // Make sure that the expression is a colliding one - !/[^a-zA-Z0-9_-]/.test(a) && - - // and that there are no elements that match it - // (this is the one truly ambiguous case) - !document.getElementsByTagName(a).length ) - - // Use the default method, in case it works some voodoo - return jQuery._$( a ); - */ - // Watch for when a jQuery object is passed as the selector if ( a.jquery ) - return a; + return jQuery( 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 jQuery( c ).find(a); // If the context is global, return a new object if ( window == this ) @@ -71,14 +61,119 @@ function jQuery(a,c) { // 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 && fn.constructor == Function ) this.each(fn); } // 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

one

two

three

+ * @result [

two

] + * + * @example $("

Hello

").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 Element 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

one

two

three

+ * @result [

two

] + * + * @example $(document).ready( loaded ); + * @desc Executes the "loaded" function when the DOM is ready to + * be manipulated. + * + * @name $ + * @param Element 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 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; @@ -102,6 +197,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 +212,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 +231,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 +246,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. @@ -193,10 +296,26 @@ 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 + * @result + * + * @example $("img").each(function(i){ + * alert( "Image #" + i + " is " + this ); + * }); * @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 +324,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 +357,12 @@ jQuery.fn = jQuery.prototype = { * @before * @result Test Image * + * @test var pass = true; + * $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){ + * if ( this.getAttribute('foo') != "baz" && this.getAttribute('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 +376,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).getAttribute('foo') != "bar" ) pass = false; + * } + * ok( pass, "Set Attribute" ); + * * @name attr * @type jQuery * @param String key The name of the property to set. @@ -273,7 +414,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 @@ -340,8 +481,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; }, @@ -352,9 +494,10 @@ jQuery.fn = jQuery.prototype = { * stucture into a document, without ruining the original semantic * qualities of a document. * - * The way that is works is that it goes through the first element argument - * provided and finds the deepest element within the structure - it is that - * element that will en-wrap everything else. + * This works by going through the first element + * provided (which is generated, on the fly, from the provided HTML) + * and finds the deepest ancestor element within its + * structure - it is that element that will en-wrap everything else. * * @example $("p").wrap("
"); * @before

Test Paragraph.

@@ -362,10 +505,27 @@ jQuery.fn = jQuery.prototype = { * * @name wrap * @type jQuery - * @any String html A string of HTML, that will be created on the fly and wrapped around the target. - * @any Element elem A DOM element that will be wrapped. - * @any Array elems An array of elements, the first of which will be wrapped. - * @any Object obj Any object, converted to a string, then a text node. + * @param String html A string of HTML, that will be created on the fly and wrapped around the target. + * @cat DOM/Manipulation + */ + + /** + * Wrap all matched elements with a structure of other elements. + * This wrapping process is most useful for injecting additional + * stucture into a document, without ruining the original semantic + * qualities of a document. + * + * This works by going through the first element + * provided and finding the deepest ancestor element within its + * structure - it is that element that will en-wrap everything else. + * + * @example $("p").wrap("
"); + * @before

Test Paragraph.

+ * @result

Test Paragraph.

+ * + * @name wrap + * @type jQuery + * @param Element elem A DOM element that will be wrapped. * @cat DOM/Manipulation */ wrap: function() { @@ -390,7 +550,8 @@ jQuery.fn = jQuery.prototype = { }, /** - * Append any number of elements to the inside of all matched elements. + * Append any number of elements to the inside of every matched elements, + * generated from the provided HTML. * This operation is similar to doing an appendChild to all the * specified elements, adding them into the document. * @@ -400,10 +561,37 @@ jQuery.fn = jQuery.prototype = { * * @name append * @type jQuery - * @any String html A string of HTML, that will be created on the fly and appended to the target. - * @any Element elem A DOM element that will be appended. - * @any Array elems An array of elements, all of which will be appended. - * @any Object obj Any object, converted to a string, then a text node. + * @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] ); + * @before

I would like to say:

Hello + * @result

I would like to say: Hello

+ * + * @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") ); + * @before

I would like to say:

Hello + * @result

I would like to say: Hello

+ * + * @name append + * @type jQuery + * @param Array elems An array of elements, all of which will be appended. * @cat DOM/Manipulation */ append: function() { @@ -413,20 +601,48 @@ jQuery.fn = jQuery.prototype = { }, /** - * Prepend any number of elements to the inside of all matched elements. + * Prepend any number of elements to the inside of every matched elements, + * generated from the provided HTML. + * This operation is the best way to insert dynamically created elements + * inside, at the beginning, of all the matched element. + * + * @example $("p").prepend("Hello"); + * @before

I would like to say:

+ * @result

HelloI would like to say:

+ * + * @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 + */ + + /** + * Append 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] ); + * @before

I would like to say:

Hello + * @result

HelloI would like to say:

+ * + * @name prepend + * @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 the best way to insert a set of elements inside, at the * beginning, of all the matched element. * - * @example $("p").prepend("Hello"); - * @before

, how are you?

- * @result

Hello, how are you?

+ * @example $("p").prepend( $("b") ); + * @before

I would like to say:

Hello + * @result

HelloI would like to say:

* * @name prepend * @type jQuery - * @any String html A string of HTML, that will be created on the fly and prepended to the target. - * @any Element elem A DOM element that will be prepended. - * @any Array elems An array of elements, all of which will be prepended. - * @any Object obj Any object, converted to a string, then a text node. + * @param Array elems An array of elements, all of which will be appended. * @cat DOM/Manipulation */ prepend: function() { @@ -436,18 +652,42 @@ jQuery.fn = jQuery.prototype = { }, /** - * Insert any number of elements before each of the matched elements. + * Insert any number of dynamically generated elements before each of the + * matched elements. * * @example $("p").before("Hello"); - * @before

how are you?

- * @result Hello

how are you?

+ * @before

I would like to say:

+ * @result Hello

I would like to say:

+ * + * @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] ); + * @before

I would like to say:

Hello + * @result Hello

I would like to say:

+ * + * @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") ); + * @before

I would like to say:

Hello + * @result Hello

I would like to say:

* * @name before * @type jQuery - * @any String html A string of HTML, that will be created on the fly and inserted. - * @any Element elem A DOM element that will beinserted. - * @any Array elems An array of elements, all of which will be inserted. - * @any Object obj Any object, converted to a string, then a text node. + * @param Array elems An array of elements, all of which will be appended. * @cat DOM/Manipulation */ before: function() { @@ -457,18 +697,42 @@ jQuery.fn = jQuery.prototype = { }, /** + * Insert any number of dynamically generated elements after each of the + * matched elements. + * + * @example $("p").after("Hello"); + * @before

I would like to say:

+ * @result

I would like to say:

Hello + * + * @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 Hello

I would like to say:

+ * @result

I would like to say:

Hello + * + * @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("

I'm doing fine.

"); - * @before

How are you?

- * @result

How are you?

I'm doing fine.

+ * @example $("p").after( $("b") ); + * @before Hello

I would like to say:

+ * @result

I would like to say:

Hello * * @name after * @type jQuery - * @any String html A string of HTML, that will be created on the fly and inserted. - * @any Element elem A DOM element that will beinserted. - * @any Array elems An array of elements, all of which will be inserted. - * @any Object obj Any object, converted to a string, then a text node. + * @param Array elems An array of elements, all of which will be appended. * @cat DOM/Manipulation */ after: function() { @@ -516,6 +780,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 @@ -593,6 +863,7 @@ jQuery.fn = jQuery.prototype = { * @example $("p").not("#selected") * @before

Hello

Hello Again

* @result [

Hello

] + * @test cmpOK($("#main > p#ap > a").not("#google").length, "==", 2, ".not") * * @name not * @type jQuery @@ -630,7 +901,7 @@ jQuery.fn = jQuery.prototype = { * @name add * @type jQuery * @param Array els An array of Elements to add - * @cat jQuery + * @cat DOM/Traversing */ /** @@ -644,7 +915,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 ? @@ -659,7 +930,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; @@ -675,6 +946,7 @@ jQuery.fn = jQuery.prototype = { * @param Number int * @param Function fn The function doing the DOM manipulation. * @type jQuery + * @cat Core */ domManip: function(args, table, dir, fn){ var clone = this.size() > 1; @@ -683,7 +955,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 ) { @@ -708,6 +980,7 @@ jQuery.fn = jQuery.prototype = { * @param Array a * @param Array args * @type jQuery + * @cat Core */ pushStack: function(a,args) { var fn = args && args[args.length-1]; @@ -720,7 +993,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 ); } @@ -734,8 +1007,8 @@ jQuery.fn = jQuery.prototype = { * @private * @name extend * @param Object obj - * @param Object prop * @type Object + * @cat Core */ /** @@ -756,11 +1029,10 @@ jQuery.extend = jQuery.fn.extend = function(obj,prop) { jQuery.extend({ /** - * - * * @private * @name init * @type undefined + * @cat Core */ init: function(){ jQuery.initDone = true; @@ -779,7 +1051,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 ); }); }; }); @@ -806,7 +1078,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 ); @@ -846,7 +1118,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); } @@ -879,66 +1151,90 @@ jQuery.extend({ if (jQuery.css(e,"display") != "none") { oHeight = e.offsetHeight; oWidth = e.offsetWidth; - } else - jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "" }, - function(){ - oHeight = e.clientHeight; - oWidth = e.clientWidth; - }); + } else { + e = jQuery(e.cloneNode(true)).css({ + visibility: "hidden", position: "absolute", display: "block" + }).appendTo(e.parentNode)[0]; + + oHeight = e.clientHeight; + oWidth = e.clientWidth; + + e.parentNode.removeChild(e); + } }); return p == "height" ? oHeight : oWidth; } else if ( p == "opacity" && jQuery.browser.msie ) - return parseFloat( jQuery.curCSS(e,"filter").replace(/[^0-9.]/,"") ) || 1; + return parseFloat( jQuery.curCSS(e,"filter").replace(/[^0-9.]/,"") ) || 1; return jQuery.curCSS( e, p ); }, - curCSS: function(e,p,force) { - var r; + curCSS: function(elem, prop, force) { + var ret; - if (!force && e.style[p]) - r = e.style[p]; - else if (e.currentStyle) { - p = p.replace(/\-(\w)/g,function(m,c){return c.toUpperCase()}); - r = e.currentStyle[p]; + if (!force && elem.style[prop]) { + + ret = elem.style[prop]; + + } else if (elem.currentStyle) { + + var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase()}); + ret = elem.currentStyle[prop] || elem.currentStyle[newProp]; + } else if (document.defaultView && document.defaultView.getComputedStyle) { - p = p.replace(/([A-Z])/g,"-$1").toLowerCase(); - var s = document.defaultView.getComputedStyle(e,""); - r = s ? s.getPropertyValue(p) : null; + + prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase(); + var cur = document.defaultView.getComputedStyle(elem, null); + + if ( cur ) + ret = cur.getPropertyValue(prop); + else if ( prop == 'display' ) + ret = 'none'; + else + jQuery.swap(elem, { display: 'block' }, function() { + ret = document.defaultView.getComputedStyle(this,null).getPropertyValue(prop); + }); + } - return r; + return ret; }, clean: function(a) { 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; }, @@ -958,6 +1254,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", @@ -976,7 +1273,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 +1302,99 @@ 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 $.find + * @type Array + * @private + * @cat Core + */ find: function( t, context ) { // Make sure that the context is a DOM Element if ( context && context.nodeType == undefined ) @@ -1038,6 +1429,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); @@ -1074,8 +1467,9 @@ jQuery.extend({ ); } } - } + } + if ( t ) { var val = jQuery.filter(t,r); ret = r = val.r; @@ -1100,28 +1494,28 @@ jQuery.extend({ return r; }, - attr: function(o,a,v){ - if ( a && a.constructor == String ) { - var fix = { - "for": "htmlFor", - "class": "className", - "float": "cssFloat" - }; - - a = (fix[a] && fix[a].replace && fix[a] || a) - .replace(/-([a-z])/ig,function(z,b){ - return b.toUpperCase(); - }); - - if ( v != undefined ) { - o[a] = v; - if ( o.setAttribute && a != "disabled" ) - o.setAttribute(a,v); - } - - return o[a] || o.getAttribute && o.getAttribute(a) || ""; - } else - return ""; + attr: function(elem, name, value){ + var fix = { + "for": "htmlFor", + "class": "className", + "float": "cssFloat", + innerHTML: "innerHTML", + className: "className", + value: "value", + disabled: "disabled" + }; + + if ( fix[name] ) { + if ( value != undefined ) elem[fix[name]] = value; + return elem[fix[name]]; + } else if ( elem.getAttribute ) { + if ( value != undefined ) elem.setAttribute( name, value ); + return elem.getAttribute( name, 2 ); + } else { + name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();}); + if ( value != undefined ) elem[name] = value; + return elem[name]; + } }, // The regular expressions that power the parsing engine @@ -1155,7 +1549,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 ); @@ -1200,10 +1594,10 @@ jQuery.extend({ /** * Remove the whitespace from the beginning and end of a string. * - * @private * @name $.trim * @type String * @param String str The string to trim. + * @cat Javascript */ trim: function(t){ return t.replace(/^\s+|\s+$/g, ""); @@ -1216,15 +1610,16 @@ jQuery.extend({ * @name $.parents * @type Array * @param Element elem The element to find the ancestors of. + * @cat DOM/Traversing */ - 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; }, /** @@ -1234,59 +1629,62 @@ jQuery.extend({ * @name $.sibling * @type Array * @param Element elem The element to find all the siblings of (including itself). + * @cat DOM/Traversing */ - 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 * @type Array * @param Array a The first array to merge. * @param Array b The second array to merge. + * @cat Javascript */ - 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; }, /** @@ -1294,28 +1692,28 @@ jQuery.extend({ * in to this method will be passed two arguments: 'a' (which is the * array item) and 'i' (which is the index of the item in the array). * - * @private * @name $.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. + * @cat Javascript */ - 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; }, /** @@ -1326,30 +1724,32 @@ jQuery.extend({ * from the array. Both of these changes imply that the size of the array may not * be the same size upon completion, as it was when it started. * - * @private * @name $.map * @type Array * @param Array array The Array to translate. * @param Function fn The function to process each item against. + * @cat Javascript */ - 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; }, /* @@ -1485,7 +1885,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 @@ -1846,6 +2246,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. @@ -2041,7 +2449,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

Hello

Hello Again
@@ -2069,7 +2477,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

Hello

Hello Again
@@ -2214,10 +2622,15 @@ 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: { + + removeAttr: function( key ) { + this.removeAttribute( key ); + }, + /** * Displays each of the set of matched elements if they are hidden. * @@ -2225,11 +2638,17 @@ 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 */ - _show: function(){ + show: function(){ this.style.display = this.oldblock ? this.oldblock : ""; if ( jQuery.css(this,"display") == "none" ) this.style.display = "block"; @@ -2242,11 +2661,17 @@ 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 */ - _hide: function(){ + hide: function(){ this.oldblock = this.oldblock || jQuery.css(this,"display"); if ( this.oldblock == "none" ) this.oldblock = "block"; @@ -2266,9 +2691,8 @@ jQuery.macros = { * @type jQuery * @cat Effects */ - _toggle: function(){ - var d = jQuery.css(this,"display"); - $(this)[ !d || d == "none" ? "show" : "hide" ](); + toggle: function(){ + jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ].apply( jQuery(this), arguments ); }, /** @@ -2277,6 +2701,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 +2726,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 @@ -2348,7 +2788,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 ); }, @@ -2396,7 +2836,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 ); }, @@ -2462,3 +2902,5 @@ jQuery.macros = { } } }; + +jQuery.init();