193 # Parsing through the text line by line. The main thing
194 # happening here is handling of block-level elements p, pre,
195 # and making lists from lines starting with * # : etc.
198 $lastPrefix = $output =
'';
199 $this->DTopen = $inBlockElem =
false;
201 $pendingPTag =
false;
202 $inBlockquote =
false;
204 for ( $textLines->rewind(); $textLines->valid(); ) {
205 $inputLine = $textLines->current();
207 $notLastLine = $textLines->valid();
210 if ( !$this->lineStart ) {
211 $output .= $inputLine;
212 $this->lineStart =
true;
220 $lastPrefixLength = strlen( $lastPrefix );
221 $preCloseMatch = preg_match(
'/<\\/pre/i', $inputLine );
222 $preOpenMatch = preg_match(
'/<pre/i', $inputLine );
223 # If not in a <pre> element, scan for and figure out what prefixes are there.
224 if ( !$this->inPre ) {
225 # Multiple prefixes may abut each other for nested lists.
226 $prefixLength = strspn( $inputLine,
'*#:;' );
227 $prefix = substr( $inputLine, 0, $prefixLength );
230 # ; and : are both from definition-lists, so they're equivalent
231 # for the purposes of determining whether or not we need to open/close
233 $prefix2 = str_replace(
';',
':', $prefix );
234 $t = substr( $inputLine, $prefixLength );
235 $this->inPre = (bool)$preOpenMatch;
237 # Don't interpret any other prefixes in preformatted text
239 $prefix = $prefix2 =
'';
244 if ( $prefixLength && $lastPrefix === $prefix2 ) {
245 # Same as the last item, so no need to deal with nesting or opening stuff
246 $output .= $this->
nextItem( substr( $prefix, -1 ) );
247 $pendingPTag =
false;
249 if ( substr( $prefix, -1 ) ===
';' ) {
250 # The one nasty exception: definition lists work like this:
251 # ; title : definition text
252 # So we check for : in the remainder text to split up the
253 # title and definition, without b0rking links.
258 $output .= trim( $term ) . $this->
nextItem(
':' );
261 } elseif ( $prefixLength || $lastPrefixLength ) {
262 # We need to open or close prefixes, or both.
264 # Either open or close a level...
265 $commonPrefixLength = $this->
getCommon( $prefix, $lastPrefix );
266 $pendingPTag =
false;
268 # Close all the prefixes which aren't shared.
269 while ( $commonPrefixLength < $lastPrefixLength ) {
271 $output .= $this->
closeList( $lastPrefix[$lastPrefixLength - 1] );
275 # Continue the current prefix if appropriate.
276 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
277 $output .= $this->
nextItem( $prefix[$commonPrefixLength - 1] );
280 # Close an open <dt> if we have a <dd> (":") starting on this line
281 if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] ===
':' ) {
285 # Open prefixes where appropriate.
286 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
289 while ( $prefixLength > $commonPrefixLength ) {
290 $char = $prefix[$commonPrefixLength];
291 $output .= $this->
openList( $char );
293 if ( $char ===
';' ) {
294 # @todo FIXME: This is dupe of code above
298 $output .= trim( $term ) . $this->
nextItem(
':' );
301 ++$commonPrefixLength;
303 if ( !$prefixLength && $lastPrefix ) {
306 $lastPrefix = $prefix2;
309 # If we have no prefixes, go to paragraph mode.
310 if ( $prefixLength == 0 ) {
311 # No prefix (not in list)--go to paragraph mode
312 # @todo consider using a stack for nestable elements like span, table and div
315 $blockElems =
'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl';
317 $antiBlockElems =
'td|th';
319 $openMatch = preg_match(
321 .
"({$blockElems})|\\/({$antiBlockElems})|"
323 .
'\\/?(tr|caption|dt|dd|li)'
327 $closeMatch = preg_match(
329 .
"\\/({$blockElems})|({$antiBlockElems})|"
331 .
'\\/?(center|blockquote|div|hr|mw:|aside|figure)'
341 if ( $openMatch || $closeMatch ) {
342 $pendingPTag =
false;
345 if ( !$this->inPre || $preOpenMatch ) {
349 if ( $preOpenMatch && !$preCloseMatch ) {
353 while ( preg_match(
'/<(\\/?)blockquote[\s>]/i',
$t,
354 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
356 $inBlockquote = !$bqMatch[1][0];
357 $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
359 $inBlockElem = !$closeMatch;
360 } elseif ( !$inBlockElem && !$this->inPre ) {
361 if ( substr(
$t, 0, 1 ) ==
' '
362 && ( $this->lastParagraph ===
'pre' || trim(
$t ) !=
'' )
366 if ( $this->lastParagraph !==
'pre' ) {
367 $pendingPTag =
false;
369 $this->lastParagraph =
'pre';
371 $t = substr(
$t, 1 );
372 } elseif ( preg_match(
'/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS',
$t ) ) {
373 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
374 # But it should clear $pendingPTag.
375 if ( $pendingPTag ) {
377 $pendingPTag =
false;
381 if ( trim(
$t ) ===
'' ) {
382 if ( $pendingPTag ) {
383 $output .= $pendingPTag .
'<br />';
384 $pendingPTag =
false;
385 $this->lastParagraph =
'p';
386 } elseif ( $this->lastParagraph !==
'p' ) {
388 $pendingPTag =
'<p>';
390 $pendingPTag =
'</p><p>';
392 } elseif ( $pendingPTag ) {
393 $output .= $pendingPTag;
394 $pendingPTag =
false;
395 $this->lastParagraph =
'p';
396 } elseif ( $this->lastParagraph !==
'p' ) {
398 $this->lastParagraph =
'p';
403 # somewhere above we forget to get out of pre block (T2785)
404 if ( $preCloseMatch && $this->inPre ) {
405 $this->inPre =
false;
407 if ( $pendingPTag ===
false ) {
408 if ( $prefixLength === 0 ) {
417 $output .= trim(
$t );
421 while ( $prefixLength ) {
422 $output .= $this->
closeList( $prefix2[$prefixLength - 1] );
445 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
450 if ( $m[0][0] ===
':' ) {
451 # Easy; no tag nesting to worry about
452 $colonPos = $m[0][1];
453 $before = substr( $str, 0, $colonPos );
454 $after = substr( $str, $colonPos + 1 );
458 # Ugly state machine to walk through avoiding tags.
462 $len = strlen( $str );
463 for ( $i = $m[0][1]; $i < $len; $i++ ) {
470 # Could be either a <start> tag or an </end> tag
474 if ( $ltLevel === 0 ) {
476 $before = substr( $str, 0, $i );
477 $after = substr( $str, $i + 1 );
480 # Embedded in a tag; don't break it.
483 # Skip ahead looking for something interesting
484 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
485 # Nothing else interesting
488 if ( $m[0][0] ===
'-{' ) {
493 # Skip ahead to next interesting character.
500 # In language converter markup -{ ... }-
501 if ( !preg_match(
'/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
502 # Nothing else interesting to find; abort!
503 # We're nested in language converter markup, but there
504 # are no close tags left. Abort!
507 if ( $m[0][0] ===
'-{' ) {
510 } elseif ( $m[0][0] ===
'}-' ) {
513 if ( $lcLevel === 0 ) {
526 # Slash may be followed by >?
542 # Illegal early close? This shouldn't happen D:
552 if ( $ltLevel > 0 ) {
555 # ignore the excess close tag, but keep looking for
556 # colons. (This matches Parsoid behavior.)
557 wfDebug( __METHOD__ .
": Invalid input; too many close tags" );
564 # Yes, a self-closed tag <blah/>
567 # Probably we're jumping the gun, and this is an attribute
591 throw new MWException(
"State machine error in " . __METHOD__ );
594 if ( $ltLevel > 0 || $lcLevel > 0 ) {
596 __METHOD__ .
": Invalid input; not enough close tags " .
597 "(level $ltLevel/$lcLevel, state $state)"