diff --git a/src/SriServiceProvider.php b/src/SriServiceProvider.php index e42c601..6e409f4 100644 --- a/src/SriServiceProvider.php +++ b/src/SriServiceProvider.php @@ -3,6 +3,7 @@ namespace Elhebert\SubresourceIntegrity; use Illuminate\Support\Str; +use Illuminate\Support\HtmlString; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; @@ -29,39 +30,57 @@ public function boot() ]); Blade::directive('mixSri', function (string $path, bool $crossOrigin = false) { + $path = $this->removeQuotes($path); + if (Str::startsWith($path, ['http', 'https', '//'])) { $href = $path; } else { $href = mix($path); } - $integrity = SriFacade::html($path, $crossOrigin); - - if (Str::endsWith($path, 'css')) { - return ""; - } elseif (Str::endsWith($path, 'js')) { - return ""; - } else { - throw new \Exception('Invalid file'); - } + return $this->parseAndGenerateUrl($path, $href, $crossOrigin); }); Blade::directive('assetSri', function (string $path, bool $crossOrigin = false) { + $path = $this->removeQuotes($path); + if (Str::startsWith($path, ['http', 'https', '//'])) { $href = $path; } else { $href = asset($path); } - $integrity = SriFacade::html($path, $crossOrigin); - - if (Str::endsWith($path, 'css')) { - return ""; - } elseif (Str::endsWith($path, 'js')) { - return ""; - } else { - throw new \Exception('Invalid file'); - } + return $this->parseAndGenerateUrl($path, $href, $crossOrigin); }); } + + private function removeQuotes(string $path): string + { + $values = ['\'', '"']; + + return str_replace($values, '', $path); + } + + private function parseAndGenerateUrl(string $path, string $href, bool $crossOrigin): HtmlString + { + $integrity = SriFacade::html($href, $crossOrigin); + + if (Str::endsWith($path, 'css')) { + return $this->generateCssUrl($href, $integrity); + } elseif (Str::endsWith($path, 'js')) { + return $this->generateJsUrl($href, $integrity); + } else { + throw new \Exception('Invalid file'); + } + } + + private function generateJsUrl(string $href, string $integrity): HtmlString + { + return new HtmlString(""); + } + + private function generateCssUrl(string $href, string $integrity): HtmlString + { + return new HtmlString(""); + } }