Followup r111903; Introduce a new Tidy hack to prevent tidy from stripping the <link...
authorDaniel Friesen <dantman@users.mediawiki.org>
Mon, 20 Feb 2012 02:12:48 +0000 (02:12 +0000)
committerDaniel Friesen <dantman@users.mediawiki.org>
Mon, 20 Feb 2012 02:12:48 +0000 (02:12 +0000)
We do this by replacing every <link> and <meta> with a <html-link> or <html-meta> element and adding html-link and html-meta to tidy's new-empty-tags config so that Tidy doesn't strip it, and then restoring the <html-*> elements back to normal.

includes/parser/Tidy.php
includes/tidy.conf

index 8bd80b2..2ae9e99 100644 (file)
@@ -41,9 +41,15 @@ class MWTidyWrapper {
                        dechex( mt_rand( 0, 0x7fffffff ) ) . dechex( mt_rand( 0, 0x7fffffff ) );
                $this->mMarkerIndex = 0;
 
+               // Replace <mw:editsection> elements with placeholders
                $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
                        array( &$this, 'replaceEditSectionLinksCallback' ), $text );
 
+               // 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 );
+
+               // Wrap the whole thing in a doctype and body for Tidy.
                $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
                        ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
                        '<head><title>test</title></head><body>'.$wrappedtext.'</body></html>';
@@ -68,7 +74,13 @@ class MWTidyWrapper {
         * @return string
         */
        public function postprocess( $text ) {
-               return $this->mTokens->replace( $text );
+               // Revert <html-{link,meta}> back to <{link,meta}>
+               $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
+
+               // Restore the contents of placeholder tokens
+               $text = $this->mTokens->replace( $text );
+
+               return $text;
        }
 
 }
index b0ff73b..6ae8d45 100644 (file)
@@ -18,4 +18,6 @@ fix-backslash: no
 fix-uri: no
 
 # Don't strip html5 elements we support
+# html-{meta,link} is a hack we use to prevent Tidy from stripping <meta> and <link> used in the body for Microdata
+new-empty-tags: html-meta, html-link
 new-inline-tags: data, time