179 # Parsing through the text line by line. The main thing
180 # happening here is handling of block-level elements p, pre,
181 # and making lists from lines starting with * # : etc.
185 $this->DTopen = $inBlockElem =
false;
187 $pendingPTag =
false;
188 $inBlockquote =
false;
190 foreach ( $textLines as $inputLine ) {
192 if ( !$this->lineStart ) {
194 $this->lineStart =
true;
202 $lastPrefixLength = strlen( $lastPrefix );
203 $preCloseMatch = preg_match(
'/<\\/pre/i', $inputLine );
204 $preOpenMatch = preg_match(
'/<pre/i', $inputLine );
205 # If not in a <pre> element, scan for and figure out what prefixes are there.
206 if ( !$this->inPre ) {
207 # Multiple prefixes may abut each other for nested lists.
208 $prefixLength = strspn( $inputLine,
'*#:;' );
209 $prefix = substr( $inputLine, 0, $prefixLength );
212 # ; and : are both from definition-lists, so they're equivalent
213 # for the purposes of determining whether or not we need to open/close
215 $prefix2 = str_replace(
';',
':', $prefix );
216 $t = substr( $inputLine, $prefixLength );
217 $this->inPre = (bool)$preOpenMatch;
219 # Don't interpret any other prefixes in preformatted text
221 $prefix = $prefix2 =
'';
226 if ( $prefixLength && $lastPrefix === $prefix2 ) {
227 # Same as the last item, so no need to deal with nesting or opening stuff
229 $pendingPTag =
false;
231 if ( substr( $prefix, -1 ) ===
';' ) {
232 # The one nasty exception: definition lists work like this:
233 # ; title : definition text
234 # So we check for : in the remainder text to split up the
235 # title and definition, without b0rking links.
243 } elseif ( $prefixLength || $lastPrefixLength ) {
244 # We need to open or close prefixes, or both.
246 # Either open or close a level...
247 $commonPrefixLength = $this->
getCommon( $prefix, $lastPrefix );
248 $pendingPTag =
false;
250 # Close all the prefixes which aren't shared.
251 while ( $commonPrefixLength < $lastPrefixLength ) {
256 # Continue the current prefix if appropriate.
257 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
261 # Close an open <dt> if we have a <dd> (":") starting on this line
262 if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] ===
':' ) {
266 # Open prefixes where appropriate.
267 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
270 while ( $prefixLength > $commonPrefixLength ) {
271 $char = $prefix[$commonPrefixLength];
274 if (
';' === $char ) {
275 # @todo FIXME: This is dupe of code above
282 ++$commonPrefixLength;
284 if ( !$prefixLength && $lastPrefix ) {
287 $lastPrefix = $prefix2;
290 # If we have no prefixes, go to paragraph mode.
291 if ( 0 == $prefixLength ) {
292 # No prefix (not in list)--go to paragraph mode
293 # @todo consider using a stack for nestable elements like span, table and div
294 $openMatch = preg_match(
295 '/(?:<table|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|'
296 .
'<p|<ul|<ol|<dl|<li|<\\/tr|<\\/td|<\\/th)\\b/iS',
299 $closeMatch = preg_match(
300 '/(?:<\\/table|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'
301 .
'<td|<th|<\\/?blockquote|<\\/?div|<hr|<\\/pre|<\\/p|<\\/mw:|'
302 . Parser::MARKER_PREFIX
303 .
'-pre|<\\/li|<\\/ul|<\\/ol|<\\/dl|<\\/?center)\\b/iS',
307 if ( $openMatch || $closeMatch ) {
308 $pendingPTag =
false;
311 if ( !$this->inPre || $preOpenMatch ) {
315 if ( $preOpenMatch && !$preCloseMatch ) {
319 while ( preg_match(
'/<(\\/?)blockquote[\s>]/i',
$t,
320 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
322 $inBlockquote = !$bqMatch[1][0];
323 $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
325 $inBlockElem = !$closeMatch;
326 } elseif ( !$inBlockElem && !$this->inPre ) {
327 if (
' ' == substr(
$t, 0, 1 )
328 && ( $this->lastSection ===
'pre' || trim(
$t ) !=
'' )
332 if ( $this->lastSection !==
'pre' ) {
333 $pendingPTag =
false;
335 $this->lastSection =
'pre';
337 $t = substr(
$t, 1 );
338 } elseif ( preg_match(
'/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS',
$t ) ) {
339 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
340 # But it should clear $pendingPTag.
341 if ( $pendingPTag ) {
343 $pendingPTag =
false;
344 $this->lastSection =
'';
348 if ( trim(
$t ) ===
'' ) {
349 if ( $pendingPTag ) {
350 $output .= $pendingPTag .
'<br />';
351 $pendingPTag =
false;
352 $this->lastSection =
'p';
354 if ( $this->lastSection !==
'p' ) {
356 $this->lastSection =
'';
357 $pendingPTag =
'<p>';
359 $pendingPTag =
'</p><p>';
363 if ( $pendingPTag ) {
365 $pendingPTag =
false;
366 $this->lastSection =
'p';
367 } elseif ( $this->lastSection !==
'p' ) {
369 $this->lastSection =
'p';
375 # somewhere above we forget to get out of pre block (T2785)
376 if ( $preCloseMatch && $this->inPre ) {
377 $this->inPre =
false;
379 if ( $pendingPTag ===
false ) {
380 if ( $prefixLength === 0 ) {
389 while ( $prefixLength ) {
392 if ( !$prefixLength ) {
396 if ( $this->lastSection !==
'' ) {
397 $output .=
'</' . $this->lastSection .
'>';
398 $this->lastSection =
'';
415 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
420 if ( $m[0][0] ===
':' ) {
421 # Easy; no tag nesting to worry about
422 $colonPos = $m[0][1];
423 $before = substr( $str, 0, $colonPos );
424 $after = substr( $str, $colonPos + 1 );
428 # Ugly state machine to walk through avoiding tags.
432 $len = strlen( $str );
433 for ( $i = $m[0][1]; $i < $len; $i++ ) {
440 # Could be either a <start> tag or an </end> tag
444 if ( $ltLevel === 0 ) {
446 $before = substr( $str, 0, $i );
447 $after = substr( $str, $i + 1 );
450 # Embedded in a tag; don't break it.
453 # Skip ahead looking for something interesting
454 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
455 # Nothing else interesting
458 if ( $m[0][0] ===
'-{' ) {
463 # Skip ahead to next interesting character.
470 # In language converter markup -{ ... }-
471 if ( !preg_match(
'/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
472 # Nothing else interesting to find; abort!
473 # We're nested in language converter markup, but there
474 # are no close tags left. Abort!
476 } elseif ( $m[0][0] ===
'-{' ) {
479 } elseif ( $m[0][0] ===
'}-' ) {
482 if ( $lcLevel === 0 ) {
495 # Slash may be followed by >?
511 # Illegal early close? This shouldn't happen D:
521 if ( $ltLevel > 0 ) {
524 # ignore the excess close tag, but keep looking for
525 # colons. (This matches Parsoid behavior.)
526 wfDebug( __METHOD__ .
": Invalid input; too many close tags\n" );
533 # Yes, a self-closed tag <blah/>
536 # Probably we're jumping the gun, and this is an attribute
560 throw new MWException(
"State machine error in " . __METHOD__ );
563 if ( $ltLevel > 0 || $lcLevel > 0 ) {
565 __METHOD__ .
": Invalid input; not enough close tags " .
566 "(level $ltLevel/$lcLevel, state $state)\n"