diff --git a/flight/template/View.php b/flight/template/View.php index 453f533..babcf5d 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -166,21 +166,13 @@ class View $js = $component->js(); if ($css && !array_key_exists($template, $this->styles)) { - $view .= << - $css - - html; + $view .= $this->renderComponentStyle($css); $this->styles[$template] = true; } if ($js && !array_key_exists($template, $this->scripts)) { - $view .= << - $js - - html; + $view .= $this->renderComponentScript($js); $this->scripts[$template] = true; } @@ -328,4 +320,30 @@ class View return $arguments; } + + private function renderComponentStyle(string $css): string + { + if (preg_match('/^\s*]*>.*<\/style>\s*$/is', $css) === 1) { + return $css; + } + + return << + $css + + html; + } + + private function renderComponentScript(string $js): string + { + if (preg_match('/^\s*]*>.*<\/script>\s*$/is', $js) === 1) { + return $js; + } + + return << + $js + + html; + } } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 78c5d2d..1357fb7 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -343,6 +343,19 @@ class ViewTest extends TestCase html, ], + [ + 'page-with-class-component-with-custom-style-tag', + <<<'html' + + my-class-component-with-custom-style-tag + + + html, + ], [ 'page-with-class-component-with-scripts', <<<'html' @@ -351,6 +364,15 @@ class ViewTest extends TestCase html, ], + [ + 'page-with-class-component-with-custom-script-tag', + <<<'html' + my-class-component-with-custom-script-tag + + html, + ], [ 'page-with-class-component-that-extends-another-class-component', <<<'html' diff --git a/tests/views/components/my-class-component-with-custom-script-tag.php b/tests/views/components/my-class-component-with-custom-script-tag.php new file mode 100644 index 0000000..87ee8b1 --- /dev/null +++ b/tests/views/components/my-class-component-with-custom-script-tag.php @@ -0,0 +1,22 @@ + + console.log('my-class-component-with-custom-script-tag') + + js; + } +}; diff --git a/tests/views/components/my-class-component-with-custom-style-tag.php b/tests/views/components/my-class-component-with-custom-style-tag.php new file mode 100644 index 0000000..f6c65b8 --- /dev/null +++ b/tests/views/components/my-class-component-with-custom-style-tag.php @@ -0,0 +1,28 @@ + + my-class-component-with-custom-style-tag + + html; + } + + public function css(): string + { + return <<<'css' + + css; + } +}; diff --git a/tests/views/pages/page-with-class-component-with-custom-script-tag.php b/tests/views/pages/page-with-class-component-with-custom-script-tag.php new file mode 100644 index 0000000..bec475e --- /dev/null +++ b/tests/views/pages/page-with-class-component-with-custom-script-tag.php @@ -0,0 +1 @@ + diff --git a/tests/views/pages/page-with-class-component-with-custom-style-tag.php b/tests/views/pages/page-with-class-component-with-custom-style-tag.php new file mode 100644 index 0000000..5f21043 --- /dev/null +++ b/tests/views/pages/page-with-class-component-with-custom-style-tag.php @@ -0,0 +1 @@ +