From 1930600e4e989fe66462605fc85b2b1483cc769b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 25 Apr 2014 13:33:19 +0200 Subject: [PATCH] fixed url_encode when raw is false and URL is an array --- CHANGELOG | 3 ++- composer.json | 2 +- doc/filters/url_encode.rst | 10 ++++++++-- ext/twig/php_twig.h | 2 +- lib/Twig/Environment.php | 2 +- lib/Twig/Extension/Core.php | 6 +++++- test/Twig/Tests/Fixtures/filters/urlencode.test | 8 ++++++-- .../Fixtures/filters/urlencode_deprecated.test | 16 ++++++++++++++++ 8 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/urlencode_deprecated.test diff --git a/CHANGELOG b/CHANGELOG index 64fb31c..483aa42 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ -* 1.15.2 (2014-XX-XX) +* 1.16.0 (2014-XX-XX) + * changed url_encode to always encode according to RFC 3986 * fixed inheritance in a 'use'-hierarchy * removed the __toString policy check when the sandbox is disabled * fixed recursively calling blocks in templates with inheritance diff --git a/composer.json b/composer.json index f1288a5..98b8df0 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.15-dev" + "dev-master": "1.16-dev" } } } diff --git a/doc/filters/url_encode.rst b/doc/filters/url_encode.rst index 7a35ed1..5944e59 100644 --- a/doc/filters/url_encode.rst +++ b/doc/filters/url_encode.rst @@ -4,6 +4,10 @@ .. versionadded:: 1.12.3 Support for encoding an array as query string was added in Twig 1.12.3. +.. versionadded:: 1.16.0 + The ``raw`` argument was removed in Twig 1.16.0. Twig now always encodes + according to RFC 3986. + The ``url_encode`` filter percent encodes a given string as URL segment or an array as query string: @@ -12,7 +16,7 @@ or an array as query string: {{ "path-seg*ment"|url_encode }} {# outputs "path-seg%2Ament" #} - {{ "string with spaces"|url_encode(true) }} + {{ "string with spaces"|url_encode }} {# outputs "string%20with%20spaces" #} {{ {'param': 'value', 'foo': 'bar'}|url_encode }} @@ -21,7 +25,9 @@ or an array as query string: .. note:: Internally, Twig uses the PHP `urlencode`_ (or `rawurlencode`_ if you pass - ``true`` as the first parameter) or the `http_build_query`_ function. + ``true`` as the first parameter) or the `http_build_query`_ function. Note + that as of Twig 1.16.0, ``urlencode`` **always** uses ``rawurlencode`` (the + ``raw`` argument was removed.) .. _`urlencode`: http://php.net/urlencode .. _`rawurlencode`: http://php.net/rawurlencode diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index e3c1f2d..8a1c999 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.15.2-DEV" +#define PHP_TWIG_VERSION "1.16.0-DEV" #include "php.h" diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 636aa89..d53d620 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,7 +16,7 @@ */ class Twig_Environment { - const VERSION = '1.15.2-DEV'; + const VERSION = '1.16.0-DEV'; protected $charset; protected $loader; diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 50005b1..750ef0f 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -587,7 +587,7 @@ function twig_number_format_filter(Twig_Environment $env, $number, $decimal = nu } /** - * URL encodes a string as a path segment or an array as a query string. + * URL encodes (RFC 3986) a string as a path segment or an array as a query string. * * @param string|array $url A URL or an array of query parameters * @@ -596,6 +596,10 @@ function twig_number_format_filter(Twig_Environment $env, $number, $decimal = nu function twig_urlencode_filter($url) { if (is_array($url)) { + if (defined('PHP_QUERY_RFC3986')) { + return http_build_query($url, '', '&', PHP_QUERY_RFC3986); + } + return http_build_query($url, '', '&'); } diff --git a/test/Twig/Tests/Fixtures/filters/urlencode.test b/test/Twig/Tests/Fixtures/filters/urlencode.test index de956e7..8726159 100644 --- a/test/Twig/Tests/Fixtures/filters/urlencode.test +++ b/test/Twig/Tests/Fixtures/filters/urlencode.test @@ -1,12 +1,16 @@ --TEST-- "url_encode" filter +--CONDITION-- +defined('PHP_QUERY_RFC3986') --TEMPLATE-- {{ {foo: "bar", number: 3, "spéßi%l": "e%c0d@d", "spa ce": ""}|url_encode }} {{ {foo: "bar", number: 3, "spéßi%l": "e%c0d@d", "spa ce": ""}|url_encode|raw }} {{ {}|url_encode|default("default") }} +{{ 'spéßi%le%c0d@dspa ce'|url_encode }} --DATA-- return array() --EXPECT-- -foo=bar&number=3&sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&spa+ce= -foo=bar&number=3&sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&spa+ce= +foo=bar&number=3&sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&spa%20ce= +foo=bar&number=3&sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&spa%20ce= default +sp%C3%A9%C3%9Fi%25le%25c0d%40dspa%20ce diff --git a/test/Twig/Tests/Fixtures/filters/urlencode_deprecated.test b/test/Twig/Tests/Fixtures/filters/urlencode_deprecated.test new file mode 100644 index 0000000..11800e9 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/urlencode_deprecated.test @@ -0,0 +1,16 @@ +--TEST-- +"url_encode" filter for PHP < 5.4 and HHVM +--CONDITION-- +defined('PHP_QUERY_RFC3986') +--TEMPLATE-- +{{ {foo: "bar", number: 3, "spéßi%l": "e%c0d@d", "spa ce": ""}|url_encode }} +{{ {foo: "bar", number: 3, "spéßi%l": "e%c0d@d", "spa ce": ""}|url_encode|raw }} +{{ {}|url_encode|default("default") }} +{{ 'spéßi%le%c0d@dspa ce'|url_encode }} +--DATA-- +return array() +--EXPECT-- +foo=bar&number=3&sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&spa%20ce= +foo=bar&number=3&sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&spa%20ce= +default +sp%C3%A9%C3%9Fi%25le%25c0d%40dspa%20ce -- 1.7.2.5