diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 80a85714f6..c1ae3b71fa 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -1933,6 +1933,16 @@ public function random($value) * @return string */ public function 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))); } @@ -2843,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 03fc062a60..6429391082 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,26 @@ 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_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%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 }}')); + } } class SimpleEntryObject implements Arrayable