X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fparser%2FBlockLevelPass.php;h=50c7e2376ec00a8996b4022aaaddadda57c3fb2c;hp=a69b3d2a5a3e0dbd2582eefbe46ffa399c358a30;hb=a38af7ba26579bb3004f673e44d39710887763aa;hpb=342749942bb05852b645bbda23bc60695c25ac79 diff --git a/includes/parser/BlockLevelPass.php b/includes/parser/BlockLevelPass.php index a69b3d2a5a..50c7e2376e 100644 --- a/includes/parser/BlockLevelPass.php +++ b/includes/parser/BlockLevelPass.php @@ -25,7 +25,7 @@ class BlockLevelPass { private $DTopen = false; private $inPre = false; - private $lastSection = ''; + private $lastParagraph = ''; private $lineStart; private $text; @@ -61,18 +61,29 @@ class BlockLevelPass { $this->lineStart = $lineStart; } + /** + * @return bool + */ + private function hasOpenParagraph() { + return $this->lastParagraph !== ''; + } + /** * 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 = 'lastSection . ">\n"; + if ( $this->hasOpenParagraph() ) { + $result = 'lastParagraph . '>'; + if ( !$atTheEnd ) { + $result .= "\n"; + } } $this->inPre = false; - $this->lastSection = ''; + $this->lastParagraph = ''; return $result; } @@ -188,7 +199,11 @@ class BlockLevelPass { $pendingPTag = false; $inBlockquote = false; - foreach ( $textLines as $inputLine ) { + for ( $textLines->rewind(); $textLines->valid(); ) { + $inputLine = $textLines->current(); + $textLines->next(); + $notLastLine = $textLines->valid(); + # Fix up $lineStart if ( !$this->lineStart ) { $output .= $inputLine; @@ -341,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() . '
';
-							$this->lastSection = 'pre';
+							$this->lastParagraph = 'pre';
 						}
 						$t = substr( $t, 1 );
 					} elseif ( preg_match( '/^(?:]*>.*?<\\/style>\s*|]*>\s*)+$/iS', $t ) ) {
@@ -357,7 +372,6 @@ class BlockLevelPass {
 						if ( $pendingPTag ) {
 							$output .= $this->closeParagraph();
 							$pendingPTag = false;
-							$this->lastSection = '';
 						}
 					} else {
 						# paragraph
@@ -365,25 +379,20 @@ class BlockLevelPass {
 							if ( $pendingPTag ) {
 								$output .= $pendingPTag . '
'; $pendingPTag = false; - $this->lastSection = 'p'; + $this->lastParagraph = 'p'; + } elseif ( $this->lastParagraph !== 'p' ) { + $output .= $this->closeParagraph(); + $pendingPTag = '

'; } else { - if ( $this->lastSection !== 'p' ) { - $output .= $this->closeParagraph(); - $this->lastSection = ''; - $pendingPTag = '

'; - } else { - $pendingPTag = '

'; - } - } - } else { - if ( $pendingPTag ) { - $output .= $pendingPTag; - $pendingPTag = false; - $this->lastSection = 'p'; - } elseif ( $this->lastSection !== 'p' ) { - $output .= $this->closeParagraph() . '

'; - $this->lastSection = 'p'; + $pendingPTag = '

'; } + } elseif ( $pendingPTag ) { + $output .= $pendingPTag; + $pendingPTag = false; + $this->lastParagraph = 'p'; + } elseif ( $this->lastParagraph !== 'p' ) { + $output .= $this->closeParagraph() . '

'; + $this->lastParagraph = 'p'; } } } @@ -395,7 +404,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 ( $notLastLine || $this->hasOpenParagraph() ) { + $output .= "\n"; + } } else { // Trim whitespace in list items $output .= trim( $t ); @@ -405,15 +418,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 .= 'lastSection . '>'; - $this->lastSection = ''; - } - + $output .= $this->closeParagraph( true ); return $output; }