Support custom style/script tags in class components

Allow class components to return full <style> or <script> tags from css() and js(), while preserving default wrapping for raw CSS/JS strings. Add data-provider coverage and fixtures for both custom style and custom script tag behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
580-view-class-steroids
fadrian06 2 weeks ago
parent dd238319da
commit 48035d6a92

@ -166,21 +166,13 @@ class View
$js = $component->js(); $js = $component->js();
if ($css && !array_key_exists($template, $this->styles)) { if ($css && !array_key_exists($template, $this->styles)) {
$view .= <<<html $view .= $this->renderComponentStyle($css);
<style>
$css
</style>
html;
$this->styles[$template] = true; $this->styles[$template] = true;
} }
if ($js && !array_key_exists($template, $this->scripts)) { if ($js && !array_key_exists($template, $this->scripts)) {
$view .= <<<html $view .= $this->renderComponentScript($js);
<script>
$js
</script>
html;
$this->scripts[$template] = true; $this->scripts[$template] = true;
} }
@ -328,4 +320,30 @@ class View
return $arguments; return $arguments;
} }
private function renderComponentStyle(string $css): string
{
if (preg_match('/^\s*<style\b[^>]*>.*<\/style>\s*$/is', $css) === 1) {
return $css;
}
return <<<html
<style>
$css
</style>
html;
}
private function renderComponentScript(string $js): string
{
if (preg_match('/^\s*<script\b[^>]*>.*<\/script>\s*$/is', $js) === 1) {
return $js;
}
return <<<html
<script>
$js
</script>
html;
}
} }

@ -343,6 +343,19 @@ class ViewTest extends TestCase
</style> </style>
html, html,
], ],
[
'page-with-class-component-with-custom-style-tag',
<<<'html'
<span class="my-class-component-with-custom-style-tag">
my-class-component-with-custom-style-tag
</span>
<style media="print" data-component="my-class-component-with-custom-style-tag">
.my-class-component-with-custom-style-tag {
color: purple;
}
</style>
html,
],
[ [
'page-with-class-component-with-scripts', 'page-with-class-component-with-scripts',
<<<'html' <<<'html'
@ -351,6 +364,15 @@ class ViewTest extends TestCase
<script>console.log('my-class-component-with-scripts')</script> <script>console.log('my-class-component-with-scripts')</script>
html, html,
], ],
[
'page-with-class-component-with-custom-script-tag',
<<<'html'
my-class-component-with-custom-script-tag
<script type="module" data-component="my-class-component-with-custom-script-tag">
console.log('my-class-component-with-custom-script-tag')
</script>
html,
],
[ [
'page-with-class-component-that-extends-another-class-component', 'page-with-class-component-that-extends-another-class-component',
<<<'html' <<<'html'

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
use flight\template\Component;
return new class extends Component
{
public function html(): string
{
return 'my-class-component-with-custom-script-tag';
}
public function js(): string
{
return <<<'js'
<script type="module" data-component="my-class-component-with-custom-script-tag">
console.log('my-class-component-with-custom-script-tag')
</script>
js;
}
};

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
use flight\template\Component;
return new class extends Component
{
public function html(): string
{
return <<<'html'
<span class="my-class-component-with-custom-style-tag">
my-class-component-with-custom-style-tag
</span>
html;
}
public function css(): string
{
return <<<'css'
<style media="print" data-component="my-class-component-with-custom-style-tag">
.my-class-component-with-custom-style-tag {
color: purple;
}
</style>
css;
}
};

@ -0,0 +1 @@
<f-my-class-component-with-custom-script-tag />

@ -0,0 +1 @@
<f-my-class-component-with-custom-style-tag />
Loading…
Cancel
Save