aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorjeresig <jeresig@gmail.com>2011-04-10 16:37:09 -0400
committerjeresig <jeresig@gmail.com>2011-04-10 16:37:09 -0400
commit523db95de2fe9a66e515d75a7cbcce204c40c93b (patch)
treebfa768c3b947f68b253f8bc052b7d58e838957e8 /src
parent52f31054fd97e20ba423d406a484885b2639178c (diff)
parent1a167767305202797cf4c839eb64bd7adfb00182 (diff)
downloadjquery-523db95de2fe9a66e515d75a7cbcce204c40c93b.tar.gz
jquery-523db95de2fe9a66e515d75a7cbcce204c40c93b.zip
Merge branch '2773_find_closest' of https://github.com/timmywil/jquery into timmywil-2773_find_closest
Conflicts: test/unit/traversing.js
Diffstat (limited to 'src')
-rw-r--r--src/core.js2
-rw-r--r--src/traversing.js40
2 files changed, 29 insertions, 13 deletions
diff --git a/src/core.js b/src/core.js
index 8f24000fa..323349e8b 100644
--- a/src/core.js
+++ b/src/core.js
@@ -88,7 +88,7 @@ jQuery.fn = jQuery.prototype = {
if ( selector === "body" && !context && document.body ) {
this.context = document;
this[0] = document.body;
- this.selector = "body";
+ this.selector = selector;
this.length = 1;
return this;
}
diff --git a/src/traversing.js b/src/traversing.js
index 6a971402e..5b6d489a0 100644
--- a/src/traversing.js
+++ b/src/traversing.js
@@ -17,17 +17,30 @@ var runtil = /Until$/,
jQuery.fn.extend({
find: function( selector ) {
+ var self = this,
+ i, l;
+
+ if ( typeof selector !== "string" ) {
+ return jQuery( selector ).filter(function() {
+ for ( i = 0, l = self.length; i < l; i++ ) {
+ if ( jQuery.contains( self[ i ], this ) ) {
+ return true;
+ }
+ }
+ });
+ }
+
var ret = this.pushStack( "", "find", selector ),
- length = 0;
+ length, n, r;
- for ( var i = 0, l = this.length; i < l; i++ ) {
+ for ( i = 0, l = this.length; i < l; i++ ) {
length = ret.length;
jQuery.find( selector, this[i], ret );
if ( i > 0 ) {
// Make sure that the results are unique
- for ( var n = length; n < ret.length; n++ ) {
- for ( var r = 0; r < length; r++ ) {
+ for ( n = length; n < ret.length; n++ ) {
+ for ( r = 0; r < length; r++ ) {
if ( ret[r] === ret[n] ) {
ret.splice(n--, 1);
break;
@@ -67,7 +80,8 @@ jQuery.fn.extend({
closest: function( selectors, context ) {
var ret = [], i, l, cur = this[0];
-
+
+ // Array
if ( jQuery.isArray( selectors ) ) {
var match, selector,
matches = {},
@@ -77,8 +91,8 @@ jQuery.fn.extend({
for ( i = 0, l = selectors.length; i < l; i++ ) {
selector = selectors[i];
- if ( !matches[selector] ) {
- matches[selector] = jQuery.expr.match.POS.test( selector ) ?
+ if ( !matches[ selector ] ) {
+ matches[ selector ] = POS.test( selector ) ?
jQuery( selector, context || this.context ) :
selector;
}
@@ -86,9 +100,9 @@ jQuery.fn.extend({
while ( cur && cur.ownerDocument && cur !== context ) {
for ( selector in matches ) {
- match = matches[selector];
+ match = matches[ selector ];
- if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
+ if ( match.jquery ? match.index( cur ) > -1 : jQuery( cur ).is( match ) ) {
ret.push({ selector: selector, elem: cur, level: level });
}
}
@@ -101,8 +115,10 @@ jQuery.fn.extend({
return ret;
}
- var pos = POS.test( selectors ) ?
- jQuery( selectors, context || this.context ) : null;
+ // String
+ var pos = POS.test( selectors ) || typeof selectors !== "string" ?
+ jQuery( selectors, context || this.context ) :
+ 0;
for ( i = 0, l = this.length; i < l; i++ ) {
cur = this[i];
@@ -121,7 +137,7 @@ jQuery.fn.extend({
}
}
- ret = ret.length > 1 ? jQuery.unique(ret) : ret;
+ ret = ret.length > 1 ? jQuery.unique( ret ) : ret;
return this.pushStack( ret, "closest", selectors );
},