Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Tags/ComponentProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ComponentProxy extends Tags
{
public static $isolated = true;
protected static $componentStack = [];
protected $rawParameters = [];

private function makeComponentTagCompiler(): ComponentTagCompiler
{
Expand All @@ -24,6 +25,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();
Expand All @@ -38,7 +46,7 @@ public function index()
$className = $tagCompiler->componentClass($componentName);

$data = $this->params->except(['component_name___', 'component_bound___'])->all();
$attributes = new ComponentAttributeBag($data);
$attributes = new ComponentAttributeBag($this->withLiteralBooleanStrings($data));
$constructorParameters = [];

$scopeData = $this->context->all();
Expand Down Expand Up @@ -125,6 +133,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 escapeBoundAttributes(array $attributes): array
{
$bound = explode(',', $this->params->get('component_bound___', ''));
Expand Down
66 changes: 66 additions & 0 deletions tests/Antlers/Components/BooleanStringAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests\Antlers\Components;

use Illuminate\Support\Facades\Blade;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\Antlers\ParserTestCase;

class BooleanStringAttributesTest extends ParserTestCase
{
public static function componentProvider()
{
return [
'antlers component' => ['boolean_strings'],
'blade component' => ['boolean_strings_blade'],
];
}

#[Test]
#[DataProvider('componentProvider')]
public function written_true_and_false_attributes_keep_their_strings($component)
{
$this->assertStringStartsWith(
'<div aria-hidden="true" aria-expanded="false">',
trim($this->renderString('<x-'.$component.' aria-hidden="true" aria-expanded="false" />'))
);
}

#[Test]
#[DataProvider('componentProvider')]
public function bound_true_and_false_strings_keep_their_strings($component)
{
$this->assertStringStartsWith(
'<div aria-hidden="true" aria-expanded="false">',
trim($this->renderString('<x-'.$component.' :aria-hidden="\'true\'" :aria-expanded="state" />', ['state' => 'false']))
);
}

#[Test]
#[DataProvider('componentProvider')]
public function bound_booleans_are_still_booleans($component)
{
$this->assertStringStartsWith(
'<div hidden="hidden">',
trim($this->renderString('<x-'.$component.' :hidden="yes" :inert="no" />', ['yes' => true, 'no' => false]))
);
}

#[Test]
#[DataProvider('componentProvider')]
public function attributes_match_blade($component)
{
$this->assertSame(
trim(Blade::render('<x-'.$component.' aria-hidden="true" aria-expanded="false" :data-state="$state" />', ['state' => 'false'])),
trim($this->renderString('<x-'.$component.' aria-hidden="true" aria-expanded="false" :data-state="state" />', ['state' => 'false']))
);
}

#[Test]
public function props_still_receive_booleans()
{
$this->assertStringEndsWith('>true</div>', trim($this->renderString('<x-boolean_strings_blade open="true" />')));
$this->assertStringEndsWith('>false</div>', trim($this->renderString('<x-boolean_strings_blade open="false" />')));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@props(['open' => null])
<div {{ attributes }}></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@props(['open' => null])
<div {{ $attributes }}>{{ var_export($open, true) }}</div>
Loading