Skip to content

Commit 5f5d2e5

Browse files
committed
Merge branch '2.0-traversing' of https://github.com/orkel/jquery
* '2.0-traversing' of https://github.com/orkel/jquery: Code style Reduce traversing module
2 parents df7431b + 3894157 commit 5f5d2e5

File tree

2 files changed

+80
-71
lines changed

2 files changed

+80
-71
lines changed

src/traversing.js

+73-69
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,38 @@ var runtil = /Until$/,
1212

1313
jQuery.fn.extend({
1414
find: function( selector ) {
15-
var i, ret, self;
15+
var self, matched, i,
16+
l = this.length;
1617

1718
if ( typeof selector !== "string" ) {
1819
self = this;
1920
return this.pushStack( jQuery( selector ).filter(function() {
20-
for ( i = 0; i < self.length; i++ ) {
21+
for ( i = 0; i < l; i++ ) {
2122
if ( jQuery.contains( self[ i ], this ) ) {
2223
return true;
2324
}
2425
}
2526
}) );
2627
}
2728

28-
ret = [];
29-
for ( i = 0; i < this.length; i++ ) {
30-
jQuery.find( selector, this[ i ], ret );
29+
matched = [];
30+
for ( i = 0; i < l; i++ ) {
31+
jQuery.find( selector, this[ i ], matched );
3132
}
3233

3334
// Needed because $( selector, context ) becomes $( context ).find( selector )
34-
ret = this.pushStack( jQuery.unique( ret ) );
35-
ret.selector = ( this.selector ? this.selector + " " : "" ) + selector;
36-
return ret;
35+
matched = this.pushStack( jQuery.unique( matched ) );
36+
matched.selector = ( this.selector ? this.selector + " " : "" ) + selector;
37+
return matched;
3738
},
3839

3940
has: function( target ) {
40-
var i,
41-
targets = jQuery( target, this ),
42-
len = targets.length;
41+
var targets = jQuery( target, this ),
42+
l = targets.length;
4343

4444
return this.filter(function() {
45-
for ( i = 0; i < len; i++ ) {
45+
var i = 0;
46+
for ( ; i < l; i++ ) {
4647
if ( jQuery.contains( this, targets[i] ) ) {
4748
return true;
4849
}
@@ -64,7 +65,7 @@ jQuery.fn.extend({
6465
// If this is a positional/relative selector, check membership in the returned set
6566
// so $("p:first").is("p:last") won't return true for a doc with two "p".
6667
rneedsContext.test( selector ) ?
67-
jQuery( selector, this.context ).index( this[0] ) >= 0 :
68+
jQuery( selector, this.context ).index( this[ 0 ] ) >= 0 :
6869
jQuery.filter( selector, this ).length > 0 :
6970
this.filter( selector ).length > 0 );
7071
},
@@ -73,24 +74,24 @@ jQuery.fn.extend({
7374
var cur,
7475
i = 0,
7576
l = this.length,
76-
ret = [],
77-
pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ?
77+
matched = [],
78+
pos = ( rneedsContext.test( selectors ) || typeof selectors !== "string" ) ?
7879
jQuery( selectors, context || this.context ) :
7980
0;
8081

8182
for ( ; i < l; i++ ) {
82-
cur = this[i];
83+
cur = this[ i ];
8384

84-
while ( cur && cur.ownerDocument && cur !== context && cur.nodeType !== 11 ) {
85-
if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
86-
ret.push( cur );
85+
while ( cur && cur.ownerDocument && cur !== context ) {
86+
if ( pos ? pos.index( cur ) > -1 : jQuery.find.matchesSelector( cur, selectors ) ) {
87+
matched.push( cur );
8788
break;
8889
}
89-
cur = cur.parentNode;
90+
cur = cur.parentElement;
9091
}
9192
}
9293

93-
return this.pushStack( ret.length > 1 ? jQuery.unique( ret ) : ret );
94+
return this.pushStack( matched.length > 1 ? jQuery.unique( matched ) : matched );
9495
},
9596

9697
// Determine the position of an element within
@@ -99,18 +100,20 @@ jQuery.fn.extend({
99100

100101
// No argument, return index in parent
101102
if ( !elem ) {
102-
return ( this[0] && this[0].parentNode ) ? this.first().prevAll().length : -1;
103+
return ( this[ 0 ] && this[ 0 ].parentNode ) ? this.first().prevAll().length : -1;
103104
}
104105

105106
// index in selector
106107
if ( typeof elem === "string" ) {
107-
return jQuery.inArray( this[0], jQuery( elem ) );
108+
return core_indexOf.call( jQuery( elem ), this[ 0 ] );
108109
}
109110

110111
// Locate the position of the desired element
111-
return jQuery.inArray(
112+
return core_indexOf.call( this,
113+
112114
// If it receives a jQuery object, the first element is used
113-
elem.jquery ? elem[0] : elem, this );
115+
elem.jquery ? elem[ 0 ] : elem
116+
);
114117
},
115118

116119
add: function( selector, context ) {
@@ -131,48 +134,42 @@ jQuery.fn.extend({
131134

132135
jQuery.fn.andSelf = jQuery.fn.addBack;
133136

134-
function sibling( cur, dir ) {
135-
do {
136-
cur = cur[ dir ];
137-
} while ( cur && cur.nodeType !== 1 );
138-
139-
return cur;
140-
}
141-
142137
jQuery.each({
143138
parent: function( elem ) {
144-
var parent = elem.parentNode;
145-
return parent && parent.nodeType !== 11 ? parent : null;
139+
return elem.parentElement;
146140
},
147141
parents: function( elem ) {
148-
return jQuery.dir( elem, "parentNode" );
142+
return jQuery.dir( elem, "parentElement" );
149143
},
150144
parentsUntil: function( elem, i, until ) {
151-
return jQuery.dir( elem, "parentNode", until );
145+
return jQuery.dir( elem, "parentElement", until );
152146
},
153147
next: function( elem ) {
154-
return sibling( elem, "nextSibling" );
148+
return elem.nextElementSibling;
155149
},
156150
prev: function( elem ) {
157-
return sibling( elem, "previousSibling" );
151+
return elem.previousElementSibling;
158152
},
159153
nextAll: function( elem ) {
160-
return jQuery.dir( elem, "nextSibling" );
154+
return jQuery.dir( elem, "nextElementSibling" );
161155
},
162156
prevAll: function( elem ) {
163-
return jQuery.dir( elem, "previousSibling" );
157+
return jQuery.dir( elem, "previousElementSibling" );
164158
},
165159
nextUntil: function( elem, i, until ) {
166-
return jQuery.dir( elem, "nextSibling", until );
160+
return jQuery.dir( elem, "nextElementSibling", until );
167161
},
168162
prevUntil: function( elem, i, until ) {
169-
return jQuery.dir( elem, "previousSibling", until );
163+
return jQuery.dir( elem, "previousElementSibling", until );
170164
},
171165
siblings: function( elem ) {
172166
return jQuery.sibling( ( elem.parentNode || {} ).firstChild, elem );
173167
},
174168
children: function( elem ) {
175-
return jQuery.sibling( elem.firstChild );
169+
var children = elem.children;
170+
171+
// documentFragment or document does not have children property
172+
return children ? jQuery.merge( [], children ) : jQuery.sibling( elem.firstChild );
176173
},
177174
contents: function( elem ) {
178175
return jQuery.nodeName( elem, "iframe" ) ?
@@ -181,23 +178,27 @@ jQuery.each({
181178
}
182179
}, function( name, fn ) {
183180
jQuery.fn[ name ] = function( until, selector ) {
184-
var ret = jQuery.map( this, fn, until );
181+
var matched = jQuery.map( this, fn, until );
185182

186183
if ( !runtil.test( name ) ) {
187184
selector = until;
188185
}
189186

190187
if ( selector && typeof selector === "string" ) {
191-
ret = jQuery.filter( selector, ret );
188+
matched = jQuery.filter( selector, matched );
192189
}
193190

194-
ret = this.length > 1 && !guaranteedUnique[ name ] ? jQuery.unique( ret ) : ret;
191+
if ( this.length > 1 ) {
192+
if ( !guaranteedUnique[ name ] ) {
193+
jQuery.unique( matched );
194+
}
195195

196-
if ( this.length > 1 && rparentsprev.test( name ) ) {
197-
ret = ret.reverse();
196+
if ( rparentsprev.test( name ) ) {
197+
matched.reverse();
198+
}
198199
}
199200

200-
return this.pushStack( ret );
201+
return this.pushStack( matched );
201202
};
202203
});
203204

@@ -208,33 +209,32 @@ jQuery.extend({
208209
}
209210

210211
return elems.length === 1 ?
211-
jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] :
212-
jQuery.find.matches(expr, elems);
212+
jQuery.find.matchesSelector( elems[ 0 ], expr ) ? [ elems[ 0 ] ] : [] :
213+
jQuery.find.matches( expr, elems );
213214
},
214215

215216
dir: function( elem, dir, until ) {
216-
var matched = [],
217-
cur = elem[ dir ];
217+
var cur = elem[ dir ],
218+
matched = [];
218219

219-
while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
220-
if ( cur.nodeType === 1 ) {
221-
matched.push( cur );
222-
}
223-
cur = cur[dir];
220+
while ( cur && ( !until || !jQuery( cur ).is( until ) ) ) {
221+
matched.push( cur );
222+
cur = cur[ dir ];
224223
}
224+
225225
return matched;
226226
},
227227

228228
sibling: function( n, elem ) {
229-
var r = [];
229+
var matched = [];
230230

231231
for ( ; n; n = n.nextSibling ) {
232232
if ( n.nodeType === 1 && n !== elem ) {
233-
r.push( n );
233+
matched.push( n );
234234
}
235235
}
236236

237-
return r;
237+
return matched;
238238
}
239239
});
240240

@@ -245,30 +245,34 @@ function winnow( elements, qualifier, keep ) {
245245
// Set to 0 to skip string check
246246
qualifier = qualifier || 0;
247247

248+
var filtered;
249+
248250
if ( jQuery.isFunction( qualifier ) ) {
249251
return jQuery.grep(elements, function( elem, i ) {
250252
var retVal = !!qualifier.call( elem, i, elem );
251253
return retVal === keep;
252254
});
255+
}
253256

254-
} else if ( qualifier.nodeType ) {
257+
if ( qualifier.nodeType ) {
255258
return jQuery.grep(elements, function( elem ) {
256259
return ( elem === qualifier ) === keep;
257260
});
261+
}
258262

259-
} else if ( typeof qualifier === "string" ) {
260-
var filtered = jQuery.grep(elements, function( elem ) {
263+
if ( typeof qualifier === "string" ) {
264+
filtered = jQuery.grep(elements, function( elem ) {
261265
return elem.nodeType === 1;
262266
});
263267

264268
if ( isSimple.test( qualifier ) ) {
265-
return jQuery.filter(qualifier, filtered, !keep);
266-
} else {
267-
qualifier = jQuery.filter( qualifier, filtered );
269+
return jQuery.filter( qualifier, filtered, !keep );
268270
}
271+
272+
qualifier = jQuery.filter( qualifier, filtered );
269273
}
270274

271275
return jQuery.grep(elements, function( elem ) {
272-
return ( jQuery.inArray( elem, qualifier ) >= 0 ) === keep;
276+
return ( core_indexOf.call( qualifier, elem ) >= 0 ) === keep;
273277
});
274278
}

test/unit/traversing.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,15 @@ test("eq('-1') #10616", function() {
658658
});
659659

660660
test("index(no arg) #10977", function() {
661-
expect(1);
662-
661+
expect(2);
662+
663663
var $list = jQuery("<ul id='indextest'><li>THIS ONE</li><li class='one'>a</li><li class='two'>b</li><li class='three'>c</li></ul>");
664664
jQuery("#qunit-fixture").append( $list );
665665
strictEqual ( jQuery( "#indextest li:not(.one,.two)" ).index() , 0, "No Argument Index Check" );
666666
$list.remove();
667+
668+
var fragment = document.createDocumentFragment(),
669+
div = fragment.appendChild( document.createElement("div") );
670+
671+
equal( jQuery( div ).index(), 0, "If jQuery#index called on element whos parent is fragment, it still should work correctly" );
667672
});

0 commit comments

Comments
 (0)