Fix up various issues leading to bug 40780.
authorTimo Tijhof <ttijhof@wikimedia.org>
Wed, 10 Oct 2012 02:23:30 +0000 (04:23 +0200)
committerGerrit Code Review <gerrit@wikimedia.org>
Sat, 13 Oct 2012 22:10:07 +0000 (22:10 +0000)
* jquery.autoEllipsis didn't return 'this', which means whenever it
  was used in a chain, it would return undefined.

  So in mediawiki.searchSuggest append( ... autoEllipsis() ) would
  apply it to a detached node that stays detached because it
  isn't passed to append.

* Remove redundant passing of this in jQuery context to $().
  On jQuery.prototype, 'this' is obviously a jQuery object. In
  jquery.highlightText, which is called from jquery.autoEllipsis.

* Remove redundant .empty() call before calling .text( .. ),
  .text() replaces all content and naturally is forced to empty
  the element first.

* Merge use of $el and $container in jquery.autoEllipsis.js. They
  were both pointing to the same object, having two names was only
  confusing.

* Spaces.

Change-Id: I4649ec0c89d38c4d79d1dceec28227902cd48d32

resources/jquery/jquery.autoEllipsis.js
resources/jquery/jquery.highlightText.js
resources/mediawiki/mediawiki.searchSuggest.js

index 04bb301..49a932a 100644 (file)
@@ -1,14 +1,15 @@
 /**
- * Plugin that automatically truncates the plain text contents of an element and adds an ellipsis
+ * Plugin that automatically truncates the plain text contents of an element
+ * and adds an ellipsis.
  */
 ( function ( $ ) {
 
 var
        // Cache ellipsed substrings for every string-width-position combination
-       cache = { },
+       cache = {},
 
        // Use a separate cache when match highlighting is enabled
-       matchTextCache = { };
+       matchTextCache = {};
 
 $.fn.autoEllipsis = function ( options ) {
        options = $.extend( {
@@ -18,30 +19,30 @@ $.fn.autoEllipsis = function ( options ) {
                hasSpan: false,
                matchText: null
        }, options );
-       $(this).each( function () {
-               var $container, $trimmableText,
+
+       return this.each( function () {
+               var $trimmableText,
                        text, trimmableText, w, pw,
                        l, r, i, side, m,
-                       $el = $(this);
+                       // container element - used for measuring against
+                       $container = $(this);
+
                if ( options.restoreText ) {
-                       if ( !$el.data( 'autoEllipsis.originalText' ) ) {
-                               $el.data( 'autoEllipsis.originalText', $el.text() );
+                       if ( !$container.data( 'autoEllipsis.originalText' ) ) {
+                               $container.data( 'autoEllipsis.originalText', $container.text() );
                        } else {
-                               $el.text( $el.data( 'autoEllipsis.originalText' ) );
+                               $container.text( $container.data( 'autoEllipsis.originalText' ) );
                        }
                }
 
-               // container element - used for measuring against
-               $container = $el;
-
                // trimmable text element - only the text within this element will be trimmed
                if ( options.hasSpan ) {
-                       $trimmableText = $el.children( options.selector );
+                       $trimmableText = $container.children( options.selector );
                } else {
                        $trimmableText = $( '<span>' )
                                .css( 'whiteSpace', 'nowrap' )
-                               .text( $el.text() );
-                       $el
+                               .text( $container.text() );
+                       $container
                                .empty()
                                .append( $trimmableText );
                }
index 0844da7..f720e07 100644 (file)
@@ -58,7 +58,7 @@
        };
 
        $.fn.highlightText = function ( matchString ) {
-               return $( this ).each( function () {
+               return this.each( function () {
                        var $el = $( this );
                        $el.data( 'highlightText', { originalText: $el.text() } );
                        $.highlightText.splitAndHighlight( this, matchString );
index 42c839c..ca719ab 100644 (file)
@@ -85,7 +85,7 @@
                                        var jqXhr = $(this).data( 'request' );
                                        // If the delay setting has caused the fetch to have not even happened
                                        // yet, the jqXHR object will have never been set.
-                                       if ( jqXhr && $.isFunction ( jqXhr.abort ) ) {
+                                       if ( jqXhr && $.isFunction( jqXhr.abort ) ) {
                                                jqXhr.abort();
                                                $(this).removeData( 'request' );
                                        }
                                                        .append(
                                                                $( '<div>' )
                                                                        .addClass( 'special-label' )
-                                                                       .text( mw.msg( 'searchsuggest-containing' ) )
-                                                       )
-                                                       .append(
+                                                                       .text( mw.msg( 'searchsuggest-containing' ) ),
                                                                $( '<div>' )
                                                                        .addClass( 'special-query' )
                                                                        .text( query )
                                                        .show();
                                        } else {
                                                $el.find( '.special-query' )
-                                                       .empty()
                                                        .text( query )
                                                        .autoEllipsis();
                                        }