From 803e67f7a5333d3274cf6c0da40feb79be5089a1 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 22 May 2025 08:27:32 +0100 Subject: [PATCH 1/2] Add an option to rawurlencode to not ignore forward slashes --- src/Modifiers/CoreModifiers.php | 6 +++++- tests/Antlers/Runtime/CoreModifiersTest.php | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 80a85714f6..93d74efa18 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -1932,8 +1932,12 @@ public function random($value) * * @return string */ - public function rawurlencode($value) + public function rawurlencode($value, $params) { + if (! Arr::get($params, 0, true)) { + return rawurlencode($value); + } + return implode('/', array_map('rawurlencode', explode('/', $value))); } diff --git a/tests/Antlers/Runtime/CoreModifiersTest.php b/tests/Antlers/Runtime/CoreModifiersTest.php index 03fc062a60..226669384a 100644 --- a/tests/Antlers/Runtime/CoreModifiersTest.php +++ b/tests/Antlers/Runtime/CoreModifiersTest.php @@ -80,6 +80,7 @@ protected function setUp(): void ], 'remove_left_var' => 'https://', 'test_currency_symbol' => '£32.00', + 'test_url_encode' => 'please and thank you/Mommy', ]; } @@ -565,6 +566,12 @@ public function test_null_values_on_count_does_not_trigger_error() $this->assertSame('No', $this->renderString($template, ['variable' => collect()], true)); $this->assertSame('Yes', $this->renderString($template, ['variable' => ['One']], true)); } + + public function test_rawurlencode() + { + $this->assertSame('please%20and%20thank%20you/Mommy', $this->resultOf('{{ test_url_encode | rawurlencode }}')); + $this->assertSame('please%20and%20thank%20you%2FMommy', $this->resultOf('{{ test_url_encode | rawurlencode(false) }}')); + } } class SimpleEntryObject implements Arrayable From f4cc5060b28c8bf3057aa4fb1dfaece532703a7b Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 20 Jun 2025 08:26:37 +0100 Subject: [PATCH 2/2] Add new modifiers for except_slashes and change behaviour of current ones --- src/Modifiers/CoreModifiers.php | 24 +++++++++++++++++---- tests/Antlers/Runtime/CoreModifiersTest.php | 18 ++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 93d74efa18..c1ae3b71fa 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -1932,12 +1932,18 @@ public function random($value) * * @return string */ - public function rawurlencode($value, $params) + public function rawurlencode($value) { - if (! Arr::get($params, 0, true)) { - return rawurlencode($value); - } + return rawurlencode($value); + } + /** + * URL-encode according to RFC 3986, but allowing slashes to persist + * + * @return string + */ + public function rawurlencode_except_slashes($value) + { return implode('/', array_map('rawurlencode', explode('/', $value))); } @@ -2847,6 +2853,16 @@ public function urldecode($value) * @return string */ public function urlencode($value) + { + return urlencode($value); + } + + /** + * URL-encodes string, but allowing slashes to persist + * + * @return string + */ + public function urlencode_except_slashes($value) { return implode('/', array_map('urlencode', explode('/', $value))); } diff --git a/tests/Antlers/Runtime/CoreModifiersTest.php b/tests/Antlers/Runtime/CoreModifiersTest.php index 226669384a..6429391082 100644 --- a/tests/Antlers/Runtime/CoreModifiersTest.php +++ b/tests/Antlers/Runtime/CoreModifiersTest.php @@ -567,10 +567,24 @@ public function test_null_values_on_count_does_not_trigger_error() $this->assertSame('Yes', $this->renderString($template, ['variable' => ['One']], true)); } + public function test_urlencode() + { + $this->assertSame('please+and+thank+you%2FMommy', $this->resultOf('{{ test_url_encode | urlencode }}')); + } + + public function test_urlencode_except_slashes() + { + $this->assertSame('please+and+thank+you/Mommy', $this->resultOf('{{ test_url_encode | urlencode_except_slashes }}')); + } + public function test_rawurlencode() { - $this->assertSame('please%20and%20thank%20you/Mommy', $this->resultOf('{{ test_url_encode | rawurlencode }}')); - $this->assertSame('please%20and%20thank%20you%2FMommy', $this->resultOf('{{ test_url_encode | rawurlencode(false) }}')); + $this->assertSame('please%20and%20thank%20you%2FMommy', $this->resultOf('{{ test_url_encode | rawurlencode }}')); + } + + public function test_rawurlencode_except_slashes() + { + $this->assertSame('please%20and%20thank%20you/Mommy', $this->resultOf('{{ test_url_encode | rawurlencode_except_slashes }}')); } }