] );
$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 );
}
}
'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
}