HtmlFormatter: fix check for empty ruleset
authorMax Semenik <maxsem.wiki@gmail.com>
Wed, 30 Apr 2014 18:46:52 +0000 (11:46 -0700)
committerMax Semenik <maxsem.wiki@gmail.com>
Thu, 1 May 2014 20:54:49 +0000 (13:54 -0700)
This ensures that no unneeded HTML parse will occur

Change-Id: I2fae4bea555f823e5244c1430f9f3f43a3f78b71

includes/HtmlFormatter.php
tests/phpunit/includes/HtmlFormatterTest.php

index 83d0530..987a683 100644 (file)
@@ -136,7 +136,13 @@ class HtmlFormatter {
                wfProfileIn( __METHOD__ );
                $removals = $this->parseItemsToRemove();
 
-               if ( !$removals ) {
+               // Bail out early if nothing to do
+               if ( array_reduce( $removals,
+                       function( $carry, $item ) {
+                               return $carry && !$item;
+                       },
+                       true
+               ) ) {
                        wfProfileOut( __METHOD__ );
                        return array();
                }
@@ -287,11 +293,12 @@ class HtmlFormatter {
                                // XML code paths if possible and fix there.
                                $html = str_replace( '&#13;', '', $html );
                        }
-                       $html = preg_replace( '/<!--.*?-->|^.*?<body>|<\/body>.*$/s', '', $html );
                        wfProfileOut( __METHOD__ . '-fixes' );
                } else {
                        $html = $this->html;
                }
+               // Remove stuff added by wrapHTML()
+               $html = preg_replace( '/<!--.*?-->|^.*?<body>|<\/body>.*$/s', '', $html );
                $html = $this->onHtmlReady( $html );
 
                wfProfileIn( __METHOD__ . '-flatten' );
index 10ccc4f..c1b637b 100644 (file)
@@ -6,7 +6,11 @@
 class HtmlFormatterTest extends MediaWikiTestCase {
        /**
         * @dataProvider getHtmlData
-        * @covers HtmlFormatter::getText
+        *
+        * @param string $input
+        * @param $expectedText
+        * @param array $expectedRemoved
+        * @param callable|bool $callback
         */
        public function testTransform( $input, $expectedText,
                $expectedRemoved = array(), $callback = false
@@ -93,6 +97,8 @@ class HtmlFormatterTest extends MediaWikiTestCase {
                        array(
                                '<span title="&quot; \' &amp;">&lt;Тест!&gt;</span> &amp;&lt;&#38;&#0038;&#x26;&#x026;',
                                '<span title="&quot; \' &amp;">&lt;Тест!&gt;</span> &amp;&lt;&amp;&amp;&amp;&amp;',
+                               array(),
+                               $removeTags, // Have some rules to trigger a DOM parse
                        ),
                        // https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
                        array(
@@ -103,4 +109,19 @@ class HtmlFormatterTest extends MediaWikiTestCase {
                        ),
                );
        }
+
+       public function testQuickProcessing() {
+               $f = new MockHtmlFormatter( 'foo' );
+               $f->filterContent();
+               $this->assertFalse( $f->hasDoc, 'HtmlFormatter should not needlessly parse HTML' );
+       }
+}
+
+class MockHtmlFormatter extends HtmlFormatter {
+       public $hasDoc = false;
+
+       public function getDoc() {
+               $this->hasDoc = true;
+               return parent::getDoc();
+       }
 }