Skip to content

Commit

Permalink
fixed contains operator to handle arrays with strings with special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Gupta authored and Amit Gupta committed Nov 12, 2024
1 parent 827bc73 commit a1144af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/Template/Liquid/Condition.pm
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,14 @@ sub contains {
my $_r = $s->{template}{context}->get($r);
$l = $_l if defined $_l;
$r = $_r if defined $_r;
$r = quotemeta $r;
return if defined $r && !defined $l;
return defined($l->{$r}) ? 1 : !1 if ref $l eq 'HASH';
return (grep { $_ eq $r } @$l) ? 1 : !1 if ref $l eq 'ARRAY';
if (ref $l eq 'ARRAY') {
for my $i (0 .. $#$l) {
return 1 if $l->[$i] eq $r;
}
return !1;
}
return $l =~ qr[${r}] ? 1 : !1;
}
Expand Down
12 changes: 12 additions & 0 deletions t/0200_tags/02006_if.t
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,25 @@ is( Template::Liquid->parse(
{% if 'This string' != 'This string' %}Yep.{% endif %}
INPUT

EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render(email => 'test@example.com'), <<'EXPECTED', q[assigned list contains email]);
{% assign list = "foo@bar.com, another@example.com, test@example.com" | split: ", " %}{% if list contains email %}Found.{% endif %}
INPUT
Found.
EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render(list => [qw[some other value]]), <<'EXPECTED', q[list contains 'other']);
{% if list contains 'other' %}Yep.{% endif %}
INPUT
Yep.
EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render(foo => 'bar'), <<'EXPECTED', q[assigned list contains foo]);
{% assign list = "a, b, c, bar" | split : ", " %}{% if list contains foo %}Yep.{% endif %}
INPUT
Yep.
EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render(list => [qw[some other value]]), <<'EXPECTED', q[list contains 'missing element']);
{% if list contains 'missing element' %}Yep.{% endif %}
Expand Down

0 comments on commit a1144af

Please sign in to comment.