mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-28 16:20:03 +02:00
Fixed url view function improperly stripping leading dotslash on Windows
This commit is contained in:
@@ -16,12 +16,12 @@ class Url extends ViewFunction
|
||||
*/
|
||||
public function __invoke(string $path = '/'): string
|
||||
{
|
||||
$path = preg_replace('/^[.\/]+/', '', $path);
|
||||
$path = preg_replace('/^.?(\/|\\\)+/', '', $path);
|
||||
|
||||
if (is_file($path)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
return empty($path) ? '' : sprintf('?dir=%s', ltrim($path, './'));
|
||||
return empty($path) ? '' : sprintf('?dir=%s', $path);
|
||||
}
|
||||
}
|
||||
|
@@ -7,32 +7,37 @@ use Tests\TestCase;
|
||||
|
||||
class UrlTest extends TestCase
|
||||
{
|
||||
public function test_it_can_return_a_directory_url(): void
|
||||
public function test_it_can_return_a_url_for_a_directory(): void
|
||||
{
|
||||
$url = new Url($this->container, $this->config);
|
||||
|
||||
// Forward slashes
|
||||
$this->assertEquals('', $url('/'));
|
||||
$this->assertEquals('', $url('./'));
|
||||
$this->assertEquals('?dir=some/path', $url('some/path'));
|
||||
$this->assertEquals('?dir=some/path', $url('./some/path'));
|
||||
$this->assertEquals('?dir=some/path', $url('./some/path'));
|
||||
$this->assertEquals('?dir=some/file.test', $url('some/file.test'));
|
||||
$this->assertEquals('?dir=some/file.test', $url('./some/file.test'));
|
||||
|
||||
// Back slashes
|
||||
$this->assertEquals('', $url('\\'));
|
||||
$this->assertEquals('', $url('.\\'));
|
||||
$this->assertEquals('?dir=some\path', $url('some\path'));
|
||||
$this->assertEquals('?dir=some\path', $url('.\some\path'));
|
||||
$this->assertEquals('?dir=some\file.test', $url('some\file.test'));
|
||||
$this->assertEquals('?dir=some\file.test', $url('.\some\file.test'));
|
||||
}
|
||||
|
||||
public function test_it_can_return_a_directory_url_in_a_subdirectory(): void
|
||||
{
|
||||
$_SERVER['SCRIPT_NAME'] = '/some/dir/index.php';
|
||||
|
||||
$url = new Url($this->container, $this->config);
|
||||
|
||||
$this->assertEquals('', $url('/'));
|
||||
$this->assertEquals('?dir=some/path', $url('some/path'));
|
||||
$this->assertEquals('?dir=some/file.test', $url('some/file.test'));
|
||||
}
|
||||
|
||||
public function test_it_can_return_a_file_url(): void
|
||||
public function test_it_can_return_a_url_for_a_file(): void
|
||||
{
|
||||
chdir($this->filePath('.'));
|
||||
|
||||
$url = new Url($this->container, $this->config);
|
||||
|
||||
$this->assertEquals('README.md', $url('README.md'));
|
||||
$this->assertEquals('README.md', $url('./README.md'));
|
||||
$this->assertEquals('subdir/alpha.scss', $url('subdir/alpha.scss'));
|
||||
$this->assertEquals('subdir/alpha.scss', $url('./subdir/alpha.scss'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user