Skip to content

Commit 30b2a6f

Browse files
authored
fix(view): throw exception when parsing xml views with short_open_tag enabled (#1795)
1 parent 3556acb commit 30b2a6f

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\View\Exceptions;
6+
7+
use Exception;
8+
9+
final class XmlDeclarationCouldNotBeParsed extends Exception
10+
{
11+
public function __construct()
12+
{
13+
parent::__construct('Cannot compile views with XML declarations when PHP\'s `short_open_tag` is enabled.');
14+
}
15+
}

packages/view/src/Parser/TempestViewCompiler.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Tempest\View\Element;
1212
use Tempest\View\Elements\ElementFactory;
1313
use Tempest\View\Exceptions\ViewNotFound;
14+
use Tempest\View\Exceptions\XmlDeclarationCouldNotBeParsed;
1415
use Tempest\View\ShouldBeRemoved;
1516
use Tempest\View\View;
1617

@@ -40,6 +41,11 @@ public function compile(string|View $view): string
4041
// 1. Retrieve template
4142
$template = $this->retrieveTemplate($view);
4243

44+
// Check for XML declarations when short_open_tag is enabled
45+
if (ini_get('short_open_tag') && str_contains($template, needle: '<?xml')) {
46+
throw new XmlDeclarationCouldNotBeParsed();
47+
}
48+
4349
// 2. Remove comments before parsing
4450
$template = $this->removeComments($template);
4551

packages/view/tests/StandaloneViewRendererTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Tempest\View\Exceptions\ViewComponentPathWasInvalid;
77
use Tempest\View\Exceptions\ViewComponentPathWasNotFound;
8+
use Tempest\View\Exceptions\XmlDeclarationCouldNotBeParsed;
89
use Tempest\View\Renderers\TempestViewRenderer;
910
use Tempest\View\ViewCache;
1011
use Tempest\View\ViewComponent;
@@ -106,6 +107,18 @@ public function test_with_cache_disabled(): void
106107
HTML, $html);
107108
}
108109

110+
public function test_xml_declaration_with_short_open_tag(): void
111+
{
112+
if (! ini_get('short_open_tag')) {
113+
$this->markTestSkipped('This test requires short_open_tag to be enabled.');
114+
}
115+
116+
$this->expectException(XmlDeclarationCouldNotBeParsed::class);
117+
118+
$renderer = TempestViewRenderer::make();
119+
$renderer->render('<?xml version="1.0" encoding="UTF-8" ?><test></test>');
120+
}
121+
109122
protected function assertSnippetsMatch(string $expected, string $actual): void
110123
{
111124
$expected = str_replace([PHP_EOL, ' '], '', $expected);

tests/Integration/View/TempestViewRendererTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tempest\Discovery\DiscoveryLocation;
99
use Tempest\Support\Html\HtmlString;
1010
use Tempest\View\Exceptions\ElementWasInvalid;
11+
use Tempest\View\Exceptions\XmlDeclarationCouldNotBeParsed;
1112
use Tempest\View\Renderers\TempestViewRenderer;
1213
use Tempest\View\ViewCache;
1314
use Tests\Tempest\Fixtures\Controllers\RelativeViewController;
@@ -800,7 +801,7 @@ public function test_multiline_view_comments(): void
800801
<x-input
801802
name="test"
802803
type="text"
803-
class="block dark:bg-neutral-900 disabled:opacity-50 px-4 py-2.5 sm:py-3 border-1 border-gray-500 dark:border-neutral-700 focus:border-blue-500 rounded-lg focus:ring-blue-500 dark:focus:ring-neutral-600 w-full dark:text-neutral-400 sm:text-sm disabled:pointer-events-none dark:placeholder-neutral-500" placeholder="This is placeholder" />
804+
class="block dark:bg-neutral-900 disabled:opacity-50 px-4 py-2.5 sm:py-3 border-1 border-gray-500 focus:border-blue-500 dark:border-neutral-700 rounded-lg focus:ring-blue-500 dark:focus:ring-neutral-600 w-full dark:text-neutral-400 sm:text-sm disabled:pointer-events-none dark:placeholder-neutral-500" placeholder="This is placeholder" />
804805
<!-- end -->
805806
</div>
806807
--}}
@@ -812,6 +813,10 @@ class="block dark:bg-neutral-900 disabled:opacity-50 px-4 py-2.5 sm:py-3 border-
812813

813814
public function test_parse_rss_feed(): void
814815
{
816+
if (ini_get('short_open_tag')) {
817+
$this->expectException(XmlDeclarationCouldNotBeParsed::class);
818+
}
819+
815820
$rss = <<<'XML'
816821
<?xml version="1.0" encoding="UTF-8" ?>
817822
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">

0 commit comments

Comments
 (0)