X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/tidy/RaggettWrapper.php diff --git a/includes/tidy/RaggettWrapper.php b/includes/tidy/RaggettWrapper.php new file mode 100644 index 00000000..b793a58a --- /dev/null +++ b/includes/tidy/RaggettWrapper.php @@ -0,0 +1,100 @@ +mTokens = []; + $this->mMarkerIndex = 0; + + // Replace elements with placeholders + $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX, + [ $this, 'replaceCallback' ], $text ); + // ...and markers + $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/', + [ $this, 'replaceCallback' ], $wrappedtext ); + // ... and tags + $wrappedtext = preg_replace_callback( '/\/s', + [ $this, 'replaceCallback' ], $wrappedtext ); + // Modify inline Microdata and elements so they say and so + // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config + $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', ' tags, but those aren't empty. + $wrappedtext = preg_replace_callback( '!]*)>(.*?)!s', function ( $m ) { + return '' + . $this->replaceCallback( [ $m[2] ] ) + . ''; + }, $wrappedtext ); + + // Preserve empty li elements (T49673) by abusing Tidy's datafld hack + // The whitespace class is as in TY_(InitMap) + $wrappedtext = preg_replace( "!
  • ([ \r\n\t\f]*)
  • !", + '
  • \1
  • ', $wrappedtext ); + + // Wrap the whole thing in a doctype and body for Tidy. + $wrappedtext = '' . + 'test' . $wrappedtext . ''; + + return $wrappedtext; + } + + /** + * @param array $m + * @return string + */ + private function replaceCallback( array $m ) { + $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX; + $this->mMarkerIndex++; + $this->mTokens[$marker] = $m[0]; + return $marker; + } + + /** + * @param string $text + * @return string + */ + public function postprocess( $text ) { + // Revert back to <{link,meta,style}> + $text = preg_replace( '!]*?)(/{0,1}>)!', '<$1$2$3', $text ); + $text = preg_replace( '!<(/?)html-(style)([^>]*)>!', '<$1$2$3>', $text ); + + // Remove datafld + $text = str_replace( '
  • mTokens ); + + return $text; + } + +}