Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Util\StringUtil;

/**
* Trims string data.
Expand All @@ -30,11 +31,7 @@ public function preSubmit(FormEvent $event)
return;
}

if (null !== $result = @preg_replace('/^[\pZ\p{Cc}]+|[\pZ\p{Cc}]+$/u', '', $data)) {
$event->setData($result);
} else {
$event->setData(trim($data));
}
$event->setData(StringUtil::trim($data));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,67 +39,4 @@ public function testTrimSkipNonStrings()

$this->assertSame(1234, $event->getData());
}

/**
* @dataProvider spaceProvider
*/
public function testTrimUtf8Separators($hex)
{
if (!function_exists('mb_convert_encoding')) {
$this->markTestSkipped('The "mb_convert_encoding" function is not available');
}

// Convert hexadecimal representation into binary
// H: hex string, high nibble first (UCS-2BE)
// *: repeat until end of string
$binary = pack('H*', $hex);

// Convert UCS-2BE to UTF-8
$symbol = mb_convert_encoding($binary, 'UTF-8', 'UCS-2BE');
$symbol = $symbol."ab\ncd".$symbol;

$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
$event = new FormEvent($form, $symbol);

$filter = new TrimListener();
$filter->preSubmit($event);

$this->assertSame("ab\ncd", $event->getData());
}

public function spaceProvider()
{
return array(
// separators
array('0020'),
array('00A0'),
array('1680'),
// array('180E'),
array('2000'),
array('2001'),
array('2002'),
array('2003'),
array('2004'),
array('2005'),
array('2006'),
array('2007'),
array('2008'),
array('2009'),
array('200A'),
array('2028'),
array('2029'),
array('202F'),
array('205F'),
array('3000'),
// controls
array('0009'),
array('000A'),
array('000B'),
array('000C'),
array('000D'),
array('0085'),
// zero width space
// array('200B'),
);
}
}
72 changes: 72 additions & 0 deletions src/Symfony/Component/Form/Tests/Util/StringUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Symfony\Component\Form\Tests\Util;

use Symfony\Component\Form\Util\StringUtil;

class StringUtilTest extends \PHPUnit_Framework_TestCase
{
public function testTrim()
{
$data = ' Foo! ';

$this->assertEquals('Foo!', StringUtil::trim($data));
}

/**
* @dataProvider spaceProvider
*/
public function testTrimUtf8Separators($hex)
{
if (!function_exists('mb_convert_encoding')) {
$this->markTestSkipped('The "mb_convert_encoding" function is not available');
}

// Convert hexadecimal representation into binary
// H: hex string, high nibble first (UCS-2BE)
// *: repeat until end of string
$binary = pack('H*', $hex);

// Convert UCS-2BE to UTF-8
$symbol = mb_convert_encoding($binary, 'UTF-8', 'UCS-2BE');
$symbol = $symbol."ab\ncd".$symbol;

$this->assertSame("ab\ncd", StringUtil::trim($symbol));
}

public function spaceProvider()
{
return array(
// separators
array('0020'),
array('00A0'),
array('1680'),
// array('180E'),
array('2000'),
array('2001'),
array('2002'),
array('2003'),
array('2004'),
array('2005'),
array('2006'),
array('2007'),
array('2008'),
array('2009'),
array('200A'),
array('2028'),
array('2029'),
array('202F'),
array('205F'),
array('3000'),
// controls
array('0009'),
array('000A'),
array('000B'),
array('000C'),
array('000D'),
array('0085'),
// zero width space
// array('200B'),
);
}
}
43 changes: 43 additions & 0 deletions src/Symfony/Component/Form/Util/StringUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Util;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class StringUtil
{
/**
* This class should not be instantiated.
*/
private function __construct()
{
}

/**
* Returns the trimmed data.
*
* @param string $string
*
* @return string
*/
public static function trim($string)
{
if (null !== $result = @preg_replace('/^[\pZ\p{Cc}]+|[\pZ\p{Cc}]+$/u', '', $string)) {
$string = $result;
} else {
$string = trim($string);
}

return $string;
}
}