X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjquery%2Fjquery.js;h=330e05adceddeddb646740b5e286fd2086cc1381;hb=30dc79f1d0aca3ead3980f890a546bc245feb5d5;hp=883bc38549224ea7107b3f1eb50692b2f5091a28;hpb=ef1ee513d314123017fce770e8fe0a9c85b9eb87;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 883bc38..330e05a 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -22,12 +22,12 @@ window.undefined = window.undefined; */ var jQuery = function(a,c) { - // Shortcut for document ready (because $(document).each() is silly) + // 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 ) @@ -48,7 +48,7 @@ var jQuery = function(a,c) { } // Watch for when an array is passed in - this.get( a.constructor == Array || a.length && a != window && !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, [] ) : @@ -73,17 +73,14 @@ if ( typeof $ != "undefined" ) 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. @@ -93,29 +90,36 @@ var $ = jQuery; * @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. Internally, an - * element is created and it's innerHTML property set to the given markup. - * It is therefore both quite flexible and limited. + * @example $("input:radio", document.forms[0]) + * @desc Searches for all inputs of type radio within the first form in the 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, or a string of HTML to create on the fly. + * @param String expr An expression to search with + * @param Element context (optional) A DOM Element, or Document, representing the base context. * @cat Core * @type jQuery + * @see $(Element) + * @see $(Element) */ - + /** - * 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 + * This function accepts a string of raw HTML. * - * @example $("div", xml.responseXML) - * @desc This finds all div elements within the specified XML document. + * 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. + * + * @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. Internally, an + * element is created and it's innerHTML property set to the given markup. + * It is therefore both quite flexible and limited. * * @name $ - * @param String expr An expression to search with. - * @param Element context A DOM Element, or Document, representing the base context. + * @param String html A string of HTML to create on the fly. * @cat Core * @type jQuery */ @@ -158,6 +162,8 @@ var $ = jQuery; * technically, chainable - there really isn't much use for chaining against it. * You can have as many $(document).ready events on your page as you like. * + * See ready(Function) for details about the ready event. + * * @example $(function(){ * // Document is ready * }); @@ -251,37 +257,34 @@ jQuery.fn = jQuery.prototype = { * @param Number num Access the element in the Nth position. * @cat Core */ + get: function( num ) { + return num == undefined ? + + // Return a 'clean' array + jQuery.merge( this, [] ) : + // Return just the object + this[num]; + }, + /** * Set the jQuery object to an array of elements. * - * @example $("img").get([ document.body ]); - * @result $("img").get() == [ document.body ] + * @example $("img").set([ document.body ]); + * @result $("img").set() == [ document.body ] * * @private - * @name get + * @name set * @type jQuery * @param Elements elems An array of elements * @cat Core */ - get: function( num ) { - // Watch for when an array (of elements) is passed in - if ( num && num.constructor == Array ) { - - // Use a tricky hack to make the jQuery object - // look and feel like an array - this.length = 0; - [].push.apply( this, num ); - - return this; - } else - return num == undefined ? - - // Return a 'clean' array - jQuery.merge( this, [] ) : - - // Return just the object - this[num]; + set: function( array ) { + // Use a tricky hack to make the jQuery object + // look and feel like an array + this.length = 0; + [].push.apply( this, array ); + return this; }, /** @@ -294,17 +297,12 @@ jQuery.fn = jQuery.prototype = { * argument representing the position of the element in the matched * set. * - * @example $("img").each(function(){ - * this.src = "test.jpg"; - * }); - * @before - * @result - * * @example $("img").each(function(i){ - * alert( "Image #" + i + " is " + this ); + * this.src = "test" + i + ".jpg"; * }); * @before - * @result + * @result + * @desc Iterates over two images and sets their src property * * @name each * @type jQuery @@ -378,6 +376,10 @@ jQuery.fn = jQuery.prototype = { /** * Set a single property to a value, on all matched elements. * + * Note that you can't set the name property of input elements in IE. + * Use $(html) or $().append(html) or $().html(html) to create elements + * on the fly including the name property. + * * @example $("img").attr("src","test.jpg"); * @before * @result @@ -390,7 +392,7 @@ jQuery.fn = jQuery.prototype = { */ attr: function( key, value, type ) { // Check to see if we're setting style values - return key.constructor != String || value != undefined ? + return typeof key != "string" || value != undefined ? this.each(function(){ // See if we're setting a hash of styles if ( value == undefined ) @@ -773,7 +775,7 @@ jQuery.fn = jQuery.prototype = { end: function() { if( !(this.stack && this.stack.length) ) return this; - return this.get( this.stack.pop() ); + return this.set( this.stack.pop() ); }, /** @@ -957,8 +959,8 @@ jQuery.fn = jQuery.prototype = { /** * 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
@@ -985,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] ] ); - } }); }, @@ -1031,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; @@ -1040,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; @@ -1207,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 ) @@ -1310,11 +1305,6 @@ jQuery.extend({ 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) { if (prop == "cssFloat" || prop == "styleFloat") @@ -1329,34 +1319,58 @@ jQuery.extend({ ret = 'none'; else jQuery.swap(elem, { display: 'block' }, function() { - ret = document.defaultView.getComputedStyle(this,null).getPropertyValue(prop); + var c = document.defaultView.getComputedStyle(this, ''); + ret = c && c.getPropertyValue(prop) || ''; }); + } else if (elem.currentStyle) { + + var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();}); + ret = elem.currentStyle[prop] || elem.currentStyle[newProp]; + } return ret; }, - clean: function(a) { + clean: function(a) { var r = []; for ( var i = 0; i < a.length; i++ ) { var arg = a[i]; if ( typeof arg == "string" ) { // Convert html string into DOM nodes // Trim whitespace, otherwise indexOf won't work as expected - var s = jQuery.trim(arg), div = document.createElement("div"), wrap = [0,"",""]; + var s = jQuery.trim(arg), s3 = s.substring(0,3), s6 = s.substring(0,6), + div = document.createElement("div"), wrap = [0,"",""]; - if ( !s.indexOf("", ""]; - else if ( !s.indexOf("", ""]; - else if ( !s.indexOf("", ""]; // tbody auto-inserted - else if ( !s.indexOf("", ""]; + else if ( s3 == " matched above wrap = [3, "", "
"]; // Go to html and back, then peel off extra wrappers div.innerHTML = wrap[1] + s + wrap[2]; while ( wrap[0]-- ) div = div.firstChild; + + // Remove IE's autoinserted from table fragments + if ( jQuery.browser.msie ) { + var tb = null; + // String was a , *may* have spurious + if ( s6 == " or + else if ( wrap[1] == "
" && s.indexOf("= 0 ; --n ) + if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length ) + tb[n].parentNode.removeChild(tb[n]); + } + } + arg = div.childNodes; } @@ -1373,7 +1387,7 @@ jQuery.extend({ expr: { "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()", - "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]", + "#": "a.getAttribute('id')==m[2]", ":": { // Position Checks lt: "i - * jQBrowser plugin for advanced browser detection: + * There are situations where object detections is not reliable enough, in that + * cases it makes sense to use browser detection. Simply try to avoid both! + * + * A combination of browser and object detection yields quite reliable results. * * @example $.browser.msie - * @desc returns true if the current useragent is some version of microsoft's internet explorer + * @desc Returns true if the current useragent is some version of microsoft's internet explorer * * @example if($.browser.safari) { $( function() { alert("this is safari!"); } ); } * @desc Alerts "this is safari!" only for safari browsers * + * @property * @name $.browser * @type Boolean * @cat Javascript */ + +/* + * Wheather the W3C compliant box model is being used. + * + * @property + * @name $.boxModel + * @type Boolean + * @cat Javascript + */ new function() { var b = navigator.userAgent.toLowerCase(); @@ -2440,6 +2457,9 @@ jQuery.macros = { /** * Get the html contents of the first matched element. * + * A wrapper for the innerHTML property of DOM elements, therefore + * not available for XML documents. + * * @example $("div").html(); * @before
* @result @@ -2452,6 +2472,9 @@ jQuery.macros = { /** * Set the html contents of every matched element. * + * A wrapper for the innerHTML property of DOM elements, therefore + * not available for XML documents. + * * @example $("div").html("new stuff"); * @before
* @result
new stuff
@@ -2842,6 +2865,7 @@ jQuery.macros = { * @cat DOM */ removeAttr: function( key ) { + jQuery.attr( this, key, "" ); this.removeAttribute( key ); }, @@ -2949,7 +2973,7 @@ jQuery.macros = { * @cat DOM */ toggleClass: function( c ){ - jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this,c); + jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c); }, /**