Skip to content
Open
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
72 changes: 24 additions & 48 deletions src/BreadcrumbsComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,30 @@

class BreadcrumbsComponent extends Component
{
/**
* @var Manager
*/
public $breadcrumbs;

/**
* @var string|null
*/
public $route;

/**
* @var mixed|null
*/
public $parameters;

/**
* @var string|null
*/
public $class;

/**
* @var string|null
*/
public $active;

/**
* Create a new component instance.
*
* @param Manager $manager
* @param string|null $route
* @param mixed|null $parameters
* @param string|null $class
* @param string|null $active
*/
public function __construct(
Manager $manager,
string $route = null,
$parameters = null,
string $class = null,
string $active = null
)
{
$this->breadcrumbs = $manager;
$this->route = $route;
$this->parameters = $parameters;
$this->class = $class;
$this->active = $active;
}
public Manager $manager,
public ?string $route = null,
public $parameters = null,
public ?string $class = null,
public ?string $active = null,
public bool $escape = true,
) {}

/**
* @return Collection
* @throws \Throwable
*
* @return Collection
*/
public function generate(): Collection
{
if ($this->route !== null) {
return $this->breadcrumbs->generate($this->route, $this->parameters);
return $this->manager->generate($this->route, $this->parameters);
}

return $this->breadcrumbs->current($this->parameters);
return $this->manager->current($this->parameters);
}

/**
Expand All @@ -78,7 +42,7 @@ public function generate(): Collection
*/
public function shouldRender(): bool
{
return $this->breadcrumbs->has($this->route);
return $this->manager->has($this->route);
}

/**
Expand All @@ -90,4 +54,16 @@ public function render()
{
return view('breadcrumbs::breadcrumbs');
}

/**
* @param Crumb $crumb
*
* @return string|null
*/
public function title(Crumb $crumb): ?string
{
$title = $crumb->title();

return $this->escape ? e($title) : $title;
}
}
16 changes: 15 additions & 1 deletion tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public function testParameters(): void
->assertSee('value 3');
}

public function testEscapedHtmlIsRenderedAsText(): void
{
$this->getComponent('escaped')
->assertSee('<strong>Important Notice</strong>')
->assertDontSee('<strong>Important Notice</strong>', false);
}

public function testUnescapedHtmlIsRenderedAsHtml(): void
{
$this->getComponent('unescaped')
->assertSee('<strong>Important Notice</strong>', false);
}

/**
* @param string $template
*
Expand All @@ -59,7 +72,8 @@ protected function getComponent(string $template): TestResponse
return $trail
->push('Home')
->push('Arguments', $value)
->push('Arguments2', $value2);
->push('Arguments2', $value2)
->push('<strong>Important Notice</strong>');
});

return $this->get("component/$template");
Expand Down
3 changes: 3 additions & 0 deletions tests/views/escaped.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-tabuna-breadcrumbs
:escape="true"
/>
3 changes: 3 additions & 0 deletions tests/views/unescaped.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-tabuna-breadcrumbs
:escape="false"
/>
10 changes: 7 additions & 3 deletions views/breadcrumbs.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
@foreach ($generate() as $crumbs)
@if ($crumbs->url() && !$loop->last)
<li class="{{$class}}">
<a href="{{ $crumbs->url() }}">{{ $crumbs->title() }}</a>
<li {{ $attributes->merge(['class' => $class]) }}>
<a href="{{ $crumbs->url() }}">
{!! $title($crumbs) !!}
</a>
</li>
@else
<li class="{{$class}} {{$active}}">{{ $crumbs->title() }}</li>
<li {{ $attributes->merge(['class' => $class. ' '. $active]) }}>
{!! $title($crumbs) !!}
</li>
@endif
@endforeach