fix(backurl): avoid double fragment when using withBackAndFragment
This commit is contained in:
@@ -152,6 +152,11 @@ class BackUrl
|
||||
*/
|
||||
public static function withBackAndFragment(string $path, string $safeBackForLinks = '', string $fragment = ''): string
|
||||
{
|
||||
// 若调用方传入了 path 自带 fragment,同时又传入 fragment 参数,避免出现 "#old#new"。
|
||||
if ((string) $fragment !== '' && str_contains((string) $path, '#')) {
|
||||
[$path] = explode('#', (string) $path, 2);
|
||||
}
|
||||
|
||||
$url = self::withBack($path, $safeBackForLinks);
|
||||
|
||||
$fragment = ltrim((string) $fragment, '#');
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Support\BackUrl;
|
||||
use Illuminate\Support\Arr;
|
||||
use Tests\TestCase;
|
||||
|
||||
class BackUrlWithBackAndFragmentOverridesExistingFragmentTest extends TestCase
|
||||
{
|
||||
public function test_with_back_and_fragment_should_override_fragment_in_path_when_fragment_param_is_provided(): void
|
||||
{
|
||||
$back = '/admin/platform-orders/2';
|
||||
$backQuery = Arr::query(['back' => $back]);
|
||||
|
||||
$url = BackUrl::withBackAndFragment('/admin/site-subscriptions/2#old', $back, 'new');
|
||||
|
||||
$this->assertSame('/admin/site-subscriptions/2?' . $backQuery . '#new', $url);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user