From c84d332c17a6861bf94a2c6a9516e050b384a286 Mon Sep 17 00:00:00 2001 From: Luqgreg Date: Sat, 11 Aug 2018 16:47:52 +0200 Subject: [PATCH] Markers are handled by parser, no need to do it twice --- extension.json | 1 - ...rtableInfoboxParserTagController.class.php | 40 +-------- ...PortableInfoboxParserTagControllerTest.php | 85 ++----------------- 3 files changed, 10 insertions(+), 116 deletions(-) diff --git a/extension.json b/extension.json index f90b25e..ec89eaa 100644 --- a/extension.json +++ b/extension.json @@ -78,7 +78,6 @@ }, "Hooks": { "ParserFirstCallInit": "PortableInfoboxParserTagController::parserTagInit", - "ParserAfterTidy": "PortableInfoboxParserTagController::replaceInfoboxMarkers", "BeforePageDisplay": "PortableInfoboxHooks::onBeforePageDisplay", "BeforePageDisplayMobile": "PortableInfoboxHooks::onBeforePageDisplayMobile", "BeforeParserrenderImageGallery": "PortableInfoboxHooks::onBeforeParserrenderImageGallery", diff --git a/includes/controllers/PortableInfoboxParserTagController.class.php b/includes/controllers/PortableInfoboxParserTagController.class.php index 3052fb3..29a3f5d 100644 --- a/includes/controllers/PortableInfoboxParserTagController.class.php +++ b/includes/controllers/PortableInfoboxParserTagController.class.php @@ -46,19 +46,6 @@ class PortableInfoboxParserTagController { return true; } - /** - * Parser hook: used to replace infobox markes put on rendering - * - * @param $text - * - * @return string - */ - public static function replaceInfoboxMarkers( Parser $parser, &$text ) { - $text = static::getInstance()->replaceMarkers( $text ); - - return true; - } - /** * @param $markup * @param Parser $parser @@ -120,32 +107,7 @@ class PortableInfoboxParserTagController { return $this->handleError( wfMessage( 'portable-infobox-xml-parse-error-infobox-tag-attribute-unsupported', [ $e->getMessage() ] )->escaped() ); } - $marker = Parser::MARKER_PREFIX . '-' . self::PARSER_TAG_NAME . '-' . sprintf( '%08X', $this->markerNumber ) . Parser::MARKER_SUFFIX; - $this->markers[$marker] = $renderedValue; - - return [ $marker, 'markerType' => 'nowiki' ]; - } - - /** - * @desc Moves the first marker to the top of article content - * - * @param String $text - */ - public function moveFirstMarkerToTop( &$text ) { - if ( !empty( $this->markers ) ) { - $firstMarker = array_keys( $this->markers )[ 0 ]; - - // Skip if the first marker is already at the top - if ( strpos( $text, $firstMarker ) !== 0 ) { - // Remove first marker and the following whitespace - $text = preg_replace( '/' . $firstMarker . '\s*/', '', $text, 1 ); - $text = $firstMarker . ' ' . $text; - } - } - } - - public function replaceMarkers( $text ) { - return strtr( $text, $this->markers ); + return [ $renderedValue, 'markerType' => 'nowiki' ]; } protected function saveToParserOutput( \ParserOutput $parserOutput, Nodes\NodeInfobox $raw ) { diff --git a/tests/phpunit/PortableInfoboxParserTagControllerTest.php b/tests/phpunit/PortableInfoboxParserTagControllerTest.php index 3ed8d63..5595b8d 100644 --- a/tests/phpunit/PortableInfoboxParserTagControllerTest.php +++ b/tests/phpunit/PortableInfoboxParserTagControllerTest.php @@ -68,9 +68,8 @@ class PortableInfoboxParserTagControllerTest extends MediaWikiTestCase { public function testEmptyInfobox() { $text = ''; - $marker = $this->controller->renderInfobox( $text, [ ], $this->parser, + $output = $this->controller->renderInfobox( $text, [], $this->parser, $this->parser->getPreprocessor()->newFrame() )[0]; - $output = $this->controller->replaceMarkers( $marker ); $this->assertEquals( $output, '', 'Should be empty' ); } @@ -81,11 +80,10 @@ class PortableInfoboxParserTagControllerTest extends MediaWikiTestCase { public function testThemes( $staticTheme, $variableTheme, $classes, $message ) { $text = 'test'; - $marker = $this->controller->renderInfobox( $text, + $output = $this->controller->renderInfobox( $text, [ 'theme' => $staticTheme, 'theme-source' => 'testVar' ], $this->parser, $this->parser->getPreprocessor()->newCustomFrame( [ 'testVar' => $variableTheme ] ) )[0]; - $output = $this->controller->replaceMarkers( $marker ); $this->assertTrue( array_reduce( $classes, function ( $result, $class ) use ( $output ) { return $result && $this->containsClassName( $output, $class ); @@ -122,9 +120,8 @@ class PortableInfoboxParserTagControllerTest extends MediaWikiTestCase { * @dataProvider getLayoutDataProvider */ public function testGetLayout( $layout, $expectedOutput, $text, $message ) { - $marker = $this->controller->renderInfobox( $text, $layout, $this->parser, + $output = $this->controller->renderInfobox( $text, $layout, $this->parser, $this->parser->getPreprocessor()->newFrame() )[0]; - $output = $this->controller->replaceMarkers( $marker ); $this->assertTrue( $this->containsClassName( $output, @@ -159,13 +156,13 @@ class PortableInfoboxParserTagControllerTest extends MediaWikiTestCase { 'message' => 'layout is an integer' ], [ - 'layout' => [ 'layout' => [ ] ], + 'layout' => [ 'layout' => [] ], 'expectedOutput' => 'pi-layout-default', 'text' => 'test', 'message' => 'layout an empty table' ], [ - 'layout' => [ ], + 'layout' => [], 'expectedOutput' => 'pi-layout-default', 'text' => 'test', 'message' => 'layout is not set' @@ -177,9 +174,8 @@ class PortableInfoboxParserTagControllerTest extends MediaWikiTestCase { * @dataProvider getColorDataProvider */ public function testGetColor( $params, $expectedOutput, $text, $templateInvocation, $message ) { - $marker = $this->controller->renderInfobox( $text, $params, $this->parser, - $this->parser->getPreprocessor()->newCustomFrame( $templateInvocation ) )[ 0 ]; - $output = $this->controller->replaceMarkers( $marker ); + $output = $this->controller->renderInfobox( $text, $params, $this->parser, + $this->parser->getPreprocessor()->newCustomFrame( $templateInvocation ) )[0]; $this->assertEquals($this->normalizeHTML($expectedOutput), $this->normalizeHTML($output), $message); } @@ -303,11 +299,10 @@ class PortableInfoboxParserTagControllerTest extends MediaWikiTestCase { '; - $marker = $this->controller->renderInfobox( $text, [ ], $this->parser, + $output = $this->controller->renderInfobox( $text, [], $this->parser, $this->parser->getPreprocessor()->newCustomFrame( $params ) )[0]; - $output = $this->controller->replaceMarkers( $marker ); - $result = [ ]; + $result = []; $xpath = $this->getXPath( $output ); // get all data nodes from parsed infobox $dataNodes = $xpath->query( '//aside/div[contains(@class,\'pi-data\')]' ); @@ -335,66 +330,4 @@ class PortableInfoboxParserTagControllerTest extends MediaWikiTestCase { [ 'abc' => 'minus one', '0' => 'zero', '1' => 'one', '2' => 'two', '3' => 'three' ] ], ]; } - - /** - * @dataProvider moveFirstMarkerToTopDataProvider - */ - public function testMoveFirstMarkerToTop( $markers, $text, $expected ) { - $reflection = new ReflectionClass( $this->controller ); - $reflection_property = $reflection->getProperty( 'markers' ); - $reflection_property->setAccessible( true ); - $reflection_property->setValue( $this->controller, $markers ); - - $this->controller->moveFirstMarkerToTop( $text ); - $this->assertEquals( $expected, $text ); - } - - public function moveFirstMarkerToTopDataProvider() { - return [ - [ - 'markers' => [ - 'infobox-1' => '1', - 'infobox-2' => '2', - ], - 'text' => 'infobox-1 some text infobox-2', - 'expected' => 'infobox-1 some text infobox-2', - 'message' => 'first infobox already at the top' - ], - [ - 'markers' => [ - 'infobox-1' => '1', - 'infobox-2' => '2', - ], - 'text' => 'some text infobox-1 infobox-2', - 'expected' => 'infobox-1 some text infobox-2', - 'message' => 'first infobox below text' - ], - [ - 'markers' => [ - 'infobox-1' => '1' - ], - 'text' => 'nospaceinfobox-1', - 'expected' => 'infobox-1 nospace', - 'message' => 'no space between input elements' - ], - [ - 'markers' => [ - 'infobox-1' => '1', - 'infobox-2' => '2', - ], - 'text' => 'some text - some more - infobox-1 - more text - infobox-2 - and some more', - 'expected' => 'infobox-1 some text - some more - more text - infobox-2 - and some more', - 'message' => 'multiple lines' - ], - ]; - } }