3f549d0750d3bdfad5d585c3590fe66dcdeb39fb
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 array( &$this, 'replaceCallback' ), $text );
44 // ...and <mw:toc> markers
45 $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/',
46 array( &$this, 'replaceCallback' ), $wrappedtext );
47 // ... and <math> tags
48 $wrappedtext = preg_replace_callback( '/\<math(.*?)\<\\/math\>/s',
49 array( &$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 // Wrap the whole thing in a doctype and body for Tidy.
55 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' .
56 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>' .
57 '<head><title>test</title></head><body>' . $wrappedtext . '</body></html>';
67 public function replaceCallback( $m ) {
68 $marker = Parser
::MARKER_PREFIX
. "-item-{$this->mMarkerIndex}" . Parser
::MARKER_SUFFIX
;
69 $this->mMarkerIndex++
;
70 $this->mTokens
->setPair( $marker, $m[0] );
78 public function postprocess( $text ) {
79 // Revert <html-{link,meta}> back to <{link,meta}>
80 $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
82 // Restore the contents of placeholder tokens
83 $text = $this->mTokens
->replace( $text );