MediaWiki  master
BlockLevelPass.php
Go to the documentation of this file.
1 <?php
2 
27  private $DTopen = false;
28  private $inPre = false;
29  private $lastParagraph = '';
30  private $lineStart;
31  private $text;
32 
33  # State constants for the definition list colon extraction
34  private const COLON_STATE_TEXT = 0;
35  private const COLON_STATE_TAG = 1;
36  private const COLON_STATE_TAGSTART = 2;
37  private const COLON_STATE_CLOSETAG = 3;
38  private const COLON_STATE_TAGSLASH = 4;
39  private const COLON_STATE_COMMENT = 5;
40  private const COLON_STATE_COMMENTDASH = 6;
41  private const COLON_STATE_COMMENTDASHDASH = 7;
42  private const COLON_STATE_LC = 8;
43 
52  public static function doBlockLevels( $text, $lineStart ) {
53  $pass = new self( $text, $lineStart );
54  return $pass->execute();
55  }
56 
61  private function __construct( $text, $lineStart ) {
62  $this->text = $text;
63  $this->lineStart = $lineStart;
64  }
65 
69  private function hasOpenParagraph() {
70  return $this->lastParagraph !== '';
71  }
72 
79  private function closeParagraph( $atTheEnd = false ) {
80  $result = '';
81  if ( $this->hasOpenParagraph() ) {
82  $result = '</' . $this->lastParagraph . '>';
83  if ( !$atTheEnd ) {
84  $result .= "\n";
85  }
86  }
87  $this->inPre = false;
88  $this->lastParagraph = '';
89  return $result;
90  }
91 
101  private function getCommon( $st1, $st2 ) {
102  $shorter = min( strlen( $st1 ), strlen( $st2 ) );
103 
104  for ( $i = 0; $i < $shorter; ++$i ) {
105  if ( $st1[$i] !== $st2[$i] ) {
106  break;
107  }
108  }
109  return $i;
110  }
111 
119  private function openList( $char ) {
120  $result = $this->closeParagraph();
121 
122  if ( $char === '*' ) {
123  $result .= "<ul><li>";
124  } elseif ( $char === '#' ) {
125  $result .= "<ol><li>";
126  } elseif ( $char === ':' ) {
127  $result .= "<dl><dd>";
128  } elseif ( $char === ';' ) {
129  $result .= "<dl><dt>";
130  $this->DTopen = true;
131  } else {
132  $result = '<!-- ERR 1 -->';
133  }
134 
135  return $result;
136  }
137 
144  private function nextItem( $char ) {
145  if ( $char === '*' || $char === '#' ) {
146  return "</li>\n<li>";
147  } elseif ( $char === ':' || $char === ';' ) {
148  $close = "</dd>\n";
149  if ( $this->DTopen ) {
150  $close = "</dt>\n";
151  }
152  if ( $char === ';' ) {
153  $this->DTopen = true;
154  return $close . '<dt>';
155  } else {
156  $this->DTopen = false;
157  return $close . '<dd>';
158  }
159  }
160  return '<!-- ERR 2 -->';
161  }
162 
169  private function closeList( $char ) {
170  if ( $char === '*' ) {
171  $text = "</li></ul>";
172  } elseif ( $char === '#' ) {
173  $text = "</li></ol>";
174  } elseif ( $char === ':' ) {
175  if ( $this->DTopen ) {
176  $this->DTopen = false;
177  $text = "</dt></dl>";
178  } else {
179  $text = "</dd></dl>";
180  }
181  } else {
182  return '<!-- ERR 3 -->';
183  }
184  return $text;
185  }
186 
191  private function execute() {
192  $text = $this->text;
193  # Parsing through the text line by line. The main thing
194  # happening here is handling of block-level elements p, pre,
195  # and making lists from lines starting with * # : etc.
196  $textLines = StringUtils::explode( "\n", $text );
197 
198  $lastPrefix = $output = '';
199  $this->DTopen = $inBlockElem = false;
200  $prefixLength = 0;
201  $pendingPTag = false;
202  $inBlockquote = false;
203 
204  for ( $textLines->rewind(); $textLines->valid(); ) {
205  $inputLine = $textLines->current();
206  $textLines->next();
207  $notLastLine = $textLines->valid();
208 
209  # Fix up $lineStart
210  if ( !$this->lineStart ) {
211  $output .= $inputLine;
212  $this->lineStart = true;
213  continue;
214  }
215  # * = ul
216  # # = ol
217  # ; = dt
218  # : = dd
219 
220  $lastPrefixLength = strlen( $lastPrefix );
221  $preCloseMatch = preg_match( '/<\\/pre/i', $inputLine );
222  $preOpenMatch = preg_match( '/<pre/i', $inputLine );
223  # If not in a <pre> element, scan for and figure out what prefixes are there.
224  if ( !$this->inPre ) {
225  # Multiple prefixes may abut each other for nested lists.
226  $prefixLength = strspn( $inputLine, '*#:;' );
227  $prefix = substr( $inputLine, 0, $prefixLength );
228 
229  # eh?
230  # ; and : are both from definition-lists, so they're equivalent
231  # for the purposes of determining whether or not we need to open/close
232  # elements.
233  $prefix2 = str_replace( ';', ':', $prefix );
234  $t = substr( $inputLine, $prefixLength );
235  $this->inPre = (bool)$preOpenMatch;
236  } else {
237  # Don't interpret any other prefixes in preformatted text
238  $prefixLength = 0;
239  $prefix = $prefix2 = '';
240  $t = $inputLine;
241  }
242 
243  # List generation
244  if ( $prefixLength && $lastPrefix === $prefix2 ) {
245  # Same as the last item, so no need to deal with nesting or opening stuff
246  $output .= $this->nextItem( substr( $prefix, -1 ) );
247  $pendingPTag = false;
248 
249  if ( substr( $prefix, -1 ) === ';' ) {
250  # The one nasty exception: definition lists work like this:
251  # ; title : definition text
252  # So we check for : in the remainder text to split up the
253  # title and definition, without b0rking links.
254  $term = $t2 = '';
255  if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
256  $t = $t2;
257  // Trim whitespace in list items
258  $output .= trim( $term ) . $this->nextItem( ':' );
259  }
260  }
261  } elseif ( $prefixLength || $lastPrefixLength ) {
262  # We need to open or close prefixes, or both.
263 
264  # Either open or close a level...
265  $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
266  $pendingPTag = false;
267 
268  # Close all the prefixes which aren't shared.
269  while ( $commonPrefixLength < $lastPrefixLength ) {
270  // @phan-suppress-next-line PhanTypeInvalidDimOffset
271  $output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
272  --$lastPrefixLength;
273  }
274 
275  # Continue the current prefix if appropriate.
276  if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
277  $output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
278  }
279 
280  # Close an open <dt> if we have a <dd> (":") starting on this line
281  if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] === ':' ) {
282  $output .= $this->nextItem( ':' );
283  }
284 
285  # Open prefixes where appropriate.
286  if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
287  $output .= "\n";
288  }
289  while ( $prefixLength > $commonPrefixLength ) {
290  $char = $prefix[$commonPrefixLength];
291  $output .= $this->openList( $char );
292 
293  if ( $char === ';' ) {
294  # @todo FIXME: This is dupe of code above
295  if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
296  $t = $t2;
297  // Trim whitespace in list items
298  $output .= trim( $term ) . $this->nextItem( ':' );
299  }
300  }
301  ++$commonPrefixLength;
302  }
303  if ( !$prefixLength && $lastPrefix ) {
304  $output .= "\n";
305  }
306  $lastPrefix = $prefix2;
307  }
308 
309  # If we have no prefixes, go to paragraph mode.
310  if ( $prefixLength == 0 ) {
311  # No prefix (not in list)--go to paragraph mode
312  # @todo consider using a stack for nestable elements like span, table and div
313 
314  // P-wrapping and indent-pre are suppressed inside, not outside
315  $blockElems = 'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl';
316  // P-wrapping and indent-pre are suppressed outside, not inside
317  $antiBlockElems = 'td|th';
318 
319  $openMatch = preg_match(
320  '/<('
321  . "({$blockElems})|\\/({$antiBlockElems})|"
322  // Always suppresses
323  . '\\/?(tr|caption|dt|dd|li)'
324  . ')\\b/iS',
325  $t
326  );
327  $closeMatch = preg_match(
328  '/<('
329  . "\\/({$blockElems})|({$antiBlockElems})|"
330  // Never suppresses
331  . '\\/?(center|blockquote|div|hr|mw:)'
332  . ')\\b/iS',
333  $t
334  );
335 
336  // Any match closes the paragraph, but only when `!$closeMatch`
337  // do we enter block mode. The oddities with table rows and
338  // cells are to avoid paragraph wrapping in interstitial spaces
339  // leading to fostered content.
340 
341  if ( $openMatch || $closeMatch ) {
342  $pendingPTag = false;
343  // Only close the paragraph if we're not inside a <pre> tag, or if
344  // that <pre> tag has just been opened
345  if ( !$this->inPre || $preOpenMatch ) {
346  // @todo T7718: paragraph closed
347  $output .= $this->closeParagraph();
348  }
349  if ( $preOpenMatch && !$preCloseMatch ) {
350  $this->inPre = true;
351  }
352  $bqOffset = 0;
353  while ( preg_match( '/<(\\/?)blockquote[\s>]/i', $t,
354  $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
355  ) {
356  $inBlockquote = !$bqMatch[1][0]; // is this a close tag?
357  $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
358  }
359  $inBlockElem = !$closeMatch;
360  } elseif ( !$inBlockElem && !$this->inPre ) {
361  if ( substr( $t, 0, 1 ) == ' '
362  && ( $this->lastParagraph === 'pre' || trim( $t ) != '' )
363  && !$inBlockquote
364  ) {
365  # pre
366  if ( $this->lastParagraph !== 'pre' ) {
367  $pendingPTag = false;
368  $output .= $this->closeParagraph() . '<pre>';
369  $this->lastParagraph = 'pre';
370  }
371  $t = substr( $t, 1 );
372  } elseif ( preg_match( '/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS', $t ) ) {
373  # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
374  # But it should clear $pendingPTag.
375  if ( $pendingPTag ) {
376  $output .= $this->closeParagraph();
377  $pendingPTag = false;
378  }
379  } else {
380  # paragraph
381  if ( trim( $t ) === '' ) {
382  if ( $pendingPTag ) {
383  $output .= $pendingPTag . '<br />';
384  $pendingPTag = false;
385  $this->lastParagraph = 'p';
386  } elseif ( $this->lastParagraph !== 'p' ) {
387  $output .= $this->closeParagraph();
388  $pendingPTag = '<p>';
389  } else {
390  $pendingPTag = '</p><p>';
391  }
392  } elseif ( $pendingPTag ) {
393  $output .= $pendingPTag;
394  $pendingPTag = false;
395  $this->lastParagraph = 'p';
396  } elseif ( $this->lastParagraph !== 'p' ) {
397  $output .= $this->closeParagraph() . '<p>';
398  $this->lastParagraph = 'p';
399  }
400  }
401  }
402  }
403  # somewhere above we forget to get out of pre block (T2785)
404  if ( $preCloseMatch && $this->inPre ) {
405  $this->inPre = false;
406  }
407  if ( $pendingPTag === false ) {
408  if ( $prefixLength === 0 ) {
409  $output .= $t;
410  // Add a newline if there's an open paragraph
411  // or we've yet to reach the last line.
412  if ( $notLastLine || $this->hasOpenParagraph() ) {
413  $output .= "\n";
414  }
415  } else {
416  // Trim whitespace in list items
417  $output .= trim( $t );
418  }
419  }
420  }
421  while ( $prefixLength ) {
422  $output .= $this->closeList( $prefix2[$prefixLength - 1] );
423  --$prefixLength;
424  // Note that a paragraph is only ever opened when `prefixLength`
425  // is zero, but we'll choose to be overly cautious.
426  if ( !$prefixLength && $this->hasOpenParagraph() ) {
427  $output .= "\n";
428  }
429  }
430  $output .= $this->closeParagraph( true );
431  return $output;
432  }
433 
444  private function findColonNoLinks( $str, &$before, &$after ) {
445  if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
446  # Nothing to find!
447  return false;
448  }
449 
450  if ( $m[0][0] === ':' ) {
451  # Easy; no tag nesting to worry about
452  $colonPos = $m[0][1];
453  $before = substr( $str, 0, $colonPos );
454  $after = substr( $str, $colonPos + 1 );
455  return $colonPos;
456  }
457 
458  # Ugly state machine to walk through avoiding tags.
459  $state = self::COLON_STATE_TEXT;
460  $ltLevel = 0;
461  $lcLevel = 0;
462  $len = strlen( $str );
463  for ( $i = $m[0][1]; $i < $len; $i++ ) {
464  $c = $str[$i];
465 
466  switch ( $state ) {
468  switch ( $c ) {
469  case "<":
470  # Could be either a <start> tag or an </end> tag
472  break;
473  case ":":
474  if ( $ltLevel === 0 ) {
475  # We found it!
476  $before = substr( $str, 0, $i );
477  $after = substr( $str, $i + 1 );
478  return $i;
479  }
480  # Embedded in a tag; don't break it.
481  break;
482  default:
483  # Skip ahead looking for something interesting
484  if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
485  # Nothing else interesting
486  return false;
487  }
488  if ( $m[0][0] === '-{' ) {
489  $state = self::COLON_STATE_LC;
490  $lcLevel++;
491  $i = $m[0][1] + 1;
492  } else {
493  # Skip ahead to next interesting character.
494  $i = $m[0][1] - 1;
495  }
496  break;
497  }
498  break;
500  # In language converter markup -{ ... }-
501  if ( !preg_match( '/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
502  # Nothing else interesting to find; abort!
503  # We're nested in language converter markup, but there
504  # are no close tags left. Abort!
505  break 2;
506  } elseif ( $m[0][0] === '-{' ) {
507  $i = $m[0][1] + 1;
508  $lcLevel++;
509  } elseif ( $m[0][0] === '}-' ) {
510  $i = $m[0][1] + 1;
511  $lcLevel--;
512  if ( $lcLevel === 0 ) {
513  $state = self::COLON_STATE_TEXT;
514  }
515  }
516  break;
518  # In a <tag>
519  switch ( $c ) {
520  case ">":
521  $ltLevel++;
522  $state = self::COLON_STATE_TEXT;
523  break;
524  case "/":
525  # Slash may be followed by >?
527  break;
528  default:
529  # ignore
530  }
531  break;
533  switch ( $c ) {
534  case "/":
536  break;
537  case "!":
538  $state = self::COLON_STATE_COMMENT;
539  break;
540  case ">":
541  # Illegal early close? This shouldn't happen D:
542  $state = self::COLON_STATE_TEXT;
543  break;
544  default:
545  $state = self::COLON_STATE_TAG;
546  }
547  break;
549  # In a </tag>
550  if ( $c === ">" ) {
551  if ( $ltLevel > 0 ) {
552  $ltLevel--;
553  } else {
554  # ignore the excess close tag, but keep looking for
555  # colons. (This matches Parsoid behavior.)
556  wfDebug( __METHOD__ . ": Invalid input; too many close tags" );
557  }
558  $state = self::COLON_STATE_TEXT;
559  }
560  break;
562  if ( $c === ">" ) {
563  # Yes, a self-closed tag <blah/>
564  $state = self::COLON_STATE_TEXT;
565  } else {
566  # Probably we're jumping the gun, and this is an attribute
567  $state = self::COLON_STATE_TAG;
568  }
569  break;
571  if ( $c === "-" ) {
573  }
574  break;
576  if ( $c === "-" ) {
578  } else {
579  $state = self::COLON_STATE_COMMENT;
580  }
581  break;
583  if ( $c === ">" ) {
584  $state = self::COLON_STATE_TEXT;
585  } else {
586  $state = self::COLON_STATE_COMMENT;
587  }
588  break;
589  default:
590  throw new MWException( "State machine error in " . __METHOD__ );
591  }
592  }
593  if ( $ltLevel > 0 || $lcLevel > 0 ) {
594  wfDebug(
595  __METHOD__ . ": Invalid input; not enough close tags " .
596  "(level $ltLevel/$lcLevel, state $state)"
597  );
598  return false;
599  }
600  return false;
601  }
602 }
BlockLevelPass
Definition: BlockLevelPass.php:26
BlockLevelPass\COLON_STATE_COMMENT
const COLON_STATE_COMMENT
Definition: BlockLevelPass.php:39
BlockLevelPass\openList
openList( $char)
Open the list item element identified by the prefix character.
Definition: BlockLevelPass.php:119
BlockLevelPass\COLON_STATE_CLOSETAG
const COLON_STATE_CLOSETAG
Definition: BlockLevelPass.php:37
BlockLevelPass\getCommon
getCommon( $st1, $st2)
getCommon() returns the length of the longest common substring of both arguments, starting at the beg...
Definition: BlockLevelPass.php:101
BlockLevelPass\$lastParagraph
$lastParagraph
Definition: BlockLevelPass.php:29
BlockLevelPass\execute
execute()
Execute the pass.
Definition: BlockLevelPass.php:191
MWException
MediaWiki exception.
Definition: MWException.php:26
BlockLevelPass\$DTopen
$DTopen
Definition: BlockLevelPass.php:27
BlockLevelPass\$lineStart
$lineStart
Definition: BlockLevelPass.php:30
BlockLevelPass\doBlockLevels
static doBlockLevels( $text, $lineStart)
Make lists from lines starting with ':', '*', '#', etc.
Definition: BlockLevelPass.php:52
StringUtils\explode
static explode( $separator, $subject)
Workalike for explode() with limited memory usage.
Definition: StringUtils.php:326
BlockLevelPass\closeList
closeList( $char)
Close the current list item identified by the prefix character.
Definition: BlockLevelPass.php:169
BlockLevelPass\hasOpenParagraph
hasOpenParagraph()
Definition: BlockLevelPass.php:69
BlockLevelPass\$inPre
$inPre
Definition: BlockLevelPass.php:28
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
BlockLevelPass\$text
$text
Definition: BlockLevelPass.php:31
BlockLevelPass\COLON_STATE_TAGSLASH
const COLON_STATE_TAGSLASH
Definition: BlockLevelPass.php:38
BlockLevelPass\COLON_STATE_TAG
const COLON_STATE_TAG
Definition: BlockLevelPass.php:35
BlockLevelPass\__construct
__construct( $text, $lineStart)
Definition: BlockLevelPass.php:61
BlockLevelPass\COLON_STATE_LC
const COLON_STATE_LC
Definition: BlockLevelPass.php:42
BlockLevelPass\findColonNoLinks
findColonNoLinks( $str, &$before, &$after)
Split up a string on ':', ignoring any occurrences inside tags to prevent illegal overlapping.
Definition: BlockLevelPass.php:444
BlockLevelPass\closeParagraph
closeParagraph( $atTheEnd=false)
If a pre or p is open, return the corresponding close tag and update the state.
Definition: BlockLevelPass.php:79
BlockLevelPass\COLON_STATE_TAGSTART
const COLON_STATE_TAGSTART
Definition: BlockLevelPass.php:36
$t
$t
Definition: testCompression.php:74
BlockLevelPass\COLON_STATE_TEXT
const COLON_STATE_TEXT
Definition: BlockLevelPass.php:34
BlockLevelPass\COLON_STATE_COMMENTDASH
const COLON_STATE_COMMENTDASH
Definition: BlockLevelPass.php:40
BlockLevelPass\COLON_STATE_COMMENTDASHDASH
const COLON_STATE_COMMENTDASHDASH
Definition: BlockLevelPass.php:41
BlockLevelPass\nextItem
nextItem( $char)
Close the current list item and open the next one.
Definition: BlockLevelPass.php:144