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