From 9f7a12af3dbc940561e82a6a8661b28f00a6a244 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 12 Apr 2017 17:12:17 +0200 Subject: [PATCH] Filters: trim() is content type aware --- src/Latte/Runtime/FilterExecutor.php | 2 +- src/Latte/Runtime/Filters.php | 8 ++++---- tests/Latte/Filters.trim().phpt | 15 +++++++++------ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Latte/Runtime/FilterExecutor.php b/src/Latte/Runtime/FilterExecutor.php index 4b5d721e7..0a5c0d91b 100644 --- a/src/Latte/Runtime/FilterExecutor.php +++ b/src/Latte/Runtime/FilterExecutor.php @@ -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], ]; diff --git a/src/Latte/Runtime/Filters.php b/src/Latte/Runtime/Filters.php index 0dccbffbf..16cfef29b 100644 --- a/src/Latte/Runtime/Filters.php +++ b/src/Latte/Runtime/Filters.php @@ -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); diff --git a/tests/Latte/Filters.trim().phpt b/tests/Latte/Filters.trim().phpt index 85017c8b3..e1d253966 100644 --- a/tests/Latte/Filters.trim().phpt +++ b/tests/Latte/Filters.trim().phpt @@ -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);