From dfdd555b9a343e35ad4dc6983208fa42adcd3079 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 29 Oct 2011 14:50:10 +0200 Subject: [PATCH] fixed in operator for empty strings --- CHANGELOG | 1 + lib/Twig/Extension/Core.php | 3 ++ test/Twig/Tests/Fixtures/expressions/in.test | 20 ------------------ test/Twig/Tests/Fixtures/tests/in.test | 28 ++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 20 deletions(-) delete mode 100644 test/Twig/Tests/Fixtures/expressions/in.test create mode 100644 test/Twig/Tests/Fixtures/tests/in.test diff --git a/CHANGELOG b/CHANGELOG index 1b6a012..d758fa9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.4.0 + * fixed in operator for empty strings * fixed the "defined" test and the "default" filter (now works with more than one call (foo.bar.foo) and for both values of the strict_variables option) * changed the way extensions are loaded (addFilter/addFunction/addGlobal/addTest/addNodeVisitor/addTokenParser/addExtension can now be called in any order) * added Twig_Environment::display() diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 965a9cc..3aae743 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -443,6 +443,9 @@ function twig_in_filter($value, $compare) if (is_array($compare)) { return in_array($value, $compare); } elseif (is_string($compare)) { + if (!$compare) { + return empty($value); + } return false !== strpos($compare, (string) $value); } elseif (is_object($compare) && $compare instanceof Traversable) { return in_array($value, iterator_to_array($compare, false)); diff --git a/test/Twig/Tests/Fixtures/expressions/in.test b/test/Twig/Tests/Fixtures/expressions/in.test deleted file mode 100644 index a5cbfb8..0000000 --- a/test/Twig/Tests/Fixtures/expressions/in.test +++ /dev/null @@ -1,20 +0,0 @@ ---TEST-- -Twig supports the in operator ---TEMPLATE-- -{% if bar in foo %} -TRUE -{% endif %} -{% if not (bar in foo) %} -{% else %} -TRUE -{% endif %} -{% if bar not in foo %} -{% else %} -TRUE -{% endif %} ---DATA-- -return array('bar' => 'bar', 'foo' => array('bar' => 'bar')) ---EXPECT-- -TRUE -TRUE -TRUE diff --git a/test/Twig/Tests/Fixtures/tests/in.test b/test/Twig/Tests/Fixtures/tests/in.test new file mode 100644 index 0000000..ba40f25 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tests/in.test @@ -0,0 +1,28 @@ +--TEST-- +Twig supports the in operator +--TEMPLATE-- +{% if bar in foo %} +TRUE +{% endif %} +{% if not (bar in foo) %} +{% else %} +TRUE +{% endif %} +{% if bar not in foo %} +{% else %} +TRUE +{% endif %} +{% if '' not in foo %} +TRUE +{% endif %} +{% if '' in '' %} +TRUE +{% endif %} +--DATA-- +return array('bar' => 'bar', 'foo' => array('bar' => 'bar')) +--EXPECT-- +TRUE +TRUE +TRUE +TRUE +TRUE -- 1.7.2.5