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
296 $blockElems =
'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl';
298 $antiBlockElems =
'td|th';
300 $openMatch = preg_match(
302 .
"({$blockElems})|\\/({$antiBlockElems})|"
304 .
'\\/?(tr|dt|dd|li)'
308 $closeMatch = preg_match(
310 .
"\\/({$blockElems})|({$antiBlockElems})|"
312 .
'\\/?(center|blockquote|div|hr|mw:)'
322 if ( $openMatch || $closeMatch ) {
323 $pendingPTag =
false;
326 if ( !$this->inPre || $preOpenMatch ) {
330 if ( $preOpenMatch && !$preCloseMatch ) {
334 while ( preg_match(
'/<(\\/?)blockquote[\s>]/i',
$t,
335 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
337 $inBlockquote = !$bqMatch[1][0];
338 $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
340 $inBlockElem = !$closeMatch;
341 } elseif ( !$inBlockElem && !$this->inPre ) {
342 if (
' ' == substr(
$t, 0, 1 )
343 && ( $this->lastSection ===
'pre' || trim(
$t ) !=
'' )
347 if ( $this->lastSection !==
'pre' ) {
348 $pendingPTag =
false;
350 $this->lastSection =
'pre';
352 $t = substr(
$t, 1 );
353 } elseif ( preg_match(
'/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS',
$t ) ) {
354 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
355 # But it should clear $pendingPTag.
356 if ( $pendingPTag ) {
358 $pendingPTag =
false;
359 $this->lastSection =
'';
363 if ( trim(
$t ) ===
'' ) {
364 if ( $pendingPTag ) {
365 $output .= $pendingPTag .
'<br />';
366 $pendingPTag =
false;
367 $this->lastSection =
'p';
369 if ( $this->lastSection !==
'p' ) {
371 $this->lastSection =
'';
372 $pendingPTag =
'<p>';
374 $pendingPTag =
'</p><p>';
378 if ( $pendingPTag ) {
380 $pendingPTag =
false;
381 $this->lastSection =
'p';
382 } elseif ( $this->lastSection !==
'p' ) {
384 $this->lastSection =
'p';
390 # somewhere above we forget to get out of pre block (T2785)
391 if ( $preCloseMatch && $this->inPre ) {
392 $this->inPre =
false;
394 if ( $pendingPTag ===
false ) {
395 if ( $prefixLength === 0 ) {
404 while ( $prefixLength ) {
407 if ( !$prefixLength ) {
411 if ( $this->lastSection !==
'' ) {
412 $output .=
'</' . $this->lastSection .
'>';
413 $this->lastSection =
'';
430 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
435 if ( $m[0][0] ===
':' ) {
436 # Easy; no tag nesting to worry about
437 $colonPos = $m[0][1];
438 $before = substr( $str, 0, $colonPos );
439 $after = substr( $str, $colonPos + 1 );
443 # Ugly state machine to walk through avoiding tags.
447 $len = strlen( $str );
448 for ( $i = $m[0][1]; $i < $len; $i++ ) {
455 # Could be either a <start> tag or an </end> tag
459 if ( $ltLevel === 0 ) {
461 $before = substr( $str, 0, $i );
462 $after = substr( $str, $i + 1 );
465 # Embedded in a tag; don't break it.
468 # Skip ahead looking for something interesting
469 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
470 # Nothing else interesting
473 if ( $m[0][0] ===
'-{' ) {
478 # Skip ahead to next interesting character.
485 # In language converter markup -{ ... }-
486 if ( !preg_match(
'/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
487 # Nothing else interesting to find; abort!
488 # We're nested in language converter markup, but there
489 # are no close tags left. Abort!
491 } elseif ( $m[0][0] ===
'-{' ) {
494 } elseif ( $m[0][0] ===
'}-' ) {
497 if ( $lcLevel === 0 ) {
510 # Slash may be followed by >?
526 # Illegal early close? This shouldn't happen D:
536 if ( $ltLevel > 0 ) {
539 # ignore the excess close tag, but keep looking for
540 # colons. (This matches Parsoid behavior.)
541 wfDebug( __METHOD__ .
": Invalid input; too many close tags\n" );
548 # Yes, a self-closed tag <blah/>
551 # Probably we're jumping the gun, and this is an attribute
575 throw new MWException(
"State machine error in " . __METHOD__ );
578 if ( $ltLevel > 0 || $lcLevel > 0 ) {
580 __METHOD__ .
": Invalid input; not enough close tags " .
581 "(level $ltLevel/$lcLevel, state $state)\n"