From ec9296e0a89b8a3a3fb69543c7edb5f2adb3fd8f Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 10 Oct 2012 04:23:30 +0200 Subject: [PATCH] Fix up various issues leading to bug 40780. * 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 | 31 ++++++++++--------- resources/jquery/jquery.highlightText.js | 2 +- .../mediawiki/mediawiki.searchSuggest.js | 7 ++--- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/resources/jquery/jquery.autoEllipsis.js b/resources/jquery/jquery.autoEllipsis.js index 04bb301c23..49a932a1e6 100644 --- a/resources/jquery/jquery.autoEllipsis.js +++ b/resources/jquery/jquery.autoEllipsis.js @@ -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 = $( '' ) .css( 'whiteSpace', 'nowrap' ) - .text( $el.text() ); - $el + .text( $container.text() ); + $container .empty() .append( $trimmableText ); } diff --git a/resources/jquery/jquery.highlightText.js b/resources/jquery/jquery.highlightText.js index 0844da7c23..f720e07f0d 100644 --- a/resources/jquery/jquery.highlightText.js +++ b/resources/jquery/jquery.highlightText.js @@ -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 ); diff --git a/resources/mediawiki/mediawiki.searchSuggest.js b/resources/mediawiki/mediawiki.searchSuggest.js index 42c839c71e..ca719ab8b6 100644 --- a/resources/mediawiki/mediawiki.searchSuggest.js +++ b/resources/mediawiki/mediawiki.searchSuggest.js @@ -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' ); } @@ -119,9 +119,7 @@ .append( $( '
' ) .addClass( 'special-label' ) - .text( mw.msg( 'searchsuggest-containing' ) ) - ) - .append( + .text( mw.msg( 'searchsuggest-containing' ) ), $( '
' ) .addClass( 'special-query' ) .text( query ) @@ -130,7 +128,6 @@ .show(); } else { $el.find( '.special-query' ) - .empty() .text( query ) .autoEllipsis(); } -- 2.20.1