Skip to content

Commit

Permalink
feat: add svgWithClass
Browse files Browse the repository at this point in the history
  • Loading branch information
evolkmann committed Aug 23, 2024
1 parent 3b3a766 commit 9582bab
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/HtmlHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace VolkmannDesignCode\KirbyUtils;

use Kirby\Cms\File;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Html;
use Kirby\Toolkit\Str;

class HtmlHelpers {

static function svgWithClass(string|File $file, string|array $classes): string|null {
$classNames = is_string($classes) ? [$classes] : $classes;
$class = Html::attr('class', A::join($classNames, ' '));
return Str::replace(svg($file), '<svg', '<svg ' . $class);
}

}
22 changes: 22 additions & 0 deletions tests/HtmlHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

use Kirby\Cms\App;
use PHPUnit\Framework\TestCase;
use VolkmannDesignCode\KirbyUtils\HtmlHelpers;

final class HtmlHelpersTest extends TestCase
{
public function testSvgWithClasses(): void
{
$svgPath = App::instance()->root() . '/tests/fixtures/kirby.svg';
$regular = svg($svgPath);
$this->assertStringStartsNotWith($regular, '<svg class="');

$withClass = HtmlHelpers::svgWithClass($svgPath, 'kirby');
$this->assertStringStartsWith('<svg class="kirby"', $withClass);

$withMultipleClasses = HtmlHelpers::svgWithClass($svgPath, ['kirby', 'icon']);
$this->assertStringStartsWith('<svg class="kirby icon"', $withMultipleClasses);
}

}
4 changes: 4 additions & 0 deletions tests/fixtures/kirby.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9582bab

Please sign in to comment.