Make default values fall through in switch

Bug 19093.  Previously {{#switch:foo|bar|#default=baz}} would behave
differently from {{#switch:foo|#default|bar=baz}}, even though they'd
behave identically if "#default" was replaced by "foo".

I changed switchObj but not switchHook.  In testing, switchObj was the
only method that seemed to be used, and I didn't want to make untested
changes to switchHook, so if that's used, the behavior may remain in
some cases.  It seems like code is duplicated between them -- whatever
the difference is, that should probably be factored out.

This passes all existing parser tests for #switch, of which there are
none, so don't blame me if it breaks something.  ;)
This commit is contained in:
Aryeh Gregor 2009-06-05 16:53:59 +00:00
parent 08bcb1bdc7
commit 6905775132
2 changed files with 15 additions and 2 deletions

View file

@ -187,7 +187,7 @@ class ExtParserFunctions {
return ''; return '';
} }
$primary = trim( $frame->expand( array_shift( $args ) ) ); $primary = trim( $frame->expand( array_shift( $args ) ) );
$found = false; $found = $defaultFound = false;
$default = null; $default = null;
$lastItemHadNoEquals = false; $lastItemHadNoEquals = false;
$mwDefault =& MagicWord::get( 'default' ); $mwDefault =& MagicWord::get( 'default' );
@ -209,7 +209,7 @@ class ExtParserFunctions {
if ( $test == $primary ) { if ( $test == $primary ) {
# Found a match, return now # Found a match, return now
return trim( $frame->expand( $valueNode ) ); return trim( $frame->expand( $valueNode ) );
} elseif ( $mwDefault->matchStartAndRemove( $test ) ) { } elseif ( $defaultFound || $mwDefault->matchStartAndRemove( $test ) ) {
$default = $valueNode; $default = $valueNode;
} # else wrong case, continue } # else wrong case, continue
} }
@ -220,6 +220,8 @@ class ExtParserFunctions {
$test = trim( $frame->expand( $valueNode ) ); $test = trim( $frame->expand( $valueNode ) );
if ( $test == $primary ) { if ( $test == $primary ) {
$found = true; $found = true;
} elseif ( $mwDefault->matchStartAndRemove( $test ) ) {
$defaultFound = true;
} }
} }
} }

View file

@ -41,3 +41,14 @@ Time test after the 2038 32-bit Apocalype
<p>2061 <p>2061
</p> </p>
!! end !! end
!! test
Bug 19093: Default values don't fall through in switch
!! input
<{{#switch: foo | bar | #default = DEF }}>
<{{#switch: foo | #default | bar = DEF }}>
!! result
<p>&lt;DEF&gt;
&lt;DEF&gt;
</p>
!! end