From 5859eff1b0c6e2438f4e21a4532e7f547d7cf2c2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Fri, 9 Feb 2018 13:30:12 -0800 Subject: [PATCH] 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 --- includes/parser/ParserOutput.php | 10 ++++++++-- tests/phpunit/includes/parser/ParserOutputTest.php | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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 } -- 2.20.1