Fix Tidy quietly breaking TOC disabling
authorMax Semenik <maxsem.wiki@gmail.com>
Fri, 25 Oct 2013 18:02:11 +0000 (22:02 +0400)
committerBartosz Dziewoński <matma.rex@gmail.com>
Fri, 25 Oct 2013 19:04:51 +0000 (19:04 +0000)
The functionality was introduced in I2889bcb9
but doesn't work in presence if Tidy.

Change-Id: Ibf96cc3bc94fac75fd92ec5b9205011fcb68f0c2

includes/parser/Tidy.php
tests/phpunit/includes/parser/TidyTest.php [new file with mode: 0644]

index 0625140..32b16aa 100644 (file)
@@ -61,7 +61,10 @@ class MWTidyWrapper {
 
                // Replace <mw:editsection> elements with placeholders
                $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
-                       array( &$this, 'replaceEditSectionLinksCallback' ), $text );
+                       array( &$this, 'replaceCallback' ), $text );
+               // ...and <mw:toc> markers
+               $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/',
+                       array( &$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
@@ -80,7 +83,7 @@ class MWTidyWrapper {
         *
         * @return string
         */
-       function replaceEditSectionLinksCallback( $m ) {
+       function replaceCallback( $m ) {
                $marker = "{$this->mUniqPrefix}-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
                $this->mMarkerIndex++;
                $this->mTokens->setPair( $marker, $m[0] );
diff --git a/tests/phpunit/includes/parser/TidyTest.php b/tests/phpunit/includes/parser/TidyTest.php
new file mode 100644 (file)
index 0000000..57a88b9
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * @group Parser
+ */
+class TidyTest extends MediaWikiTestCase {
+       public function setUp() {
+               parent::setUp();
+               $check = MWTidy::tidy( '' );
+               if ( strpos( $check, '<!--' ) !== false ) {
+                       $this->markTestSkipped( 'Tidy not found' );
+               }
+       }
+
+       /**
+        * @dataProvider provideTestWrapping
+        */
+       public function testTidyWrapping( $expected, $text, $msg = '' ) {
+               $text = MWTidy::tidy( $text );
+               // We don't care about where Tidy wants to stick is <p>s
+               $text = trim( preg_replace( '#</?p>#', '', $text ) );
+               // Windows, we love you!
+               $text = str_replace( "\r", '', $text );
+               $this->assertEquals( $expected, $text, $msg );
+       }
+
+       public function provideTestWrapping() {
+               return array(
+                       array(
+                               '<mw:editsection page="foo" section="bar">foo</mw:editsection>',
+                               '<mw:editsection page="foo" section="bar">foo</mw:editsection>',
+                               '<mw:editsection> should survive tidy'
+                       ),
+                       array(
+                               '<editsection page="foo" section="bar">foo</editsection>',
+                               '<editsection page="foo" section="bar">foo</editsection>',
+                               '<editsection> should survive tidy'
+                       ),
+                       array( '<mw:toc>foo</mw:toc>', '<mw:toc>foo</mw:toc>', '<mw:toc> should survive tidy' ),
+                       array( "<link foo=\"bar\" />\nfoo", '<link foo="bar"/>foo', '<link> should survive tidy' ),
+                       array( "<meta foo=\"bar\" />\nfoo", '<meta foo="bar"/>foo', '<meta> should survive tidy' ),
+               );
+       }
+}
\ No newline at end of file