From 0de32d17a81f12f7acd4ed7748721d1b1a720da1 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 16 Apr 2024 12:03:06 +0100 Subject: [PATCH] Make the SVG tag fail gracefully when `src` is empty --- src/Tags/Svg.php | 4 ++++ tests/Tags/SvgTagTest.php | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Tags/Svg.php b/src/Tags/Svg.php index eabcdf34c8..549928ebb6 100644 --- a/src/Tags/Svg.php +++ b/src/Tags/Svg.php @@ -48,6 +48,10 @@ public function index() $svg = $this->params->get('src'); } + if (empty($svg)) { + return ''; + } + $attributes = $this->renderAttributesFromParams(except: ['src', 'title', 'desc', 'sanitize']); if ($this->params->get('title') || $this->params->get('desc')) { diff --git a/tests/Tags/SvgTagTest.php b/tests/Tags/SvgTagTest.php index d2c82f12b1..f8b68ee958 100644 --- a/tests/Tags/SvgTagTest.php +++ b/tests/Tags/SvgTagTest.php @@ -15,9 +15,9 @@ public function setUp(): void File::copy(__DIR__.'/../../resources/svg/icons/light/users.svg', resource_path('users.svg')); } - private function tag($tag) + private function tag($tag, $variables = []) { - return Parse::template($tag, []); + return Parse::template($tag, $variables); } /** @test */ @@ -79,4 +79,14 @@ public function sanitizing_doesnt_remove_an_xml_tag() $this->assertEquals($svg, $this->tag('{{ svg src="xmltag" }}')); } + + /** @test */ + public function fails_gracefully_when_src_is_empty() + { + $output = $this->tag('{{ svg :src="icon" }}', [ + 'icon' => null, + ]); + + $this->assertEmpty((string) $output); + } }