name = $name; $this->contents = $this->deferContent($contents, $attributes['defer'] ?? false); unset($attributes['defer']); $this->attributes = $attributes; } public function name(): string { return $this->name; } public function contents(): string { return $this->contents; } /** * This method adds a title element and an aria-labelledby attribute to the SVG. * To comply with accessibility standards, SVGs should have a title element. * Check accessibility patterns for icons: https://www.deque.com/blog/creating-accessible-svgs/ */ public function addTitle(string $title): string { // create title element $titleElement = ''.$title.''; // add role attribute to svg element $this->attributes['role'] = 'img'; // add title element to svg return preg_replace('/]*>/', "$0$titleElement", $this->contents); } public function toHtml(): string { // Check if the title attribute is set and add a title element to the SVG if (array_key_exists('title', $this->attributes)) { $this->contents = $this->addTitle($this->attributes['title']); } return str_replace( 'renderAttributes()), $this->contents, ); } protected function deferContent(string $contents, $defer = false): string { if ($defer === false) { return $contents; } $svgContent = Str::of($contents) ->replaceMatches('/]*>/', '') ->replaceMatches('/<\/svg>/', '') ->__toString(); // Force Unix line endings for hash. $hashContent = str_replace(PHP_EOL, "\n", $svgContent); $hash = 'icon-'.(is_string($defer) ? $defer : md5($hashContent)); $contents = str_replace($svgContent, strtr('', [':href' => '#'.$hash]), $contents).PHP_EOL; $svgContent = ltrim($svgContent, PHP_EOL); $contents .= << {$svgContent} @endpush @endonce BLADE; return $contents; } }