From db4fa6fb9ffc4601e7a02b6c30f22962db63615c Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Fri, 26 Oct 2018 10:05:34 -0400 Subject: [PATCH] Fix OutputPage::parseInternal() by stripping
wrapper We should probably strip the
wrapper in OutputPage::parse() as well: this behavior was added in 1.30, but it only applies when $interface is false. However, that requires a more careful audit (a lot more places call parse() than parseInline()) and so I'll defer that for now. Change-Id: Iad5412f03af29c04deb653969dd71f6c86f0ae50 --- includes/OutputPage.php | 63 ++++++++++++++++------- tests/phpunit/includes/OutputPageTest.php | 36 +++++++++---- 2 files changed, 70 insertions(+), 29 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index ca3f6a31b2..97d9d8333b 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2091,6 +2091,9 @@ class OutputPage extends ContextSource { /** * Parse wikitext and return the HTML. * + * @todo The output is wrapped in a
iff $interface is false; it's + * probably best to always strip the wrapper. + * * @param string $text * @param bool $linestart Is this the start of a line? * @param bool $interface Use interface language (instead of content language) while parsing @@ -2101,6 +2104,45 @@ class OutputPage extends ContextSource { * @return string HTML */ public function parse( $text, $linestart = true, $interface = false, $language = null ) { + return $this->parseInternal( + $text, $linestart, $interface, $language + )->getText( [ + 'enableSectionEditLinks' => false, + ] ); + } + + /** + * Parse wikitext, strip paragraph wrapper, and return the HTML. + * + * @param string $text + * @param bool $linestart Is this the start of a line? + * @param bool $interface Use interface language (instead of content language) while parsing + * language sensitive magic words like GRAMMAR and PLURAL + * @return string HTML + */ + public function parseInline( $text, $linestart = true, $interface = false ) { + $parsed = $this->parseInternal( + $text, $linestart, $interface, /*language*/null + )->getText( [ + 'enableSectionEditLinks' => false, + 'wrapperDivClass' => '', /* no wrapper div */ + ] ); + return Parser::stripOuterParagraph( $parsed ); + } + + /** + * Parse wikitext and return the HTML (internal implementation helper) + * + * @param string $text + * @param bool $linestart Is this the start of a line? + * @param bool $interface Use interface language (instead of content language) while parsing + * language sensitive magic words like GRAMMAR and PLURAL. This also disables + * LanguageConverter. + * @param Language|null $language Target language object, will override $interface + * @throws MWException + * @return ParserOutput + */ + private function parseInternal( $text, $linestart, $interface, $language ) { global $wgParser; if ( is_null( $this->getTitle() ) ) { @@ -2127,26 +2169,7 @@ class OutputPage extends ContextSource { $popts->setTargetLanguage( $oldLang ); } - return $parserOutput->getText( [ - 'enableSectionEditLinks' => false, - ] ); - } - - /** - * Parse wikitext, strip paragraphs, and return the HTML. - * - * @todo This doesn't work as expected at all. If $interface is false, there's always a - * wrapping
, so stripOuterParagraph() does nothing. - * - * @param string $text - * @param bool $linestart Is this the start of a line? - * @param bool $interface Use interface language (instead of content language) while parsing - * language sensitive magic words like GRAMMAR and PLURAL - * @return string HTML - */ - public function parseInline( $text, $linestart = true, $interface = false ) { - $parsed = $this->parse( $text, $linestart, $interface ); - return Parser::stripOuterParagraph( $parsed ); + return $parserOutput; } /** diff --git a/tests/phpunit/includes/OutputPageTest.php b/tests/phpunit/includes/OutputPageTest.php index 90413c49ce..cd20bb2c95 100644 --- a/tests/phpunit/includes/OutputPageTest.php +++ b/tests/phpunit/includes/OutputPageTest.php @@ -1826,28 +1826,44 @@ class OutputPageTest extends MediaWikiTestCase { public function provideParse() { return [ - 'List at start of line' => [ - [ '* List' ], + 'List at start of line (content)' => [ + [ '* List', true, false ], "
  • List
\n
", + "
  • List
\n", ], - 'List not at start' => [ - [ "* ''Not'' list", false ], + 'List at start of line (interface)' => [ + [ '* List', true, true ], + "
  • List
\n", + ], + 'List not at start (content)' => [ + [ "* ''Not'' list", false, false ], '
* Not list
', + '* Not list', + ], + 'List not at start (interface)' => [ + [ "* ''Not'' list", false, true ], + '* Not list', ], - 'Interface' => [ + 'Interface message' => [ [ "''Italic''", true, true ], "

Italic\n

", 'Italic', ], - 'formatnum' => [ - [ '{{formatnum:123456.789}}' ], + 'formatnum (content)' => [ + [ '{{formatnum:123456.789}}', true, false ], "

123,456.789\n

", + "123,456.789", + ], + 'formatnum (interface)' => [ + [ '{{formatnum:123456.789}}', true, true ], + "

123,456.789\n

", + "123,456.789", ], - 'Language' => [ + 'Language (content)' => [ [ '{{formatnum:123456.789}}', true, false, Language::factory( 'is' ) ], "

123.456,789\n

", ], - 'Language with interface' => [ + 'Language (interface)' => [ [ '{{formatnum:123456.789}}', true, true, Language::factory( 'is' ) ], "

123.456,789\n

", '123.456,789', @@ -1856,6 +1872,8 @@ class OutputPageTest extends MediaWikiTestCase { [ '== Header ==' ], '

' . "Header

\n
", + '

Header

' . + "\n", ] ]; } -- 2.20.1