Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[lhc/web/wiklou.git] / includes / tidy / RaggettWrapper.php
index 56d5ce7..855282d 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 namespace MediaWiki\Tidy;
 
-use ReplacementArray;
 use ParserOutput;
 use Parser;
 
@@ -16,40 +15,46 @@ use Parser;
  * duplicated. Perhaps we should create an abstract marker hiding class.
  *
  * @ingroup Parser
+ * @deprecated since 1.32
  */
 class RaggettWrapper {
 
        /**
-        * @var ReplacementArray
+        * @var array
         */
        protected $mTokens;
 
+       /**
+        * @var int
+        */
        protected $mMarkerIndex;
 
-       public function __construct() {
-               $this->mTokens = null;
-       }
-
        /**
         * @param string $text
         * @return string
         */
        public function getWrapped( $text ) {
-               $this->mTokens = new ReplacementArray;
+               $this->mTokens = [];
                $this->mMarkerIndex = 0;
 
                // Replace <mw:editsection> elements with placeholders
                $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
-                       [ &$this, 'replaceCallback' ], $text );
+                       [ $this, 'replaceCallback' ], $text );
                // ...and <mw:toc> markers
                $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/',
-                       [ &$this, 'replaceCallback' ], $wrappedtext );
+                       [ $this, 'replaceCallback' ], $wrappedtext );
                // ... and <math> tags
                $wrappedtext = preg_replace_callback( '/\<math(.*?)\<\\/math\>/s',
-                       [ &$this, 'replaceCallback' ], $wrappedtext );
+                       [ $this, 'replaceCallback' ], $wrappedtext );
                // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> 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}>)!', '<html-$1$2$3', $wrappedtext );
+               // Similar for inline <style> tags, but those aren't empty.
+               $wrappedtext = preg_replace_callback( '!<style([^>]*)>(.*?)</style>!s', function ( $m ) {
+                       return '<html-style' . $m[1] . '>'
+                               . $this->replaceCallback( [ $m[2] ] )
+                               . '</html-style>';
+               }, $wrappedtext );
 
                // Preserve empty li elements (T49673) by abusing Tidy's datafld hack
                // The whitespace class is as in TY_(InitMap)
@@ -66,13 +71,12 @@ class RaggettWrapper {
 
        /**
         * @param array $m
-        *
         * @return string
         */
-       public function replaceCallback( $m ) {
+       private function replaceCallback( array $m ) {
                $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
                $this->mMarkerIndex++;
-               $this->mTokens->setPair( $marker, $m[0] );
+               $this->mTokens[$marker] = $m[0];
                return $marker;
        }
 
@@ -81,14 +85,15 @@ class RaggettWrapper {
         * @return string
         */
        public function postprocess( $text ) {
-               // Revert <html-{link,meta}> back to <{link,meta}>
+               // Revert <html-{link,meta,style}> back to <{link,meta,style}>
                $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
+               $text = preg_replace( '!<(/?)html-(style)([^>]*)>!', '<$1$2$3>', $text );
 
                // Remove datafld
                $text = str_replace( '<li datafld=""', '<li', $text );
 
                // Restore the contents of placeholder tokens
-               $text = $this->mTokens->replace( $text );
+               $text = strtr( $text, $this->mTokens );
 
                return $text;
        }