Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.86% |
278 / 290 |
|
50.00% |
5 / 10 |
CRAP | |
0.00% |
0 / 1 |
| BlockLevelPass | |
96.19% |
278 / 289 |
|
50.00% |
5 / 10 |
123 | |
0.00% |
0 / 1 |
| doBlockLevels | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| hasOpenParagraph | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| closeParagraph | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getCommon | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| openList | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
5.01 | |||
| nextItem | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
7.03 | |||
| closeList | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |||
| execute | |
97.04% |
131 / 135 |
|
0.00% |
0 / 1 |
54 | |||
| findColonNoLinks | |
96.04% |
97 / 101 |
|
0.00% |
0 / 1 |
43 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This is the part of the wikitext parser which handles automatic paragraphs |
| 5 | * and conversion of start-of-line prefixes to HTML lists. |
| 6 | * |
| 7 | * @license GPL-2.0-or-later |
| 8 | * @file |
| 9 | * @ingroup Parser |
| 10 | * @internal |
| 11 | */ |
| 12 | |
| 13 | namespace MediaWiki\Parser; |
| 14 | |
| 15 | use LogicException; |
| 16 | use Wikimedia\RemexHtml\HTMLData; |
| 17 | use Wikimedia\StringUtils\StringUtils; |
| 18 | |
| 19 | class BlockLevelPass { |
| 20 | /** @var bool */ |
| 21 | private $DTopen = false; |
| 22 | /** @var bool */ |
| 23 | private $inPre = false; |
| 24 | /** @var string */ |
| 25 | private $lastParagraph = ''; |
| 26 | /** @var bool */ |
| 27 | private $lineStart; |
| 28 | /** @var string */ |
| 29 | private $text; |
| 30 | |
| 31 | # State constants for the definition list colon extraction |
| 32 | private const COLON_STATE_TEXT = 0; |
| 33 | private const COLON_STATE_TAG = 1; |
| 34 | private const COLON_STATE_TAGSTART = 2; |
| 35 | private const COLON_STATE_CLOSETAG = 3; |
| 36 | private const COLON_STATE_TAGSLASH = 4; |
| 37 | private const COLON_STATE_COMMENT = 5; |
| 38 | private const COLON_STATE_COMMENTDASH = 6; |
| 39 | private const COLON_STATE_COMMENTDASHDASH = 7; |
| 40 | private const COLON_STATE_LC = 8; |
| 41 | |
| 42 | /** |
| 43 | * Make lists from lines starting with ':', '*', '#', etc. |
| 44 | * |
| 45 | * @param string $text |
| 46 | * @param bool $lineStart Whether or not this is at the start of a line. |
| 47 | * @return string The lists rendered as HTML |
| 48 | * @internal |
| 49 | */ |
| 50 | public static function doBlockLevels( $text, $lineStart ) { |
| 51 | $pass = new self( $text, $lineStart ); |
| 52 | return $pass->execute(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param string $text |
| 57 | * @param bool $lineStart |
| 58 | */ |
| 59 | private function __construct( $text, $lineStart ) { |
| 60 | $this->text = $text; |
| 61 | $this->lineStart = $lineStart; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @return bool |
| 66 | */ |
| 67 | private function hasOpenParagraph() { |
| 68 | return $this->lastParagraph !== ''; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * If a pre or p is open, return the corresponding close tag and update |
| 73 | * the state. If no tag is open, return an empty string. |
| 74 | * @param bool $atTheEnd Omit trailing newline if we've reached the end. |
| 75 | * @return string |
| 76 | */ |
| 77 | private function closeParagraph( $atTheEnd = false ) { |
| 78 | $result = ''; |
| 79 | if ( $this->hasOpenParagraph() ) { |
| 80 | $result = '</' . $this->lastParagraph . '>'; |
| 81 | if ( !$atTheEnd ) { |
| 82 | $result .= "\n"; |
| 83 | } |
| 84 | } |
| 85 | $this->inPre = false; |
| 86 | $this->lastParagraph = ''; |
| 87 | return $result; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * getCommon() returns the length of the longest common substring |
| 92 | * of both arguments, starting at the beginning of both. |
| 93 | * |
| 94 | * @param string $st1 |
| 95 | * @param string $st2 |
| 96 | * |
| 97 | * @return int |
| 98 | */ |
| 99 | private function getCommon( $st1, $st2 ) { |
| 100 | $shorter = min( strlen( $st1 ), strlen( $st2 ) ); |
| 101 | |
| 102 | for ( $i = 0; $i < $shorter; ++$i ) { |
| 103 | if ( $st1[$i] !== $st2[$i] ) { |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | return $i; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Open the list item element identified by the prefix character. |
| 112 | * |
| 113 | * @param string $char |
| 114 | * |
| 115 | * @return string |
| 116 | */ |
| 117 | private function openList( $char ) { |
| 118 | $result = $this->closeParagraph(); |
| 119 | |
| 120 | if ( $char === '*' ) { |
| 121 | $result .= "<ul><li>"; |
| 122 | } elseif ( $char === '#' ) { |
| 123 | $result .= "<ol><li>"; |
| 124 | } elseif ( $char === ':' ) { |
| 125 | $result .= "<dl><dd>"; |
| 126 | } elseif ( $char === ';' ) { |
| 127 | $result .= "<dl><dt>"; |
| 128 | $this->DTopen = true; |
| 129 | } else { |
| 130 | $result = '<!-- ERR 1 -->'; |
| 131 | } |
| 132 | |