[ic] Bug in "EITHER" coretag
Kevin Walsh
kevin at cursor.biz
Fri Mar 9 20:17:02 EST 2007
Carl Bailey <carl at triangleresearch.com> wrote:
> We found what we believe to be a fault in the [either] coretag.
>
> We used the following code on a page:
> [either][cgi idxnum][or]0[/either]
> When the cgi value is absent this tag returns nothing -- a null string,
> rather than the desired '0' character.
>
> The reason for this is that the either coretag splits its body on
> "[or]" and interpolates each chunk in turn, removes leading and
> trailing spaces from the result and returns the first "true" result.
> However in determining what to return, it uses the statement "return
> $result if $result;" Obviously when $result = '0', this fails to
> return anything. Further, it's not hard to picture a scenario where
> NOT trimming space characters would be desired.
>
> We therefore propose, for your consideration, the following replacement
> to the tag, which adds a "notrim" option, and corrects the return
> problem as well. We have used this tag successfully in our IC 5.2
> environment:
>
> UserTag either Order notrim
> UserTag either hasEndTag
> UserTag either PosNumber 0
> UserTag either Version $Revision: 1.3.1 $
> UserTag either Routine <<EOR
> sub {
> my ($notrim, $body) = @_;
> my @ary = split /\[or\]/, $body;
> my $result;
> while(@ary) {
> $result = interpolate_html(shift @ary);
> unless ($notrim) {
> $result =~ s/^\s+//;
> $result =~ s/\s+$//;
> }
> return $result if length($result);
> }
> return;
> }
> EOR
>
> Usage Example:
> [seti delimiter][either notrim=1][scratch user_delim][or]
> [/either][/seti]
>
Oops - I sent the previous followup too early. Sorry about that.
I don't like the above, as it changes the behaviour of the [either]
tag. The above will treat "0" as a true, rather than a false, value.
I think that the following patch may be better:
--- code/SystemTag/either.coretag 24 Mar 2006 19:30:11 -0000 1.4
+++ code/SystemTag/either.coretag 10 Mar 2007 01:14:40 -0000
@@ -16,6 +16,6 @@
$result =~ s/\s+$//;
return $result if $result;
}
- return;
+ return $result;
}
EOR
The above simply returns the last block in the split list if none of
them were found to be true. Using the example you posted, the changed
tag would return "0".
--
_/ _/ _/_/_/_/ _/ _/ _/_/_/ _/ _/
_/_/_/ _/_/ _/ _/ _/ _/_/ _/ K e v i n W a l s h
_/ _/ _/ _/ _/ _/ _/ _/_/ kevin at cursor.biz
_/ _/ _/_/_/_/ _/ _/_/_/ _/ _/
More information about the interchange-users
mailing list