Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / tidy / RaggettWrapper.php
blob9f6feb8e55a55cd63bfa20b271479467cde53aed
1 <?php
2 namespace MediaWiki\Tidy;
4 use ParserOutput;
5 use Parser;
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
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.
17 * @ingroup Parser
19 class RaggettWrapper {
21 /**
22 * @var array
24 protected $mTokens;
26 /**
27 * @var int
29 protected $mMarkerIndex;
31 /**
32 * @param string $text
33 * @return string
35 public function getWrapped( $text ) {
36 $this->mTokens = [];
37 $this->mMarkerIndex = 0;
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 );
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 );
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>';
62 return $wrappedtext;
65 /**
66 * @param array $m
67 * @return string
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;
76 /**
77 * @param string $text
78 * @return string
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 );
84 // Remove datafld
85 $text = str_replace( '<li datafld=""', '<li', $text );
87 // Restore the contents of placeholder tokens
88 $text = strtr( $text, $this->mTokens );
90 return $text;