RFC T157418: Trim whitespace in table cells, list items, headings
[lhc/web/wiklou.git] / includes / parser / BlockLevelPass.php
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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Parser
24 */
25 class BlockLevelPass {
26 private $DTopen = false;
27 private $inPre = false;
28 private $lastSection = '';
29 private $lineStart;
30 private $text;
31
32 # State constants for the definition list colon extraction
33 const COLON_STATE_TEXT = 0;
34 const COLON_STATE_TAG = 1;
35 const COLON_STATE_TAGSTART = 2;
36 const COLON_STATE_CLOSETAG = 3;
37 const COLON_STATE_TAGSLASH = 4;
38 const COLON_STATE_COMMENT = 5;
39 const COLON_STATE_COMMENTDASH = 6;
40 const COLON_STATE_COMMENTDASHDASH = 7;
41 const COLON_STATE_LC = 8;
42
43 /**
44 * Make lists from lines starting with ':', '*', '#', etc.
45 *
46 * @param string $text
47 * @param bool $lineStart Whether or not this is at the start of a line.
48 * @return string The lists rendered as HTML
49 */
50 public static function doBlockLevels( $text, $lineStart ) {
51 $pass = new self( $text, $lineStart );
52 return $pass->execute();
53 }
54
55 /**
56 * Private constructor
57 */
58 private function __construct( $text, $lineStart ) {
59 $this->text = $text;
60 $this->lineStart = $lineStart;
61 }
62
63 /**
64 * If a pre or p is open, return the corresponding close tag and update
65 * the state. If no tag is open, return an empty string.
66 * @return string
67 */
68 private function closeParagraph() {
69 $result = '';
70 if ( $this->lastSection !== '' ) {
71 $result = '</' . $this->lastSection . ">\n";
72 }
73 $this->inPre = false;
74 $this->lastSection = '';
75 return $result;
76 }
77
78 /**
79 * getCommon() returns the length of the longest common substring
80 * of both arguments, starting at the beginning of both.
81 *
82 * @param string $st1
83 * @param string $st2
84 *
85 * @return int
86 */
87 private function getCommon( $st1, $st2 ) {
88 $shorter = min( strlen( $st1 ), strlen( $st2 ) );
89
90 for ( $i = 0; $i < $shorter; ++$i ) {
91 if ( $st1[$i] !== $st2[$i] ) {
92 break;
93 }
94 }
95 return $i;
96 }
97
98 /**
99 * Open the list item element identified by the prefix character.
100 *
101 * @param string $char
102 *
103 * @return string
104 */
105 private function openList( $char ) {
106 $result = $this->closeParagraph();
107
108 if ( '*' === $char ) {
109 $result .= "<ul><li>";
110 } elseif ( '#' === $char ) {
111 $result .= "<ol><li>";
112 } elseif ( ':' === $char ) {
113 $result .= "<dl><dd>";
114 } elseif ( ';' === $char ) {
115 $result .= "<dl><dt>";
116 $this->DTopen = true;
117 } else {
118 $result = '<!-- ERR 1 -->';
119 }
120
121 return $result;
122 }
123
124 /**
125 * Close the current list item and open the next one.
126 * @param string $char
127 *
128 * @return string
129 */
130 private function nextItem( $char ) {
131 if ( '*' === $char || '#' === $char ) {
132 return "</li>\n<li>";
133 } elseif ( ':' === $char || ';' === $char ) {
134 $close = "</dd>\n";
135 if ( $this->DTopen ) {
136 $close = "</dt>\n";
137 }
138 if ( ';' === $char ) {
139 $this->DTopen = true;
140 return $close . '<dt>';
141 } else {
142 $this->DTopen = false;
143 return $close . '<dd>';
144 }
145 }
146 return '<!-- ERR 2 -->';
147 }
148
149 /**
150 * Close the current list item identified by the prefix character.
151 * @param string $char
152 *
153 * @return string
154 */
155 private function closeList( $char ) {
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>";
164 } else {
165 $text = "</dd></dl>";
166 }
167 } else {
168 return '<!-- ERR 3 -->';
169 }
170 return $text;
171 }
172
173 /**
174 * Execute the pass.
175 * @return string
176 */
177 private function execute() {
178 $text = $this->text;
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.
182 $textLines = StringUtils::explode( "\n", $text );
183
184 $lastPrefix = $output = '';
185 $this->DTopen = $inBlockElem = false;
186 $prefixLength = 0;
187 $pendingPTag = false;
188 $inBlockquote = false;
189
190 foreach ( $textLines as $inputLine ) {
191 # Fix up $lineStart
192 if ( !$this->lineStart ) {
193 $output .= $inputLine;
194 $this->lineStart = true;
195 continue;
196 }
197 # * = ul
198 # # = ol
199 # ; = dt
200 # : = dd
201
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 );
210
211 # eh?
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
214 # elements.
215 $prefix2 = str_replace( ';', ':', $prefix );
216 $t = substr( $inputLine, $prefixLength );
217 $this->inPre = (bool)$preOpenMatch;
218 } else {
219 # Don't interpret any other prefixes in preformatted text
220 $prefixLength = 0;
221 $prefix = $prefix2 = '';
222 $t = $inputLine;
223 }
224
225 # List generation
226 if ( $prefixLength && $lastPrefix === $prefix2 ) {
227 # Same as the last item, so no need to deal with nesting or opening stuff
228 $output .= $this->nextItem( substr( $prefix, -1 ) );
229 $pendingPTag = false;
230
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.
236 $term = $t2 = '';
237 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
238 $t = $t2;
239 // Trim whitespace in list items
240 $output .= trim( $term ) . $this->nextItem( ':' );
241 }
242 }
243 } elseif ( $prefixLength || $lastPrefixLength ) {
244 # We need to open or close prefixes, or both.
245
246 # Either open or close a level...
247 $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
248 $pendingPTag = false;
249
250 # Close all the prefixes which aren't shared.
251 while ( $commonPrefixLength < $lastPrefixLength ) {
252 $output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
253 --$lastPrefixLength;
254 }
255
256 # Continue the current prefix if appropriate.
257 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
258 $output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
259 }
260
261 # Close an open <dt> if we have a <dd> (":") starting on this line
262 if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] === ':' ) {
263 $output .= $this->nextItem( ':' );
264 }
265
266 # Open prefixes where appropriate.
267 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
268 $output .= "\n";
269 }
270 while ( $prefixLength > $commonPrefixLength ) {
271 $char = $prefix[$commonPrefixLength];
272 $output .= $this->openList( $char );
273
274 if ( ';' === $char ) {
275 # @todo FIXME: This is dupe of code above
276 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
277 $t = $t2;
278 // Trim whitespace in list items
279 $output .= trim( $term ) . $this->nextItem( ':' );
280 }
281 }
282 ++$commonPrefixLength;
283 }
284 if ( !$prefixLength && $lastPrefix ) {
285 $output .= "\n";
286 }
287 $lastPrefix = $prefix2;
288 }
289
290 # If we have no prefixes, go to paragraph mode.
291 if ( 0 == $prefixLength ) {
292 # No prefix (not in list)--go to paragraph mode
293 # @todo consider using a stack for nestable elements like span, table and div
294 $openMatch = preg_match(
295 '/(?:<table|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|'
296 . '<p|<ul|<ol|<dl|<li|<\\/tr|<\\/td|<\\/th)\\b/iS',
297 $t
298 );
299 $closeMatch = preg_match(
300 '/(?:<\\/table|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'
301 . '<td|<th|<\\/?blockquote|<\\/?div|<hr|<\\/pre|<\\/p|<\\/mw:|'
302 . Parser::MARKER_PREFIX
303 . '-pre|<\\/li|<\\/ul|<\\/ol|<\\/dl|<\\/?center)\\b/iS',
304 $t
305 );
306
307 if ( $openMatch || $closeMatch ) {
308 $pendingPTag = false;
309 # @todo T7718: paragraph closed
310 $output .= $this->closeParagraph();
311 if ( $preOpenMatch && !$preCloseMatch ) {
312 $this->inPre = true;
313 }
314 $bqOffset = 0;
315 while ( preg_match( '/<(\\/?)blockquote[\s>]/i', $t,
316 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
317 ) {
318 $inBlockquote = !$bqMatch[1][0]; // is this a close tag?
319 $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
320 }
321 $inBlockElem = !$closeMatch;
322 } elseif ( !$inBlockElem && !$this->inPre ) {
323 if ( ' ' == substr( $t, 0, 1 )
324 && ( $this->lastSection === 'pre' || trim( $t ) != '' )
325 && !$inBlockquote
326 ) {
327 # pre
328 if ( $this->lastSection !== 'pre' ) {
329 $pendingPTag = false;
330 $output .= $this->closeParagraph() . '<pre>';
331 $this->lastSection = 'pre';
332 }
333 $t = substr( $t, 1 );
334 } elseif ( preg_match( '/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS', $t ) ) {
335 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
336 # But it should clear $pendingPTag.
337 if ( $pendingPTag ) {
338 $output .= $this->closeParagraph();
339 $pendingPTag = false;
340 $this->lastSection = '';
341 }
342 } else {
343 # paragraph
344 if ( trim( $t ) === '' ) {
345 if ( $pendingPTag ) {
346 $output .= $pendingPTag . '<br />';
347 $pendingPTag = false;
348 $this->lastSection = 'p';
349 } else {
350 if ( $this->lastSection !== 'p' ) {
351 $output .= $this->closeParagraph();
352 $this->lastSection = '';
353 $pendingPTag = '<p>';
354 } else {
355 $pendingPTag = '</p><p>';
356 }
357 }
358 } else {
359 if ( $pendingPTag ) {
360 $output .= $pendingPTag;
361 $pendingPTag = false;
362 $this->lastSection = 'p';
363 } elseif ( $this->lastSection !== 'p' ) {
364 $output .= $this->closeParagraph() . '<p>';
365 $this->lastSection = 'p';
366 }
367 }
368 }
369 }
370 }
371 # somewhere above we forget to get out of pre block (T2785)
372 if ( $preCloseMatch && $this->inPre ) {
373 $this->inPre = false;
374 }
375 if ( $pendingPTag === false ) {
376 if ( $prefixLength === 0 ) {
377 $output .= $t;
378 $output .= "\n";
379 } else {
380 // Trim whitespace in list items
381 $output .= trim( $t );
382 }
383 }
384 }
385 while ( $prefixLength ) {
386 $output .= $this->closeList( $prefix2[$prefixLength - 1] );
387 --$prefixLength;
388 if ( !$prefixLength ) {
389 $output .= "\n";
390 }
391 }
392 if ( $this->lastSection !== '' ) {
393 $output .= '</' . $this->lastSection . '>';
394 $this->lastSection = '';
395 }
396
397 return $output;
398 }
399
400 /**
401 * Split up a string on ':', ignoring any occurrences inside tags
402 * to prevent illegal overlapping.
403 *
404 * @param string $str The string to split
405 * @param string &$before Set to everything before the ':'
406 * @param string &$after Set to everything after the ':'
407 * @throws MWException
408 * @return string The position of the ':', or false if none found
409 */
410 private function findColonNoLinks( $str, &$before, &$after ) {
411 if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
412 # Nothing to find!
413 return false;
414 }
415
416 if ( $m[0][0] === ':' ) {
417 # Easy; no tag nesting to worry about
418 $colonPos = $m[0][1];
419 $before = substr( $str, 0, $colonPos );
420 $after = substr( $str, $colonPos + 1 );
421 return $colonPos;
422 }
423
424 # Ugly state machine to walk through avoiding tags.
425 $state = self::COLON_STATE_TEXT;
426 $ltLevel = 0;
427 $lcLevel = 0;
428 $len = strlen( $str );
429 for ( $i = $m[0][1]; $i < $len; $i++ ) {
430 $c = $str[$i];
431
432 switch ( $state ) {
433 case self::COLON_STATE_TEXT:
434 switch ( $c ) {
435 case "<":
436 # Could be either a <start> tag or an </end> tag
437 $state = self::COLON_STATE_TAGSTART;
438 break;
439 case ":":
440 if ( $ltLevel === 0 ) {
441 # We found it!
442 $before = substr( $str, 0, $i );
443 $after = substr( $str, $i + 1 );
444 return $i;
445 }
446 # Embedded in a tag; don't break it.
447 break;
448 default:
449 # Skip ahead looking for something interesting
450 if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
451 # Nothing else interesting
452 return false;
453 }
454 if ( $m[0][0] === '-{' ) {
455 $state = self::COLON_STATE_LC;
456 $lcLevel++;
457 $i = $m[0][1] + 1;
458 } else {
459 # Skip ahead to next interesting character.
460 $i = $m[0][1] - 1;
461 }
462 break;
463 }
464 break;
465 case self::COLON_STATE_LC:
466 # In language converter markup -{ ... }-
467 if ( !preg_match( '/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
468 # Nothing else interesting to find; abort!
469 # We're nested in language converter markup, but there
470 # are no close tags left. Abort!
471 break 2;
472 } elseif ( $m[0][0] === '-{' ) {
473 $i = $m[0][1] + 1;
474 $lcLevel++;
475 } elseif ( $m[0][0] === '}-' ) {
476 $i = $m[0][1] + 1;
477 $lcLevel--;
478 if ( $lcLevel === 0 ) {
479 $state = self::COLON_STATE_TEXT;
480 }
481 }
482 break;
483 case self::COLON_STATE_TAG:
484 # In a <tag>
485 switch ( $c ) {
486 case ">":
487 $ltLevel++;
488 $state = self::COLON_STATE_TEXT;
489 break;
490 case "/":
491 # Slash may be followed by >?
492 $state = self::COLON_STATE_TAGSLASH;
493 break;
494 default:
495 # ignore
496 }
497 break;
498 case self::COLON_STATE_TAGSTART:
499 switch ( $c ) {
500 case "/":
501 $state = self::COLON_STATE_CLOSETAG;
502 break;
503 case "!":
504 $state = self::COLON_STATE_COMMENT;
505 break;
506 case ">":
507 # Illegal early close? This shouldn't happen D:
508 $state = self::COLON_STATE_TEXT;
509 break;
510 default:
511 $state = self::COLON_STATE_TAG;
512 }
513 break;
514 case self::COLON_STATE_CLOSETAG:
515 # In a </tag>
516 if ( $c === ">" ) {
517 if ( $ltLevel > 0 ) {
518 $ltLevel--;
519 } else {
520 # ignore the excess close tag, but keep looking for
521 # colons. (This matches Parsoid behavior.)
522 wfDebug( __METHOD__ . ": Invalid input; too many close tags\n" );
523 }
524 $state = self::COLON_STATE_TEXT;
525 }
526 break;
527 case self::COLON_STATE_TAGSLASH:
528 if ( $c === ">" ) {
529 # Yes, a self-closed tag <blah/>
530 $state = self::COLON_STATE_TEXT;
531 } else {
532 # Probably we're jumping the gun, and this is an attribute
533 $state = self::COLON_STATE_TAG;
534 }
535 break;
536 case self::COLON_STATE_COMMENT:
537 if ( $c === "-" ) {
538 $state = self::COLON_STATE_COMMENTDASH;
539 }
540 break;
541 case self::COLON_STATE_COMMENTDASH:
542 if ( $c === "-" ) {
543 $state = self::COLON_STATE_COMMENTDASHDASH;
544 } else {
545 $state = self::COLON_STATE_COMMENT;
546 }
547 break;
548 case self::COLON_STATE_COMMENTDASHDASH:
549 if ( $c === ">" ) {
550 $state = self::COLON_STATE_TEXT;
551 } else {
552 $state = self::COLON_STATE_COMMENT;
553 }
554 break;
555 default:
556 throw new MWException( "State machine error in " . __METHOD__ );
557 }
558 }
559 if ( $ltLevel > 0 || $lcLevel > 0 ) {
560 wfDebug(
561 __METHOD__ . ": Invalid input; not enough close tags " .
562 "(level $ltLevel/$lcLevel, state $state)\n"
563 );
564 return false;
565 }
566 return false;
567 }
568 }