fixed url_encode when raw is false and URL is an array

This commit is contained in:
Fabien Potencier
2014-04-25 13:33:19 +02:00
parent 49ba6b4759
commit 1930600e4e
8 changed files with 40 additions and 9 deletions
+2 -1
View File
@@ -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
+1 -1
View File
@@ -36,7 +36,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.15-dev"
"dev-master": "1.16-dev"
}
}
}
+8 -2
View File
@@ -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
+1 -1
View File
@@ -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"
+1 -1
View File
@@ -16,7 +16,7 @@
*/
class Twig_Environment
{
const VERSION = '1.15.2-DEV';
const VERSION = '1.16.0-DEV';
protected $charset;
protected $loader;
+5 -1
View File
@@ -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, '', '&');
}
@@ -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
@@ -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&amp;number=3&amp;sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&amp;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