Merge "mediawiki.util: Optimise logic in addPortletLink"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 2 Aug 2013 04:35:16 +0000 (04:35 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 2 Aug 2013 04:35:16 +0000 (04:35 +0000)
resources/mediawiki/mediawiki.util.js

index 6f76839..071a52b 100644 (file)
                                $link.attr( 'accesskey', accesskey );
                        }
 
-                       // nextnode is a DOM element (was the only option before MW 1.17, in wikibits.js)
-                       // so we make it a jQuery object!
-                       if ( nextnode && nextnode.nodeType ) {
-                               nextnode = $( nextnode );
-                       }
-
-                       // Where to put our node ?
-                       // - nextnode is a jQuery object that represents exactly one element
-                       if ( nextnode && nextnode.jquery && nextnode.length === 1 && nextnode[0].parentNode === $ul[0] ) {
-                               nextnode.before( $item );
-
-                       // - nextnode is a CSS selector for jQuery
-                       } else if ( typeof nextnode === 'string' && $ul.find( nextnode ).length !== 0 ) {
-                               $ul.find( nextnode ).eq( 0 ).before( $item );
-
-                       // If the jQuery selector isn't found within the <ul>,
-                       // or if nextnode was invalid or not passed at all,
-                       // then just append it at the end of the <ul> (this is the default behavior)
-                       } else {
-                               $ul.append( $item );
+                       if ( nextnode ) {
+                               if ( nextnode.nodeType || typeof nextnode === 'string' ) {
+                                       // nextnode is a DOM element (was the only option before MW 1.17, in wikibits.js)
+                                       // or nextnode is a CSS selector for jQuery
+                                       nextnode = $ul.find( nextnode );
+                               } else if ( !nextnode.jquery || nextnode[0].parentNode !== $ul[0] ) {
+                                       // Fallback
+                                       $ul.append( $item );
+                                       return $item[0];
+                               }
+                               if ( nextnode.length === 1 ) {
+                                       // nextnode is a jQuery object that represents exactly one element
+                                       nextnode.before( $item );
+                                       return $item[0];
+                               }
                        }
 
+                       // Fallback (this is the default behavior)
+                       $ul.append( $item );
                        return $item[0];
+
                },
 
                /**