Don't extract template parameters from <pre>

<pre> behaves very similar to <nowiki> in so far that whatever it
contains doesn't get parsed as wikitext. It might contain {{{…}}}
tripple brackets, but these aren't going to be activated as template
parameters.

Bug: T91326
Change-Id: I05c24e369d97c48161c565e2ef30969ec28c6a23
This commit is contained in:
Thiemo Kreuz 2022-01-19 14:51:52 +01:00 committed by Krinkle
parent 192691cdb4
commit d3f00177ba
3 changed files with 6 additions and 4 deletions

View file

@ -216,9 +216,10 @@ class ApiTemplateData extends ApiBase {
* @return array[] Parameter info in the same format as the templatedata 'params' key. * @return array[] Parameter info in the same format as the templatedata 'params' key.
*/ */
private function getRawParams( string $wikitext ): array { private function getRawParams( string $wikitext ): array {
// Ignore wikitext within nowiki tags and comments // Ignore non-wikitext content in comments and wikitext-escaping tags
$wikitext = preg_replace( '/<!--.*?-->/s', '', $wikitext ); $wikitext = preg_replace( '/<!--.*?-->/s', '', $wikitext );
$wikitext = preg_replace( '/<nowiki\s*>.*?<\/nowiki\s*>/s', '', $wikitext ); $wikitext = preg_replace( '/<nowiki\s*>.*?<\/nowiki\s*>/s', '', $wikitext );
$wikitext = preg_replace( '/<pre\s*>.*?<\/pre\s*>/s', '', $wikitext );
// This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js // This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js
if ( !preg_match_all( '/{{{+([^\n#={|}]*?)([<|]|}}})/m', $wikitext, $rawParams ) ) { if ( !preg_match_all( '/{{{+([^\n#={|}]*?)([<|]|}}})/m', $wikitext, $rawParams ) ) {

View file

@ -173,9 +173,10 @@ SourceHandler.prototype.extractParametersFromTemplateCode = function ( templateC
// This regex matches the one in TemplateDataBlob.php // This regex matches the one in TemplateDataBlob.php
paramExtractor = /{{{+([^\n#={|}]*?)([<|]|}}})/mg; paramExtractor = /{{{+([^\n#={|}]*?)([<|]|}}})/mg;
// Strip everything in nowiki tags and HTML comments // Ignore non-wikitext content in comments and wikitext-escaping tags
templateCode = templateCode.replace( /<!--[\s\S]*?-->/g, '' ) templateCode = templateCode.replace( /<!--[\s\S]*?-->/g, '' )
.replace( /<nowiki\s*>[\s\S]*?<\/nowiki\s*>/g, '' ); .replace( /<nowiki\s*>[\s\S]*?<\/nowiki\s*>/g, '' )
.replace( /<pre\s*>[\s\S]*?<\/pre\s*>/g, '' );
var matches; var matches;
while ( ( matches = paramExtractor.exec( templateCode ) ) !== null ) { while ( ( matches = paramExtractor.exec( templateCode ) ) !== null ) {

View file

@ -1407,7 +1407,7 @@ class TemplateDataBlobTest extends MediaWikiIntegrationTestCase {
], ],
'Params within comments and nowiki tags' => [ 'Params within comments and nowiki tags' => [
'Lorem <!-- {{{name}}} --> ipsum <nowiki > {{{middlename}}}' . 'Lorem <!-- {{{name}}} --> ipsum <nowiki > {{{middlename}}}' .
'</nowiki> {{{surname}}}', '</nowiki> <pre>{{{pre}}}</pre> {{{surname}}}',
[ 'surname' => [] ] [ 'surname' => [] ]
], ],
'Param within comments and param name outside with comment' => [ 'Param within comments and param name outside with comment' => [