(bug 28978) jquery.autoEllipsis cache doesn't take position into account, leads to...
authorRoan Kattouw <catrope@users.mediawiki.org>
Sat, 14 May 2011 17:56:01 +0000 (17:56 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Sat, 14 May 2011 17:56:01 +0000 (17:56 +0000)
resources/jquery/jquery.autoEllipsis.js

index 0ec1ad3..7bdc7f5 100644 (file)
@@ -3,7 +3,7 @@
  */
 ( function( $ ) {
 
-// Cache ellipsed substrings for every string-width combination
+// Cache ellipsed substrings for every string-width-position combination
 var cache = { };
 // Use a seperate cache when match highlighting is enabled
 var matchTextCache = { };
@@ -49,29 +49,40 @@ $.fn.autoEllipsis = function( options ) {
                var w = $container.width();
                var pw = $protectedText ? $protectedText.width() : 0;
                // Try cache
-               if ( !( text in cache ) ) {
-                       cache[text] = {};
-               }
-               if ( options.matchText && !( text in matchTextCache ) ) {
-                       matchTextCache[text] = {};
-               }
-               if ( options.matchText && !( options.matchText in matchTextCache[text] ) ) {
-                       matchTextCache[text][options.matchText] = {};
-               }
-               if ( !options.matchText && w in cache[text] ) {
-                       $container.html( cache[text][w] );
-                       if ( options.tooltip ) {
-                               $container.attr( 'title', text );
+               if ( options.matchText ) {
+                       if ( !( text in matchTextCache ) ) {
+                               matchTextCache[text] = {};
                        }
-                       return;
-               }
-               if( options.matchText && options.matchText in matchTextCache[text] && w in matchTextCache[text][options.matchText] ) {
-                       $container.html( matchTextCache[text][options.matchText][w] );
-                       if ( options.tooltip ) {
-                               $container.attr( 'title', text );
+                       if ( !( options.matchText in matchTextCache[text] ) ) {
+                               matchTextCache[text][options.matchText] = {};
+                       }
+                       if ( !( w in matchTextCache[text][options.matchText] ) ) {
+                               matchTextCache[text][options.matchText][w] = {};
+                       }
+                       if ( options.position in matchTextCache[text][options.matchText][w] ) {
+                               $container.html( matchTextCache[text][options.matchText][w][options.position] );
+                               if ( options.tooltip ) {
+                                       $container.attr( 'title', text );
+                               }
+                               return;
+                       }
+               } else {
+                       if ( !( text in cache ) ) {
+                               cache[text] = {};
+                       }
+                       if ( !( w in cache[text] ) ) {
+                               cache[text][w] = {};
+                       }
+                       if ( options.position in cache[text][w] ) {
+                               $container.html( cache[text][w][options.position] );
+                               if ( options.tooltip ) {
+                                       $container.attr( 'title', text );
+                               }
+                               console.log("YAY CACHE HIT");
+                               return;
                        }
-                       return;
                }
+               
                if ( $trimmableText.width() + pw > w ) {
                        switch ( options.position ) {
                                case 'right':
@@ -122,9 +133,9 @@ $.fn.autoEllipsis = function( options ) {
                }
                if ( options.matchText ) {
                        $container.highlightText( options.matchText );
-                       matchTextCache[text][options.matchText][w] = $container.html();
+                       matchTextCache[text][options.matchText][w][options.position] = $container.html();
                } else {
-                       cache[text][w] = $container.html();
+                       cache[text][w][options.position] = $container.html();
                }
                
        } );