Merge "Simplify HTMLTitleTextField::validate"
[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 * @deprecated since 1.32
19 */
20 class RaggettWrapper {
21
22 /**
23 * @var array
24 */
25 protected $mTokens;
26
27 /**
28 * @var int
29 */
30 protected $mMarkerIndex;
31
32 /**
33 * @param string $text
34 * @return string
35 */
36 public function getWrapped( $text ) {
37 $this->mTokens = [];
38 $this->mMarkerIndex = 0;
39
40 // Replace <mw:editsection> elements with placeholders
41 $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
42 [ $this, 'replaceCallback' ], $text );
43 // ...and <mw:toc> markers
44 $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/',
45 [ $this, 'replaceCallback' ], $wrappedtext );
46 // ... and <math> tags
47 $wrappedtext = preg_replace_callback( '/\<math(.*?)\<\\/math\>/s',
48 [ $this, 'replaceCallback' ], $wrappedtext );
49 // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> so
50 // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
51 $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
52 // Similar for inline <style> tags, but those aren't empty.
53 $wrappedtext = preg_replace_callback( '!<style([^>]*)>(.*?)</style>!s', function ( $m ) {
54 return '<html-style' . $m[1] . '>'
55 . $this->replaceCallback( [ $m[2] ] )
56 . '</html-style>';
57 }, $wrappedtext );
58
59 // Preserve empty li elements (T49673) by abusing Tidy's datafld hack
60 // The whitespace class is as in TY_(InitMap)
61 $wrappedtext = preg_replace( "!<li>([ \r\n\t\f]*)</li>!",
62 '<li datafld="" class="mw-empty-elt">\1</li>', $wrappedtext );
63
64 // Wrap the whole thing in a doctype and body for Tidy.
65 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' .
66 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>' .
67 '<head><title>test</title></head><body>' . $wrappedtext . '</body></html>';
68
69 return $wrappedtext;
70 }
71
72 /**
73 * @param array $m
74 * @return string
75 */
76 private function replaceCallback( array $m ) {
77 $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
78 $this->mMarkerIndex++;
79 $this->mTokens[$marker] = $m[0];
80 return $marker;
81 }
82
83 /**
84 * @param string $text
85 * @return string
86 */
87 public function postprocess( $text ) {
88 // Revert <html-{link,meta,style}> back to <{link,meta,style}>
89 $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
90 $text = preg_replace( '!<(/?)html-(style)([^>]*)>!', '<$1$2$3>', $text );
91
92 // Remove datafld
93 $text = str_replace( '<li datafld=""', '<li', $text );
94
95 // Restore the contents of placeholder tokens
96 $text = strtr( $text, $this->mTokens );
97
98 return $text;
99 }
100
101 }