From 65e5693db961e10ad96b0017865256334f8c9933 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Thu, 19 Jun 2025 10:01:42 +0200 Subject: [PATCH 1/5] Do not render empty class or style attribute --- src/Helpers/Html.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Helpers/Html.php b/src/Helpers/Html.php index b77d49f..61c8bd3 100644 --- a/src/Helpers/Html.php +++ b/src/Helpers/Html.php @@ -217,6 +217,13 @@ public static function renderAttributes(Environment $env, array $attributes):str continue; } + $value = trim($value); + + // Prevent printing empty style or class attributes + if (($key === 'style' || $key === 'class') && $value === '') { + continue; + } + $renderedAttributes[] = sprintf('%s="%s"', $key, $value); } From 28a2a37206a2ad8e27771ef48c371353a7591a67 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Thu, 19 Jun 2025 10:01:45 +0200 Subject: [PATCH 2/5] Add tests --- tests/Helpers/HtmlTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Helpers/HtmlTest.php b/tests/Helpers/HtmlTest.php index 45ff492..be6418d 100644 --- a/tests/Helpers/HtmlTest.php +++ b/tests/Helpers/HtmlTest.php @@ -1,5 +1,7 @@ loader->setTemplate('index', $tpl); assertMatchesSnapshot(test()->twig->render('index')); }); + + +test('The Html::renderAttributes() method does not print empty class or style attribute', function() { + expect(Html::renderAttributes(test()->twig, ['class' => 'foo', 'style' => '', 'id' => '']))->toBe(' class="foo" id=""'); + expect(Html::renderAttributes(test()->twig, ['class' => ' ', 'id' => '']))->toBe(' id=""'); + expect(Html::renderAttributes(test()->twig, ['class' => '', 'style' => ['display' => 'none'], 'id' => '']))->toBe(' style="display: none;" id=""'); +}); From c84dd2615baf49072eed01cb396537851eaea102 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Thu, 19 Jun 2025 10:07:46 +0200 Subject: [PATCH 3/5] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19cbb30..1a0c23a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Changed + +- Do not render empty `class` or `style` attribute ([#38](https://github.com/studiometa/twig-toolkit/pull/38), [65e5693](https://github.com/studiometa/twig-toolkit/commit/65e5693)) + ## v2.1.0 (2025-06-17) ### Added From bdb96e77a3949529d06a04decfb743c95290f466 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Thu, 19 Jun 2025 10:16:57 +0200 Subject: [PATCH 4/5] Add missing tests --- tests/Helpers/UrlTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Helpers/UrlTest.php b/tests/Helpers/UrlTest.php index 1bf124a..ea30a37 100644 --- a/tests/Helpers/UrlTest.php +++ b/tests/Helpers/UrlTest.php @@ -16,3 +16,7 @@ test('The `Url` class should not encode URL parameters', function () { expect((string)Url::fromString('http://localhost/?key=value&foo=1/2'))->toBe('http://localhost?key=value&foo=1/2'); }); + +test('The `Url::withQuery` method should replace the current query', function() { + expect((string)Url::fromString('http://localhost/?key=value&foo=1/2')->withQuery('foo=bar'))->toBe('http://localhost?foo=bar'); +}); From 57b25028e89ba295e91fb556a3dac173bccbb2d2 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Thu, 19 Jun 2025 10:19:22 +0200 Subject: [PATCH 5/5] Bump version number --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a0c23a..5092cc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +## v2.1.1 (2025-06-19) + ### Changed - Do not render empty `class` or `style` attribute ([#38](https://github.com/studiometa/twig-toolkit/pull/38), [65e5693](https://github.com/studiometa/twig-toolkit/commit/65e5693))