diff --git a/includes/OutputTransform/Stages/DeduplicateStyles.php b/includes/OutputTransform/Stages/DeduplicateStyles.php index 22a0f9ce428..b46208d24d1 100644 --- a/includes/OutputTransform/Stages/DeduplicateStyles.php +++ b/includes/OutputTransform/Stages/DeduplicateStyles.php @@ -2,11 +2,12 @@ namespace Mediawiki\OutputTransform\Stages; -use MediaWiki\Html\Html; +use MediaWiki\Html\HtmlHelper; use Mediawiki\OutputTransform\ContentTextTransformStage; use MediaWiki\Parser\ParserOutput; -use MediaWiki\Parser\Sanitizer; use ParserOptions; +use Wikimedia\RemexHtml\Serializer\SerializerNode; +use Wikimedia\RemexHtml\Tokenizer\PlainAttributes; /** * Generates a list of unique style links @@ -20,28 +21,32 @@ class DeduplicateStyles extends ContentTextTransformStage { protected function transformText( string $text, ParserOutput $po, ?ParserOptions $popts, array &$options ): string { $seen = []; - return preg_replace_callback( '##s', - static function ( $m ) use ( &$seen ) { - $attr = Sanitizer::decodeTagAttributes( $m[1] ); - if ( !isset( $attr['data-mw-deduplicate'] ) ) { - return $m[0]; - } - - $key = $attr['data-mw-deduplicate']; + return HtmlHelper::modifyElements( + $text, + static function ( SerializerNode $node ): bool { + return $node->name === 'style' && + ( $node->attrs['data-mw-deduplicate'] ?? '' ) !== ''; + }, + static function ( SerializerNode $node ) use ( &$seen ): SerializerNode { + $key = $node->attrs['data-mw-deduplicate']; if ( !isset( $seen[$key] ) ) { $seen[$key] = true; - - return $m[0]; + return $node; } - // We were going to use an empty - + - - + + - + EOF; diff --git a/tests/phpunit/includes/content/JsonContentHandlerIntegrationTest.php b/tests/phpunit/includes/content/JsonContentHandlerIntegrationTest.php index 9b91214e172..7955ced731f 100644 --- a/tests/phpunit/includes/content/JsonContentHandlerIntegrationTest.php +++ b/tests/phpunit/includes/content/JsonContentHandlerIntegrationTest.php @@ -75,7 +75,9 @@ class JsonContentHandlerIntegrationTest extends MediaWikiLangTestCase { true ); $this->assertInstanceOf( ParserOutput::class, $parserOutput ); - $this->assertEquals( $expected, $parserOutput->getText() ); + $this->assertEquals( $expected, $parserOutput->getText( [ + 'deduplicateStyles' => false, + ] ) ); } /** diff --git a/tests/phpunit/includes/content/TextContentHandlerIntegrationTest.php b/tests/phpunit/includes/content/TextContentHandlerIntegrationTest.php index 8d13f4b3a6f..230ff215d11 100644 --- a/tests/phpunit/includes/content/TextContentHandlerIntegrationTest.php +++ b/tests/phpunit/includes/content/TextContentHandlerIntegrationTest.php @@ -38,7 +38,9 @@ class TextContentHandlerIntegrationTest extends MediaWikiLangTestCase { $contentRenderer = $this->getServiceContainer()->getContentRenderer(); $po = $contentRenderer->getParserOutput( $content, $title, null, $parserOptions ); - $html = $po->getText(); + $html = $po->getText( [ + 'deduplicateStyles' => false, + ] ); $html = preg_replace( '##sm', '', $html ); // strip comments if ( $expectedHtml !== null ) { diff --git a/tests/phpunit/includes/page/WikiPageDbTest.php b/tests/phpunit/includes/page/WikiPageDbTest.php index 591d0a3fff9..a1f760f2827 100644 --- a/tests/phpunit/includes/page/WikiPageDbTest.php +++ b/tests/phpunit/includes/page/WikiPageDbTest.php @@ -937,7 +937,9 @@ class WikiPageDbTest extends MediaWikiLangTestCase { $opt = $page->makeParserOptions( 'canonical' ); $po = $page->getParserOutput( $opt ); - $text = $po->getText(); + $text = $po->getText( [ + 'deduplicateStyles' => false, + ] ); $text = trim( preg_replace( '//sm', '', $text ) ); # strip injected comments $text = preg_replace( '!\s*(
|)!m', '\1', $text ); # don't let tidy confuse us diff --git a/tests/phpunit/includes/parser/ParserOutputTest.php b/tests/phpunit/includes/parser/ParserOutputTest.php index a4ef882a99e..6469af5f5bd 100644 --- a/tests/phpunit/includes/parser/ParserOutputTest.php +++ b/tests/phpunit/includes/parser/ParserOutputTest.php @@ -449,12 +449,12 @@ EOF [], $dedupText, <<