feat: throw exception when duplicated label is found under legacy ID mode

This commit is contained in:
alistair3149 2024-11-16 01:55:25 -05:00 committed by alistair3149
parent df9897441a
commit ab8e306fc6
2 changed files with 23 additions and 5 deletions

View file

@ -43,9 +43,7 @@
"name": "ext.tabberNeue/config.json", "name": "ext.tabberNeue/config.json",
"config": { "config": {
"enableAnimation": "TabberNeueEnableAnimation", "enableAnimation": "TabberNeueEnableAnimation",
"parseTabName": "TabberNeueParseTabName", "updateLocationOnTabChange": "TabberNeueUpdateLocationOnTabChange"
"updateLocationOnTabChange": "TabberNeueUpdateLocationOnTabChange",
"useLegacyTabIds": "TabberNeueUseLegacyTabIds"
} }
}, },
"ext.tabberNeue/Transclude.js", "ext.tabberNeue/Transclude.js",

View file

@ -17,6 +17,7 @@ namespace MediaWiki\Extension\TabberNeue;
use Html; use Html;
use JsonException; use JsonException;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use InvalidArgumentException;
use Parser; use Parser;
use PPFrame; use PPFrame;
use Sanitizer; use Sanitizer;
@ -27,11 +28,14 @@ class Tabber {
/** @var bool Flag that checks if this is a nested tabber */ /** @var bool Flag that checks if this is a nested tabber */
private static $isNested = false; private static $isNested = false;
/** @var bool */
private static $parseTabName = false;
/** @var bool */ /** @var bool */
private static $useCodex = false; private static $useCodex = false;
/** @var bool */ /** @var bool */
private static $parseTabName = false; private static $useLegacyId = false;
/** /**
* Parser callback for <tabber> tag * Parser callback for <tabber> tag
@ -49,6 +53,8 @@ class Tabber {
self::$parseTabName = $config->get( 'TabberNeueParseTabName' ); self::$parseTabName = $config->get( 'TabberNeueParseTabName' );
self::$useCodex = $config->get( 'TabberNeueUseCodex' ); self::$useCodex = $config->get( 'TabberNeueUseCodex' );
self::$useLegacyId = $config->get( 'TabberNeueUseLegacyTabIds' );
$count = count( $parserOutput->getExtensionData( 'tabber-count' ) ?? [] ); $count = count( $parserOutput->getExtensionData( 'tabber-count' ) ?? [] );
$html = self::render( $input ?? '', $count, $parser, $frame ); $html = self::render( $input ?? '', $count, $parser, $frame );
@ -189,6 +195,7 @@ class Tabber {
* @param PPFrame $frame Mediawiki PPFrame Object * @param PPFrame $frame Mediawiki PPFrame Object
* *
* @return array<string, string> * @return array<string, string>
* @throws MWException
*/ */
private static function getTabData( string $tab, int $count, Parser $parser, PPFrame $frame ): array { private static function getTabData( string $tab, int $count, Parser $parser, PPFrame $frame ): array {
$data = []; $data = [];
@ -206,7 +213,20 @@ class Tabber {
$data['content'] = self::getTabContent( $content, $parser, $frame ); $data['content'] = self::getTabContent( $content, $parser, $frame );
$id = Sanitizer::escapeIdForAttribute( htmlspecialchars( $data['label'] ) ) . '-' . $count; $id = Sanitizer::escapeIdForAttribute( htmlspecialchars( $data['label'] ) );
if ( self::$useLegacyId === true ) {
$parserOutput = $parser->getOutput();
$existingIds = $parserOutput->getExtensionData( 'tabber-ids' ) ?? [];
if ( in_array( $id, $existingIds ) ) {
throw new InvalidArgumentException(
'Duplicated Tabber labels is not allowed with $wgTabberNeueUseLegacyTabIds = true.' .
'Label was: ' . $label
);
}
$parserOutput->appendExtensionData( 'tabber-ids', $id );
} else {
$id = "$id-$count";
}
$data['id'] = $id; $data['id'] = $id;
return $data; return $data;
} }