Skip to content

Commit

Permalink
Split named arguments before evaluation (#14)
Browse files Browse the repository at this point in the history
* `#ueswitch`: Split arguments before evaluation
* `arrangeParams`: Split arguments before evaluation
The 1st argument is always evaluated, so we still have to check whether arguments are strings or PPNodes.
* Simplify `arrangeParams`
Only the 1st argument may be already expanded, so no need to check for the type of every argument
  • Loading branch information
Derugon authored Dec 27, 2024
1 parent 6e1af74 commit 34af568
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
18 changes: 14 additions & 4 deletions includes/ParserPower.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,22 @@ class ParserPower {
*/
public static function arrangeParams( PPFrame $frame, array $unexpandedParams ) {
$params = [];

if ( isset( $unexpandedParams[0] ) && is_string( $unexpandedParams[0] ) ) {
$pair = explode( '=', array_shift( $unexpandedParams ), 2 );
if ( count( $pair ) === 2 ) {
$params[trim( $pair[0] )] = trim( $pair[1] );
} else {
$params[] = trim( $pair[0] );
}
}

foreach ( $unexpandedParams as $unexpandedParam ) {
$param = explode( '=', $frame->expand( $unexpandedParam ), 2 );
if ( count( $param ) == 2 ) {
$params[trim( $param[0] )] = trim( $param[1] );
$bits = $unexpandedParam->splitArg();
if ( $bits['index'] === '' ) {
$params[ParserPower::expand( $frame, $bits['name'] )] = ParserPower::expand( $frame, $bits['value'] );
} else {
$params[] = trim( $param[0] );
$params[] = ParserPower::expand( $frame, $bits['value'] );
}
}

Expand Down
26 changes: 18 additions & 8 deletions includes/SimpleFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,36 +335,46 @@ public static function ueswitchRender( Parser $parser, PPFrame $frame, array $pa
return [ '', 'noparse' => false ];
}

$lastItem = $frame->expand( $params[count( $params ) - 1] );
$lastBits = $params[count( $params ) - 1]->splitArg();
$frame->expand( $lastBits['name'] );
$lastValue = $frame->expand( $lastBits['value'] );

$default = '';
$mwDefaultFound = false;
$mwDefault = $parser->getMagicWordFactory()->get( 'default' );

$keyFound = false;
foreach ( $params as $param ) {
$pair = explode( '=', $frame->expand( $param ), 2 );
$bits = $param->splitArg();
if ( $bits['index'] === '' ) {
$key = ParserPower::expand( $frame, $bits['name'] );
$value = ParserPower::expand( $frame, $bits['value'] );
} else {
$key = ParserPower::expand( $frame, $bits['value'] );
$value = null;
}

if ( !$keyFound ) {
$key = ParserPower::unescape( trim( $pair[0] ) );
$key = ParserPower::unescape( $key );
if ( $key === $switchKey ) {
$keyFound = true;
} elseif ( $mwDefault->matchStartToEnd( $key ) ) {
$mwDefaultFound = true;
}
}

if ( count( $pair ) > 1 ) {
if ( $value !== null ) {
if ( $keyFound ) {
return [ ParserPower::unescape( trim( $pair[1] ) ), 'noparse' => false ];
return [ ParserPower::unescape( $value ), 'noparse' => false ];
} elseif ( $mwDefaultFound ) {
$default = $pair[1];
$default = $value;
$mwDefaultFound = false;
}
}
}

if ( strpos( $lastItem, '=' ) === false ) {
$default = $lastItem;
if ( $lastBits['index'] !== '' ) {
$default = $lastValue;
}
return [ ParserPower::expand( $frame, $default, ParserPower::UNESCAPE ), 'noparse' => false ];
}
Expand Down

0 comments on commit 34af568

Please sign in to comment.