X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Ftraversing.js;h=d0dd52db876ec4be0aef138e5bc3e88c1957d6e8;hb=812a247dc997fa09583c0557c6173f9ec58e7d60;hp=42444c3f2261646b66b875e8c2ce634eb678f0c8;hpb=9368b188c06dd66d43e3a488465078e1d6eb765f;p=jquery.git diff --git a/src/traversing.js b/src/traversing.js index 42444c3..d0dd52d 100644 --- a/src/traversing.js +++ b/src/traversing.js @@ -7,17 +7,17 @@ var runtil = /Until$/, // Implement the identical functionality for filter and not var winnow = function( elements, qualifier, keep ) { if ( jQuery.isFunction( qualifier ) ) { - return jQuery.grep(elements, function(elem, i) { + return jQuery.grep(elements, function( elem, i ) { return !!qualifier.call( elem, i, elem ) === keep; }); } else if ( qualifier.nodeType ) { - return jQuery.grep(elements, function(elem, i) { + return jQuery.grep(elements, function( elem, i ) { return (elem === qualifier) === keep; }); } else if ( typeof qualifier === "string" ) { - var filtered = jQuery.grep(elements, function(elem) { + var filtered = jQuery.grep(elements, function( elem ) { return elem.nodeType === 1; }); @@ -28,7 +28,7 @@ var winnow = function( elements, qualifier, keep ) { } } - return jQuery.grep(elements, function(elem, i) { + return jQuery.grep(elements, function( elem, i ) { return (jQuery.inArray( elem, qualifier ) >= 0) === keep; }); }; @@ -114,7 +114,7 @@ jQuery.fn.extend({ var pos = jQuery.expr.match.POS.test( selectors ) ? jQuery( selectors, context || this.context ) : null; - return this.map(function(i, cur){ + return this.map(function( i, cur ) { while ( cur && cur.ownerDocument && cur !== context ) { if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) { return cur; @@ -146,9 +146,9 @@ jQuery.fn.extend({ jQuery.makeArray( selector ), all = jQuery.merge( this.get(), set ); - return this.pushStack( set[0] && (set[0].setInterval || set[0].nodeType === 9 || (set[0].parentNode && set[0].parentNode.nodeType !== 11)) ? - jQuery.unique( all ) : - all ); + return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ? + all : + jQuery.unique( all ) ); }, andSelf: function() { @@ -156,20 +156,53 @@ jQuery.fn.extend({ } }); +// A painfully simple check to see if an element is disconnected +// from a document (should be improved, where feasible). +function isDisconnected( node ) { + return !node || !node.parentNode || node.parentNode.nodeType === 11; +} + jQuery.each({ - parent: function(elem){var parent = elem.parentNode; return parent && parent.nodeType !== 11 ? parent : null;}, - parents: function(elem){return jQuery.dir(elem,"parentNode");}, - parentsUntil: function(elem,i,until){return jQuery.dir(elem,"parentNode",until);}, - next: function(elem){return jQuery.nth(elem,2,"nextSibling");}, - prev: function(elem){return jQuery.nth(elem,2,"previousSibling");}, - nextAll: function(elem){return jQuery.dir(elem,"nextSibling");}, - prevAll: function(elem){return jQuery.dir(elem,"previousSibling");}, - nextUntil: function(elem,i,until){return jQuery.dir(elem,"nextSibling",until);}, - prevUntil: function(elem,i,until){return jQuery.dir(elem,"previousSibling",until);}, - siblings: function(elem){return jQuery.sibling(elem.parentNode.firstChild,elem);}, - children: function(elem){return jQuery.sibling(elem.firstChild);}, - contents: function(elem){return jQuery.nodeName(elem,"iframe")?elem.contentDocument||elem.contentWindow.document:jQuery.makeArray(elem.childNodes);} -}, function(name, fn){ + parent: function( elem ) { + var parent = elem.parentNode; + return parent && parent.nodeType !== 11 ? parent : null; + }, + parents: function( elem ) { + return jQuery.dir( elem, "parentNode" ); + }, + parentsUntil: function( elem, i, until ) { + return jQuery.dir( elem, "parentNode", until ); + }, + next: function( elem ) { + return jQuery.nth( elem, 2, "nextSibling" ); + }, + prev: function( elem ) { + return jQuery.nth( elem, 2, "previousSibling" ); + }, + nextAll: function( elem ) { + return jQuery.dir( elem, "nextSibling" ); + }, + prevAll: function( elem ) { + return jQuery.dir( elem, "previousSibling" ); + }, + nextUntil: function( elem, i, until ) { + return jQuery.dir( elem, "nextSibling", until ); + }, + prevUntil: function( elem, i, until ) { + return jQuery.dir( elem, "previousSibling", until ); + }, + siblings: function( elem ) { + return jQuery.sibling( elem.parentNode.firstChild, elem ); + }, + children: function( elem ) { + return jQuery.sibling( elem.firstChild ); + }, + contents: function( elem ) { + return jQuery.nodeName( elem, "iframe" ) ? + elem.contentDocument || elem.contentWindow.document : + jQuery.makeArray( elem.childNodes ); + } +}, function( name, fn ) { jQuery.fn[ name ] = function( until, selector ) { var ret = jQuery.map( this, fn, until );