r51509 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r51508‎ | r51509 | r51510 >
Date:16:53, 5 June 2009
Author:simetrical
Status:ok
Tags:
Comment:
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. ;)
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions_body.php (modified) (history)
  • /trunk/extensions/ParserFunctions/funcsParserTests.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions_body.php
@@ -187,7 +187,7 @@
188188 return '';
189189 }
190190 $primary = trim( $frame->expand( array_shift( $args ) ) );
191 - $found = false;
 191+ $found = $defaultFound = false;
192192 $default = null;
193193 $lastItemHadNoEquals = false;
194194 $mwDefault =& MagicWord::get( 'default' );
@@ -209,7 +209,7 @@
210210 if ( $test == $primary ) {
211211 # Found a match, return now
212212 return trim( $frame->expand( $valueNode ) );
213 - } elseif ( $mwDefault->matchStartAndRemove( $test ) ) {
 213+ } elseif ( $defaultFound || $mwDefault->matchStartAndRemove( $test ) ) {
214214 $default = $valueNode;
215215 } # else wrong case, continue
216216 }
@@ -220,6 +220,8 @@
221221 $test = trim( $frame->expand( $valueNode ) );
222222 if ( $test == $primary ) {
223223 $found = true;
 224+ } elseif ( $mwDefault->matchStartAndRemove( $test ) ) {
 225+ $defaultFound = true;
224226 }
225227 }
226228 }
Index: trunk/extensions/ParserFunctions/funcsParserTests.txt
@@ -41,3 +41,14 @@
4242 <p>2061
4343 </p>
4444 !! end
 45+
 46+!! test
 47+Bug 19093: Default values don't fall through in switch
 48+!! input
 49+<{{#switch: foo | bar | #default = DEF }}>
 50+<{{#switch: foo | #default | bar = DEF }}>
 51+!! result
 52+<p>&lt;DEF&gt;
 53+&lt;DEF&gt;
 54+</p>
 55+!! end

Follow-up revisions

RevisionCommit summaryAuthorDate
r51511Make r51509 work with older MediaWiki versions too...simetrical17:14, 5 June 2009

Status & tagging log