34 private $DTopen =
false;
36 private $inPre =
false;
38 private $lastParagraph =
'';
44 # State constants for the definition list colon extraction
45 private const COLON_STATE_TEXT = 0;
46 private const COLON_STATE_TAG = 1;
47 private const COLON_STATE_TAGSTART = 2;
48 private const COLON_STATE_CLOSETAG = 3;
49 private const COLON_STATE_TAGSLASH = 4;
50 private const COLON_STATE_COMMENT = 5;
51 private const COLON_STATE_COMMENTDASH = 6;
52 private const COLON_STATE_COMMENTDASHDASH = 7;
53 private const COLON_STATE_LC = 8;
64 $pass =
new self( $text, $lineStart );
65 return $pass->execute();
72 private function __construct( $text, $lineStart ) {
74 $this->lineStart = $lineStart;
80 private function hasOpenParagraph() {
81 return $this->lastParagraph !==
'';
90 private function closeParagraph( $atTheEnd =
false ) {
92 if ( $this->hasOpenParagraph() ) {
93 $result =
'</' . $this->lastParagraph .
'>';
99 $this->lastParagraph =
'';
112 private function getCommon( $st1, $st2 ) {
113 $shorter = min( strlen( $st1 ), strlen( $st2 ) );
115 for ( $i = 0; $i < $shorter; ++$i ) {
116 if ( $st1[$i] !== $st2[$i] ) {
130 private function openList( $char ) {
131 $result = $this->closeParagraph();
133 if ( $char ===
'*' ) {
134 $result .=
"<ul><li>";
135 } elseif ( $char ===
'#' ) {
136 $result .=
"<ol><li>";
137 } elseif ( $char ===
':' ) {
138 $result .=
"<dl><dd>";
139 } elseif ( $char ===
';' ) {
140 $result .=
"<dl><dt>";
141 $this->DTopen =
true;
143 $result =
'<!-- ERR 1 -->';
155 private function nextItem( $char ) {
156 if ( $char ===
'*' || $char ===
'#' ) {
157 return "</li>\n<li>";
158 } elseif ( $char ===
':' || $char ===
';' ) {
160 if ( $this->DTopen ) {
163 if ( $char ===
';' ) {
164 $this->DTopen =
true;
165 return $close .
'<dt>';
167 $this->DTopen =
false;
168 return $close .
'<dd>';
171 return '<!-- ERR 2 -->';
180 private function closeList( $char ) {
181 if ( $char ===
'*' ) {
182 $text =
"</li></ul>";
183 } elseif ( $char ===
'#' ) {
184 $text =
"</li></ol>";
185 } elseif ( $char ===
':' ) {
186 if ( $this->DTopen ) {
187 $this->DTopen =
false;
188 $text =
"</dt></dl>";
190 $text =
"</dd></dl>";
193 return '<!-- ERR 3 -->';
202 private function execute() {
204 # Parsing through the text line by line. The main thing
205 # happening here is handling of block-level elements p, pre,
206 # and making lists from lines starting with * # : etc.
209 $lastPrefix = $output =
'';
210 $this->DTopen = $inBlockElem =
false;
212 $pendingPTag =
false;
213 $inBlockquote =
false;
215 for ( $textLines->rewind(); $textLines->valid(); ) {
216 $inputLine = $textLines->current();
218 $notLastLine = $textLines->valid();
221 if ( !$this->lineStart ) {
222 $output .= $inputLine;
223 $this->lineStart =
true;
231 $lastPrefixLength = strlen( $lastPrefix );
232 $preCloseMatch = preg_match(
'/<\\/pre/i', $inputLine );
233 $preOpenMatch = preg_match(
'/<pre/i', $inputLine );
234 # If not in a <pre> element, scan for and figure out what prefixes are there.
235 if ( !$this->inPre ) {
236 # Multiple prefixes may abut each other for nested lists.
237 $prefixLength = strspn( $inputLine,
'*#:;' );
238 $prefix = substr( $inputLine, 0, $prefixLength );
241 # ; and : are both from definition-lists, so they're equivalent
242 # for the purposes of determining whether or not we need to open/close
244 $prefix2 = str_replace(
';',
':', $prefix );
245 $t = substr( $inputLine, $prefixLength );
246 $this->inPre = (bool)$preOpenMatch;
248 # Don't interpret any other prefixes in preformatted text
250 $prefix = $prefix2 =
'';
255 if ( $prefixLength && $lastPrefix === $prefix2 ) {
256 # Same as the last item, so no need to deal with nesting or opening stuff
257 $output .= $this->nextItem( substr( $prefix, -1 ) );
258 $pendingPTag =
false;
260 if ( substr( $prefix, -1 ) ===
';' ) {
261 # The one nasty exception: definition lists work like this:
262 # ; title : definition text
263 # So we check for : in the remainder text to split up the
264 # title and definition, without b0rking links.
266 if ( $this->findColonNoLinks( $t, $term, $t2 ) !==
false ) {
269 $output .= trim( $term ) . $this->nextItem(
':' );
272 } elseif ( $prefixLength || $lastPrefixLength ) {
273 # We need to open or close prefixes, or both.
275 # Either open or close a level...
276 $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
277 $pendingPTag =
false;
279 # Close all the prefixes which aren't shared.
280 while ( $commonPrefixLength < $lastPrefixLength ) {
282 $output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
286 # Continue the current prefix if appropriate.
287 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
288 $output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
291 # Close an open <dt> if we have a <dd> (":") starting on this line
292 if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] ===
':' ) {
293 $output .= $this->nextItem(
':' );
296 # Open prefixes where appropriate.
297 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
300 while ( $prefixLength > $commonPrefixLength ) {
301 $char = $prefix[$commonPrefixLength];
302 $output .= $this->openList( $char );
304 if ( $char ===
';' ) {
305 # @todo FIXME: This is dupe of code above
306 if ( $this->findColonNoLinks( $t, $term, $t2 ) !==
false ) {
309 $output .= trim( $term ) . $this->nextItem(
':' );
312 ++$commonPrefixLength;
314 if ( !$prefixLength && $lastPrefix ) {
317 $lastPrefix = $prefix2;
320 # If we have no prefixes, go to paragraph mode.
321 if ( $prefixLength == 0 ) {
322 # No prefix (not in list)--go to paragraph mode
323 # @todo consider using a stack for nestable elements like span, table and div
326 $blockElems =
'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl';
328 $antiBlockElems =
'td|th';
330 $openMatch = preg_match(
332 .
"({$blockElems})|\\/({$antiBlockElems})|"
334 .
'\\/?(tr|caption|dt|dd|li)'
338 $closeMatch = preg_match(
340 .
"\\/({$blockElems})|({$antiBlockElems})|"
342 .
'\\/?(center|blockquote|div|hr|mw:|aside|figure)|'
344 .
'meta property="mw:'
354 if ( $openMatch || $closeMatch ) {
355 $pendingPTag =
false;
358 if ( !$this->inPre || $preOpenMatch ) {
360 $output .= $this->closeParagraph();
362 if ( $preOpenMatch && !$preCloseMatch ) {
366 while ( preg_match(
'/<(\\/?)blockquote[\s>]/i', $t,
367 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
369 $inBlockquote = !$bqMatch[1][0];
370 $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
372 $inBlockElem = !$closeMatch;
373 } elseif ( !$inBlockElem && !$this->inPre ) {
374 if ( substr( $t, 0, 1 ) ==
' '
375 && ( $this->lastParagraph ===
'pre' || trim( $t ) !=
'' )
379 if ( $this->lastParagraph !==
'pre' ) {
380 $pendingPTag =
false;
381 $output .= $this->closeParagraph() .
'<pre>';
382 $this->lastParagraph =
'pre';
384 $t = substr( $t, 1 );
385 } elseif ( preg_match(
'/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS', $t ) ) {
386 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
387 # But it should clear $pendingPTag.
388 if ( $pendingPTag ) {
389 $output .= $this->closeParagraph();
390 $pendingPTag =
false;
394 if ( trim( $t ) ===
'' ) {
395 if ( $pendingPTag ) {
396 $output .= $pendingPTag .
'<br />';
397 $pendingPTag =
false;
398 $this->lastParagraph =
'p';
399 } elseif ( $this->lastParagraph !==
'p' ) {
400 $output .= $this->closeParagraph();
401 $pendingPTag =
'<p>';
403 $pendingPTag =
'</p><p>';
405 } elseif ( $pendingPTag ) {
406 $output .= $pendingPTag;
407 $pendingPTag =
false;
408 $this->lastParagraph =
'p';
409 } elseif ( $this->lastParagraph !==
'p' ) {
410 $output .= $this->closeParagraph() .
'<p>';
411 $this->lastParagraph =
'p';
416 # somewhere above we forget to get out of pre block (T2785)
417 if ( $preCloseMatch && $this->inPre ) {
418 $this->inPre =
false;
420 if ( $pendingPTag ===
false ) {
421 if ( $prefixLength === 0 ) {
425 if ( $notLastLine || $this->hasOpenParagraph() ) {
430 $output .= trim( $t );
434 while ( $prefixLength ) {
436 $output .= $this->closeList( $prefix2[$prefixLength - 1] );
440 if ( !$prefixLength && $this->hasOpenParagraph() ) {
444 $output .= $this->closeParagraph(
true );
457 private function findColonNoLinks( $str, &$before, &$after ) {
458 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
463 if ( $m[0][0] ===
':' ) {
464 # Easy; no tag nesting to worry about
465 $colonPos = $m[0][1];
466 $before = substr( $str, 0, $colonPos );
467 $after = substr( $str, $colonPos + 1 );
471 # Ugly state machine to walk through avoiding tags.
472 $state = self::COLON_STATE_TEXT;
475 $len = strlen( $str );
476 for ( $i = $m[0][1]; $i < $len; $i++ ) {
480 case self::COLON_STATE_TEXT:
483 # Could be either a <start> tag or an </end> tag
484 $state = self::COLON_STATE_TAGSTART;
487 if ( $ltLevel === 0 ) {
489 $before = substr( $str, 0, $i );
490 $after = substr( $str, $i + 1 );
493 # Embedded in a tag; don't break it.
496 # Skip ahead looking for something interesting
497 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
498 # Nothing else interesting
501 if ( $m[0][0] ===
'-{' ) {
502 $state = self::COLON_STATE_LC;
506 # Skip ahead to next interesting character.
512 case self::COLON_STATE_LC:
513 # In language converter markup -{ ... }-
514 if ( !preg_match(
'/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
515 # Nothing else interesting to find; abort!
516 # We're nested in language converter markup, but there
517 # are no close tags left. Abort!
520 if ( $m[0][0] ===
'-{' ) {
523 } elseif ( $m[0][0] ===
'}-' ) {
526 if ( $lcLevel === 0 ) {
527 $state = self::COLON_STATE_TEXT;
531 case self::COLON_STATE_TAG:
536 $state = self::COLON_STATE_TEXT;
539 # Slash may be followed by >?
540 $state = self::COLON_STATE_TAGSLASH;
546 case self::COLON_STATE_TAGSTART:
549 $state = self::COLON_STATE_CLOSETAG;
552 $state = self::COLON_STATE_COMMENT;
555 # Illegal early close? This shouldn't happen D:
556 $state = self::COLON_STATE_TEXT;
559 $state = self::COLON_STATE_TAG;
562 case self::COLON_STATE_CLOSETAG:
565 if ( $ltLevel > 0 ) {
568 # ignore the excess close tag, but keep looking for
569 # colons. (This matches Parsoid behavior.)
570 wfDebug( __METHOD__ .
": Invalid input; too many close tags" );
572 $state = self::COLON_STATE_TEXT;
575 case self::COLON_STATE_TAGSLASH:
577 # Yes, a self-closed tag <blah/>
578 $state = self::COLON_STATE_TEXT;
580 # Probably we're jumping the gun, and this is an attribute
581 $state = self::COLON_STATE_TAG;
584 case self::COLON_STATE_COMMENT:
586 $state = self::COLON_STATE_COMMENTDASH;
589 case self::COLON_STATE_COMMENTDASH:
591 $state = self::COLON_STATE_COMMENTDASHDASH;
593 $state = self::COLON_STATE_COMMENT;
596 case self::COLON_STATE_COMMENTDASHDASH:
598 $state = self::COLON_STATE_TEXT;
600 $state = self::COLON_STATE_COMMENT;
604 throw new LogicException(
"State machine error in " . __METHOD__ );
607 if ( $ltLevel > 0 || $lcLevel > 0 ) {
609 __METHOD__ .
": Invalid input; not enough close tags " .
610 "(level $ltLevel/$lcLevel, state $state)"