BlockLevelPass: further fixes for T218817
authorArlo Breault <abreault@wikimedia.org>
Wed, 20 Mar 2019 21:02:39 +0000 (17:02 -0400)
committerArlo Breault <abreault@wikimedia.org>
Wed, 20 Mar 2019 21:31:29 +0000 (17:31 -0400)
The previous fix for T218817 (I22eebb70af1b19d7c25241fc78bfcced4470e78a)
was a bit premature: we didn't notice that ExplodeIterator *also*
used a different Iterator::key() than ArrayIterator -- it used
the string position as a key, not the line number.  Combined with
an inequality test for "not the last line" meant that almost every
line was now the "last line" and we were missing a lot of needed
newlines.

Count the lines ourselves to fix the problem.

Bug: T208070
Bug: T218817
Change-Id: I55a2c4c0ec304292162c51aa88b206fea0142392

includes/parser/BlockLevelPass.php

index 6611e20..6d6af77 100644 (file)
@@ -192,6 +192,7 @@ 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 = '';
@@ -200,7 +201,9 @@ class BlockLevelPass {
                $pendingPTag = false;
                $inBlockquote = false;
 
-               foreach ( $textLines as $i => $inputLine ) {
+               $nextLineNum = 0;
+               foreach ( $textLines as $inputLine ) {
+                       $nextLineNum += 1;
                        # Fix up $lineStart
                        if ( !$this->lineStart ) {
                                $output .= $inputLine;
@@ -407,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 ( $i < $lineCount - 1 || $this->hasOpenParagraph() ) {
+                                       if ( $nextLineNum < $lineCount || $this->hasOpenParagraph() ) {
                                                $output .= "\n";
                                        }
                                } else {