]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/tidy/RaggettWrapper.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.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                 // Similar for inline <style> tags, but those aren't empty.
52                 $wrappedtext = preg_replace_callback( '!<style([^>]*)>(.*?)</style>!s', function ( $m ) {
53                         return '<html-style' . $m[1] . '>'
54                                 . $this->replaceCallback( [ $m[2] ] )
55                                 . '</html-style>';
56                 }, $wrappedtext );
57
58                 // Preserve empty li elements (T49673) by abusing Tidy's datafld hack
59                 // The whitespace class is as in TY_(InitMap)
60                 $wrappedtext = preg_replace( "!<li>([ \r\n\t\f]*)</li>!",
61                         '<li datafld="" class="mw-empty-elt">\1</li>', $wrappedtext );
62
63                 // Wrap the whole thing in a doctype and body for Tidy.
64                 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' .
65                         ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>' .
66                         '<head><title>test</title></head><body>' . $wrappedtext . '</body></html>';
67
68                 return $wrappedtext;
69         }
70
71         /**
72          * @param array $m
73          * @return string
74          */
75         private function replaceCallback( array $m ) {
76                 $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
77                 $this->mMarkerIndex++;
78                 $this->mTokens[$marker] = $m[0];
79                 return $marker;
80         }
81
82         /**
83          * @param string $text
84          * @return string
85          */
86         public function postprocess( $text ) {
87                 // Revert <html-{link,meta,style}> back to <{link,meta,style}>
88                 $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
89                 $text = preg_replace( '!<(/?)html-(style)([^>]*)>!', '<$1$2$3>', $text );
90
91                 // Remove datafld
92                 $text = str_replace( '<li datafld=""', '<li', $text );
93
94                 // Restore the contents of placeholder tokens
95                 $text = strtr( $text, $this->mTokens );
96
97                 return $text;
98         }
99
100 }