From 7738554eee146ccc04508fd39d29d648bce88efc Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Wed, 24 Apr 2024 16:35:11 -0400 Subject: [PATCH] [OutputTransform] Add data-mw-parsoid-version to wrapper div Adding a data-mw-parsoid-version attribute to the wrapper div helps to unambiguously mark parsoid-generated output in a way which is compatible with CSS rules and client-side JavaScript. By embedding the current version of parsoid in the data attribute, sophisticated CSS rules can match against a specific version of Parsoid in order to facilitate proper behavior; for example: div[data-mw-parsoid-version^="0.20.0"] This could be useful in deployment scenarios where the parser cache might contain content generated by older or newer versions of Parsoid, for roll-forward or roll-back deployment scenarios, respectively. Bug: T363378 Change-Id: I941d31479eebb12ea1f4dcdb0a1737033ddc8ac1 --- includes/OutputTransform/Stages/AddWrapperDivClass.php | 6 +++++- tests/phpunit/includes/api/ApiParseTest.php | 6 +++--- .../includes/content/WikitextContentHandlerIntegrationTest.php | 9 ++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/includes/OutputTransform/Stages/AddWrapperDivClass.php b/includes/OutputTransform/Stages/AddWrapperDivClass.php index 9602df02c88..784dce10f78 100644 --- a/includes/OutputTransform/Stages/AddWrapperDivClass.php +++ b/includes/OutputTransform/Stages/AddWrapperDivClass.php @@ -30,11 +30,15 @@ class AddWrapperDivClass extends ContentTextTransformStage { protected function transformText( string $text, ParserOutput $po, ?ParserOptions $popts, array &$options ): string { $wrapperDivClass = $options['wrapperDivClass']; $pageLang = $this->getLanguageWithFallbackGuess( $po ); + $parsoidVersion = $po->getExtensionData( 'core:parsoid-version' ); + $extraAttrs = $parsoidVersion === null ? [] : [ + 'data-mw-parsoid-version' => $parsoidVersion, + ]; return Html::rawElement( 'div', [ 'class' => 'mw-content-' . $pageLang->getDir() . ' ' . $wrapperDivClass, 'lang' => $pageLang->toBcp47Code(), 'dir' => $pageLang->getDir(), - ], $text ); + ] + $extraAttrs, $text ); } private function getLanguageWithFallbackGuess( ParserOutput $po ): Language { diff --git a/tests/phpunit/includes/api/ApiParseTest.php b/tests/phpunit/includes/api/ApiParseTest.php index 44ea12b84ff..197f3574560 100644 --- a/tests/phpunit/includes/api/ApiParseTest.php +++ b/tests/phpunit/includes/api/ApiParseTest.php @@ -107,9 +107,9 @@ class ApiParseTest extends ApiTestCase { $html = substr( $html, strlen( $expectedStart ) ); - # Parsoid-based transformations may add an ID attribute to the - # wrapper div - $possibleIdAttr = '/^( id="[^"]+")?>/'; + # Parsoid-based transformations may add ID and data-mw-parsoid-version + # attributes to the wrapper div + $possibleIdAttr = '/^( (id|data-mw[^=]*)="[^"]+")*>/'; $html = preg_replace( $possibleIdAttr, '', $html ); $possibleParserCache = '/\n)\n/'; diff --git a/tests/phpunit/includes/content/WikitextContentHandlerIntegrationTest.php b/tests/phpunit/includes/content/WikitextContentHandlerIntegrationTest.php index 1fb4dc5b117..5ed39cf7ad7 100644 --- a/tests/phpunit/includes/content/WikitextContentHandlerIntegrationTest.php +++ b/tests/phpunit/includes/content/WikitextContentHandlerIntegrationTest.php @@ -6,6 +6,7 @@ use MediaWiki\Linker\LinkTarget; use MediaWiki\MainConfigNames; use MediaWiki\Title\Title; use MediaWiki\Title\TitleValue; +use Wikimedia\Parsoid\Parsoid; /** * @group ContentHandler @@ -61,6 +62,8 @@ class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegratio 'suppressTOC', 'targetLanguage', ] ); + $parsoidVersion = 'data-mw-parsoid-version="' . Parsoid::version() . '"'; + yield 'Basic render' => [ 'title' => 'WikitextContentTest_testGetParserOutput', 'model' => CONTENT_MODEL_WIKITEXT, @@ -78,7 +81,7 @@ class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegratio 'title' => 'WikitextContentTest_testGetParserOutput', 'model' => CONTENT_MODEL_WIKITEXT, 'text' => "hello ''world''\n", - 'expectedHtml' => '
' . "

hello world

\n
", + 'expectedHtml' => "

hello world

\n
", 'expectedFields' => [ 'Links' => [ ], @@ -92,7 +95,7 @@ class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegratio 'title' => 'WikitextContentTest_testGetParserOutput', 'model' => CONTENT_MODEL_WIKITEXT, 'text' => "#REDIRECT [[Main Page]]", - 'expectedHtml' => '
' . "

Redirect to:

", + 'expectedHtml' => "

Redirect to:

", 'expectedFields' => [ 'Links' => [ [ 'Main_Page' => 0 ], @@ -107,7 +110,7 @@ class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegratio 'title' => 'WikitextContentTest_testGetParserOutput', 'model' => CONTENT_MODEL_WIKITEXT, 'text' => "== Hello ==", - 'expectedHtml' => '

Hello

[edit]
', + 'expectedHtml' => "
" . '

Hello

[edit]
', 'expectedFields' => [ 'Links' => [ ], -- 2.11.4.GIT