2 namespace MediaWiki\Tidy
;
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
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.
20 class RaggettWrapper
{
23 * @var ReplacementArray
27 protected $mMarkerIndex;
29 public function __construct() {
30 $this->mTokens
= null;
37 public function getWrapped( $text ) {
38 $this->mTokens
= new ReplacementArray
;
39 $this->mMarkerIndex
= 0;
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 );
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-elt">\1</li>', $wrappedtext );
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>';
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] );
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 );
88 $text = str_replace( '<li datafld=""', '<li', $text );
90 // Restore the contents of placeholder tokens
91 $text = $this->mTokens
->replace( $text );