From ae8c7c84f2ba4c601fa42e1aa8b3fe3bba4619f2 Mon Sep 17 00:00:00 2001
From: Peter Thomson
Date: Sat, 3 Aug 2024 09:26:33 +1200
Subject: [PATCH] Options were crashing Alpine when not properly escaped and
also when 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.
---
src/Builder.php | 7 ++++++-
src/resources/views/chart-template-livewire.blade.php | 2 +-
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/src/Builder.php b/src/Builder.php
index 0783fba..abd82e1 100644
--- a/src/Builder.php
+++ b/src/Builder.php
@@ -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']),
diff --git a/src/resources/views/chart-template-livewire.blade.php b/src/resources/views/chart-template-livewire.blade.php
index c509581..81e36e3 100644
--- a/src/resources/views/chart-template-livewire.blade.php
+++ b/src/resources/views/chart-template-livewire.blade.php
@@ -1,6 +1,6 @@