Skip to content

Commit

Permalink
Filters: trim() is content type aware
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 17, 2017
1 parent b6163f1 commit 9f7a12a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Latte/Runtime/FilterExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FilterExecutor
'striphtml' => ['Latte\Runtime\Filters::stripHtml', TRUE],
'striptags' => ['Latte\Runtime\Filters::stripTags', TRUE],
'substr' => ['Latte\Runtime\Filters::substring', FALSE],
'trim' => ['Latte\Runtime\Filters::trim', FALSE],
'trim' => ['Latte\Runtime\Filters::trim', TRUE],
'truncate' => ['Latte\Runtime\Filters::truncate', FALSE],
'upper' => ['Latte\Runtime\Filters::upper', FALSE],
];
Expand Down
8 changes: 4 additions & 4 deletions src/Latte/Runtime/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,11 @@ private static function strLength($s)

/**
* Strips whitespace.
* @param string plain text
* @param string plain text
* @return string plain text
* @param string
* @param string
* @return string
*/
public static function trim($s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
public static function trim(FilterInfo $info, $s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
{
$charlist = preg_quote($charlist, '#');
$s = preg_replace('#^['.$charlist.']+|['.$charlist.']+\z#u', '', (string) $s);
Expand Down
15 changes: 9 additions & 6 deletions tests/Latte/Filters.trim().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
* Test: Latte\Runtime\Filters::trim()
*/

use Latte\Engine;
use Latte\Runtime\Filters;
use Latte\Runtime\FilterInfo;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


Assert::same('x', Filters::trim(" \t\n\r\x00\x0B\xC2\xA0x"));
Assert::same('a b', Filters::trim(' a b '));
Assert::same(' a b ', Filters::trim(' a b ', ''));
Assert::same('e', Filters::trim("\xc5\x98e-", "\xc5\x98-")); // Ře-
$info = new FilterInfo(Engine::CONTENT_TEXT);
Assert::same('x', Filters::trim($info, " \t\n\r\x00\x0B\xC2\xA0x"));
Assert::same('a b', Filters::trim($info, ' a b '));
Assert::same(' a b ', Filters::trim($info, ' a b ', ''));
Assert::same('e', Filters::trim($info, "\xc5\x98e-", "\xc5\x98-")); // Ře-

Assert::exception(function () {
Filters::trim("\xC2x\xA0");
Assert::exception(function () use ($info) {
Filters::trim($info, "\xC2x\xA0");
}, 'Latte\RegexpException', NULL);

0 comments on commit 9f7a12a

Please sign in to comment.