32 # State constants for the definition list colon extraction
51 $pass =
new self(
$text, $lineStart );
52 return $pass->execute();
60 $this->lineStart = $lineStart;
70 if ( $this->lastSection !==
'' ) {
71 $result =
'</' . $this->lastSection .
">\n";
74 $this->lastSection =
'';
88 $shorter = min( strlen( $st1 ), strlen( $st2 ) );
90 for ( $i = 0; $i < $shorter; ++$i ) {
91 if ( $st1[$i] !== $st2[$i] ) {
108 if (
'*' === $char ) {
110 } elseif (
'#' === $char ) {
112 } elseif (
':' === $char ) {
114 } elseif (
';' === $char ) {
116 $this->DTopen =
true;
131 if (
'*' === $char ||
'#' === $char ) {
132 return "</li>\n<li>";
133 } elseif (
':' === $char ||
';' === $char ) {
135 if ( $this->DTopen ) {
138 if (
';' === $char ) {
139 $this->DTopen =
true;
140 return $close .
'<dt>';
142 $this->DTopen =
false;
143 return $close .
'<dd>';
146 return '<!-- ERR 2 -->';
156 if (
'*' === $char ) {
157 $text =
"</li></ul>";
158 } elseif (
'#' === $char ) {
159 $text =
"</li></ol>";
160 } elseif (
':' === $char ) {
161 if ( $this->DTopen ) {
162 $this->DTopen =
false;
163 $text =
"</dt></dl>";
165 $text =
"</dd></dl>";
168 return '<!-- ERR 3 -->';
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.
242 } elseif ( $prefixLength || $lastPrefixLength ) {
243 # We need to open or close prefixes, or both.
245 # Either open or close a level...
246 $commonPrefixLength = $this->
getCommon( $prefix, $lastPrefix );
247 $pendingPTag =
false;
249 # Close all the prefixes which aren't shared.
250 while ( $commonPrefixLength < $lastPrefixLength ) {
255 # Continue the current prefix if appropriate.
256 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
260 # Close an open <dt> if we have a <dd> (":") starting on this line
261 if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] ===
':' ) {
265 # Open prefixes where appropriate.
266 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
269 while ( $prefixLength > $commonPrefixLength ) {
270 $char = $prefix[$commonPrefixLength];
273 if (
';' === $char ) {
274 # @todo FIXME: This is dupe of code above
280 ++$commonPrefixLength;
282 if ( !$prefixLength && $lastPrefix ) {
285 $lastPrefix = $prefix2;
288 # If we have no prefixes, go to paragraph mode.
289 if ( 0 == $prefixLength ) {
290 # No prefix (not in list)--go to paragraph mode
291 # @todo consider using a stack for nestable elements like span, table and div
292 $openMatch = preg_match(
293 '/(?:<table|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|'
294 .
'<p|<ul|<ol|<dl|<li|<\\/tr|<\\/td|<\\/th)\\b/iS',
297 $closeMatch = preg_match(
298 '/(?:<\\/table|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'
299 .
'<td|<th|<\\/?blockquote|<\\/?div|<hr|<\\/pre|<\\/p|<\\/mw:|'
300 . Parser::MARKER_PREFIX
301 .
'-pre|<\\/li|<\\/ul|<\\/ol|<\\/dl|<\\/?center)\\b/iS',
305 if ( $openMatch || $closeMatch ) {
306 $pendingPTag =
false;
307 # @todo T7718: paragraph closed
309 if ( $preOpenMatch && !$preCloseMatch ) {
313 while ( preg_match(
'/<(\\/?)blockquote[\s>]/i',
$t,
314 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
316 $inBlockquote = !$bqMatch[1][0];
317 $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
319 $inBlockElem = !$closeMatch;
320 } elseif ( !$inBlockElem && !$this->inPre ) {
321 if (
' ' == substr(
$t, 0, 1 )
322 && ( $this->lastSection ===
'pre' || trim(
$t ) !=
'' )
326 if ( $this->lastSection !==
'pre' ) {
327 $pendingPTag =
false;
329 $this->lastSection =
'pre';
331 $t = substr(
$t, 1 );
334 if ( trim(
$t ) ===
'' ) {
335 if ( $pendingPTag ) {
336 $output .= $pendingPTag .
'<br />';
337 $pendingPTag =
false;
338 $this->lastSection =
'p';
340 if ( $this->lastSection !==
'p' ) {
342 $this->lastSection =
'';
343 $pendingPTag =
'<p>';
345 $pendingPTag =
'</p><p>';
349 if ( $pendingPTag ) {
351 $pendingPTag =
false;
352 $this->lastSection =
'p';
353 } elseif ( $this->lastSection !==
'p' ) {
355 $this->lastSection =
'p';
361 # somewhere above we forget to get out of pre block (T2785)
362 if ( $preCloseMatch && $this->inPre ) {
363 $this->inPre =
false;
365 if ( $pendingPTag ===
false ) {
367 if ( $prefixLength === 0 ) {
372 while ( $prefixLength ) {
375 if ( !$prefixLength ) {
379 if ( $this->lastSection !==
'' ) {
380 $output .=
'</' . $this->lastSection .
'>';
381 $this->lastSection =
'';
398 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
403 if ( $m[0][0] ===
':' ) {
404 # Easy; no tag nesting to worry about
405 $colonPos = $m[0][1];
406 $before = substr( $str, 0, $colonPos );
407 $after = substr( $str, $colonPos + 1 );
411 # Ugly state machine to walk through avoiding tags.
415 $len = strlen( $str );
416 for ( $i = $m[0][1]; $i < $len; $i++ ) {
423 # Could be either a <start> tag or an </end> tag
427 if ( $ltLevel === 0 ) {
429 $before = substr( $str, 0, $i );
430 $after = substr( $str, $i + 1 );
433 # Embedded in a tag; don't break it.
436 # Skip ahead looking for something interesting
437 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
438 # Nothing else interesting
441 if ( $m[0][0] ===
'-{' ) {
446 # Skip ahead to next interesting character.
453 # In language converter markup -{ ... }-
454 if ( !preg_match(
'/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
455 # Nothing else interesting to find; abort!
456 # We're nested in language converter markup, but there
457 # are no close tags left. Abort!
459 } elseif ( $m[0][0] ===
'-{' ) {
462 } elseif ( $m[0][0] ===
'}-' ) {
465 if ( $lcLevel === 0 ) {
478 # Slash may be followed by >?
494 # Illegal early close? This shouldn't happen D:
504 if ( $ltLevel > 0 ) {
507 # ignore the excess close tag, but keep looking for
508 # colons. (This matches Parsoid behavior.)
509 wfDebug( __METHOD__ .
": Invalid input; too many close tags\n" );
516 # Yes, a self-closed tag <blah/>
519 # Probably we're jumping the gun, and this is an attribute
543 throw new MWException(
"State machine error in " . __METHOD__ );
546 if ( $ltLevel > 0 || $lcLevel > 0 ) {
548 __METHOD__ .
": Invalid input; not enough close tags " .
549 "(level $ltLevel/$lcLevel, state $state)\n"