Skip to content

Commit

Permalink
Options were crashing Alpine when not properly escaped and also when …
Browse files Browse the repository at this point in the history
…options were null. This change uses the Builder to manage null options and provide a fallback. It also uses a normal blade variable instead of an unguarded variable so the value is properly escaped.
  • Loading branch information
Peter Thomson committed Aug 2, 2024
1 parent e0776ad commit ae8c7c8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ public function render()
$inLivewire = $chart['inLivewire'] ?? (class_exists('Livewire\\Livewire') ? \Livewire\Livewire::isLivewireRequest() : false);
$view = ($inLivewire ? 'laravelchartjs::chart-template-livewire' : 'laravelchartjs::chart-template');

$options = $chart['optionsRaw'] ?? json_encode($chart['options']);
$optionsFallback = "{}";

$optionsSimple = $chart['options'] ? json_encode($chart['options'], true) : null;

$options = $chart['optionsRaw'] ?? $optionsSimple ?? $optionsFallback;


return view($view)->with([
'datasets' => json_encode($chart['datasets']),
Expand Down
2 changes: 1 addition & 1 deletion src/resources/views/chart-template-livewire.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<div
x-data="chart(@js($this->{$model}), {!! $options !!})"
x-data="chart(@js($this->{$model}), {{ $options }} )"
wire:loading.class="opacity-50"
>
<canvas width="@js($size['width'])" height="@js($size['height'])" wire:ignore></canvas>
Expand Down

0 comments on commit ae8c7c8

Please sign in to comment.