Skip to content

[6.x] Change urlencode and rawurlencode modifiers to not ignore forward slashes #11812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2025
Merged
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
20 changes: 20 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down Expand Up @@ -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)));
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Antlers/Runtime/CoreModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}

Expand Down Expand Up @@ -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
Expand Down