r63358 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63357‎ | r63358 | r63359 >
Date:12:32, 7 March 2010
Author:aaron
Status:ok (Comments)
Tags:
Comment:
* truncate() comment fix
* truncateHtml() tweaks:
** Fixed miscount of remaining length wrt. entities
** Improved performance of "ellipsis makes string longer" check
Modified paths:
  • /trunk/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/Language.php
@@ -2174,7 +2174,7 @@
21752175 $string = $this->removeBadCharFirst( $string );
21762176 $string = $ellipsis . $string;
21772177 }
2178 - # Do not truncate if the ellipsis makes the string longer (bug 22181)
 2178+ # Do not truncate if the ellipsis makes the string longer/equal (bug 22181)
21792179 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
21802180 return $string;
21812181 } else {
@@ -2248,7 +2248,7 @@
22492249 }
22502250 $text = MWTidy::tidy( $text ); // fix tags
22512251 $displayLen = 0; // innerHTML legth so far
2252 - $doTruncate = true; // truncated string plus '...' shorter than original?
 2252+ $testingEllipsis = false; // checking if ellipses will make string longer/equal?
22532253 $tagType = 0; // 0-open, 1-close
22542254 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
22552255 $entityState = 0; // 0-not entity, 1-entity
@@ -2301,15 +2301,20 @@
23022302 }
23032303 }
23042304 }
2305 - if( !$doTruncate ) continue;
2306 - # Truncate if not in the middle of a bracket/entity...
 2305+ # Consider truncation once the display length has reached the maximim.
 2306+ # Double-check that we're not in the middle of a bracket/entity...
23072307 if ( $displayLen >= $length && $bracketState == 0 && $entityState == 0 ) {
2308 - $remaining = substr( $text, $pos + 1 ); // remaining string
2309 - $remaining = StringUtils::delimiterReplace( '<', '>', '', $remaining ); // rm tags
2310 - $remaining = StringUtils::delimiterReplace( '&', ';', '', $remaining ); // rm entities
2311 - $doTruncate = ( strlen($remaining) > strlen($ellipsis) );
2312 - if ( $doTruncate ) {
2313 - $ret = $this->removeBadCharLast( $ret ) . $ellipsis;
 2308+ if ( !$testingEllipsis ) {
 2309+ $testingEllipsis = true;
 2310+ # Save were we are; we will truncate here unless
 2311+ # the ellipsis actually makes the string longer.
 2312+ $pOpenTags = $openTags; // save state
 2313+ $pRet = $ret; // save state
 2314+ } elseif ( $displayLen > ($length + strlen($ellipsis)) ) {
 2315+ # Ellipsis won't make string longer/equal, the truncation point was OK.
 2316+ $openTags = $pOpenTags; // reload state
 2317+ $ret = $this->removeBadCharLast( $pRet ); // reload state, multi-byte char fix
 2318+ $ret .= $ellipsis; // add ellipsis
23142319 break;
23152320 }
23162321 }

Comments

#Comment by Nikerabbit (talk | contribs)   14:53, 7 March 2010
+# Save were we are; we will truncate here unless

Typo?

Status & tagging log