From 591a44ba54bee4f0a89f336171a545ea616b34a5 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Wed, 30 Apr 2014 11:46:52 -0700 Subject: [PATCH] HtmlFormatter: fix check for empty ruleset This ensures that no unneeded HTML parse will occur Change-Id: I2fae4bea555f823e5244c1430f9f3f43a3f78b71 --- includes/HtmlFormatter.php | 11 ++++++++-- tests/phpunit/includes/HtmlFormatterTest.php | 23 +++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/includes/HtmlFormatter.php b/includes/HtmlFormatter.php index 83d0530de2..987a6830b1 100644 --- a/includes/HtmlFormatter.php +++ b/includes/HtmlFormatter.php @@ -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( ' ', '', $html ); } - $html = preg_replace( '/|^.*?|<\/body>.*$/s', '', $html ); wfProfileOut( __METHOD__ . '-fixes' ); } else { $html = $this->html; } + // Remove stuff added by wrapHTML() + $html = preg_replace( '/|^.*?|<\/body>.*$/s', '', $html ); $html = $this->onHtmlReady( $html ); wfProfileIn( __METHOD__ . '-flatten' ); diff --git a/tests/phpunit/includes/HtmlFormatterTest.php b/tests/phpunit/includes/HtmlFormatterTest.php index 10ccc4fa29..c1b637b80e 100644 --- a/tests/phpunit/includes/HtmlFormatterTest.php +++ b/tests/phpunit/includes/HtmlFormatterTest.php @@ -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( '<Тест!> &<&&&&', '<Тест!> &<&&&&', + 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(); + } } -- 2.20.1