X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=ddb08a4381f980e417d8ca22552b92ff97b41d09;hb=9c94ef4c411867d38f301ccbf406af21e277188c;hp=1d96eacf61990c6ffb75fe5dac0022e651d1c778;hpb=6850c4b29017bcb31e16f0d6c94110a8b8a82304;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 1d96eac..ddb08a4 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -614,10 +614,13 @@ jQuery.fn = jQuery.prototype = { */ wrap: function() { // The elements to wrap the target around - var a = jQuery.clean(arguments); + var a, args = arguments; // Wrap each of the matched elements individually return this.each(function(){ + if ( !a ) + a = jQuery.clean(args, this.ownerDocument); + // Clone the structure that we're using to wrap var b = a[0].cloneNode(true); @@ -768,12 +771,17 @@ jQuery.fn = jQuery.prototype = { }, /** - * End the most recent 'destructive' operation, reverting the list of matched elements - * back to its previous state. After an end operation, the list of matched elements will - * revert to the last state of matched elements. + * Revert the most recent 'destructive' operation, changing the set of matched elements + * to its previous state (right before the destructive operation). * * If there was no destructive operation before, an empty set is returned. * + * A 'destructive' operation is any operation that changes the set of + * matched jQuery elements. These functions are: add, + * children, clone, filter, + * find, not, next, + * parent, parents, prev and siblings. + * * @example $("p").find("span").end(); * @before

Hello, how are you?

* @result [

...

] @@ -809,9 +817,9 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ find: function(t) { - return this.pushStack( jQuery.map( this, function(a){ + return this.pushStack( jQuery.unique( jQuery.map( this, function(a){ return jQuery.find(t,a); - }), t ); + }) ), t ); }, /** @@ -924,6 +932,9 @@ jQuery.fn = jQuery.prototype = { * of matched elements. This method is used to remove one or more * elements from a jQuery object. * + * Please note: the expression cannot use a reference to the + * element name. See the two examples below. + * * @example $("p").not( $("div p.selected") ) * @before

Hello

Hello Again

* @result [

Hello

] @@ -952,8 +963,12 @@ jQuery.fn = jQuery.prototype = { * to the set of matched elements. * * @example $("p").add("span") - * @before

Hello

Hello Again - * @result [

Hello

, Hello Again ] + * @before (HTML)

Hello

Hello Again + * @result (jQuery object matching 2 elements) [

Hello

, Hello Again ] + * @desc Compare the above result to the result of $('p'), + * which would just result in [

Hello

]
. + * Using add(), matched elements of $('span') are simply + * added to the returned jQuery-object. * * @name add * @type jQuery @@ -1026,7 +1041,7 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ is: function(expr) { - return expr ? jQuery.filter(expr,this).r.length > 0 : false; + return expr ? jQuery.multiFilter(expr,this).length > 0 : false; }, /** @@ -1109,12 +1124,15 @@ jQuery.fn = jQuery.prototype = { * @cat Core */ domManip: function(args, table, dir, fn){ - var clone = this.length > 1; - var a = jQuery.clean(args); - if ( dir < 0 ) - a.reverse(); + var clone = this.length > 1, a; return this.each(function(){ + if ( !a ) { + a = jQuery.clean(args, this.ownerDocument); + if ( dir < 0 ) + a.reverse(); + } + var obj = this; if ( table && jQuery.nodeName(this, "table") && jQuery.nodeName(a[0], "tr") ) @@ -1243,7 +1261,7 @@ jQuery.extend({ // is the only cross-browser way to do this. --John isFunction: function( fn ) { return !!fn && typeof fn != "string" && !fn.nodeName && - typeof fn[0] == "undefined" && /function/i.test( fn + "" ); + fn.constructor != Array && /function/i.test( fn + "" ); }, // check if an element is in a XML document @@ -1256,7 +1274,7 @@ jQuery.extend({ }, /** - * A generic iterator function, which can be used to seemlessly + * A generic iterator function, which can be used to seamlessly * iterate over both objects and arrays. This function is not the same * as $().each() - which is used to iterate, exclusively, over a jQuery * object. This function can be used to iterate over anything. @@ -1327,10 +1345,7 @@ jQuery.extend({ // internal only, use is(".class") has: function( t, c ) { - t = t.className || t; - // escape regex characters - c = c.replace(/([\.\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1"); - return t && new RegExp("(^|\\s)" + c + "(\\s|$)").test( t ); + return jQuery.inArray( c, (t.className || t).toString().split(/\s+/) ) > -1; } }, @@ -1390,12 +1405,14 @@ jQuery.extend({ curCSS: function(elem, prop, force) { var ret; + + if (prop == "opacity" && jQuery.browser.msie) { + ret = jQuery.attr(elem.style, "opacity"); + return ret == "" ? "1" : ret; + } - if (prop == "opacity" && jQuery.browser.msie) - return jQuery.attr(elem.style, "opacity"); - if (prop == "float" || prop == "cssFloat") - prop = jQuery.browser.msie ? "styleFloat" : "cssFloat"; + prop = jQuery.browser.msie ? "styleFloat" : "cssFloat"; if (!force && elem.style[prop]) ret = elem.style[prop]; @@ -1419,17 +1436,16 @@ jQuery.extend({ }); } 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, doc) { var r = []; + doc = doc || document; jQuery.each( a, function(i,arg){ if ( !arg ) return; @@ -1440,7 +1456,7 @@ jQuery.extend({ // Convert html string into DOM nodes if ( typeof arg == "string" ) { // Trim whitespace, otherwise indexOf won't work as expected - var s = jQuery.trim(arg), div = document.createElement("div"), tb = []; + var s = jQuery.trim(arg), div = doc.createElement("div"), tb = []; var wrap = // option or optgroup @@ -1460,7 +1476,7 @@ jQuery.extend({ [0,"",""]; // Go to html and back, then peel off extra wrappers - div.innerHTML = wrap[1] + s + wrap[2]; + div.innerHTML = wrap[1] + arg + wrap[2]; // Move to the right depth while ( wrap[0]-- ) @@ -1483,7 +1499,7 @@ jQuery.extend({ } - arg = div.childNodes; + arg = jQuery.makeArray( div.childNodes ); } if ( arg.length === 0 && !jQuery.nodeName(arg, "form") ) @@ -1515,24 +1531,21 @@ jQuery.extend({ }; // IE actually uses filters for opacity ... elem is actually elem.style - if ( name == "opacity" && jQuery.browser.msie && value != undefined ) { - // IE has trouble with opacity if it does not have layout - // Force it by setting the zoom level - elem.zoom = 1; - - // Set the alpha filter to set the opacity - return elem.filter = elem.filter.replace(/alpha\([^\)]*\)/gi,"") + - ( value == 1 ? "" : "alpha(opacity=" + value * 100 + ")" ); + if ( name == "opacity" && jQuery.browser.msie ) { + if ( value != undefined ) { + // IE has trouble with opacity if it does not have layout + // Force it by setting the zoom level + elem.zoom = 1; + + // Set the alpha filter to set the opacity + elem.filter = (elem.filter || "").replace(/alpha\([^)]*\)/,"") + + (parseFloat(value).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"); + } - } else if ( name == "opacity" && jQuery.browser.msie ) return elem.filter ? - parseFloat( elem.filter.match(/alpha\(opacity=(.*)\)/)[1] ) / 100 : 1; + (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() : ""; + } - // Mozilla doesn't play well with opacity 1 - if ( name == "opacity" && jQuery.browser.mozilla && value == 1 ) - value = 0.9999; - - // Certain attributes only work when accessed via the old DOM 0 way if ( fix[name] ) { if ( value != undefined ) elem[fix[name]] = value; @@ -1574,7 +1587,8 @@ jQuery.extend({ makeArray: function( a ) { var r = []; - if ( a.constructor != Array ) + // Need to use typeof to fight Safari childNodes crashes + if ( typeof a != "array" ) for ( var i = 0, al = a.length; i < al; i++ ) r.push( a[i] ); else @@ -1612,19 +1626,27 @@ jQuery.extend({ * @cat JavaScript */ merge: function(first, second) { - var r = [].slice.call( first, 0 ); + // We have to loop this way because IE & Opera overwrite the length + // expando of getElementsByTagName + for ( var i = 0; second[i]; i++ ) + first.push(second[i]); + return first; + }, - // Now check for duplicates between the two arrays - // and only add the unique items - for ( var i = 0, sl = second.length; i < sl; i++ ) - // Check for duplicates - if ( jQuery.inArray( second[i], r ) == -1 ) - // The item is unique, add it - first.push( second[i] ); + unique: function(first) { + var r = [], num = jQuery.mergeNum++; - return first; + for ( var i = 0, fl = first.length; i < fl; i++ ) + if ( first[i].mergeNum != num ) { + first[i].mergeNum = num; + r.push(first[i]); + } + + return r; }, + mergeNum: 0, + /** * Filter items out of an array, by using a filter function. * @@ -1718,17 +1740,7 @@ jQuery.extend({ } } - var r = result.length ? [ result[0] ] : []; - - check: for ( var i = 1, rl = result.length; i < rl; i++ ) { - for ( var j = 0; j < i; j++ ) - if ( result[i] == r[j] ) - continue check; - - r.push( result[i] ); - } - - return r; + return result; } }); @@ -1783,7 +1795,7 @@ new function() { * Get a set of elements containing the unique parents of the matched * set of elements. * - * Can be filtered with an optional expressions. + * You may use an optional expression to filter the set of parent elements that will match. * * @example $("p").parent() * @before

Hello

Hello

@@ -1805,7 +1817,7 @@ new function() { * Get a set of elements containing the unique ancestors of the matched * set of elements (except for the root element). * - * Can be filtered with an optional expressions. + * The matched elements can be filtered with an optional expression. * * @example $("span").parents() * @before

Hello

Hello Again
@@ -1827,9 +1839,10 @@ new function() { * Get a set of elements containing the unique next siblings of each of the * matched set of elements. * - * It only returns the very next sibling, not all next siblings. + * It only returns the very next sibling for each element, not all + * next siblings. * - * Can be filtered with an optional expressions. + * You may provide an optional expression to filter the match. * * @example $("p").next() * @before

Hello

Hello Again

And Again
@@ -1851,9 +1864,9 @@ new function() { * Get a set of elements containing the unique previous siblings of each of the * matched set of elements. * - * Can be filtered with an optional expressions. + * Use an optional expression to filter the matched set. * - * It only returns the immediately previous sibling, not all previous siblings. + * Only the immediately previous sibling is returned, not all previous siblings. * * @example $("p").prev() * @before

Hello

Hello Again

And Again

@@ -1897,7 +1910,8 @@ new function() { * Get a set of elements containing all of the unique children of each of the * matched set of elements. * - * Can be filtered with an optional expressions. + * This set can be filtered with an optional expression that will cause + * only elements matching the selector to be collected. * * @example $("div").children() * @before

Hello

Hello Again

And Again

@@ -2250,7 +2264,7 @@ jQuery.each( [ "eq", "lt", "gt", "contains" ], function(i,n){ */ /** - * Set the CSS width of every matched element. If no explicit unit + * Set the CSS height of every matched element. If no explicit unit * was specified (like 'em' or '%') then "px" is added to the width. * * @example $("p").height(20);