Avoid counting input lines twice in BlockLevelPass::execute()
[lhc/web/wiklou.git] / includes / parser / BlockLevelPass.php
index 6d6af77..f925038 100644 (file)
@@ -192,8 +192,6 @@ class BlockLevelPass {
                # happening here is handling of block-level elements p, pre,
                # and making lists from lines starting with * # : etc.
                $textLines = StringUtils::explode( "\n", $text );
-               # Count this way because $textLines could be an ExplodeIterator
-               $lineCount = substr_count( $text, "\n" ) + 1;
 
                $lastPrefix = $output = '';
                $this->DTopen = $inBlockElem = false;
@@ -201,9 +199,11 @@ class BlockLevelPass {
                $pendingPTag = false;
                $inBlockquote = false;
 
-               $nextLineNum = 0;
-               foreach ( $textLines as $inputLine ) {
-                       $nextLineNum += 1;
+               for ( $textLines->rewind(); $textLines->valid(); ) {
+                       $inputLine = $textLines->current();
+                       $textLines->next();
+                       $notLastLine = $textLines->valid();
+
                        # Fix up $lineStart
                        if ( !$this->lineStart ) {
                                $output .= $inputLine;
@@ -410,7 +410,7 @@ class BlockLevelPass {
                                        $output .= $t;
                                        // Add a newline if there's an open paragraph
                                        // or we've yet to reach the last line.
-                                       if ( $nextLineNum < $lineCount || $this->hasOpenParagraph() ) {
+                                       if ( $notLastLine || $this->hasOpenParagraph() ) {
                                                $output .= "\n";
                                        }
                                } else {