Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment
authorGergő Tisza <tgr.huwiki@gmail.com>
Fri, 9 Feb 2018 21:30:12 +0000 (13:30 -0800)
committerGergő Tisza <tgr.huwiki@gmail.com>
Fri, 9 Feb 2018 22:17:22 +0000 (14:17 -0800)
The closing div might be followed by debug information in HTML comments.

Bug: T186927
Change-Id: I72d4079dfe9ca9b3a14476ace1364eb5efdee846

includes/parser/ParserOutput.php
tests/phpunit/includes/parser/ParserOutputTest.php

index e2efaff..346a151 100644 (file)
@@ -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 );
                        }
                }
 
index efcc4e0..1f3ee67 100644 (file)
@@ -349,6 +349,11 @@ EOF
                        'Unwrap without a mw-parser-output wrapper' => [
                                [ 'unwrap' => true ], [], '<div class="foobar">Content</div>', '<div class="foobar">Content</div>'
                        ],
+                       'Unwrap with extra comment at end' => [
+                               [ 'unwrap' => true ], [], '<div class="mw-parser-output"><p>Test document.</p></div>
+<!-- Saved in parser cache... -->', '<p>Test document.</p>
+<!-- Saved in parser cache... -->'
+                       ],
                ];
                // phpcs:enable
        }