2016-05-06 02:56:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is the part of the wikitext parser which handles automatic paragraphs
|
|
|
|
|
* and conversion of start-of-line prefixes to HTML lists.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Parser
|
2020-01-25 15:45:36 +00:00
|
|
|
* @internal
|
2016-05-06 02:56:37 +00:00
|
|
|
*/
|
2024-10-03 18:39:06 +00:00
|
|
|
|
|
|
|
|
namespace MediaWiki\Parser;
|
|
|
|
|
|
|
|
|
|
use LogicException;
|
|
|
|
|
use StringUtils;
|
|
|
|
|
|
2016-05-06 02:56:37 +00:00
|
|
|
class BlockLevelPass {
|
2024-09-07 20:46:08 +00:00
|
|
|
/** @var bool */
|
2016-05-06 04:30:40 +00:00
|
|
|
private $DTopen = false;
|
2024-09-07 20:46:08 +00:00
|
|
|
/** @var bool */
|
2016-05-06 04:30:40 +00:00
|
|
|
private $inPre = false;
|
2024-09-07 20:46:08 +00:00
|
|
|
/** @var string */
|
2019-03-15 19:41:22 +00:00
|
|
|
private $lastParagraph = '';
|
2024-09-07 20:46:08 +00:00
|
|
|
/** @var bool */
|
2018-01-20 00:36:52 +00:00
|
|
|
private $lineStart;
|
2024-09-07 20:46:08 +00:00
|
|
|
/** @var string */
|
2016-05-06 02:56:37 +00:00
|
|
|
private $text;
|
|
|
|
|
|
|
|
|
|
# State constants for the definition list colon extraction
|
2020-05-15 22:40:17 +00:00
|
|
|
private const COLON_STATE_TEXT = 0;
|
|
|
|
|
private const COLON_STATE_TAG = 1;
|
|
|
|
|
private const COLON_STATE_TAGSTART = 2;
|
|
|
|
|
private const COLON_STATE_CLOSETAG = 3;
|
|
|
|
|
private const COLON_STATE_TAGSLASH = 4;
|
|
|
|
|
private const COLON_STATE_COMMENT = 5;
|
|
|
|
|
private const COLON_STATE_COMMENTDASH = 6;
|
|
|
|
|
private const COLON_STATE_COMMENTDASHDASH = 7;
|
|
|
|
|
private const COLON_STATE_LC = 8;
|
2016-05-06 02:56:37 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make lists from lines starting with ':', '*', '#', etc.
|
|
|
|
|
*
|
|
|
|
|
* @param string $text
|
2016-05-06 04:30:40 +00:00
|
|
|
* @param bool $lineStart Whether or not this is at the start of a line.
|
2016-05-06 02:56:37 +00:00
|
|
|
* @return string The lists rendered as HTML
|
2020-01-25 15:45:36 +00:00
|
|
|
* @internal
|
2016-05-06 02:56:37 +00:00
|
|
|
*/
|
2016-05-06 04:30:40 +00:00
|
|
|
public static function doBlockLevels( $text, $lineStart ) {
|
|
|
|
|
$pass = new self( $text, $lineStart );
|
2016-05-06 02:56:37 +00:00
|
|
|
return $pass->execute();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 04:30:40 +00:00
|
|
|
/**
|
2018-12-03 14:58:57 +00:00
|
|
|
* @param string $text
|
|
|
|
|
* @param bool $lineStart
|
2016-05-06 04:30:40 +00:00
|
|
|
*/
|
|
|
|
|
private function __construct( $text, $lineStart ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$this->text = $text;
|
2016-05-06 04:30:40 +00:00
|
|
|
$this->lineStart = $lineStart;
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 23:26:51 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function hasOpenParagraph() {
|
2019-03-15 19:41:22 +00:00
|
|
|
return $this->lastParagraph !== '';
|
2018-10-23 23:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-06 02:56:37 +00:00
|
|
|
/**
|
2016-05-06 04:30:40 +00:00
|
|
|
* 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.
|
2018-10-23 23:26:51 +00:00
|
|
|
* @param bool $atTheEnd Omit trailing newline if we've reached the end.
|
2016-05-06 02:56:37 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2018-10-23 23:26:51 +00:00
|
|
|
private function closeParagraph( $atTheEnd = false ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$result = '';
|
2018-10-23 23:26:51 +00:00
|
|
|
if ( $this->hasOpenParagraph() ) {
|
2019-03-15 19:41:22 +00:00
|
|
|
$result = '</' . $this->lastParagraph . '>';
|
2018-10-23 23:26:51 +00:00
|
|
|
if ( !$atTheEnd ) {
|
|
|
|
|
$result .= "\n";
|
|
|
|
|
}
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
2016-05-06 04:30:40 +00:00
|
|
|
$this->inPre = false;
|
2019-03-15 19:41:22 +00:00
|
|
|
$this->lastParagraph = '';
|
2016-05-06 02:56:37 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* getCommon() returns the length of the longest common substring
|
|
|
|
|
* of both arguments, starting at the beginning of both.
|
|
|
|
|
*
|
|
|
|
|
* @param string $st1
|
|
|
|
|
* @param string $st2
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
private function getCommon( $st1, $st2 ) {
|
2016-05-06 04:30:40 +00:00
|
|
|
$shorter = min( strlen( $st1 ), strlen( $st2 ) );
|
2016-05-06 02:56:37 +00:00
|
|
|
|
|
|
|
|
for ( $i = 0; $i < $shorter; ++$i ) {
|
2016-05-06 04:30:40 +00:00
|
|
|
if ( $st1[$i] !== $st2[$i] ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-06 04:30:40 +00:00
|
|
|
* Open the list item element identified by the prefix character.
|
2016-05-06 02:56:37 +00:00
|
|
|
*
|
|
|
|
|
* @param string $char
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function openList( $char ) {
|
|
|
|
|
$result = $this->closeParagraph();
|
|
|
|
|
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $char === '*' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$result .= "<ul><li>";
|
2018-06-30 09:43:00 +00:00
|
|
|
} elseif ( $char === '#' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$result .= "<ol><li>";
|
2018-06-30 09:43:00 +00:00
|
|
|
} elseif ( $char === ':' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$result .= "<dl><dd>";
|
2018-06-30 09:43:00 +00:00
|
|
|
} elseif ( $char === ';' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$result .= "<dl><dt>";
|
2016-05-06 04:30:40 +00:00
|
|
|
$this->DTopen = true;
|
2016-05-06 02:56:37 +00:00
|
|
|
} else {
|
|
|
|
|
$result = '<!-- ERR 1 -->';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-06 04:30:40 +00:00
|
|
|
* Close the current list item and open the next one.
|
2016-05-06 02:56:37 +00:00
|
|
|
* @param string $char
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function nextItem( $char ) {
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $char === '*' || $char === '#' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
return "</li>\n<li>";
|
2018-06-30 09:43:00 +00:00
|
|
|
} elseif ( $char === ':' || $char === ';' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$close = "</dd>\n";
|
2016-05-06 04:30:40 +00:00
|
|
|
if ( $this->DTopen ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$close = "</dt>\n";
|
|
|
|
|
}
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $char === ';' ) {
|
2016-05-06 04:30:40 +00:00
|
|
|
$this->DTopen = true;
|
2016-05-06 02:56:37 +00:00
|
|
|
return $close . '<dt>';
|
|
|
|
|
} else {
|
2016-05-06 04:30:40 +00:00
|
|
|
$this->DTopen = false;
|
2016-05-06 02:56:37 +00:00
|
|
|
return $close . '<dd>';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return '<!-- ERR 2 -->';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-06 04:30:40 +00:00
|
|
|
* Close the current list item identified by the prefix character.
|
2016-05-06 02:56:37 +00:00
|
|
|
* @param string $char
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function closeList( $char ) {
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $char === '*' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$text = "</li></ul>";
|
2018-06-30 09:43:00 +00:00
|
|
|
} elseif ( $char === '#' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$text = "</li></ol>";
|
2018-06-30 09:43:00 +00:00
|
|
|
} elseif ( $char === ':' ) {
|
2016-05-06 04:30:40 +00:00
|
|
|
if ( $this->DTopen ) {
|
|
|
|
|
$this->DTopen = false;
|
2016-05-06 02:56:37 +00:00
|
|
|
$text = "</dt></dl>";
|
|
|
|
|
} else {
|
|
|
|
|
$text = "</dd></dl>";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return '<!-- ERR 3 -->';
|
|
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 04:30:40 +00:00
|
|
|
/**
|
|
|
|
|
* Execute the pass.
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-05-06 02:56:37 +00:00
|
|
|
private function execute() {
|
|
|
|
|
$text = $this->text;
|
|
|
|
|
# Parsing through the text line by line. The main thing
|
|
|
|
|
# happening here is handling of block-level elements p, pre,
|
|
|
|
|
# and making lists from lines starting with * # : etc.
|
|
|
|
|
$textLines = StringUtils::explode( "\n", $text );
|
|
|
|
|
|
|
|
|
|
$lastPrefix = $output = '';
|
2016-05-06 04:30:40 +00:00
|
|
|
$this->DTopen = $inBlockElem = false;
|
2016-05-06 02:56:37 +00:00
|
|
|
$prefixLength = 0;
|
2016-05-06 04:30:40 +00:00
|
|
|
$pendingPTag = false;
|
2016-05-06 02:56:37 +00:00
|
|
|
$inBlockquote = false;
|
|
|
|
|
|
2019-03-20 20:37:49 +00:00
|
|
|
for ( $textLines->rewind(); $textLines->valid(); ) {
|
|
|
|
|
$inputLine = $textLines->current();
|
|
|
|
|
$textLines->next();
|
|
|
|
|
$notLastLine = $textLines->valid();
|
|
|
|
|
|
2016-05-06 04:30:40 +00:00
|
|
|
# Fix up $lineStart
|
|
|
|
|
if ( !$this->lineStart ) {
|
|
|
|
|
$output .= $inputLine;
|
|
|
|
|
$this->lineStart = true;
|
2016-05-06 02:56:37 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
# * = ul
|
|
|
|
|
# # = ol
|
|
|
|
|
# ; = dt
|
|
|
|
|
# : = dd
|
|
|
|
|
|
|
|
|
|
$lastPrefixLength = strlen( $lastPrefix );
|
2016-05-06 04:30:40 +00:00
|
|
|
$preCloseMatch = preg_match( '/<\\/pre/i', $inputLine );
|
|
|
|
|
$preOpenMatch = preg_match( '/<pre/i', $inputLine );
|
2016-05-06 02:56:37 +00:00
|
|
|
# If not in a <pre> element, scan for and figure out what prefixes are there.
|
2016-05-06 04:30:40 +00:00
|
|
|
if ( !$this->inPre ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
# Multiple prefixes may abut each other for nested lists.
|
2016-05-06 04:30:40 +00:00
|
|
|
$prefixLength = strspn( $inputLine, '*#:;' );
|
|
|
|
|
$prefix = substr( $inputLine, 0, $prefixLength );
|
2016-05-06 02:56:37 +00:00
|
|
|
|
|
|
|
|
# eh?
|
|
|
|
|
# ; and : are both from definition-lists, so they're equivalent
|
|
|
|
|
# for the purposes of determining whether or not we need to open/close
|
|
|
|
|
# elements.
|
|
|
|
|
$prefix2 = str_replace( ';', ':', $prefix );
|
2016-05-06 04:30:40 +00:00
|
|
|
$t = substr( $inputLine, $prefixLength );
|
|
|
|
|
$this->inPre = (bool)$preOpenMatch;
|
2016-05-06 02:56:37 +00:00
|
|
|
} else {
|
|
|
|
|
# Don't interpret any other prefixes in preformatted text
|
|
|
|
|
$prefixLength = 0;
|
|
|
|
|
$prefix = $prefix2 = '';
|
2016-05-06 04:30:40 +00:00
|
|
|
$t = $inputLine;
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# List generation
|
|
|
|
|
if ( $prefixLength && $lastPrefix === $prefix2 ) {
|
|
|
|
|
# Same as the last item, so no need to deal with nesting or opening stuff
|
|
|
|
|
$output .= $this->nextItem( substr( $prefix, -1 ) );
|
2016-05-06 04:30:40 +00:00
|
|
|
$pendingPTag = false;
|
2016-05-06 02:56:37 +00:00
|
|
|
|
|
|
|
|
if ( substr( $prefix, -1 ) === ';' ) {
|
|
|
|
|
# The one nasty exception: definition lists work like this:
|
|
|
|
|
# ; title : definition text
|
|
|
|
|
# So we check for : in the remainder text to split up the
|
|
|
|
|
# title and definition, without b0rking links.
|
|
|
|
|
$term = $t2 = '';
|
|
|
|
|
if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
|
|
|
|
|
$t = $t2;
|
2018-03-01 23:02:54 +00:00
|
|
|
// Trim whitespace in list items
|
|
|
|
|
$output .= trim( $term ) . $this->nextItem( ':' );
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $prefixLength || $lastPrefixLength ) {
|
|
|
|
|
# We need to open or close prefixes, or both.
|
|
|
|
|
|
|
|
|
|
# Either open or close a level...
|
|
|
|
|
$commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
|
2016-05-06 04:30:40 +00:00
|
|
|
$pendingPTag = false;
|
2016-05-06 02:56:37 +00:00
|
|
|
|
|
|
|
|
# Close all the prefixes which aren't shared.
|
|
|
|
|
while ( $commonPrefixLength < $lastPrefixLength ) {
|
2019-12-07 18:32:45 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeInvalidDimOffset
|
2016-05-06 02:56:37 +00:00
|
|
|
$output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
|
|
|
|
|
--$lastPrefixLength;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Continue the current prefix if appropriate.
|
|
|
|
|
if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
|
|
|
|
|
$output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 22:47:54 +00:00
|
|
|
# Close an open <dt> if we have a <dd> (":") starting on this line
|
|
|
|
|
if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] === ':' ) {
|
|
|
|
|
$output .= $this->nextItem( ':' );
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 02:56:37 +00:00
|
|
|
# Open prefixes where appropriate.
|
|
|
|
|
if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
|
|
|
|
|
$output .= "\n";
|
|
|
|
|
}
|
|
|
|
|
while ( $prefixLength > $commonPrefixLength ) {
|
2017-09-06 22:47:54 +00:00
|
|
|
$char = $prefix[$commonPrefixLength];
|
2016-05-06 02:56:37 +00:00
|
|
|
$output .= $this->openList( $char );
|
|
|
|
|
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $char === ';' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
# @todo FIXME: This is dupe of code above
|
|
|
|
|
if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
|
|
|
|
|
$t = $t2;
|
2018-03-01 23:02:54 +00:00
|
|
|
// Trim whitespace in list items
|
|
|
|
|
$output .= trim( $term ) . $this->nextItem( ':' );
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
++$commonPrefixLength;
|
|
|
|
|
}
|
|
|
|
|
if ( !$prefixLength && $lastPrefix ) {
|
|
|
|
|
$output .= "\n";
|
|
|
|
|
}
|
|
|
|
|
$lastPrefix = $prefix2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# If we have no prefixes, go to paragraph mode.
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $prefixLength == 0 ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
# No prefix (not in list)--go to paragraph mode
|
2016-05-06 04:30:40 +00:00
|
|
|
# @todo consider using a stack for nestable elements like span, table and div
|
2018-04-20 00:00:57 +00:00
|
|
|
|
|
|
|
|
// P-wrapping and indent-pre are suppressed inside, not outside
|
2018-07-12 18:41:20 +00:00
|
|
|
$blockElems = 'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl';
|
2018-04-20 00:00:57 +00:00
|
|
|
// P-wrapping and indent-pre are suppressed outside, not inside
|
|
|
|
|
$antiBlockElems = 'td|th';
|
|
|
|
|
|
2016-05-06 04:30:40 +00:00
|
|
|
$openMatch = preg_match(
|
2018-04-20 00:00:57 +00:00
|
|
|
'/<('
|
|
|
|
|
. "({$blockElems})|\\/({$antiBlockElems})|"
|
|
|
|
|
// Always suppresses
|
Add caption to always suppressing
In brief, the BlockLevelPass looks at opening and closing tags on a line
to determine whether it should do paragraph wrapping. The blockElems
want to stop wrapping when opened and start again when closed. The
antiBlockElems want the opposite, to start when they're opened and stop
when closed. "table" is a blockElems and "td"|"th" are anitBlockElems
so that content found in the interstitial spaces of tables are never
paragraph wrapped.
That means that, to date, "caption" elements are always found in a place
where paragraph wrapping is always suppressed and so adding them to that
set won't change any test results. However, a new test is added to spec
out this behaviour.
In the legacy parser, "captions" are always found in the right place
because handleTables runs at an earlier stage. In Parsoid, however, the
treebuilder is relied on to close table cells [0] so when we get to the
token stream paragraph wrappping pass, "caption"s are found in table
cells and therefore get wrapped, even though the treebuilder is about to
be induced to close the cell before opening the caption.
Therefore, in Parsoid, the fix would require us to make captions always-
suppressing to match the legacy parser behaviour. Thus, this change
here is just to keep these lists [1] consistent between the two
parsers.
[0] https://github.com/wikimedia/parsoid/blob/5e11a3f3908932e097c42d3e205c78721a84dedf/src/Wt2Html/TokenizerUtils.php#L138-L151
[1] https://github.com/wikimedia/parsoid/blob/5e11a3f3908932e097c42d3e205c78721a84dedf/src/Wt2Html/TT/ParagraphWrapper.php#L71-L78
Bug: T210647
Change-Id: I8ccefd69d47dca740f50924b235dffa3873d1f99
2020-05-19 17:48:13 +00:00
|
|
|
. '\\/?(tr|caption|dt|dd|li)'
|
2018-04-20 00:00:57 +00:00
|
|
|
. ')\\b/iS',
|
2016-05-06 02:56:37 +00:00
|
|
|
$t
|
|
|
|
|
);
|
2016-05-06 04:30:40 +00:00
|
|
|
$closeMatch = preg_match(
|
2018-04-20 00:00:57 +00:00
|
|
|
'/<('
|
|
|
|
|
. "\\/({$blockElems})|({$antiBlockElems})|"
|
|
|
|
|
// Never suppresses
|
2021-12-21 03:26:38 +00:00
|
|
|
. '\\/?(center|blockquote|div|hr|mw:|aside|figure)|'
|
|
|
|
|
// Used as Parser::TOC_PLACEHOLDER
|
|
|
|
|
. 'meta property="mw:'
|
2018-04-20 00:00:57 +00:00
|
|
|
. ')\\b/iS',
|
2016-05-06 02:56:37 +00:00
|
|
|
$t
|
|
|
|
|
);
|
|
|
|
|
|
2018-04-20 00:00:57 +00:00
|
|
|
// Any match closes the paragraph, but only when `!$closeMatch`
|
|
|
|
|
// do we enter block mode. The oddities with table rows and
|
|
|
|
|
// cells are to avoid paragraph wrapping in interstitial spaces
|
|
|
|
|
// leading to fostered content.
|
|
|
|
|
|
2016-05-06 04:30:40 +00:00
|
|
|
if ( $openMatch || $closeMatch ) {
|
|
|
|
|
$pendingPTag = false;
|
Fix parsing of <pre> tags generated by extension tag hooks
When this part of BlockLevelPass::execute() encounters a block-level tag,
such as <hr>, one of $openMatch or $closeMatch will be truthy.
Without this patch, $this->closeParagraph() is unconditionally called in
this situation, which sets $this->inPre = false. If we're already inside a
<pre> tag, this makes the parser think we're no longer in a <pre>
environment, so it starts wrapping the <pre> tag's content in <p> tags as
if it was processing regular content.
We should only call $this->closeParagraph() in the case that (a) we are not
inside a <pre> tag, or (b) the block-level tag that is being opened is
itself a <pre> tag (in which case $preOpenMatch will be truthy, and
$this->inPre will have already been set to true).
This doesn't affect the parsing of <pre> tags that are written in wikitext,
since their content isn't parsed. It only affects hooks and the like that
return <pre> tags.
This doesn't solve the task T7718 that is mentioned in the code comment,
but if the testwiki test cases linked there are anything to go by, it
doesn't make the problem worse in any way.
This is required for Poem change I754f2e84f7d6efc0829765c82297f2de5f9ca149.
Change-Id: I469e633fc41d8ca73653c7e982c591092dcb1708
2017-01-08 09:15:48 +00:00
|
|
|
// Only close the paragraph if we're not inside a <pre> tag, or if
|
|
|
|
|
// that <pre> tag has just been opened
|
|
|
|
|
if ( !$this->inPre || $preOpenMatch ) {
|
|
|
|
|
// @todo T7718: paragraph closed
|
|
|
|
|
$output .= $this->closeParagraph();
|
|
|
|
|
}
|
2016-05-06 02:56:37 +00:00
|
|
|
if ( $preOpenMatch && !$preCloseMatch ) {
|
2016-05-06 04:30:40 +00:00
|
|
|
$this->inPre = true;
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
$bqOffset = 0;
|
|
|
|
|
while ( preg_match( '/<(\\/?)blockquote[\s>]/i', $t,
|
|
|
|
|
$bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
|
|
|
|
|
) {
|
|
|
|
|
$inBlockquote = !$bqMatch[1][0]; // is this a close tag?
|
|
|
|
|
$bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
|
|
|
|
|
}
|
2016-05-06 04:30:40 +00:00
|
|
|
$inBlockElem = !$closeMatch;
|
|
|
|
|
} elseif ( !$inBlockElem && !$this->inPre ) {
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( substr( $t, 0, 1 ) == ' '
|
2019-03-15 19:41:22 +00:00
|
|
|
&& ( $this->lastParagraph === 'pre' || trim( $t ) != '' )
|
2016-05-06 02:56:37 +00:00
|
|
|
&& !$inBlockquote
|
|
|
|
|
) {
|
|
|
|
|
# pre
|
2019-03-15 19:41:22 +00:00
|
|
|
if ( $this->lastParagraph !== 'pre' ) {
|
2016-05-06 04:30:40 +00:00
|
|
|
$pendingPTag = false;
|
2016-05-06 02:56:37 +00:00
|
|
|
$output .= $this->closeParagraph() . '<pre>';
|
2019-03-15 19:41:22 +00:00
|
|
|
$this->lastParagraph = 'pre';
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
$t = substr( $t, 1 );
|
2018-02-26 21:49:08 +00:00
|
|
|
} elseif ( preg_match( '/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS', $t ) ) {
|
|
|
|
|
# T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
|
|
|
|
|
# But it should clear $pendingPTag.
|
|
|
|
|
if ( $pendingPTag ) {
|
|
|
|
|
$output .= $this->closeParagraph();
|
|
|
|
|
$pendingPTag = false;
|
|
|
|
|
}
|
2016-05-06 02:56:37 +00:00
|
|
|
} else {
|
|
|
|
|
# paragraph
|
|
|
|
|
if ( trim( $t ) === '' ) {
|
2016-05-06 04:30:40 +00:00
|
|
|
if ( $pendingPTag ) {
|
|
|
|
|
$output .= $pendingPTag . '<br />';
|
|
|
|
|
$pendingPTag = false;
|
2019-03-15 19:41:22 +00:00
|
|
|
$this->lastParagraph = 'p';
|
|
|
|
|
} elseif ( $this->lastParagraph !== 'p' ) {
|
2019-03-29 20:12:24 +00:00
|
|
|
$output .= $this->closeParagraph();
|
|
|
|
|
$pendingPTag = '<p>';
|
|
|
|
|
} else {
|
|
|
|
|
$pendingPTag = '</p><p>';
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
2019-03-29 20:12:24 +00:00
|
|
|
} elseif ( $pendingPTag ) {
|
|
|
|
|
$output .= $pendingPTag;
|
|
|
|
|
$pendingPTag = false;
|
|
|
|
|
$this->lastParagraph = 'p';
|
|
|
|
|
} elseif ( $this->lastParagraph !== 'p' ) {
|
|
|
|
|
$output .= $this->closeParagraph() . '<p>';
|
|
|
|
|
$this->lastParagraph = 'p';
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-20 22:44:19 +00:00
|
|
|
# somewhere above we forget to get out of pre block (T2785)
|
2016-05-06 04:30:40 +00:00
|
|
|
if ( $preCloseMatch && $this->inPre ) {
|
|
|
|
|
$this->inPre = false;
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
2016-05-06 04:30:40 +00:00
|
|
|
if ( $pendingPTag === false ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
if ( $prefixLength === 0 ) {
|
2018-03-01 23:02:54 +00:00
|
|
|
$output .= $t;
|
2018-10-23 23:26:51 +00:00
|
|
|
// Add a newline if there's an open paragraph
|
|
|
|
|
// or we've yet to reach the last line.
|
2019-03-20 20:37:49 +00:00
|
|
|
if ( $notLastLine || $this->hasOpenParagraph() ) {
|
2018-10-23 23:26:51 +00:00
|
|
|
$output .= "\n";
|
|
|
|
|
}
|
2018-03-01 23:02:54 +00:00
|
|
|
} else {
|
|
|
|
|
// Trim whitespace in list items
|
|
|
|
|
$output .= trim( $t );
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while ( $prefixLength ) {
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeArraySuspicious $prefix set if $prefixLength is set
|
2016-05-06 02:56:37 +00:00
|
|
|
$output .= $this->closeList( $prefix2[$prefixLength - 1] );
|
|
|
|
|
--$prefixLength;
|
2018-10-23 23:26:51 +00:00
|
|
|
// Note that a paragraph is only ever opened when `prefixLength`
|
2018-10-23 22:47:48 +00:00
|
|
|
// is zero, but we'll choose to be overly cautious.
|
2018-10-23 23:26:51 +00:00
|
|
|
if ( !$prefixLength && $this->hasOpenParagraph() ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$output .= "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-23 23:26:51 +00:00
|
|
|
$output .= $this->closeParagraph( true );
|
2016-05-06 02:56:37 +00:00
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Split up a string on ':', ignoring any occurrences inside tags
|
|
|
|
|
* to prevent illegal overlapping.
|
|
|
|
|
*
|
|
|
|
|
* @param string $str The string to split
|
|
|
|
|
* @param string &$before Set to everything before the ':'
|
|
|
|
|
* @param string &$after Set to everything after the ':'
|
2022-03-08 18:41:30 +00:00
|
|
|
* @return int|false The position of the ':', or false if none found
|
2016-05-06 02:56:37 +00:00
|
|
|
*/
|
|
|
|
|
private function findColonNoLinks( $str, &$before, &$after ) {
|
2016-12-13 20:37:04 +00:00
|
|
|
if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
# Nothing to find!
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-13 20:37:04 +00:00
|
|
|
if ( $m[0][0] === ':' ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
# Easy; no tag nesting to worry about
|
2016-12-13 20:37:04 +00:00
|
|
|
$colonPos = $m[0][1];
|
2016-05-06 04:30:40 +00:00
|
|
|
$before = substr( $str, 0, $colonPos );
|
|
|
|
|
$after = substr( $str, $colonPos + 1 );
|
|
|
|
|
return $colonPos;
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Ugly state machine to walk through avoiding tags.
|
|
|
|
|
$state = self::COLON_STATE_TEXT;
|
2016-12-13 20:37:04 +00:00
|
|
|
$ltLevel = 0;
|
|
|
|
|
$lcLevel = 0;
|
2016-05-06 02:56:37 +00:00
|
|
|
$len = strlen( $str );
|
2016-12-13 20:37:04 +00:00
|
|
|
for ( $i = $m[0][1]; $i < $len; $i++ ) {
|
2016-05-06 02:56:37 +00:00
|
|
|
$c = $str[$i];
|
|
|
|
|
|
|
|
|
|
switch ( $state ) {
|
2017-12-11 03:07:50 +00:00
|
|
|
case self::COLON_STATE_TEXT:
|
|
|
|
|
switch ( $c ) {
|
|
|
|
|
case "<":
|
|
|
|
|
# Could be either a <start> tag or an </end> tag
|
|
|
|
|
$state = self::COLON_STATE_TAGSTART;
|
|
|
|
|
break;
|
|
|
|
|
case ":":
|
|
|
|
|
if ( $ltLevel === 0 ) {
|
|
|
|
|
# We found it!
|
|
|
|
|
$before = substr( $str, 0, $i );
|
|
|
|
|
$after = substr( $str, $i + 1 );
|
|
|
|
|
return $i;
|
|
|
|
|
}
|
|
|
|
|
# Embedded in a tag; don't break it.
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
# Skip ahead looking for something interesting
|
|
|
|
|
if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
|
|
|
|
|
# Nothing else interesting
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ( $m[0][0] === '-{' ) {
|
|
|
|
|
$state = self::COLON_STATE_LC;
|
|
|
|
|
$lcLevel++;
|
|
|
|
|
$i = $m[0][1] + 1;
|
|
|
|
|
} else {
|
|
|
|
|
# Skip ahead to next interesting character.
|
|
|
|
|
$i = $m[0][1] - 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
break;
|
2017-12-11 03:07:50 +00:00
|
|
|
case self::COLON_STATE_LC:
|
|
|
|
|
# In language converter markup -{ ... }-
|
|
|
|
|
if ( !preg_match( '/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
|
|
|
|
|
# Nothing else interesting to find; abort!
|
|
|
|
|
# We're nested in language converter markup, but there
|
|
|
|
|
# are no close tags left. Abort!
|
|
|
|
|
break 2;
|
2021-08-17 20:51:43 +00:00
|
|
|
}
|
|
|
|
|
if ( $m[0][0] === '-{' ) {
|
2017-12-11 03:07:50 +00:00
|
|
|
$i = $m[0][1] + 1;
|
2016-12-13 20:37:04 +00:00
|
|
|
$lcLevel++;
|
2017-12-11 03:07:50 +00:00
|
|
|
} elseif ( $m[0][0] === '}-' ) {
|
2016-12-13 20:37:04 +00:00
|
|
|
$i = $m[0][1] + 1;
|
2017-12-11 03:07:50 +00:00
|
|
|
$lcLevel--;
|
|
|
|
|
if ( $lcLevel === 0 ) {
|
|
|
|
|
$state = self::COLON_STATE_TEXT;
|
|
|
|
|
}
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
2016-12-13 20:37:04 +00:00
|
|
|
break;
|
2017-12-11 03:07:50 +00:00
|
|
|
case self::COLON_STATE_TAG:
|
|
|
|
|
# In a <tag>
|
|
|
|
|
switch ( $c ) {
|
|
|
|
|
case ">":
|
|
|
|
|
$ltLevel++;
|
|
|
|
|
$state = self::COLON_STATE_TEXT;
|
|
|
|
|
break;
|
|
|
|
|
case "/":
|
|
|
|
|
# Slash may be followed by >?
|
|
|
|
|
$state = self::COLON_STATE_TAGSLASH;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
# ignore
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
break;
|
2017-12-11 03:07:50 +00:00
|
|
|
case self::COLON_STATE_TAGSTART:
|
|
|
|
|
switch ( $c ) {
|
|
|
|
|
case "/":
|
|
|
|
|
$state = self::COLON_STATE_CLOSETAG;
|
|
|
|
|
break;
|
|
|
|
|
case "!":
|
|
|
|
|
$state = self::COLON_STATE_COMMENT;
|
|
|
|
|
break;
|
|
|
|
|
case ">":
|
|
|
|
|
# Illegal early close? This shouldn't happen D:
|
|
|
|
|
$state = self::COLON_STATE_TEXT;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$state = self::COLON_STATE_TAG;
|
|
|
|
|
}
|
2016-05-06 02:56:37 +00:00
|
|
|
break;
|
2017-12-11 03:07:50 +00:00
|
|
|
case self::COLON_STATE_CLOSETAG:
|
|
|
|
|
# In a </tag>
|
|
|
|
|
if ( $c === ">" ) {
|
|
|
|
|
if ( $ltLevel > 0 ) {
|
|
|
|
|
$ltLevel--;
|
|
|
|
|
} else {
|
|
|
|
|
# ignore the excess close tag, but keep looking for
|
|
|
|
|
# colons. (This matches Parsoid behavior.)
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": Invalid input; too many close tags" );
|
2017-12-11 03:07:50 +00:00
|
|
|
}
|
|
|
|
|
$state = self::COLON_STATE_TEXT;
|
|
|
|
|
}
|
2016-05-06 02:56:37 +00:00
|
|
|
break;
|
2017-12-11 03:07:50 +00:00
|
|
|
case self::COLON_STATE_TAGSLASH:
|
|
|
|
|
if ( $c === ">" ) {
|
|
|
|
|
# Yes, a self-closed tag <blah/>
|
|
|
|
|
$state = self::COLON_STATE_TEXT;
|
|
|
|
|
} else {
|
|
|
|
|
# Probably we're jumping the gun, and this is an attribute
|
|
|
|
|
$state = self::COLON_STATE_TAG;
|
|
|
|
|
}
|
2016-05-06 02:56:37 +00:00
|
|
|
break;
|
2017-12-11 03:07:50 +00:00
|
|
|
case self::COLON_STATE_COMMENT:
|
|
|
|
|
if ( $c === "-" ) {
|
|
|
|
|
$state = self::COLON_STATE_COMMENTDASH;
|
|
|
|
|
}
|
2016-05-06 02:56:37 +00:00
|
|
|
break;
|
2017-12-11 03:07:50 +00:00
|
|
|
case self::COLON_STATE_COMMENTDASH:
|
|
|
|
|
if ( $c === "-" ) {
|
|
|
|
|
$state = self::COLON_STATE_COMMENTDASHDASH;
|
2016-12-22 17:30:36 +00:00
|
|
|
} else {
|
2017-12-11 03:07:50 +00:00
|
|
|
$state = self::COLON_STATE_COMMENT;
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
2017-12-11 03:07:50 +00:00
|
|
|
break;
|
|
|
|
|
case self::COLON_STATE_COMMENTDASHDASH:
|
|
|
|
|
if ( $c === ">" ) {
|
|
|
|
|
$state = self::COLON_STATE_TEXT;
|
|
|
|
|
} else {
|
|
|
|
|
$state = self::COLON_STATE_COMMENT;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-06-09 16:50:09 +00:00
|
|
|
throw new LogicException( "State machine error in " . __METHOD__ );
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-12-13 20:37:04 +00:00
|
|
|
if ( $ltLevel > 0 || $lcLevel > 0 ) {
|
|
|
|
|
wfDebug(
|
|
|
|
|
__METHOD__ . ": Invalid input; not enough close tags " .
|
2020-06-01 05:00:39 +00:00
|
|
|
"(level $ltLevel/$lcLevel, state $state)"
|
2016-12-13 20:37:04 +00:00
|
|
|
);
|
2016-05-06 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-03 18:39:06 +00:00
|
|
|
|
|
|
|
|
/** @deprecated class alias since 1.43 */
|
|
|
|
|
class_alias( BlockLevelPass::class, 'BlockLevelPass' );
|