diff --git a/common/framework/Template.php b/common/framework/Template.php index f4b8ca76b..a014b1e94 100644 --- a/common/framework/Template.php +++ b/common/framework/Template.php @@ -450,11 +450,17 @@ class Template * Convert a relative path using the given basepath. * * @param string $path - * @param string $basepath + * @param ?string $basepath * @return string */ - public function convertPath(string $path, string $basepath): string + public function convertPath(string $path, ?string $basepath = null): string { + // If basepath is not provided, use the relative dir of the current instance. + if ($basepath === null) + { + $basepath = $this->relative_dirname; + } + // Path relative to the Rhymix installation directory? if (preg_match('#^\^/?(\w.+)$#s', $path, $match)) { diff --git a/tests/unit/framework/TemplateTest.php b/tests/unit/framework/TemplateTest.php new file mode 100644 index 000000000..5f97fee67 --- /dev/null +++ b/tests/unit/framework/TemplateTest.php @@ -0,0 +1,54 @@ +assertTrue($tmpl->isRelativePath('foo.html')); + $this->assertTrue($tmpl->isRelativePath('foo/bar.html')); + $this->assertTrue($tmpl->isRelativePath('../foo/bar.html')); + $this->assertTrue($tmpl->isRelativePath('foo/../bar.html')); + $this->assertTrue($tmpl->isRelativePath('^/foo/../bar.html')); + $this->assertFalse($tmpl->isRelativePath('/foo/bar.html')); + $this->assertFalse($tmpl->isRelativePath('https://foo.com/bar.html')); + $this->assertFalse($tmpl->isRelativePath('file:///C:/foo/bar.html')); + $this->assertFalse($tmpl->isRelativePath('data:image/png;base64,AAAAAAAAAAA="')); + $this->assertFalse($tmpl->isRelativePath('{$foo}')); + } + + public function testConvertPath() + { + $tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'empty.html'); + + $source = 'foo.html'; + $target = 'tests/_data/template/foo.html'; + $this->assertEquals($target, $tmpl->convertPath($source)); + + $source = 'foo/bar.js'; + $target = 'tests/_data/template/foo/bar.js'; + $this->assertEquals($target, $tmpl->convertPath($source)); + + $source = '../foo.scss'; + $target = 'tests/_data/foo.scss'; + $this->assertEquals($target, $tmpl->convertPath($source)); + + $source = '../../_output/foo/../bar.jpg'; + $target = 'tests/_output/bar.jpg'; + $this->assertEquals($target, $tmpl->convertPath($source)); + + $source = '/foo/bar.blade.php'; + $target = 'tests/_data/template/foo/bar.blade.php'; + $this->assertEquals($target, $tmpl->convertPath($source)); + + $source = '^/foo/bar.gif'; + $target = '/rhymix/foo/bar.gif'; + $this->assertEquals($target, $tmpl->convertPath($source)); + } +}