execute(); } /** * @param string $text * @param bool $lineStart */ private function __construct( $text, $lineStart ) { $this->text = $text; $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( $atTheEnd = false ) { $result = ''; if ( $this->hasOpenParagraph() ) { $result = 'lastParagraph . '>'; if ( !$atTheEnd ) { $result .= "\n"; } } $this->inPre = false; $this->lastParagraph = ''; 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 ) { $shorter = min( strlen( $st1 ), strlen( $st2 ) ); for ( $i = 0; $i < $shorter; ++$i ) { if ( $st1[$i] !== $st2[$i] ) { break; } } return $i; } /** * Open the list item element identified by the prefix character. * * @param string $char * * @return string */ private function openList( $char ) { $result = $this->closeParagraph(); if ( $char === '*' ) { $result .= ""; } elseif ( $char === '#' ) { $text = ""; } elseif ( $char === ':' ) { if ( $this->DTopen ) { $this->DTopen = false; $text = ""; } else { $text = ""; } } else { return ''; } return $text; } /** * Execute the pass. * @return string */ 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 = ''; $this->DTopen = $inBlockElem = false; $prefixLength = 0; $pendingPTag = false; $inBlockquote = false; for ( $textLines->rewind(); $textLines->valid(); ) { $inputLine = $textLines->current(); $textLines->next(); $notLastLine = $textLines->valid(); # Fix up $lineStart if ( !$this->lineStart ) { $output .= $inputLine; $this->lineStart = true; continue; } # * = ul # # = ol # ; = dt # : = dd $lastPrefixLength = strlen( $lastPrefix ); $preCloseMatch = preg_match( '/<\\/pre/i', $inputLine ); $preOpenMatch = preg_match( '/
 element, scan for and figure out what prefixes are there.
			if ( !$this->inPre ) {
				# Multiple prefixes may abut each other for nested lists.
				$prefixLength = strspn( $inputLine, '*#:;' );
				$prefix = substr( $inputLine, 0, $prefixLength );

				# 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 );
				$t = substr( $inputLine, $prefixLength );
				$this->inPre = (bool)$preOpenMatch;
			} else {
				# Don't interpret any other prefixes in preformatted text
				$prefixLength = 0;
				$prefix = $prefix2 = '';
				$t = $inputLine;
			}

			# 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 ) );
				$pendingPTag = false;

				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;
						// Trim whitespace in list items
						$output .= trim( $term ) . $this->nextItem( ':' );
					}
				}
			} elseif ( $prefixLength || $lastPrefixLength ) {
				# We need to open or close prefixes, or both.

				# Either open or close a level...
				$commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
				$pendingPTag = false;

				# Close all the prefixes which aren't shared.
				while ( $commonPrefixLength < $lastPrefixLength ) {
					// @phan-suppress-next-line PhanTypeInvalidDimOffset
					$output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
					--$lastPrefixLength;
				}

				# Continue the current prefix if appropriate.
				if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
					$output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
				}

				# Close an open 
if we have a
(":") starting on this line if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] === ':' ) { $output .= $this->nextItem( ':' ); } # Open prefixes where appropriate. if ( $lastPrefix && $prefixLength > $commonPrefixLength ) { $output .= "\n"; } while ( $prefixLength > $commonPrefixLength ) { $char = $prefix[$commonPrefixLength]; $output .= $this->openList( $char ); if ( $char === ';' ) { # @todo FIXME: This is dupe of code above if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) { $t = $t2; // Trim whitespace in list items $output .= trim( $term ) . $this->nextItem( ':' ); } } ++$commonPrefixLength; } if ( !$prefixLength && $lastPrefix ) { $output .= "\n"; } $lastPrefix = $prefix2; } # If we have no prefixes, go to paragraph mode. if ( $prefixLength == 0 ) { # No prefix (not in list)--go to paragraph mode # @todo consider using a stack for nestable elements like span, table and div // P-wrapping and indent-pre are suppressed inside, not outside $blockElems = 'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl'; // P-wrapping and indent-pre are suppressed outside, not inside $antiBlockElems = 'td|th'; $openMatch = preg_match( '/<(' . "({$blockElems})|\\/({$antiBlockElems})|" // Always suppresses . '\\/?(tr|caption|dt|dd|li)' . ')\\b/iS', $t ); $closeMatch = preg_match( '/<(' . "\\/({$blockElems})|({$antiBlockElems})|" // Never suppresses . '\\/?(center|blockquote|div|hr|mw:)' . ')\\b/iS', $t ); // 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. if ( $openMatch || $closeMatch ) { $pendingPTag = false; // Only close the paragraph if we're not inside a
 tag, or if
					// that 
 tag has just been opened
					if ( !$this->inPre || $preOpenMatch ) {
						// @todo T7718: paragraph closed
						$output .= $this->closeParagraph();
					}
					if ( $preOpenMatch && !$preCloseMatch ) {
						$this->inPre = true;
					}
					$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] );
					}
					$inBlockElem = !$closeMatch;
				} elseif ( !$inBlockElem && !$this->inPre ) {
					if ( substr( $t, 0, 1 ) == ' '
						&& ( $this->lastParagraph === 'pre' || trim( $t ) != '' )
						&& !$inBlockquote
					) {
						# pre
						if ( $this->lastParagraph !== 'pre' ) {
							$pendingPTag = false;
							$output .= $this->closeParagraph() . '
';
							$this->lastParagraph = 'pre';
						}
						$t = substr( $t, 1 );
					} elseif ( preg_match( '/^(?:]*>.*?<\\/style>\s*|]*>\s*)+$/iS', $t ) ) {
						# T186965: