parser: Omit outputting newline after final line
[lhc/web/wiklou.git] / includes / parser / BlockLevelPass.php
index a69b3d2..25f4b5c 100644 (file)
@@ -61,15 +61,26 @@ class BlockLevelPass {
                $this->lineStart = $lineStart;
        }
 
+       /**
+        * @return bool
+        */
+       private function hasOpenParagraph() {
+               return $this->lastSection !== '';
+       }
+
        /**
         * If a pre or p is open, return the corresponding close tag and update
         * the state. If no tag is open, return an empty string.
+        * @param bool $atTheEnd Omit trailing newline if we've reached the end.
         * @return string
         */
-       private function closeParagraph() {
+       private function closeParagraph( $atTheEnd = false ) {
                $result = '';
-               if ( $this->lastSection !== '' ) {
-                       $result = '</' . $this->lastSection . ">\n";
+               if ( $this->hasOpenParagraph() ) {
+                       $result = '</' . $this->lastSection . '>';
+                       if ( !$atTheEnd ) {
+                               $result .= "\n";
+                       }
                }
                $this->inPre = false;
                $this->lastSection = '';
@@ -188,7 +199,8 @@ class BlockLevelPass {
                $pendingPTag = false;
                $inBlockquote = false;
 
-               foreach ( $textLines as $inputLine ) {
+               $lineCount = count( $textLines );
+               foreach ( $textLines as $i => $inputLine ) {
                        # Fix up $lineStart
                        if ( !$this->lineStart ) {
                                $output .= $inputLine;
@@ -357,7 +369,6 @@ class BlockLevelPass {
                                                if ( $pendingPTag ) {
                                                        $output .= $this->closeParagraph();
                                                        $pendingPTag = false;
-                                                       $this->lastSection = '';
                                                }
                                        } else {
                                                # paragraph
@@ -369,7 +380,6 @@ class BlockLevelPass {
                                                        } else {
                                                                if ( $this->lastSection !== 'p' ) {
                                                                        $output .= $this->closeParagraph();
-                                                                       $this->lastSection = '';
                                                                        $pendingPTag = '<p>';
                                                                } else {
                                                                        $pendingPTag = '</p><p>';
@@ -395,7 +405,11 @@ class BlockLevelPass {
                        if ( $pendingPTag === false ) {
                                if ( $prefixLength === 0 ) {
                                        $output .= $t;
-                                       $output .= "\n";
+                                       // Add a newline if there's an open paragraph
+                                       // or we've yet to reach the last line.
+                                       if ( $i < $lineCount - 1 || $this->hasOpenParagraph() ) {
+                                               $output .= "\n";
+                                       }
                                } else {
                                        // Trim whitespace in list items
                                        $output .= trim( $t );
@@ -405,15 +419,13 @@ class BlockLevelPass {
                while ( $prefixLength ) {
                        $output .= $this->closeList( $prefix2[$prefixLength - 1] );
                        --$prefixLength;
-                       if ( !$prefixLength ) {
+                       // Note that a paragraph is only ever opened when `prefixLength`
+                       // is zero, but we'll choose to be overly cautious.
+                       if ( !$prefixLength && $this->hasOpenParagraph() ) {
                                $output .= "\n";
                        }
                }
-               if ( $this->lastSection !== '' ) {
-                       $output .= '</' . $this->lastSection . '>';
-                       $this->lastSection = '';
-               }
-
+               $output .= $this->closeParagraph( true );
                return $output;
        }