Merge "mediawiki.util: Don't hardcode selectors in updateTooltipAccessKeys if possible"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 6 Feb 2014 13:44:12 +0000 (13:44 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 6 Feb 2014 13:44:12 +0000 (13:44 +0000)
1  2 
resources/mediawiki/mediawiki.util.js

                                // Make sure we don't unset util.$content if it was preset and we don't find anything
                                return util.$content;
                        } )();
 -
 -                      // Table of contents toggle
 -                      mw.hook( 'wikipage.content' ).add( function ( $content ) {
 -                              var $tocTitle, $tocToggleLink, hideTocCookie;
 -                              $tocTitle = $content.find( '#toctitle' );
 -                              $tocToggleLink = $content.find( '#togglelink' );
 -                              // Only add it if there is a TOC and there is no toggle added already
 -                              if ( $content.find( '#toc' ).length && $tocTitle.length && !$tocToggleLink.length ) {
 -                                      hideTocCookie = $.cookie( 'mw_hidetoc' );
 -                                      $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' )
 -                                              .text( mw.msg( 'hidetoc' ) )
 -                                              .click( function ( e ) {
 -                                                      e.preventDefault();
 -                                                      util.toggleToc( $( this ) );
 -                                              } );
 -                                      $tocTitle.append(
 -                                              $tocToggleLink
 -                                                      .wrap( '<span class="toctoggle"></span>' )
 -                                                      .parent()
 -                                                              .prepend( '&nbsp;[' )
 -                                                              .append( ']&nbsp;' )
 -                                      );
 -
 -                                      if ( hideTocCookie === '1' ) {
 -                                              util.toggleToc( $tocToggleLink );
 -                                      }
 -                              }
 -                      } );
                },
  
                /* Main body */
                 *  completed (including the animation).
                 * @return {Mixed} Boolean visibility of the toc (true if it's visible)
                 * or Null if there was no table of contents.
 +               * @deprecated since 1.23 Use jQuery
                 */
                toggleToc: function ( $toggleLink, callback ) {
 -                      var $tocList = $( '#toc ul:first' );
 +                      var ret, $tocList = $( '#toc ul:first' );
  
                        // This function shouldn't be called if there's no TOC,
                        // but just in case...
 -                      if ( $tocList.length ) {
 -                              if ( $tocList.is( ':hidden' ) ) {
 -                                      $tocList.slideDown( 'fast', callback );
 -                                      $toggleLink.text( mw.msg( 'hidetoc' ) );
 -                                      $( '#toc' ).removeClass( 'tochidden' );
 -                                      $.cookie( 'mw_hidetoc', null, {
 -                                              expires: 30,
 -                                              path: '/'
 -                                      } );
 -                                      return true;
 -                              } else {
 -                                      $tocList.slideUp( 'fast', callback );
 -                                      $toggleLink.text( mw.msg( 'showtoc' ) );
 -                                      $( '#toc' ).addClass( 'tochidden' );
 -                                      $.cookie( 'mw_hidetoc', '1', {
 -                                              expires: 30,
 -                                              path: '/'
 -                                      } );
 -                                      return false;
 -                              }
 -                      } else {
 +                      if ( !$tocList.length ) {
                                return null;
                        }
 +                      ret = $tocList.is( ':hidden' );
 +                      $toggleLink.click();
 +                      $tocList.promise().done( callback );
 +                      return ret;
                },
  
                /**
  
                /**
                 * Add the appropriate prefix to the accesskey shown in the tooltip.
-                * If the nodeList parameter is given, only those nodes are updated;
-                * otherwise, all the nodes that will probably have accesskeys by
-                * default are updated.
+                *
+                * If the `$nodes` parameter is given, only those nodes are updated;
+                * otherwise, depending on browser support, we update either all elements
+                * with accesskeys on the page or a bunch of elements which are likely to
+                * have them on core skins.
                 *
                 * @param {Array|jQuery} [$nodes] A jQuery object, or array of nodes to update.
                 */
                updateTooltipAccessKeys: function ( $nodes ) {
                        if ( !$nodes ) {
-                               // Rather than going into a loop of all anchor tags, limit to few elements that
-                               // contain the relevant anchor tags.
-                               // Input and label are rare enough that no such optimization is needed
-                               $nodes = $( '#column-one a, #mw-head a, #mw-panel a, #p-logo a, input, label' );
+                               if ( document.querySelectorAll ) {
+                                       // If we're running on a browser where we can do this efficiently,
+                                       // just find all elements that have accesskeys. We can't use jQuery's
+                                       // polyfill for the selector since looping over all elements on page
+                                       // load might be too slow.
+                                       $nodes = $( document.querySelectorAll( '[accesskey]' ) );
+                               } else {
+                                       // Otherwise go through some elements likely to have accesskeys rather
+                                       // than looping over all of them. Unfortunately this will not fully
+                                       // work for custom skins with different HTML structures. Input, label
+                                       // and button should be rare enough that no optimizations are needed.
+                                       $nodes = $( '#column-one a, #mw-head a, #mw-panel a, #p-logo a, input, label, button' );
+                               }
                        } else if ( !( $nodes instanceof $ ) ) {
                                $nodes = $( $nodes );
                        }