Merge "Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 9 Feb 2018 22:34:46 +0000 (22:34 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 9 Feb 2018 22:34:46 +0000 (22:34 +0000)
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
        }