mediawiki.util: Optimise logic in addPortletLink
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 1 Aug 2013 23:38:16 +0000 (01:38 +0200)
committerTimo Tijhof <krinklemail@gmail.com>
Fri, 2 Aug 2013 03:32:09 +0000 (05:32 +0200)
Optimised to no longer:
* boolean cast "nextnode" twice.
* assert nextnode.jquery when we know it is a jQuery object.
  > !!$( Node ).jquery
* assert nextnode.length when we know it has only 1 element.
  > $( Node ).length === 1
* execute $ul.find( selector ) twice for the CSS selector case.
* check for .length in some places and do an ignorant eq(0) in
  other cases (thus ignoring the case if there are multiple
  elements).

Tests still pass :-)

Change-Id: Ibc86c2dc33a8a3ba378138525c2523ba69bca8f1

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];
+
                },
 
                /**