Skip to content

Commit

Permalink
Add toStylesheet method, disable __toString for spritesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer committed Jul 22, 2020
1 parent 246b8f9 commit a95d814
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion resources/views/spritesheet.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;" id="svg-spritesheet">
@foreach ($spritesheet as $svg)
<symbol id="{{ $svg->id() }}" viewBox="{{ implode(' ', $svg->viewBox()) }}">
{!! $svg->inner() !!}
Expand Down
1 change: 1 addition & 0 deletions resources/views/stylesheet.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<style id="svg-stylesheet">.svg-auto-size {display: inline-block;font-size: inherit;height: 1em;overflow: visible;vertical-align: -.125em;}</style>
6 changes: 3 additions & 3 deletions src/Middleware/InjectSvgSpritesheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function handle(Request $request, Closure $next)

private function injectStylesheet(SymfonyBaseResponse $response): SymfonyBaseResponse
{
if (Str::contains($content = $response->getContent(), '<head>')) {
if (Str::contains($content = $response->getContent(), '<head>') && !Str::contains($content, 'svg-stylesheet')) {
// We insert it in the top part of the <head> as then custom CSS will overrule ours
$response->setContent(str_replace('<head>', '<head>' . $this->stylesheet(), $content));
}
Expand All @@ -57,7 +57,7 @@ private function injectStylesheet(SymfonyBaseResponse $response): SymfonyBaseRes

private function injectSpritesheet(SymfonyBaseResponse $response): SymfonyBaseResponse
{
if (Str::contains($content = $response->getContent(), '<body')) {
if (Str::contains($content = $response->getContent(), '<body') && !Str::contains($content, 'svg-spritesheet')) {
// Ported from https://stackoverflow.com/questions/2216224/php-inject-iframe-right-after-body-tag
$matches = preg_split('/(<body.*?>)/i', $content, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

Expand All @@ -72,6 +72,6 @@ private function injectSpritesheet(SymfonyBaseResponse $response): SymfonyBaseRe

private function stylesheet()
{
return '<style>.svg-auto-size {display: inline-block;font-size: inherit;height: 1em;overflow: visible;vertical-align: -.125em;}</style>';
return $this->spritesheet->toStylesheet();
}
}
11 changes: 11 additions & 0 deletions src/Spritesheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Contracts\Support\Htmlable;
use LogicException;

class Spritesheet extends Collection implements Htmlable
{
Expand All @@ -15,9 +16,19 @@ public function queue(Svg $svg): void
$this->put($svg->id(), $svg);
}

public function __toString()
{
throw new LogicException("Spritesheet __toString disabled. Specify `toHtml` or `toStylesheet` manually");
}

public function toHtml(): HtmlString
{
// Regex ported from https://github.com/Hedronium/SpacelessBlade/blob/master/src/SpacelessBladeProvider.php
return new HtmlString(preg_replace('/>\\s+</', '><', view('svg::spritesheet', ['spritesheet' => $this])->render()));
}

public function toStylesheet(): HtmlString
{
return new HtmlString(preg_replace('/>\\s+</', '><', view('svg::stylesheet')->render()));
}
}

0 comments on commit a95d814

Please sign in to comment.