Merge "parser: Update outdated comment about ImageGallery"
[lhc/web/wiklou.git] / includes / tidy / RaggettWrapper.php
1 <?php
2 namespace MediaWiki\Tidy;
3
4 use ParserOutput;
5 use Parser;
6
7 /**
8 * Class used to hide mw:editsection tokens from Tidy so that it doesn't break them
9 * or break on them. This is a bit of a hack for now, but hopefully in the future
10 * we may create a real postprocessor or something that will replace this.
11 * It's called wrapper because for now it basically takes over MWTidy::tidy's task
12 * of wrapping the text in a xhtml block
13 *
14 * This re-uses some of the parser's UNIQ tricks, though some of it is private so it's
15 * duplicated. Perhaps we should create an abstract marker hiding class.
16 *
17 * @ingroup Parser
18 */
19 class RaggettWrapper {
20
21 /**
22 * @var array
23 */
24 protected $mTokens;
25
26 /**
27 * @var int
28 */
29 protected $mMarkerIndex;
30
31 /**
32 * @param string $text
33 * @return string
34 */
35 public function getWrapped( $text ) {
36 $this->mTokens = [];
37 $this->mMarkerIndex = 0;
38
39 // Replace <mw:editsection> elements with placeholders
40 $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
41 [ $this, 'replaceCallback' ], $text );
42 // ...and <mw:toc> markers
43 $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/',
44 [ $this, 'replaceCallback' ], $wrappedtext );
45 // ... and <math> tags
46 $wrappedtext = preg_replace_callback( '/\<math(.*?)\<\\/math\>/s',
47 [ $this, 'replaceCallback' ], $wrappedtext );
48 // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> so
49 // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
50 $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
51
52 // Preserve empty li elements (T49673) by abusing Tidy's datafld hack
53 // The whitespace class is as in TY_(InitMap)
54 $wrappedtext = preg_replace( "!<li>([ \r\n\t\f]*)</li>!",
55 '<li datafld="" class="mw-empty-elt">\1</li>', $wrappedtext );
56
57 // Wrap the whole thing in a doctype and body for Tidy.
58 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' .
59 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>' .
60 '<head><title>test</title></head><body>' . $wrappedtext . '</body></html>';
61
62 return $wrappedtext;
63 }
64
65 /**
66 * @param array $m
67 * @return string
68 */
69 private function replaceCallback( array $m ) {
70 $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
71 $this->mMarkerIndex++;
72 $this->mTokens[$marker] = $m[0];
73 return $marker;
74 }
75
76 /**
77 * @param string $text
78 * @return string
79 */
80 public function postprocess( $text ) {
81 // Revert <html-{link,meta}> back to <{link,meta}>
82 $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
83
84 // Remove datafld
85 $text = str_replace( '<li datafld=""', '<li', $text );
86
87 // Restore the contents of placeholder tokens
88 $text = strtr( $text, $this->mTokens );
89
90 return $text;
91 }
92
93 }