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 # Open prefixes where appropriate.
261 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
264 while ( $prefixLength > $commonPrefixLength ) {
265 $char = substr( $prefix, $commonPrefixLength, 1 );
268 if (
';' === $char ) {
269 # @todo FIXME: This is dupe of code above
275 ++$commonPrefixLength;
277 if ( !$prefixLength && $lastPrefix ) {
280 $lastPrefix = $prefix2;
283 # If we have no prefixes, go to paragraph mode.
284 if ( 0 == $prefixLength ) {
285 # No prefix (not in list)--go to paragraph mode
286 # @todo consider using a stack for nestable elements like span, table and div
287 $openMatch = preg_match(
288 '/(?:<table|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|'
289 .
'<p|<ul|<ol|<dl|<li|<\\/tr|<\\/td|<\\/th)/iS',
292 $closeMatch = preg_match(
293 '/(?:<\\/table|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'
294 .
'<td|<th|<\\/?blockquote|<\\/?div|<hr|<\\/pre|<\\/p|<\\/mw:|'
295 . Parser::MARKER_PREFIX
296 .
'-pre|<\\/li|<\\/ul|<\\/ol|<\\/dl|<\\/?center)/iS',
300 if ( $openMatch || $closeMatch ) {
301 $pendingPTag =
false;
302 # @todo T7718: paragraph closed
304 if ( $preOpenMatch && !$preCloseMatch ) {
308 while ( preg_match(
'/<(\\/?)blockquote[\s>]/i',
$t,
309 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
311 $inBlockquote = !$bqMatch[1][0];
312 $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
314 $inBlockElem = !$closeMatch;
315 } elseif ( !$inBlockElem && !$this->inPre ) {
316 if (
' ' == substr(
$t, 0, 1 )
317 && ( $this->lastSection ===
'pre' || trim(
$t ) !=
'' )
321 if ( $this->lastSection !==
'pre' ) {
322 $pendingPTag =
false;
324 $this->lastSection =
'pre';
326 $t = substr(
$t, 1 );
329 if ( trim(
$t ) ===
'' ) {
330 if ( $pendingPTag ) {
331 $output .= $pendingPTag .
'<br />';
332 $pendingPTag =
false;
333 $this->lastSection =
'p';
335 if ( $this->lastSection !==
'p' ) {
337 $this->lastSection =
'';
338 $pendingPTag =
'<p>';
340 $pendingPTag =
'</p><p>';
344 if ( $pendingPTag ) {
346 $pendingPTag =
false;
347 $this->lastSection =
'p';
348 } elseif ( $this->lastSection !==
'p' ) {
350 $this->lastSection =
'p';
356 # somewhere above we forget to get out of pre block (T2785)
357 if ( $preCloseMatch && $this->inPre ) {
358 $this->inPre =
false;
360 if ( $pendingPTag ===
false ) {
362 if ( $prefixLength === 0 ) {
367 while ( $prefixLength ) {
370 if ( !$prefixLength ) {
374 if ( $this->lastSection !==
'' ) {
375 $output .=
'</' . $this->lastSection .
'>';
376 $this->lastSection =
'';
393 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
398 if ( $m[0][0] ===
':' ) {
399 # Easy; no tag nesting to worry about
400 $colonPos = $m[0][1];
401 $before = substr( $str, 0, $colonPos );
402 $after = substr( $str, $colonPos + 1 );
406 # Ugly state machine to walk through avoiding tags.
410 $len = strlen( $str );
411 for ( $i = $m[0][1]; $i < $len; $i++ ) {
418 # Could be either a <start> tag or an </end> tag
422 if ( $ltLevel === 0 ) {
424 $before = substr( $str, 0, $i );
425 $after = substr( $str, $i + 1 );
428 # Embedded in a tag; don't break it.
431 # Skip ahead looking for something interesting
432 if ( !preg_match(
'/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
433 # Nothing else interesting
436 if ( $m[0][0] ===
'-{' ) {
441 # Skip ahead to next interesting character.
448 # In language converter markup -{ ... }-
449 if ( !preg_match(
'/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
450 # Nothing else interesting to find; abort!
451 # We're nested in language converter markup, but there
452 # are no close tags left. Abort!
454 } elseif ( $m[0][0] ===
'-{' ) {
457 } elseif ( $m[0][0] ===
'}-' ) {
460 if ( $lcLevel === 0 ) {
473 # Slash may be followed by >?
489 # Illegal early close? This shouldn't happen D:
499 if ( $ltLevel > 0 ) {
502 # ignore the excess close tag, but keep looking for
503 # colons. (This matches Parsoid behavior.)
504 wfDebug( __METHOD__ .
": Invalid input; too many close tags\n" );
511 # Yes, a self-closed tag <blah/>
514 # Probably we're jumping the gun, and this is an attribute
538 throw new MWException(
"State machine error in " . __METHOD__ );
541 if ( $ltLevel > 0 || $lcLevel > 0 ) {
543 __METHOD__ .
": Invalid input; not enough close tags " .
544 "(level $ltLevel/$lcLevel, state $state)\n"