From f5d3d8506f51cbfb8557d735b7d879fba38e82d6 Mon Sep 17 00:00:00 2001 From: Jack Sleight Date: Fri, 9 Oct 2026 20:24:34 +0100 Subject: [PATCH] Keep true and false strings in component attributes from Antlers --- src/Tags/ComponentProxy.php | 19 +++++- .../BooleanStringAttributesTest.php | 66 +++++++++++++++++++ .../components/boolean_strings.antlers.html | 2 + .../boolean_strings_blade.blade.php | 2 + 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/Antlers/Components/BooleanStringAttributesTest.php create mode 100644 tests/__fixtures__/views/components/boolean_strings.antlers.html create mode 100644 tests/__fixtures__/views/components/boolean_strings_blade.blade.php diff --git a/src/Tags/ComponentProxy.php b/src/Tags/ComponentProxy.php index 5a18593cc87..68e5105d54e 100644 --- a/src/Tags/ComponentProxy.php +++ b/src/Tags/ComponentProxy.php @@ -14,6 +14,7 @@ class ComponentProxy extends Tags { public static $isolated = true; protected static $componentStack = []; + protected $rawParameters = []; private function makeComponentTagCompiler(): ComponentTagCompiler { @@ -23,6 +24,13 @@ private function makeComponentTagCompiler(): ComponentTagCompiler return new ComponentTagCompiler($bladeCompiler->getClassComponentAliases(), $bladeCompiler->getClassComponentNamespaces(), $bladeCompiler); } + public function setParameters($parameters) + { + $this->rawParameters = $parameters; + + return parent::setParameters($parameters); + } + public function index() { $___obLevel = ob_get_level(); @@ -37,7 +45,7 @@ public function index() $className = $tagCompiler->componentClass($componentName); $data = $this->params->except('component_name___')->all(); - $attributes = new ComponentAttributeBag($data); + $attributes = new ComponentAttributeBag($this->withLiteralBooleanStrings($data)); $constructorParameters = []; $scopeData = $this->context->all(); @@ -122,6 +130,15 @@ public function componentSlot() } } + protected function withLiteralBooleanStrings(array $data): array + { + return collect($data) + ->map(fn ($value, $key) => is_bool($value) && in_array($this->rawParameters[$key] ?? null, ['true', 'false'], true) + ? $this->rawParameters[$key] + : $value) + ->all(); + } + protected function handleViewException(Throwable $e, $obLevel) { while (ob_get_level() > $obLevel) { diff --git a/tests/Antlers/Components/BooleanStringAttributesTest.php b/tests/Antlers/Components/BooleanStringAttributesTest.php new file mode 100644 index 00000000000..a1ae071f82c --- /dev/null +++ b/tests/Antlers/Components/BooleanStringAttributesTest.php @@ -0,0 +1,66 @@ + ['boolean_strings'], + 'blade component' => ['boolean_strings_blade'], + ]; + } + + #[Test] + #[DataProvider('componentProvider')] + public function written_true_and_false_attributes_keep_their_strings($component) + { + $this->assertStringStartsWith( + '