*
* Note: tries to fix broken HTML with MWTidy
*
- * @param string $text String to truncate
+ * @param string $text HTML string to truncate
* @param int $length (zero/positive) Maximum length (excluding ellipses)
* @param string $ellipsis String to append to the truncated text
* @returns string
$bracketState = 0; // 1-tag start, 2-tag name, 0-neither
$entityState = 0; // 0-not entity, 1-entity
$tag = $ret = '';
- $openTags = array();
+ $openTags = array(); // open tag stack
$textLen = strlen( $text );
for ( $pos = 0; $pos < $textLen; ++$pos ) {
$ch = $text[$pos];
if ( $displayLen == 0 ) {
return ''; // no text shown, nothing to format
}
- $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags ); // for bad HTML
+ // Close the last tag if left unclosed by bad HTML
+ $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
while ( count( $openTags ) > 0 ) {
$ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
}
return $skipCount;
}
- // truncateHtml() helper function
- // (a) push or pop $tag from $openTags as needed
- // (b) clear $tag value
+ /*
+ * truncateHtml() helper function
+ * (a) push or pop $tag from $openTags as needed
+ * (b) clear $tag value
+ * @param String &$tag Current HTML tag name we are looking at
+ * @param int $tagType (0-open tag, 1-close tag)
+ * @param char $lastCh Character before the '>' that ended this tag
+ * @param array &$openTags Open tag stack (not accounting for $tag)
+ */
private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
$tag = ltrim( $tag );
if ( $tag != '' ) {