mirror of
https://github.com/twigphp/Twig.git
synced 2026-11-11 21:06:21 +00:00
removed condition on iconv as it should be always available
This commit is contained in:
+88
-144
@@ -392,7 +392,7 @@ function twig_random(Twig_Environment $env, $values = null)
|
||||
}
|
||||
if (null !== $charset = $env->getCharset()) {
|
||||
if ('UTF-8' != $charset) {
|
||||
$values = twig_convert_encoding($values, 'UTF-8', $charset);
|
||||
$values = iconv($charset, 'UTF-8', $values);
|
||||
}
|
||||
|
||||
// unicode version of str_split()
|
||||
@@ -401,7 +401,7 @@ function twig_random(Twig_Environment $env, $values = null)
|
||||
|
||||
if ('UTF-8' != $charset) {
|
||||
foreach ($values as $i => $value) {
|
||||
$values[$i] = twig_convert_encoding($value, $charset, 'UTF-8');
|
||||
$values[$i] = iconv('UTF-8', $charset, $value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -891,7 +891,7 @@ function twig_reverse_filter(Twig_Environment $env, $item, $preserveKeys = false
|
||||
$string = (string) $item;
|
||||
|
||||
if ('UTF-8' != $charset) {
|
||||
$item = twig_convert_encoding($string, 'UTF-8', $charset);
|
||||
$item = iconv($charset, 'UTF-8', $string);
|
||||
}
|
||||
|
||||
preg_match_all('/./us', $item, $matches);
|
||||
@@ -899,7 +899,7 @@ function twig_reverse_filter(Twig_Environment $env, $item, $preserveKeys = false
|
||||
$string = implode('', array_reverse($matches[0]));
|
||||
|
||||
if ('UTF-8' != $charset) {
|
||||
$string = twig_convert_encoding($string, $charset, 'UTF-8');
|
||||
$string = iconv('UTF-8', $charset, $string);
|
||||
}
|
||||
|
||||
return $string;
|
||||
@@ -1016,16 +1016,16 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
|
||||
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
|
||||
}
|
||||
|
||||
$string = twig_convert_encoding($string, 'UTF-8', $charset);
|
||||
$string = iconv($charset, 'UTF-8', $string);
|
||||
$string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
|
||||
return twig_convert_encoding($string, $charset, 'UTF-8');
|
||||
return iconv('UTF-8', $charset, $string);
|
||||
|
||||
case 'js':
|
||||
// escape all non-alphanumeric characters
|
||||
// into their \xHH or \uHHHH representations
|
||||
if ('UTF-8' != $charset) {
|
||||
$string = twig_convert_encoding($string, 'UTF-8', $charset);
|
||||
$string = iconv($charset, 'UTF-8', $string);
|
||||
}
|
||||
|
||||
if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
|
||||
@@ -1047,14 +1047,14 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
|
||||
}, $string);
|
||||
|
||||
if ('UTF-8' != $charset) {
|
||||
$string = twig_convert_encoding($string, $charset, 'UTF-8');
|
||||
$string = iconv('UTF-8', $charset, $string);
|
||||
}
|
||||
|
||||
return $string;
|
||||
|
||||
case 'css':
|
||||
if ('UTF-8' != $charset) {
|
||||
$string = twig_convert_encoding($string, 'UTF-8', $charset);
|
||||
$string = iconv($charset, 'UTF-8', $string);
|
||||
}
|
||||
|
||||
if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
|
||||
@@ -1081,14 +1081,14 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
|
||||
}, $string);
|
||||
|
||||
if ('UTF-8' != $charset) {
|
||||
$string = twig_convert_encoding($string, $charset, 'UTF-8');
|
||||
$string = iconv('UTF-8', $charset, $string);
|
||||
}
|
||||
|
||||
return $string;
|
||||
|
||||
case 'html_attr':
|
||||
if ('UTF-8' != $charset) {
|
||||
$string = twig_convert_encoding($string, 'UTF-8', $charset);
|
||||
$string = iconv($charset, 'UTF-8', $string);
|
||||
}
|
||||
|
||||
if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
|
||||
@@ -1150,7 +1150,7 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
|
||||
}, $string);
|
||||
|
||||
if ('UTF-8' != $charset) {
|
||||
$string = twig_convert_encoding($string, $charset, 'UTF-8');
|
||||
$string = iconv('UTF-8', $charset, $string);
|
||||
}
|
||||
|
||||
return $string;
|
||||
@@ -1191,146 +1191,90 @@ function twig_escape_filter_is_safe(Twig_Node $filterArgs)
|
||||
return array('html');
|
||||
}
|
||||
|
||||
if (function_exists('mb_convert_encoding')) {
|
||||
function twig_convert_encoding($string, $to, $from)
|
||||
{
|
||||
return mb_convert_encoding($string, $to, $from);
|
||||
}
|
||||
} elseif (function_exists('iconv')) {
|
||||
function twig_convert_encoding($string, $to, $from)
|
||||
{
|
||||
return iconv($from, $to, $string);
|
||||
}
|
||||
} else {
|
||||
function twig_convert_encoding($string, $to, $from)
|
||||
{
|
||||
throw new Twig_Error_Runtime('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
|
||||
}
|
||||
function twig_convert_encoding($string, $to, $from)
|
||||
{
|
||||
return iconv($from, $to, $string);
|
||||
}
|
||||
|
||||
// add multibyte extensions if possible
|
||||
if (function_exists('mb_get_info')) {
|
||||
/**
|
||||
* Returns the length of a variable.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param mixed $thing A variable
|
||||
*
|
||||
* @return int The length of the value
|
||||
*/
|
||||
function twig_length_filter(Twig_Environment $env, $thing)
|
||||
{
|
||||
return is_scalar($thing) ? mb_strlen($thing, $env->getCharset()) : count($thing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string to uppercase.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The uppercased string
|
||||
*/
|
||||
function twig_upper_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
if (null !== ($charset = $env->getCharset())) {
|
||||
return mb_strtoupper($string, $charset);
|
||||
}
|
||||
|
||||
return strtoupper($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string to lowercase.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The lowercased string
|
||||
*/
|
||||
function twig_lower_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
if (null !== ($charset = $env->getCharset())) {
|
||||
return mb_strtolower($string, $charset);
|
||||
}
|
||||
|
||||
return strtolower($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a titlecased string.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The titlecased string
|
||||
*/
|
||||
function twig_title_string_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
if (null !== ($charset = $env->getCharset())) {
|
||||
return mb_convert_case($string, MB_CASE_TITLE, $charset);
|
||||
}
|
||||
|
||||
return ucwords(strtolower($string));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a capitalized string.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The capitalized string
|
||||
*/
|
||||
function twig_capitalize_string_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
if (null !== $charset = $env->getCharset()) {
|
||||
return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset).mb_strtolower(mb_substr($string, 1, mb_strlen($string, $charset), $charset), $charset);
|
||||
}
|
||||
|
||||
return ucfirst(strtolower($string));
|
||||
}
|
||||
/**
|
||||
* Returns the length of a variable.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param mixed $thing A variable
|
||||
*
|
||||
* @return int The length of the value
|
||||
*/
|
||||
function twig_length_filter(Twig_Environment $env, $thing)
|
||||
{
|
||||
return is_scalar($thing) ? mb_strlen($thing, $env->getCharset()) : count($thing);
|
||||
}
|
||||
// and byte fallback
|
||||
else {
|
||||
/**
|
||||
* Returns the length of a variable.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param mixed $thing A variable
|
||||
*
|
||||
* @return int The length of the value
|
||||
*/
|
||||
function twig_length_filter(Twig_Environment $env, $thing)
|
||||
{
|
||||
return is_scalar($thing) ? strlen($thing) : count($thing);
|
||||
|
||||
/**
|
||||
* Converts a string to uppercase.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The uppercased string
|
||||
*/
|
||||
function twig_upper_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
if (null !== ($charset = $env->getCharset())) {
|
||||
return mb_strtoupper($string, $charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a titlecased string.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The titlecased string
|
||||
*/
|
||||
function twig_title_string_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
return ucwords(strtolower($string));
|
||||
return strtoupper($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string to lowercase.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The lowercased string
|
||||
*/
|
||||
function twig_lower_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
if (null !== ($charset = $env->getCharset())) {
|
||||
return mb_strtolower($string, $charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a capitalized string.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The capitalized string
|
||||
*/
|
||||
function twig_capitalize_string_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
return ucfirst(strtolower($string));
|
||||
return strtolower($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a titlecased string.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The titlecased string
|
||||
*/
|
||||
function twig_title_string_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
if (null !== ($charset = $env->getCharset())) {
|
||||
return mb_convert_case($string, MB_CASE_TITLE, $charset);
|
||||
}
|
||||
|
||||
return ucwords(strtolower($string));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a capitalized string.
|
||||
*
|
||||
* @param Twig_Environment $env A Twig_Environment instance
|
||||
* @param string $string A string
|
||||
*
|
||||
* @return string The capitalized string
|
||||
*/
|
||||
function twig_capitalize_string_filter(Twig_Environment $env, $string)
|
||||
{
|
||||
if (null !== $charset = $env->getCharset()) {
|
||||
return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset).mb_strtolower(mb_substr($string, 1, mb_strlen($string, $charset), $charset), $charset);
|
||||
}
|
||||
|
||||
return ucfirst(strtolower($string));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -86,31 +86,23 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testRandomFunctionOnNonUTF8String()
|
||||
{
|
||||
if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
|
||||
$this->markTestSkipped('needs iconv or mbstring');
|
||||
}
|
||||
|
||||
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
|
||||
$twig->setCharset('ISO-8859-1');
|
||||
|
||||
$text = twig_convert_encoding('Äé', 'ISO-8859-1', 'UTF-8');
|
||||
$text = iconv('UTF-8', 'ISO-8859-1', 'Äé');
|
||||
for ($i = 0; $i < 30; ++$i) {
|
||||
$rand = twig_random($twig, $text);
|
||||
$this->assertTrue(in_array(twig_convert_encoding($rand, 'UTF-8', 'ISO-8859-1'), array('Ä', 'é'), true));
|
||||
$this->assertTrue(in_array(iconv('ISO-8859-1', 'UTF-8', $rand), array('Ä', 'é'), true));
|
||||
}
|
||||
}
|
||||
|
||||
public function testReverseFilterOnNonUTF8String()
|
||||
{
|
||||
if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
|
||||
$this->markTestSkipped('needs iconv or mbstring');
|
||||
}
|
||||
|
||||
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
|
||||
$twig->setCharset('ISO-8859-1');
|
||||
|
||||
$input = twig_convert_encoding('Äé', 'ISO-8859-1', 'UTF-8');
|
||||
$output = twig_convert_encoding(twig_reverse_filter($twig, $input), 'UTF-8', 'ISO-8859-1');
|
||||
$input = iconv('UTF-8', 'ISO-8859-1', 'Äé');
|
||||
$output = iconv('ISO-8859-1', 'UTF-8', twig_reverse_filter($twig, $input));
|
||||
|
||||
$this->assertEquals($output, 'éÄ');
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
--TEST--
|
||||
"convert_encoding" filter
|
||||
--CONDITION--
|
||||
function_exists('iconv') || function_exists('mb_convert_encoding')
|
||||
--TEMPLATE--
|
||||
{{ "愛していますか?"|convert_encoding('ISO-2022-JP', 'UTF-8')|convert_encoding('UTF-8', 'ISO-2022-JP') }}
|
||||
--DATA--
|
||||
|
||||
Reference in New Issue
Block a user