Merge "mediawiki.Title: Minor optimizations for mw.Title JavaScript code"
[lhc/web/wiklou.git] / includes / parser / BlockLevelPass.php
index 25f4b5c..50c7e23 100644 (file)
@@ -25,7 +25,7 @@
 class BlockLevelPass {
        private $DTopen = false;
        private $inPre = false;
-       private $lastSection = '';
+       private $lastParagraph = '';
        private $lineStart;
        private $text;
 
@@ -65,7 +65,7 @@ class BlockLevelPass {
         * @return bool
         */
        private function hasOpenParagraph() {
-               return $this->lastSection !== '';
+               return $this->lastParagraph !== '';
        }
 
        /**
@@ -77,13 +77,13 @@ class BlockLevelPass {
        private function closeParagraph( $atTheEnd = false ) {
                $result = '';
                if ( $this->hasOpenParagraph() ) {
-                       $result = '</' . $this->lastSection . '>';
+                       $result = '</' . $this->lastParagraph . '>';
                        if ( !$atTheEnd ) {
                                $result .= "\n";
                        }
                }
                $this->inPre = false;
-               $this->lastSection = '';
+               $this->lastParagraph = '';
                return $result;
        }
 
@@ -199,8 +199,11 @@ class BlockLevelPass {
                $pendingPTag = false;
                $inBlockquote = false;
 
-               $lineCount = count( $textLines );
-               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;
@@ -353,14 +356,14 @@ class BlockLevelPass {
                                        $inBlockElem = !$closeMatch;
                                } elseif ( !$inBlockElem && !$this->inPre ) {
                                        if ( substr( $t, 0, 1 ) == ' '
-                                               && ( $this->lastSection === 'pre' || trim( $t ) != '' )
+                                               && ( $this->lastParagraph === 'pre' || trim( $t ) != '' )
                                                && !$inBlockquote
                                        ) {
                                                # pre
-                                               if ( $this->lastSection !== 'pre' ) {
+                                               if ( $this->lastParagraph !== 'pre' ) {
                                                        $pendingPTag = false;
                                                        $output .= $this->closeParagraph() . '<pre>';
-                                                       $this->lastSection = 'pre';
+                                                       $this->lastParagraph = 'pre';
                                                }
                                                $t = substr( $t, 1 );
                                        } elseif ( preg_match( '/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS', $t ) ) {
@@ -376,24 +379,20 @@ class BlockLevelPass {
                                                        if ( $pendingPTag ) {
                                                                $output .= $pendingPTag . '<br />';
                                                                $pendingPTag = false;
-                                                               $this->lastSection = 'p';
+                                                               $this->lastParagraph = 'p';
+                                                       } elseif ( $this->lastParagraph !== 'p' ) {
+                                                               $output .= $this->closeParagraph();
+                                                               $pendingPTag = '<p>';
                                                        } else {
-                                                               if ( $this->lastSection !== 'p' ) {
-                                                                       $output .= $this->closeParagraph();
-                                                                       $pendingPTag = '<p>';
-                                                               } else {
-                                                                       $pendingPTag = '</p><p>';
-                                                               }
-                                                       }
-                                               } else {
-                                                       if ( $pendingPTag ) {
-                                                               $output .= $pendingPTag;
-                                                               $pendingPTag = false;
-                                                               $this->lastSection = 'p';
-                                                       } elseif ( $this->lastSection !== 'p' ) {
-                                                               $output .= $this->closeParagraph() . '<p>';
-                                                               $this->lastSection = 'p';
+                                                               $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 {