Merge "mediawiki.Title: Minor optimizations for mw.Title JavaScript code"
[lhc/web/wiklou.git] / includes / parser / BlockLevelPass.php
index 6611e20..50c7e23 100644 (file)
@@ -192,7 +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 );
-               $lineCount = substr_count( $text, "\n" ) + 1;
 
                $lastPrefix = $output = '';
                $this->DTopen = $inBlockElem = false;
@@ -200,7 +199,11 @@ class BlockLevelPass {
                $pendingPTag = false;
                $inBlockquote = false;
 
-               foreach ( $textLines as $i => $inputLine ) {
+               for ( $textLines->rewind(); $textLines->valid(); ) {
+                       $inputLine = $textLines->current();
+                       $textLines->next();
+                       $notLastLine = $textLines->valid();
+
                        # Fix up $lineStart
                        if ( !$this->lineStart ) {
                                $output .= $inputLine;
@@ -377,23 +380,19 @@ class BlockLevelPass {
                                                                $output .= $pendingPTag . '<br />';
                                                                $pendingPTag = false;
                                                                $this->lastParagraph = 'p';
-                                                       } else {
-                                                               if ( $this->lastParagraph !== 'p' ) {
-                                                                       $output .= $this->closeParagraph();
-                                                                       $pendingPTag = '<p>';
-                                                               } else {
-                                                                       $pendingPTag = '</p><p>';
-                                                               }
-                                                       }
-                                               } else {
-                                                       if ( $pendingPTag ) {
-                                                               $output .= $pendingPTag;
-                                                               $pendingPTag = false;
-                                                               $this->lastParagraph = 'p';
                                                        } elseif ( $this->lastParagraph !== 'p' ) {
-                                                               $output .= $this->closeParagraph() . '<p>';
-                                                               $this->lastParagraph = 'p';
+                                                               $output .= $this->closeParagraph();
+                                                               $pendingPTag = '<p>';
+                                                       } else {
+                                                               $pendingPTag = '</p><p>';
                                                        }
+                                               } elseif ( $pendingPTag ) {
+                                                       $output .= $pendingPTag;
+                                                       $pendingPTag = false;
+                                                       $this->lastParagraph = 'p';
+                                               } elseif ( $this->lastParagraph !== 'p' ) {
+                                                       $output .= $this->closeParagraph() . '<p>';
+                                                       $this->lastParagraph = 'p';
                                                }
                                        }
                                }
@@ -407,7 +406,7 @@ class BlockLevelPass {
                                        $output .= $t;
                                        // Add a newline if there's an open paragraph
                                        // or we've yet to reach the last line.
-                                       if ( $i < $lineCount - 1 || $this->hasOpenParagraph() ) {
+                                       if ( $notLastLine || $this->hasOpenParagraph() ) {
                                                $output .= "\n";
                                        }
                                } else {