191 # Parsing through the text line by line. The main thing
192 # happening here is handling of block-level elements p, pre,
193 # and making lists from lines starting with * # : etc.
194 $textLines = StringUtils::explode(
"\n",
$text );
197 $this->DTopen = $inBlockElem =
false;
199 $pendingPTag =
false;
200 $inBlockquote =
false;
202 for ( $textLines->rewind(); $textLines->valid(); ) {
203 $inputLine = $textLines->current();
205 $notLastLine = $textLines->valid();
208 if ( !$this->lineStart ) {
210 $this->lineStart =
true;
218 $lastPrefixLength =
strlen( $lastPrefix );
219 $preCloseMatch =
preg_match(
'/<\\/pre/i', $inputLine );
220 $preOpenMatch =
preg_match(
'/<pre/i', $inputLine );
221 # If not in a <pre> element, scan for and figure out what prefixes are there.
222 if ( !$this->inPre ) {
223 # Multiple prefixes may abut each other for nested lists.
224 $prefixLength =
strspn( $inputLine,
'*#:;' );
225 $prefix =
substr( $inputLine, 0, $prefixLength );
228 # ; and : are both from definition-lists, so they're equivalent
229 # for the purposes of determining whether or not we need to open/close
232 $t =
substr( $inputLine, $prefixLength );
233 $this->inPre = (
bool)$preOpenMatch;
235 # Don't interpret any other prefixes in preformatted text
237 $prefix = $prefix2 =
'';
242 if ( $prefixLength && $lastPrefix === $prefix2 ) {
243 # Same as the last item, so no need to deal with nesting or opening stuff
245 $pendingPTag =
false;
247 if (
substr( $prefix, -1 ) ===
';' ) {
248 # The one nasty exception: definition lists work like this:
249 # ; title : definition text
250 # So we check for : in the remainder text to split up the
251 # title and definition, without b0rking links.
259 }
elseif ( $prefixLength || $lastPrefixLength ) {
260 # We need to open or close prefixes, or both.
262 # Either open or close a level...
263 $commonPrefixLength = $this->
getCommon( $prefix, $lastPrefix );
264 $pendingPTag =
false;
266 # Close all the prefixes which aren't shared.
267 while ( $commonPrefixLength < $lastPrefixLength ) {
272 # Continue the current prefix if appropriate.
273 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
277 # Close an open <dt> if we have a <dd> (":") starting on this line
278 if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] ===
':' ) {
282 # Open prefixes where appropriate.
283 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
286 while ( $prefixLength > $commonPrefixLength ) {
290 if ( $char ===
';' ) {
291 # @todo FIXME: This is dupe of code above
300 if ( !$prefixLength && $lastPrefix ) {
306 # If we have no prefixes, go to paragraph mode.
307 if ( $prefixLength == 0 ) {
308 # No prefix (not in list)--go to paragraph mode
309 # @todo consider using a stack for nestable elements like span, table and div
312 $blockElems =
'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl';
314 $antiBlockElems =
'td|th';
318 .
"({$blockElems})|\\/({$antiBlockElems})|"
320 .
'\\/?(tr|dt|dd|li)'
326 .
"\\/({$blockElems})|({$antiBlockElems})|"
328 .
'\\/?(center|blockquote|div|hr|mw:)'
338 if ( $openMatch || $closeMatch ) {
339 $pendingPTag =
false;
342 if ( !$this->inPre || $preOpenMatch ) {
346 if ( $preOpenMatch && !$preCloseMatch ) {
351 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
353 $inBlockquote = !$bqMatch[1][0];
354 $bqOffset = $bqMatch[0][1] +
strlen( $bqMatch[0][0] );
357 }
elseif ( !$inBlockElem && !$this->inPre ) {
359 && ( $this->lastParagraph ===
'pre' ||
trim(
$t ) !=
'' )
363 if ( $this->lastParagraph !==
'pre' ) {
364 $pendingPTag =
false;
366 $this->lastParagraph =
'pre';
369 }
elseif (
preg_match(
'/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS',
$t ) ) {
370 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
371 # But it should clear $pendingPTag.
372 if ( $pendingPTag ) {
374 $pendingPTag =
false;
378 if (
trim(
$t ) ===
'' ) {
379 if ( $pendingPTag ) {
380 $output .= $pendingPTag .
'<br />';
381 $pendingPTag =
false;
382 $this->lastParagraph =
'p';
383 }
elseif ( $this->lastParagraph !==
'p' ) {
385 $pendingPTag =
'<p>';
387 $pendingPTag =
'</p><p>';
389 }
elseif ( $pendingPTag ) {
391 $pendingPTag =
false;
392 $this->lastParagraph =
'p';
393 }
elseif ( $this->lastParagraph !==
'p' ) {
395 $this->lastParagraph =
'p';
400 # somewhere above we forget to get out of pre block (T2785)
401 if ( $preCloseMatch && $this->inPre ) {
402 $this->inPre =
false;
404 if ( $pendingPTag ===
false ) {
405 if ( $prefixLength === 0 ) {
418 while ( $prefixLength ) {
442 if ( !
preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
447 if ( $m[0][0] ===
':' ) {
448 # Easy; no tag nesting to worry about
449 $colonPos = $m[0][1];
450 $before =
substr( $str, 0, $colonPos );
451 $after =
substr( $str, $colonPos + 1 );
455 # Ugly state machine to walk through avoiding tags.
460 for ( $i = $m[0][1]; $i <
$len; $i++ ) {
467 # Could be either a <start> tag or an </end> tag
471 if ( $ltLevel === 0 ) {
473 $before =
substr( $str, 0, $i );
474 $after =
substr( $str, $i + 1 );
477 # Embedded in a tag; don't break it.
480 # Skip ahead looking for something interesting
481 if ( !
preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
482 # Nothing else interesting
485 if ( $m[0][0] ===
'-{' ) {
490 # Skip ahead to next interesting character.
497 # In language converter markup -{ ... }-
498 if ( !
preg_match(
'/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
499 # Nothing else interesting to find; abort!
500 # We're nested in language converter markup, but there
501 # are no close tags left. Abort!
503 }
elseif ( $m[0][0] ===
'-{' ) {
506 }
elseif ( $m[0][0] ===
'}-' ) {
509 if ( $lcLevel === 0 ) {
522 # Slash may be followed by >?
538 # Illegal early close? This shouldn't happen D:
548 if ( $ltLevel > 0 ) {
551 # ignore the excess close tag, but keep looking for
552 # colons. (This matches Parsoid behavior.)
553 wfDebug( __METHOD__ .
": Invalid input; too many close tags\n" );
560 # Yes, a self-closed tag <blah/>
563 # Probably we're jumping the gun, and this is an attribute
587 throw new MWException(
"State machine error in " . __METHOD__ );
590 if ( $ltLevel > 0 || $lcLevel > 0 ) {
592 __METHOD__ .
": Invalid input; not enough close tags " .
593 "(level $ltLevel/$lcLevel, state $state)\n"