aboutsummaryrefslogtreecommitdiffstats
path: root/src/traversing.js
diff options
context:
space:
mode:
authorgnarf <gnarf@gnarf.net>2011-06-14 23:38:36 -0500
committergnarf <gnarf@gnarf.net>2011-06-14 23:38:36 -0500
commit9a96af1d59cc6a10b89b6f2489b04871a9c1c7fa (patch)
tree975ac7651be8809426a343a8c1b480ca1e9c08ac /src/traversing.js
parentb43910a496ccb2b44dcc11b64667e21521aa4994 (diff)
downloadjquery-9a96af1d59cc6a10b89b6f2489b04871a9c1c7fa.tar.gz
jquery-9a96af1d59cc6a10b89b6f2489b04871a9c1c7fa.zip
Quick improvement to the performace of .index() with no arguments - Adding a unit test for .index() of a node without a parent returns -1
Diffstat (limited to 'src/traversing.js')
-rw-r--r--src/traversing.js15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/traversing.js b/src/traversing.js
index 8c4b4ef83..35a5174e9 100644
--- a/src/traversing.js
+++ b/src/traversing.js
@@ -145,12 +145,17 @@ jQuery.fn.extend({
// Determine the position of an element within
// the matched set of elements
index: function( elem ) {
- if ( !elem || typeof elem === "string" ) {
- return jQuery.inArray( this[0],
- // If it receives a string, the selector is used
- // If it receives nothing, the siblings are used
- elem ? jQuery( elem ) : this.parent().children() );
+
+ // No argument, return index in parent
+ if ( !elem ) {
+ return ( this[0] && this[0].parentNode ) ? this.prevAll().length : -1;
+ }
+
+ // index in selector
+ if ( typeof elem === "string" ) {
+ return jQuery.inArray( this[0], jQuery( elem ) );
}
+
// Locate the position of the desired element
return jQuery.inArray(
// If it receives a jQuery object, the first element is used