From: Gergő Tisza Date: Fri, 9 Feb 2018 21:30:12 +0000 (-0800) Subject: Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment X-Git-Tag: 1.31.0-rc.0~647^2 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/modifier.php?a=commitdiff_plain;h=5859eff1b0c6e2438f4e21a4532e7f547d7cf2c2;p=lhc%2Fweb%2Fwiklou.git Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment The closing div might be followed by debug information in HTML comments. Bug: T186927 Change-Id: I72d4079dfe9ca9b3a14476ace1364eb5efdee846 --- diff --git a/includes/parser/ParserOutput.php b/includes/parser/ParserOutput.php index e2efaff40f..346a151bee 100644 --- a/includes/parser/ParserOutput.php +++ b/includes/parser/ParserOutput.php @@ -301,10 +301,16 @@ class ParserOutput extends CacheTime { ] ); $startLen = strlen( $start ); $end = Html::closeElement( 'div' ); + $endPos = strrpos( $text, $end ); $endLen = strlen( $end ); - if ( substr( $text, 0, $startLen ) === $start && substr( $text, -$endLen ) === $end ) { - $text = substr( $text, $startLen, -$endLen ); + if ( substr( $text, 0, $startLen ) === $start && $endPos !== false + // if the closing div is followed by real content, bail out of unwrapping + && preg_match( '/^(?>\s*)*\s*$/s', substr( $text, $endPos + $endLen ) ) + ) { + $text = substr( $text, $startLen ); + $text = substr( $text, 0, $endPos - $startLen ) + . substr( $text, $endPos - $startLen + $endLen ); } } diff --git a/tests/phpunit/includes/parser/ParserOutputTest.php b/tests/phpunit/includes/parser/ParserOutputTest.php index efcc4e079e..1f3ee67433 100644 --- a/tests/phpunit/includes/parser/ParserOutputTest.php +++ b/tests/phpunit/includes/parser/ParserOutputTest.php @@ -349,6 +349,11 @@ EOF 'Unwrap without a mw-parser-output wrapper' => [ [ 'unwrap' => true ], [], '
Content
', '
Content
' ], + 'Unwrap with extra comment at end' => [ + [ 'unwrap' => true ], [], '

Test document.

+', '

Test document.

+' + ], ]; // phpcs:enable }